Home > 类库下载 > java类库 > body text

Some knowledge points that need to be recorded in JAVA (basic part)

高洛峰
Release: 2016-10-10 09:06:06
Original
1514 people have browsed it

The difference between JDK and JRE:

JRE is the environment required for all JAVA programs to run. The operation of any JAVA program depends on JRE. Currently, if you choose to install JAVA from the JAVA official website, you will install JRE.

JDK is a toolkit provided for developers. It is a must-have item for developers. Generally, the JDK itself contains JRE, but generally the JDK will also install another set of JRE. This set of JRE is called the public JRE ( As shown in the figure), JDK needs to be downloaded from Oracle’s official website.

Current mainstream editors such as eclipse will find the location of the JRE and JDK. Of course, you can also modify it manually.

JAVA required environment variable description:

PATH: (required) specifies the path to the program required to compile and run the java program, usually in the jdk installation directory (note that it is JDK!!! It is different from JRE ! ! ) in the bin folder. The currently commonly used setting method is to first define a JAVA_HOME variable and reference JAVA_HOME in PATH: %JAVA_HOME%/bin;

CLASSPATH: (previously required, now not required) The purpose of setting Classpath is to tell the Java execution environment in which directories You can find the Java program (.class file) you want to execute, which is no longer needed;

JAVA_HOME: (Theoretically not necessary, actually necessary) Set a variable to store the path of the program required to run the java program, which is convenient for other place to reference (no need to enter a long list of paths, just use the JAVA_HOME variable directly). At the same time, many software now also directly call the JAVA_HOME variable, and it is easy to make errors if it is not set.

1.Java main structure description

Every .java file in java is a class. A java project is composed of many .java files, which is composed of java classes. Generally speaking, the main structure of a java class is as follows:

1. Package declaration: which package the java class belongs to in this project, that is, its position in the project;

2. Imported packages: external libraries, tools;

3. Public class body (or interface) definition: the class name and the name of the java file must be the same, and the content in the class is the main content of the java file

4. Others: definitions of other classes and interface definitions.

In the java project, from outside to inside, they are: source folder, package (package), class (.java, the thing that I have been working on for a long time). After compilation, the compiled class (.class) will be Place it in the bin folder at the same level as the source folder. Packages in different source floders can have the same name, but classes in the same package cannot have the same name (not even in the same package in two different source floders)

2. Basic data types

Basic data types 8: 4 types of integers, 2 types of floating point, characters, Boolean;

btye is up to 128-1; short is up to 32768-1; int contains up to 10 digits starting with 2; when calculating numerical values, pay attention to the range of integer values ​​and remember to add the conversion character ; When assigning long data, if the value of the integer exceeds the maximum range of int, an l description must be added after the integer;

For floating-point assignments to float, an f description must be added after the decimal;

Simple character variables are the same. Addition (that is, each element is a character variable (PS. Adding integer is also the addition of ascii code)) is equivalent to adding the ascii code of each character; (different from the addition operation of strings!!! Adding characters and strings results is a string)

3. Basic packaging classes

There are 7 basic commonly used packaged classes (be sure to pay attention to upper and lower case!!!): String, Integer, Boolean, Byte, Character, Double, Number ;

4. Type conversion:

char, int, double, etc. can be converted by forced conversion, such as (int) 'a', (char) 25; boolean type variables cannot be obtained by forced conversion

The conversion between each variable and each class needs to be achieved by calling the methods of each object. For example: to convert integer or character types to String objects, you can use String.valueOf();

5. Constant assignment:

Global constants must be assigned a value when initialized; local constants do not need to be assigned a value when initialized, but they can only be assigned once. .

6. Valid scope of variables:

Global variables are divided into instance variables and static variables (static). Static variables can be used in other classes using class names. Instance variables can only be used in this class. use.

