My thinking in Java Study Notes (7)

Source: Internet
Author: User
Cleaning: Termination and garbage collection
Here we need to understand these points
1. the garbage collector can only recycle objects generated by new. If you use or generate objects not generated by new, the garbage collector does not know how to clear them. In this case, use finalize ().
2. The Garbage Collector is used in this way. When the garbage collector wants to release the resources occupied by your object, it will first call finalize (), in addition, the resources occupied by the object will be reclaimed only when the next garbage collection action is released. If you use finalize (), this will allow you to clear your own junk data.
3. Finalize () is not automatically triggered when the object is destroyed. Suppose there is an object that will draw itself on the screen when it is generated. If you do not manually clear the screen, it will always be there, and if you put the screen cleanup function in finalize, before the object is recycled by the garbage collector, the image on the screen will be cleared first.
4. Your objects may never be recycled, because your program does not occupy system resources in the State where the garbage collector is required to exit, the system resources you use will be released when the program stops running, so you do not have to pay additional system expenses for running the garbage collector.
Why does finalize () exist?
You need to know that finalize () is not used for general cleanup. The Garbage Collector cleans up the objects generated using new, and those unconventional methods (C/C ++ native functions) to generate an object
Finalize () for cleanup. In this case, we should not use finalize () in large quantities, because it is not a place where regular cleanup operations are executed.
You must perform the cleanup action.
What is the place where normal cleanup operations are performed?
In C ++, when an object is created in a stack, a function called destructor is automatically called before the range of braces where the object is generated ends, if this object is created from heap in the new way, you must call the delete operator to clear the object. Java does not allow objects to be created from the stack. You must use new to create objects from heap. However, Java does not use the delete operator, but relies on the garbage collector to clear objects. Therefore, we can say that since Java has a garbage collector, no destructor is required. However, the garbage collector is not a destructor and cannot replace it. If you want to execute other cleanup actions in addition to releasing space, you must call a function similar to the destructor.
Death Condition
As long as the program does not rely on calling finalize (), this function has another purpose, that is, to check the death condition of the object.
So what are the conditions of death? When an object is cleared, it must be in a certain state before it can be cleared normally. That is, if a data stream is no longer used, it must be cleared, before cleaning up, you must first be shut down. This is a condition of death.
Class myclass
{
Boolean B = false;
Myclass (Boolean B)
{
This. B = B;
}
Void death ()
{
B = false;
}
Public void finalize ()
{
If (B) // The death condition is that 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 termination of action, but even if you don't use it, you can still execute finalize () If you can take up system resources to make the necessary Garbage Collector available.
}
}
How the garbage collector works
Those who have learned the program should know that the method of creating objects (except basic data types) from heap will greatly affect the system speed, however, the garbage collector can greatly improve the efficiency of object creation from heap. You may be surprised that the release of the storage space of the garbage collector can affect the storage space allocation, even more surprising, the speed of this method of creating objects from heap is close to that created by other languages from Stack!
Why? Imagine a bad guy (object) standing on a bus (heap of C ++), while others have seats, he needs to keep watching the car, in the hope that there will be no one's seat (high cost), a single person (object) will leave the seat (released) and he will rush to sit. In Java's JVM, the bus seat is changed to a conveyor belt. When there are free seats, the people behind the bus fill in the seats in front, and so on, the unlucky man just needs to go directly to the last empty seat without having to keep looking around. The garbage collector will re-arrange all objects in heap to make them more closely arranged together, so that the heap pointer can be moved closer to the front of the conveyor belt, avoiding paging replacement of memory, the efficiency is greatly improved.
To achieve faster garbage collection, GC searches for static and stack objects and determines whether the object's handle points to a heap, after cleaning, the changed handles are re-mapped. Due to the special nature of GC, when GC is started, the program being executed will be temporarily stopped.
Member Initialization
In Java, the data member variable of the basic data type of the class will be automatically initialized by the system. When the variable is defined in the function, it will not be automatically 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 a variable is defined in a function, it is not automatically initialized by the system.
II = 10;
System. Out. println (II );
System. Out. Print (
"Byte:" + B + "/N" +
"Char:" + C + "/N" +
"Short:" + S + "/N" +
"Int:" + I + "/N" +
"Long:" + L + "/N" +
"Float:" + F + "/N" +
"Double:" + D + "/N" +
"Boolean:" + BL + "/N ");
}
Public static void main (string ARGs [])
{
Test T = new test ();
T. Go ();
}
}
Result
10
Byte: 0
CHAR: the value of char is 0, and it is blank.
Short: 0
INT: 0
Long: 0
Float: 0.0
Double: 0.0
Boolean: false
Specify initial values
How can I specify the initial description? I don't want to write any more ~ You must know this.
Initialization with Constructors
Constructor can be used to execute initialization actions, so you are more flexible, but the initialization actions of constructor are after automatic initialization.
Class Test
{
Int I;
Test ()
{
I = 10;
}
}
Then, I will be automatically initialized to 0 before being initialized to 10 by the constructor. This is the same even when the variable is defined and the initial value is given.
Initialization order
The initialization sequence of variables depends on the sequence of Defined variables in the class. variables may be scattered and interspersed in functions, but all variables must be in any function, initialization is completed even before the constructor is called.
Static Data Initialization
The initialization of the static basic data type is no different from that of the non-static basic data type, but if it is the handle of an object, the initial value is null. Static initialization only occurs when necessary. If you do not generate a class object or call static data, static data will never be initialized. Static initialization only occurs when the first static access action occurs. Since then, the static object will not be initialized.
If static is not initialized when the object is generated, the initialization sequence of the variable changes to static, non-static, and method.
Static Explicit initialization
Java allows you to organize multiple static variables and put them in static blocks.
Static
{
Int I = 10;
Byte B = 20;
}
When you first generate a class object or call a static member of the class for the first time, the static block is initialized.
Non-static object initialization
Java also provides a method similar to static for non-static variable initialization.
{
Int I = 10;
Byte B = 10;
}
This method is required if we want to generate an anonymous implicit class (as described in Chapter 8th)
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.