Iv. Initialization and cleanup
1. Finalize () method: Once the garbage collector is ready to release the storage space occupied by objects, it will first call its finalize () method and
When the recycle action occurs, the memory occupied by the object is actually recycled. The Finalize method can be used to clean up garbage collection.
Finaliz is mainly used to process or release memory allocated using non-Java code. The "Local Method" in JVM may be written in C or C ++,
C and C ++ use the malloc function to allocate storage space and use the free function to release the memory. Therefore, the Finalize method is generally used to call the local method to clean up the memory.
2. recycler
Features and methods: adaptive, generational, stop-copy, marking-cleaning
Memory Allocation in a Java Virtual Machine is measured in large "blocks.
Generation Division:
Generally divided into the new generation, the old generation, each block has a corresponding algebra, if the "Block" is referenced somewhere, its algebra will increase, the garbage collector will
The newly allocated blocks are sorted after the last recycle action, which is helpful for clearing a large number of short-lived temporary objects.
Stop-copy:
Before releasing the old object, you must copy all the surviving objects from the old heap to the new heap, which results in a large amount of memory replication,
With blocks, the garbage collector can copy objects to the discarded blocks during collection. The object will be reorganized to keep it
Compact arrangement.
Mark-clear:
Starting from the stack and static storage area, traverse all references to find all surviving objects. Every time a surviving object is found
Set a tag for the object, and the object will not be recycled at this time. When all the tasks are marked as completed, the cleanup operation starts. No mark during cleaning
Will be released, and the marked surviving objects will be copied. Therefore, the remaining heap space is not consecutive.
Adaptive:
The Java virtual machine monitors all objects. If all objects are stable and the efficiency of the garbage collector is reduced, switch to "mark-clean"
Method; Agree that the Java Virtual Machine will track the effect of "mark-sweep". If there are many fragments in the heap space, switch back to "stop-Copy" mode.
V. Reuse
Combination, inheritance, and proxy
1. You can create a main () method for each class. This technique of setting a main method in each class makes Unit Testing of each class easy and easy.
In addition, you do not need to delete the main after completing the unit test. You can leave it for the next test.
2.
Link combination: "has-a" Link
Inheritance relationship: "is-a" relationship
The inheritance technology should be used with caution. whether to use combination or inheritance, the clearest way to judge is to ask whether you need to forward the Child class to the parent class.
If an upward transformation is required, inheritance is necessary. Otherwise, you should consider whether inheritance is required.
3. Final
When final modifies the referenced variable, it means that the object to which it points remains unchanged, that is, the object always points to that memory address, but the object can be
Modified. This restriction also applies to arrays, which are also objects.
4. Load
Class code is loaded only when it is used for the first time.
1) It usually refers to loading the first object of the created class.
2) when accessing the static domain and static method, the load will also occur. All static objects and static code blocks are loaded
It is initialized in sequence in the program. Only initialize once.
Vi. Polymorphism
1. in Java, except for static and final methods (private methods are implicitly specified as final methods), all other methods are later bound.
Static methods are associated with classes rather than a single object. Static methods are bound to classes during compilation.
2. polymorphism is an important technology that allows programmers to "separate changed things from unchanged things.
3.
1) allocate memory for the instance variables of the parent class
2) allocate memory for subclass instance variables
3) assign values to instance variables of the parent class (initialization)
4) execute the construction method of the parent class
5) assign values (initialize) to instance variables of the subclass)
6) construct the execution subclass
4. Inherit and clean up. In the order of initialization.
5. covariant return type, which indicates that the override method in the subclass can return a certain subtype of the return type of the parent class method.
6. In a derived class, it is best to use different names for the private method in the base class.
7. Use inheritance (inherited) to express differences in behavior (behavior), and fields to express variations in state.
"Use inheritance to express differences between behaviors, and use fields to express changes in status"
-- Inheritance enables different sub-classes to overwrite different methods. A field generally sets a reference in it and calls a referenced method,
-- This reference is the parent class or interface of several sub-classes. Through polymorphism, different sub-classes are used to instantiate the interface to call different sub-classes.
========= Fourth edition of Java programming ideology, download link ==============================
Http://download.csdn.net/detail/tzguo1314/5314416