Thinking in Java note initialization cleanup

Source: Internet
Author: User

Chapter 4 initialization cleaning
1. Reload function parameter matching.
A. the type of the input parameter is smaller than the type of the formal parameter declared in the method, and the type of the actual parameter will be upgraded. Except for char, if the char method is not accepted, the char is upgraded to int.
B. If the actual input parameter type is greater than the formal parameter type declared in the method, you must display the type conversion. Otherwise, an error is returned. However, this narrow conversion will lose information.

2. Call the constructor In the constructor.
If a class has multiple constructors, you can call one constructor in one constructor.
For example:
 
Note:
A. You can use this to call the constructor, but you cannot use the same method to construct two constructor;
B. the constructor call must be placed at the very beginning;
C. Except the constructor, no other method can call the constructor.
 
3. Final Processing and garbage collection
The garbage collector only knows how to release memory allocated by new. You can define a finalize () method in the class for memory that does not need new allocation. It is called when the garbage collector is ready to recycle. But it is not equal to the structure.
A. Objects may not always be recycled. (Garbage collection is generally run only when the memory is used up)
B. Garbage collection is not equal to the analysis structure.
C. Garbage collection is only related to memory.
 
4. You cannot use finalize () as a general cleaning method.
Finalize () is only used to use Class C when allocating memory, for example, calling the C/C ++CodeAnd the malloc () and other functions are used to allocate memory, so you have to call the free () and other methods in the finalize () method to clean up.

public class flower {
int petalcount = 0;
string S = new string ("null");
flower (INT petals) {
petalcount = petals;
system. out. println ("constructor w/INT Arg only, petalcount ="
+ petalcount);
}

Flower (string SS ){
System. Out. println ("constructior w/string Arg only, S =" + SS );
S = SS;
}
 
Flower (string S, int petals ){
This (petals );
//! This (s); // can't call two!
This. S = s; // another use of "this"
System. Out. println ("string & int ARGs ");
}
 
Flower (){
This ("hi", 47 );
System. Out. println ("default constructor (no ARGs )");
}
 
Void print (){
//! This (11); // not inside non-constructor!
System. Out. println ("petalcount =" + petalcount + "s =" + S );
}
 
Public static void main (string [] ARGs ){
Flower x = new flower ();
X. Print ();
}
}

 

5. termination conditions
The termination condition can be understoodProgramConditions that must be met when you want to disable the service. If an object in a program opens a file, all opened files should be closed at the end of the program.
An important purpose of the finalize () method is to verify the termination condition.
For example:

In this example, all book objects should be checked in before being treated as garbage collection ). But none of them exist in main. If you do not have finalize () to verify the conditions, this error will be hard to find.

6. Each basic data member of the class must have an initial value.
Boolean false
Char 0
Byte 0
Short 0
Int 0
Long 0
Float 0.0
Double 0.0

7. When defining an object reference in the class, if it is not initialized, the reference will get a null value.

8. Unlike C ++, class member variables can be assigned a value where they are defined.

9. constructor initialization cannot prevent automatic initialization. It will occur before the constructor is called.
Class couunter {
Int I;
Counter (){
I = 7;
}
}

Here I first 0 and then 7

10. Within the class, the variable is initialized before any constructor is called.

11. Initialization order
The initialization sequence is "static" Object first and then "non-static object ".

12. Object creation process (assuming there is a dog class)
1) when you first create an object of the Dog class (the constructor can be regarded as a static method) or the static method/static field of the Dog class is accessed for the first time, the Java interpreter must search for the class path to locate the dog. class file.
2) load the file dog. Class. All actions related to static initialization are executed. Therefore, static Initialization is executed only once when the class object is loaded for the first time.
3) when using new dog () to create an object, sufficient storage space will be allocated to the dog object on the heap. 4) This bucket will be cleared. This will automatically set all the basic data types in the dog object to a saved value, and the reference will be set to null.
5) execute all initialization actions that appear in the segment definition.
6) execute the constructor.

13. Static clause
Java allows multiple static initialization actions to be organized into a special "static clause", for example:
Class spoon {
Static int I;
Static {
I = 47;
}
}
Code blocks in static {} are static clauses. The statement in this code block is executed only once.

14. Non-static instance initialization clause
Like static clauses, non-static instance initialization can also be written as code blocks. For example:
Dog D1;
Dog D2;
{
D1 = new dog ();
D2 = new dog ();
}

{} Is the non-static instance initialization clause.

15. If the elements in the array are not of the basic data type, new must be used. Otherwise, an "exception" occurs when an empty reference in the array is used"

 

Class book {
Boolean checkedout = false;
Book (Boolean checkout ){
Checkedout = checkout;
}

Void checkin (){
Checkedout = false;
}

Public void finalize (){
If (checkedout)
System. Out. println ("error: checked out ");
}
}

Public class terminationcondition {
Public static void main (string [] ARGs ){
Book novel = New Book (true );
// Proper cleanup
Novel. checkin ();
// Drop the reference, forget to clean up
New Book (true );
// Force Garbage Collection & finalization
System. GC (); // force the end action
}
}

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.