CoreJava learning notes 1-basic concepts, objects and classes, corejava learning notes
I. Basic Program design structure of java
(1) java has eight basic types: Four integer types, two floating-point types, one char type, and one boolean type.
1) Four integer types: byte (1), short (2), int (4), and long (8 ).
2) int is the most common. byte and short are usually used for file processing at the underlying layer or a large array that needs to be controlled to occupy storage space.
3) floating point type: float (4) and double (8 ). Float suffix F. If no suffix is added, the default value is double (D ).
4) char represents a single character, which is usually used to represent a character constant.
5) boolean has two values: false and true.
(2) After declaring a variable, you must use the value assignment statement to explicitly initialize the variable. Do not use uninitialized variables.
(3) The keyword final indicates that the variable can be assigned only once. Once assigned, it cannot be changed. In Java, final is often used to define class constants. In this way, this constant can be used in multiple methods in a class.
(4) If a positive number is divided by 0, an exception will occur. If a floating point number is divided by 0, an infinite number or NaN will be obtained.
(5) immutable character strings have the following advantages: they can be shared.
(6) Check whether equal strings use equals instead of =. The latter requires that the address of the two strings must be the same.
(7) Input and Output example:
(8) The While LOOP statement first checks the loop conditions, so the code in the loop body may not be executed. If you want the loop body to be executed at least once, put the detection condition at the end and use do... while.
(9) The case tag can be a constant expression of the char, byte, short, and int type, and an enumeration constant;
1) from Java 7, case can support strings.
2) if there is no break at the end of the case Branch statement, the next branch statement will be executed.
(10) interrupt control process statements: break and continue.
(11) array
1) when creating an array, all elements are initialized to 0;
2) the boolean array element is initialized to false.
3) The object array element is initialized to null, indicating that these elements do not store any objects ).
4) Arrays. sort () can quickly sort Arrays.
Ii. Objects and Classes
(1) The most common relationships between classes:
1) dependency (uses-a): Class A's method is used to manipulate Class B's objects (Class B's object is the input parameter of Class A's method). Class A is dependent on Class B.
2) aggregation (has-a): Class A objects contain Class B objects (Class B objects are member variables of Class ).
3) Inheritance (is-a): A inherits from B, A is a B.
(2) to use an object, you must first construct the object and specify its initial state. Then apply the method to the object.
1) Use the constructor to construct a new instance.
(3) An object variable only references an object, rather than containing an object.
1) For example, Date deadline = new Date (); new Date () constructs a Date-type object. The value of this object is an address that references this object, the address is stored in the variable deadline.
2) if an object is null, it means that no method can be called without pointing to any object. For example:
Brithday birth = null;
Birth. toString ();
In this case, an error is reported.
(4) In a source file, there can be only one public class and multiple non-public classes.
1) For example, in addition to a common class of EmployeeTest (including the main method), EmployeeTest. java also has a non-public class of Employee.
2) when compiling the source code, the compiler creates two class files in the directory: EmployeeTest. class and Employee. class.
3) the class name (EmployeeTest) containing the main method in the program is provided to the bytecode interpreter to start the program: java EmployeeTest.
(5) about the constructor
1) The constructor has the same name as the class.
2) each class can have more than one constructor.
3) The constructor can have multiple parameters.
4) The constructor does not return values.
5) The constructor is always called along with the new operation.
(6) In each method, the keyword "this" indicates the implicit parameter, indicating the current object instance. For example:
Public void raiseSalary (double percent ){
Double raise = this. salary + percent;
This. salary + = raise;
}
Percent is an explicit parameter.
(7) If a class has a final instance variable, the variable must be initialized during class initialization. That is, the variable value must be set after each constructor is executed, and cannot be modified in subsequent operations.
(8) most final modifiers are applied to variables of the basic type or immutable type (this class is an immutable class if every method in the class does not change its object. For example, String class .)
(9) static variables belong to the class and do not belong to any object. For example:
Each Test object has its own size variable, but no matter how many objects are created, they share the age variable.
(10) System. out is an example of using static variables.
(11) Value-based call indicates that the method provides the value provided by the caller. Calling by reference indicates that the method receives the variable address provided by the caller. Java always uses value-based calls.
1) according to the following example, many people come to the conclusion that Java calls the object by address because the attribute value of the object has changed after the function is called:
In fact, the reference x of the 49 rows method is just a copy of harry. At this time, x and harry are both numerical values, which are equal to the address pointed to by the Employee object. That is, x and harry point to the address of the Employee object at the same time. You changed the attributes of the object, but the address values represented by x and harry did not change.
2) The following example shows that Java uses value-based transmission for objects:
The swap method replaces x and y, which changes the address referenced by x and y. However, neither a nor B has changed.
(12) If multiple methods have the same name and different parameters, an overload occurs.
1) To completely describe a method, you must specify the method name and parameter type. This is called the method signature.
2) the return value type is not part of the method signature. That is to say, there cannot be two methods with the same name and parameter type, but different types of value methods are returned. The following statement is incorrect:
(13) If no constructor is written when writing a class, the system will provide a non-argument constructor. This constructor sets all instance domains (member variables) to the default value. Therefore, the value of the Instance field is set to 0, the boolean data is set to false, and all object variables are set to null. (The system provides a default constructor only when the class does not provide any constructor .)
(14) The double meaning of the keyword "this:
1) First, this references the implicit parameters of the method (that is, in addition to multiple input parameters of a function, there is also an unwritten this parameter, this parameter always points to the object instance currently calling this method .)
2) The second statement. If the first statement of the constructor is like this (...), This constructor calls another constructor of the same class.