Thinking in JAVA notes-Chapter 5 initialization and cleanup, thinkingjava
5.1 use the constructor to ensure initialization.
To ensure security, force initialization before use
The Java constructor and the class name are the same. The non-parameter constructor is also called the default constructor.
5.2 method overload
Method overloading: overload. Different parameters with the same name
Method overriding: overwrite/overwrite. The Child class overwrites the parent class.
5.2.1 Method for distinguishing Heavy Loads
Unique parameter type table; otherwise, compilation fails!
5.2.2 heavy load involving basic types
If the parameter type is int, short, byte, long, and so on, the constant is automatically located to int without executing short, byte
Because the default constant is the int type, if there is no int, it will be transformed to long, float, double in turn
Note: No information will be lost during upward transformation, and automatic transformation is supported.
Downward transformation will lose information and must force type conversion
5.2.3 distinguish between overload methods with return values
Not allowed. Because an int-type method f () can also be called directly without doing anything.
5.3 default constructor
If no constructor is written, the compiler will write a non-parameter constructor to apply for memory space.
5.4ThisKeywords
Send a message to an object: The Compiler "secretly" transmits the reference parameter of the object that calls the method to the method. Therefore, the method knows which object is calling the method and the property value of the object. To obtain the reference of the current object within the method, java provides the keywordThis. This can only be used inside a method, indicating the reference to the object that calls the method. If a method calls another method of the same class, you can omit this (or we recommend that you omit this ).
Use this: The method returns the current object and passes the current object to other methods.
5.4.1 call the constructor In the constructor
This (***) method: this will produce a clear call to a constructor that meets the next parameter list. Must be at the constructor header and can only be used once!
5.4.2static
The static method does not exist.ThisThe method has the semantics of global functions.
5.5 cleanup: finalization and garbage collection are terminated.
Java classes are allowed to define a finalize () method, which is called before garbage collection to process some garbage collection tasks.
If the Garbage Collector cannot clean up tasks unrelated to memory collection, you must manually define and call other common java methods.
5.5.1 use of finalize ()
Garbage collection and its related behavior finalize are only related to memory collection. finalize: the object is allocated memory in other ways than the object creation method.
"Local method": call non-java code. You need to call the local method in finalize to call free.
5.5.2 You must clean up
Java does not allow local objects, so there is no destructor. If you need to manually clean up the work, you need to call a java method.
Garbage collection is executed only when the jvm memory is exhausted. Otherwise, the memory is directly returned to the operating system at the end of the program.
5.5.3 termination conditions
Finalize () is difficult to use, but there is an interesting usage called final Condition Verification ???
The object must be in a certain state when it is cleared; otherwise, the object cannot be recycled even if the reference is lost. finalize can capture this defect and handle the exception!
5.5.4 how the garbage collector works
The java garbage collector can increase the object creation speed ???
The java heap allocation speed is even comparable to the stack allocation speed of other languages!
Java's "heap pointer" moves to an unallocated area after an object is allocated
When the memory is exhausted, the garbage collector will intervene to recycle space and closely arrange the objects in the heap to avoid page errors.
Principles of the garbage collector: Adaptive, generational, stop-copy, Mark-clean
5.6 initialize a member
Make sure that all variables are properly initialized before use.
5.6.1 specify Initialization
Assign values where class member variables are defined. Each object has the same initial value.
5.7 constructor Initialization
5.7.1 initialization sequence: defined sequence. Automatic initialization has been completed before the constructor calls
5.7.2 static data Initialization
If Initialization is not performed, It is initialized as a standard value or null.
You can also initialize it in the definition.
Static objects are initialized only once before non-static objects! P95
5.7.3 display static Initialization
Static {
***
}
5.7.4 non-static instance Initialization
You can also define the initialization statement {} to support anonymous internal classes.
5.8 array Initialization
Arrays are object sequences or basic types of sequences encapsulated together by the same type.
The compiler does not allow you to specify the size of an array (because no space is allocated to the array object, Initialization is required)
Simple content p100-p101
5.8.1 Variable Parameter List
Essentially an implicit array f (Objcet... args)
Complicated heavy-load method recognition
5.9 Enumeration type