Local variables are only valid in the code block where the variable is defined, that is, between {{} two braces, starting from the variable declaration.

Global variables with the same name as local variables are invalid within the scope of use of local variables.

7. The difference between switch and if elseif

Both can achieve the same function, but logically speaking, if elseif must perform a check on every item in front of the matching item, while switch uses a certain search method to find a match. When there are many items to be checked, switch is more efficient. Another advantage of switch is that the code is clear, but the disadvantage is that it is difficult to perform complex operations.

8. Use foreach loop method

Use foreach loop method to quickly traverse arrays, objects, etc. The specific method is: for (int x: arr) {operation}, but it should be noted that the foreach form cannot write data

9. Record of common methods of String object (sorted according to the degree of common use according to personal understanding, parameters and specific usage are not explained, the same below)

length() gets the length of the string;

equals() determines whether the contents of the strings are the same. Note that you cannot directly use the equal sign for comparison. Using the equal sign will compare the locations in the memory! ! ! ! !

replace() substring replacement in the string;

split(), split the string according to the given symbols and save it in an array;

toCharArray(), convert the string into a character array;

trim() removes the spaces before and after the string. Note that the string is not changed, but the corresponding copy is returned;

substring() intercepts the character string;

toLowerCase(), toUpperCase() performs the case of the string Conversion;

indexOf(), lastIndexOf() finds the substring position in the string;

charAt() returns the character at the specified position in the string;

startsWith(), endsWith() determines the start and end of the string Whether it is a specified string;

compareTo(), compares strings in dictionary order;

10. Records of common methods of Arrays objects (sorted by personal understanding of commonness, parameters and specific usage are not explained)

Arrays. fill(), fills the array, can be used for array initialization and assignment;

Arrays.sort(), sorts the array;

Arrays.copyOf(), Arrays.copyOfRange copies the array;

11. Note The use of the static keyword

See the technical blog for detailed analysis: http://www.cnblogs.com/dolphin0520/p/3799052.html

When using it, methods and variables that are to be called outside the class need to be added On the static limiter, if a class (method) with the static limiter needs to call other methods or global variables, the called methods and global variables must also be static modified (this is because non-static variables must create instances to be called). Generally used for classes (methods) that do not require the creation of instances, such as classes used as tools.

It is necessary to distinguish the difference between static and permission modifiers. The purpose of static is to use static variables and methods without creating an instance, while permission modifiers specify the scope of use of the methods and variables.

12. Description of data saved by Java’s collection classes

Arraylist, HashMap and other collection classes are a feature of Java, but they can only save reference data, not basic data. That is: Integer is OK but int is not.

13. Description of permission modifiers for classes

For top-level classes (classes on the first level inside the package), only the public modifier is available, indicating that the class can be used across packages. If public is not used, it will default to Available in this package, there is at most one public class in a .java file. For inner classes of top-level classes, the permission modifiers are private, protected, public and default.

The permission issues of inner classes require in-depth study (for example, if there is a private variable in the inner class and the variable needs to be used inside the outer class, etc.), we will skip it here for now.

14. Determine whether two objects of a class are equal

Except for basic variables, reference variables (objects) cannot use two equal signs == to determine whether the values ​​are equal (whether two objects generated by a class are equal) , you need to use the equals method. However, it should be noted that the equals method of generally user-defined classes uses double equal signs for internal judgment. To achieve the effect of judgment, the user needs to overload equals. (Just write a method to judge by yourself)

15. Methods that may be commonly used in packaging classes corresponding to basic variables

Integer

equals(), compare whether the values ​​​​of two integer objects are equal

byteValue(), intValue (), shortValue(), returns the value of the corresponding type

toString(), converts the Integer object into a String object (the same applies to toBinaryString(), toHexString(), toOctalString())

Integer.valueOf(), converts the String Convert the object to an Integer object

Integer.parseInt() and convert the String object to an int variable.

Boolean

Byte

Character

Character.inUpperCase(), Character.inLowerCase(), determine whether it is an uppercase or lowercase character

Character.toUpperCase(), Character.toLowerCase(), convert to uppercase or lowercase characters

toString(), convert characters into strings

Double (Float can refer to Doublel, the method is the same)

intValue(), return an integer variable

toString(), return a string

Double.valueOf( ), convert the String object into a Double object

Summary: When you need to convert a basic class, you can use toString() to convert it into a string, and then use the valueOf of each class to convert the string into the required class.

16. Commonly used mathematical calculation methods

DecimalFormat class

Each method formats a number and gets a string;

Math class

trigonometric function method can perform various trigonometric function calculations. Including radian angle interchange

exponential function method, square, square root, cube, cubic root method, etc.

rounding function method, rounding up, down, nearest round

maximum minimum value absolute value

random number method (Produces any double value between 0 and 1)

Random class

nextInt() returns an integer, nextLong() returns a long integer, nextBoolean() returns a Boolean variable, nextFloat() returns a floating point number

17. About method rewriting (different from overloading!!!)

When overriding a method, you can modify the method's modifiers and return value type (parameter type and number are immutable). In most cases, the final method in the parent class cannot be overridden (private final is not visible in the subclass, but can Rewriting is a special case)

When a subclass method overrides a parent class method, the modifier of the subclass method must have greater authority than the modifier of the parent class method;

The subclass is more responsible for the parent class method When writing, if you want to modify the return value type in a subclass method, the type must be a subclass with the same method return value in the parent class;

18. Regarding the difference between method rewriting and overloading

in the same class , methods cannot be overridden, that is, there cannot be two methods with the same method name, number of parameters, and parameter types in a class. In the same class, methods can be overloaded, that is, multiple identical methods are allowed in a class. Name, methods with different parameter types and number of parameters exist. The rewriting of a method involves at least two classes, the parent class and the subclass. If you want to rewrite, you cannot modify the parameter type and number of the method. The overloading of the method only occurs in one class. If you want to override, you must modify it. Parameter type and number. All methods in the parent class will be inherited in the child class, so when there are two methods with the same method name and different parameters in the parent class and the child class, it can be regarded as being inherited first and then overloaded.

19. Abstract class (abstract keyword)

Abstract classes can only be inherited and have no other functions. Abstract methods in abstract classes must be rewritten after being inherited. Abstract methods have no method body. If a class contains abstract methods, the class must be an abstract class.

20. Interface

Permission modifier of interface (to be resolved). The methods in the interface are all public and do not contain method bodies that need to be inherited and rewritten.

Let’s talk about the application regardless of the principle. Interfaces are generally Using public modification, the interface without public can only be used in different classes in the same package (still protected by default). The interface with public can be used across packages, but only the interface with the same name as .java can use public. . Within the interface, except for static and default methods, other methods cannot have method bodies.

21. Object transformation

Object transformation is divided into upward transformation and downward transformation. Subclass objects can always be transformed upward (will be automatically transformed). The transformation of parent class objects must consider whether they belong to subclass objects. (Requires forced transformation). For example: A a = new B();, B is a subclass of A, then a will eventually be treated as an object of type A, but it is still essentially class B. References of the parent class type can call all properties defined in the parent class. And methods, it is beyond the reach of methods and attributes that only exist in subclasses, that is, a can call methods and attributes inherited from class A in type B, but it cannot call methods and attributes unique to class B. The same is true when calling methods. Suppose C inherits from B and inherits from A. When calling a method in the class, the C type object c will first search for the C type as a parameter within the available range (refer to the previous sentence for the available range). If the method overloaded version does not exist, it will be upcast to B and search for the method overloaded version with type B as a parameter until an executable method overloaded version is found. If it does not exist, it cannot be called and an error will be reported.

22. About class packages

In actual development, class packages should be specified for all classes. The package declaration must be the first line of non-commented code in the file. All class packages are composed of lowercase letters. When importing a package, if you use * to import all classes in the package, subclasses will not be imported. If you need to import subclasses, you need to re-import. Use import static to import static members in a class.

23. About constants

The constants defined by final are classified into local constants and global constants. Local constants cannot be redefined during their life cycle, but if necessary, they can be assigned new values ​​every time they are created. Global constants Generally use public static final, the value of the global constant will not be changed during the running of the program.

PS:

Polymorphism is not fully understood and needs to be deepened

The permission modifier of the interface is not fully understood and needs to be deepened

The difference between JDK and JRE:

JRE is required for all JAVA programs to run Environment, the operation of any JAVA program depends on JRE. Currently, if you choose to install JAVA from the JAVA official website, the JRE is installed.

JDK is a toolkit provided for developers. It is a must-have item for developers. Generally, the JDK itself contains JRE, but generally the JDK will also install another set of JRE. This set of JRE is called the public JRE ( As shown in the figure), JDK needs to be downloaded from Oracle’s official website.

Current mainstream editors such as eclipse will find the location of the JRE and JDK. Of course, you can also modify it manually.

JAVA required environment variable description:

PATH: (required) specifies the path to the program required to compile and run the java program, usually in the jdk installation directory (note that it is JDK!!! It is different from JRE ! ! ) in the bin folder. The currently commonly used setting method is to first define a JAVA_HOME variable and reference JAVA_HOME in PATH: %JAVA_HOME%/bin;

CLASSPATH: (previously required, now not required) The purpose of setting Classpath is to tell the Java execution environment in which directories You can find the Java program (.class file) you want to execute below. It is no longer needed now;

JAVA_HOME: (Theoretically not necessary, actually necessary) Set a variable to store the path of the program required to run the java program, so that it can be referenced in other places (no need to enter a long list of paths, just use the JAVA_HOME variable directly), and at the same time , many software now also directly call the JAVA_HOME variable, which is prone to errors if not set.

1.Java main structure description

Every .java file in java is a class. A java project is composed of many .java files, which is composed of java classes. Generally speaking, the main structure of a java class is as follows:

1. Package declaration: which package the java class belongs to in this project, that is, its position in the project;

2. Imported packages: external libraries, tools;

3. Public class body (or interface) definition: the class name and the name of the java file must be the same, and the content in the class is the main content of the java file

4. Others: definitions of other classes and interface definitions.

In the java project, from outside to inside, they are: source folder, package (package), class (.java, the thing that I have been working on for a long time). After compilation, the compiled class (.class) will be Place it in the bin folder at the same level as the source folder. Packages in different source floders can have the same name, but classes in the same package cannot have the same name (not even in the same package in two different source floders)

2. Basic data types

Basic data types 8: 4 types of integers, 2 types of floating point, characters, Boolean;

btye is up to 128-1; short is up to 32768-1; int contains up to 10 digits starting with 2; when calculating numerical values, pay attention to the range of integer values ​​and remember to add the conversion character ; When assigning long data, if the value of the integer exceeds the maximum range of int, an l description must be added after the integer;

For floating-point assignments to float, an f description must be added after the decimal;

Simple character variables are the same. Addition (that is, each element is a character variable (PS. Adding integer is also the addition of ascii code)) is equivalent to adding the ascii code of each character; (different from the addition operation of strings!!! Adding characters and strings results is a string)

3. Basic packaging classes

There are 7 basic commonly used packaged classes (be sure to pay attention to upper and lower case!!!): String, Integer, Boolean, Byte, Character, Double, Number ;

4. Type conversion:

char, int, double, etc. can be converted by forced conversion, such as (int) 'a', (char) 25; boolean type variables cannot be obtained by forced conversion

The conversion between each variable and each class needs to be achieved by calling the methods of each object. For example: to convert integer and character types to String objects, you can use String.valueOf();

5. Constant assignment:

Global constants must be assigned a value when initialized; local constants do not need to be assigned a value when initialized, but they can only be assigned once. .

6. Valid scope of variables:

Global variables are divided into instance variables and static variables (static). Static variables can be used in other classes using class names. Instance variables can only be used in this class. use.

Local variables are only valid in the code block where the variable is defined, that is, between {{} two braces, starting from the variable declaration.

Global variables with the same name as local variables are invalid within the scope of use of local variables.

7. The difference between switch and if elseif

Both can achieve the same function, but logically speaking, if elseif must check every item in front of the matching item, while switch uses a certain search method to find a match. When there are many items to be checked, switch is more efficient. Another advantage of switch is that the code is clear, but the disadvantage is that it is difficult to perform complex operations.

8. Use foreach loop method

Use foreach loop method to quickly traverse arrays, objects, etc. The specific method is: for (int x: arr) {operation}, but it should be noted that the foreach form cannot write data

9. Record of common methods of String object (sorted according to the degree of common use according to personal understanding, parameters and specific usage are not explained)

length() gets the length of the string;

equals() determines whether the contents of the string are the same, Note that you cannot directly use the equal sign for comparison. Using the equal sign will compare the locations in the memory! ! ! ! !

replace() substring replacement in the string;

split(), split the string according to the given symbols and save it in an array;

toCharArray(), convert the string into a character array;

trim() removes the spaces before and after the string. Note that the string is not changed, but the corresponding copy is returned;

substring() intercepts the character string;

toLowerCase(), toUpperCase() performs the case of the string Conversion;

indexOf(), lastIndexOf() finds the substring position in the string;

charAt() returns the character at the specified position in the string;

startsWith(), endsWith() determines the start and end of the string Whether it is a specified string;

compareTo(), compares strings in dictionary order;

10. Records of common methods of Arrays objects (sorted by personal understanding of commonness, parameters and specific usage are not explained)

Arrays. fill(), fills the array, can be used for array initialization and assignment;

Arrays.sort(), sorts the array;

Arrays.copyOf(), Arrays.copyOfRange copies the array;

11. Pay attention to the use of the static keyword

For detailed analysis, please see the technical blog: http://www.cnblogs.com/dolphin0520/p/3799052.html

When using it, if you want to call methods outside the class, Variables need to be added with static qualifiers, and if a class (method) with static qualifiers needs to call other methods or global variables, the called methods and global variables must also be static modified (this is because non-static variables must To create an instance it can be called). Generally used for classes (methods) that do not require the creation of instances, such as classes used as tools.

It is necessary to distinguish the difference between static and permission modifiers. The purpose of static is to use static variables and methods without creating an instance, while permission modifiers specify the scope of use of the methods and variables.

12. Description of data saved by Java’s collection classes

Arraylist, HashMap and other collection classes are a feature of Java, but they can only save reference data, not basic data. That is: Integer is OK but int is not.

13. Description of permission modifiers for classes

For top-level classes (classes on the first level inside the package), only the public modifier is available, indicating that the class can be used across packages. If public is not used, it will default to Available in this package, there is at most one public class in a .java file. For inner classes of top-level classes, the permission modifiers are private, protected, public and default.

The permission issues of inner classes require in-depth study (for example, if there is a private variable in the inner class and the variable needs to be used inside the outer class, etc.), we will skip it here for now.

14. Determine whether two objects of a class are equal

Except for basic variables, reference variables (objects) cannot use two equal signs == to determine whether the values ​​are equal (whether two objects generated by a class are equal) , you need to use the equals method. However, it should be noted that the equals method of generally user-defined classes uses double equal signs for internal judgment. To achieve the effect of judgment, the user needs to overload equals. (Just write a method to judge by yourself)

15. Methods that may be commonly used in packaging classes corresponding to basic variables

Integer

equals(), compare whether the values ​​​​of two integer objects are equal

byteValue(), intValue (), shortValue(), returns the value of the corresponding type

toString(), converts the Integer object into a String object (the same applies to toBinaryString(), toHexString(), toOctalString())

Integer.valueOf(), converts the String Convert the object to an Integer object

Integer.parseInt() and convert the String object to an int variable.

Boolean

Byte

Character

Character.inUpperCase(), Character.inLowerCase(), determine whether it is an uppercase or lowercase character

Character.toUpperCase(), Character.toLowerCase(), convert to uppercase or lowercase characters

toString(), convert characters into strings

Double (Float can refer to Doublel, the method is the same)

intValue(), return an integer variable

toString(), return a string

Double.valueOf( ), convert the String object into a Double object

Summary: When you need to convert a basic class, you can use toString() to convert it into a string, and then use the valueOf of each class to convert the string into the required class.

16. Commonly used mathematical calculation methods

DecimalFormat class

Each method formats a number and gets a string;

Math class

trigonometric function method can perform various trigonometric function calculations. Including radian angle interchange

exponential function method, square, square root, cube, cubic root method, etc.

rounding function method, rounding up, down, nearest round

maximum minimum value absolute value

random number method (Produces any double value between 0 and 1)

Random class

nextInt() returns an integer, nextLong() returns a long integer, nextBoolean() returns a Boolean variable, nextFloat() returns a floating point number

17. About method rewriting (different from overloading!!!)

When rewriting a method, you can modify the modifier and return value type of the method (parameter type and number are immutable), in most cases The final method in the parent class cannot be overridden (private final is not visible in the subclass and can be rewritten, which is a special situation)

When a subclass method overrides a parent class method, the modifier of the subclass method must be Modifier permissions are greater than those of the parent class method;

When a subclass overrides a parent class method, if you want to modify the return value type in the subclass method, the type must be a subclass of the same method return value in the parent class;

18. About the difference between method rewriting and overloading

In the same class, methods cannot be overridden, that is, there cannot be two methods with the same method name, number of parameters, and parameter types in a class. In the same class, methods can be overloaded, that is, they are allowed in a class. Multiple methods with the same method name, different parameter types, and number of parameters exist. The rewriting of a method involves at least two classes, the parent class and the subclass. If you want to rewrite, you cannot modify the parameter type and number of the method. The overloading of the method only occurs in one class. If you want to override, you must modify it. Parameter type and number. All methods in the parent class will be inherited in the child class, so when there are two methods with the same method name and different parameters in the parent class and the child class, it can be regarded as being inherited first and then overloaded.

19. Abstract class (abstract keyword)

Abstract classes can only be inherited and have no other functions. Abstract methods in abstract classes must be rewritten after being inherited. Abstract methods have no method body. If a class contains abstract methods, the class must be an abstract class.

20. Interface

Permission modifier of interface (to be resolved). The methods in the interface are all public and do not contain method bodies that need to be inherited and rewritten.

Let’s talk about the application regardless of the principle. Interfaces are generally Using public modification, the interface without public can only be used in different classes in the same package (still protected by default). The interface with public can be used across packages, but only the interface with the same name as .java can use public. . Within the interface, except for static and default methods, other methods cannot have method bodies.

21. Object transformation

Object transformation is divided into upward transformation and downward transformation. Subclass objects can always be transformed upward (will be automatically transformed). The transformation of parent class objects must consider whether they belong to subclass objects. (Requires forced transformation). For example: A a = new B();, B is a subclass of A, then a will eventually be treated as an object of type A, but it is still essentially class B. References of the parent class type can call all properties defined in the parent class. And methods, it is beyond the reach of methods and attributes that only exist in subclasses, that is, a can call methods and attributes inherited from class A in type B, but it cannot call methods and attributes unique to class B. The same is true when calling methods. Suppose C inherits from B and inherits from A. When calling a method in the class, the C type object c will first search for the C type as a parameter within the available range (refer to the previous sentence for the available range). If the method overloaded version does not exist, it will be upcast to B and search for the method overloaded version with type B as a parameter until an executable method overloaded version is found. If it does not exist, it cannot be called and an error will be reported.

22. About class packages

In actual development, class packages should be specified for all classes. The package declaration must be the first line of non-commented code in the file. All class packages are composed of lowercase letters. When importing a package, if you use * to import all classes in the package, subclasses will not be imported. If you need to import subclasses, you need to re-import. Use import static to import static members in a class.

23. About constants

The constants defined by final are classified into local constants and global constants. Local constants cannot be redefined during their life cycle, but if necessary, they can be assigned new values ​​every time they are created. Global constants Generally use public static final, the value of the global constant will not be changed during the running of the program.

PS:

Polymorphism is not fully understood and needs to be deepened

The permission modifiers of the interface are not fully understood and needs to be deepened


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template