My thinking in java learning notes (seven)

xiaoxiao2021-03-06  96

Cleaning: Termination and garbage recycling here to understand this time, the garbage collector can only recover the object generated by new, if you use or generate non-New generated objects, the garbage collector does not know how to clean him . This time you want to use Finalize (). 2, the transportation method of the garbage collector is this. When the garbage collector intends to start the release of the resources occupied by your object, first call finalize (), and reclaim the object when the next garbage recycling movement is released. The resource occupied, if you use Finalize (), he will make you perform your own cleanup action when garbage is recycled 3, Finalize () does not automatically evoke when the object is destroyed. Suppose there is an object to draw yourself on the screen during the generated process. If you don't have a manual to clean him, then he will never be there, and if you put the function of the cleanup screen in Finalize (), then when this object is recycled by the garbage collection, he is on the screen. The image will be cleared first. Your object will never be recycled because your program does not take system resources to take the status of the rubbish collection, and the system resources you take place will run when the program ends. All released, then you don't have to pay additional system expenses running in the garbage collector. What is the finalize () existent? To know, Finalize () is not used for general cleaning, the garbage collector returns to the objects generated by using New, and those unconventional methods (C / C native functions) will need Finalize () to clean up work . In this case, we should not use Finalize () because he is not a place where the regular cleaning action is placed. What do you have to perform a cleanup action is the execution site of normal cleaning action? In C , when the object is created in the STACK, then a destructor is automatically called before the end of the brace range where the object is located, if this object is in the New HEAP If you created, then you must call the Delete operator to clean up the object. Java does not allow objects to be created from the Stack, you must use New from the HEAP to create an object, but Java does not have a delete operator, but the garbage collector to clean the object, so we can say, since There is a garbage collector in Java, which does not require a destructive function. However, the garbage collector is not a destructive function after all, he can't replace the role of the arctive function. If you need other cleanup actions in addition to the release space, then you have to call the function of the function of the descending function. As long as the program does not rely on the call finalize (), then this function has a use, it is The test of the object of the subject.

So what is death? When the object is cleaned up, the object must be in some state to be cleaned up, that is, if there is a data stream that is no longer used, you need to clean up, then you have to be cleaned up. Turn off, this is a death condition class myclass {boolean b = false; myclass (boolean b) {this.b = b;} void death () {b = false;} public void finalize () {if (b) // Death condition is B Must be false system.out.println ("Error: b is true, class can't be clean); Else System.out.println (" OK! Cleanup ");}} Class Test { Public static void main (string args []) {myclass mc = new myclass (true); mc.death (); new myclass (true); system.gc (); //system.gc () is used to force the end The occurrence of action, but even if he doesn't use him, as long as you can occupy the system resources to take the rubbish, Finalize () is still executed}} The transportation of the garbage collector to learn the procedure should be Know that the practice of creating objects (except basic data types) from HEAP will significantly affect the speed of the system, but garbage back The incliner can greatly improve the efficiency of creating objects from HEAP, you might be very surprised, the release of the garbage collector's storage space can affect the distribution of storage space, and more amazing, this from HEAP The speed of the way to create an object has been approximated in other languages ​​from the speed of the Stack! Why do this? Imagine an unlucky guy (object) standing on a bus (C HEAP), and other people have seats, he needs to keep there in the car, I hope to have no one to do the seat (high price ), A single person (object) leaves the seat (released), he will rush to sit down. In Java's JVM, the bus's seat turned into a belt. When there is an empty seat, the latter people make up the front seat, and the unfortunate person will take the last flight. The seat will be on, but don't need him to stay around.

The garbage collector rearranges all objects in the Heap, allowing them to be more closely arranged, so that the Heap pointer can be moved closer to the front section of the transfer belt, avoiding the internal deposit page replacement action, and the efficiency is greatly improved. GC In order to achieve a faster garbage collection speed, he will find whether the handle of each object is pointed to a Heap to determine whether the object is referenced, whether it is cleaned, and the change will be re-mapped. . Because of this particularity of GC, when the GC is started, the execution program will temporarily stop.

Members Initialization In Java, the data member variable of the Class's basic data type will be automatically initialized by the system, and when the variable is defined in the function, he will not be initialized by the system Class Test {byte B; char C Short S; INT i; long L; float f; double d; boolean bl; void Go () {INT II; //system.out.println (II); when the variable is defined in the function, he I will not be initialized by the system; system.out.println (II); system.out.print ("byte:" b "/ n" "char:" c "/ n" " Short: " S " / N " " INT: " i " / n " " long: " " / n " " float: " f " / n " " Double: " D " / N " " Boolean: " BL " / N ");} public static void main (string args []) {test t = new test (); t.go ();}} results 10 byte: 0 char : The value of char is 0, showing blank short: 0 in T: 0 long: 0 FLOAT: 0.0 Double: 0.0 Boolean: false specifies the initial value how to specify the beginning, this I think I don't have to write it ~ This can definitely know that the constructor is initializing the action constructor can be used to execute Initialization, there is a greater elasticity, but the initialization action of the constructor is Class test {INT i; test () {I = 10;}} then, i will be automatically initialized to 0 Then, the constructor will then be initialized to 10. When it is defined, the initial value is also the same, this requires that the initialization order of the initialization variables will depend on the order of the defined variables in the Class,

Variables may be scattered throughout, inserted in a function, but all variables must be in any function, even if constructor is called, the basic data type of the initial data type of the initialization static data is initialized and the basic data type of Non-Static The data type is not different, but if he is a handle of an object, then the initial value is NULL. The initialization of Static will only happen when necessary. If you do not produce a Class object, there is no static data, but Static's data will never be initialized. STATIC's initialization only occurs when the first Static's access action occurs, STATIC objects will not be initialized if Static does not initialize when the object is generated, the initialization order of the variables will become static Non-Static, Method Static explicitly initializes Java to allow you to organize multiple static variables, placed in the Static block static {Int i = 10; Byte B = 20;} When you generate Class objects, or first call STATIC members, Static blocks are initialized in the initialization of the Non-StATIC entity to initialize the action Java to initialize the static variable initialization method {INT i = 10; byte b = 10;} If we want to produce unknown The inner hidden class, this method is necessary (Chapter 8)) The next section we will talk about the last paragraph of this chapter, Array, please continue to pay attention

If you have any questions, you can contact me like this e-mail molmd@163.net / 163.com QQ: 31349283 We can learn progress together! Welcome to my blog, http://blog.9cbs.com/maoerzuozuo has more learning content!

转载请注明原文地址:https://www.9cbs.com/read-122611.html

New Post(0)