Decided to start this most of the Java Collection, just let your notes, talk less nonsense!
Chapter 4 Initialization & Cleanup (INITIATION & CLEANUP)
Main chapter content: 4.1 Builder 4.2 Method Overload 4.3 Tailings and garbage collection 4.4 Members Initialization
Keywords: constructor, finalize (), initialization order
Key finishing:
1. Constructor Distance: Constructor belongs to a relatively special method type because it does not return a value. This is a significant difference from the VoID return value. For Void returns, although the method itself does not automatically returns anything, but still allows it to return to other things. The builder is different, but it will not only return automatically, but it does not have any options at all. If you create a class without the member, the compiler will automatically create a default member.
2.Finalize () and GC ()
(1) What is the Finalize () function? Java is not a Garbage Collection (hereinafter referred to as GC) is responsible for recycling memory? Answer: GC can only clear the memory allocated on the heap (all objects of pure Java language Use new allocation of memory on the pile, without clearing the memory allocated on the stack (when using JNI technology, you may allocate memory on the stack, such as the Java call C program, and the C program uses Malloc allocated memory.). If some objects are assigned a memory area on the stack, the GC can not control, and the memory recovery of such objects will rely on Finalize (). For example, when Java calls non-Java method (this The method may be C or C ), and may call the Malloc () function in the non-Java code to allocate memory, and unless the free () does not release memory (because Free () is C. Function), this time you want to release memory, GC does not work, so you need to call it in a fixed method inside Finalize (). The principle of Finalize should be like this: once garbage collection Ready to release the storage space occupied by the object, which first calls Finalize (), and only reclaims the memory of the object only during the next garbage collection. So if you use Finalize (), you can do some important during the garbage collection. Clear or cleaning work.
(2) Question: When is Finalize () being called? Answer: There are three situations
1. All objects are automatically called when they are run, such as running system.gc () .2. Program is called a Finalize method for each object when the program exits. 3. Explicit call finalize method
In addition to this, when an object is collected as useless information, Finalize () will be called, but the JVM does not guarantee that Finalize () must be called, that is, the finalize () call is Unsure, this is why Sun does not advocate the use of Finalize ().
3. THIS Point: The keyword can only be used in the method, which provides the corresponding handle to call the object of the method, so that the different object instances generated by the same class When calling the same method, the system can determine which An object is called. For example: myObject a = new myObject (); myObject b = new myObject (); AF (); // (3) bf (); // (4) The compiler is compiled, actually The (3), (4) sentence is explained as MyObject.f (a); myObject.f (b);, this will pass the information of the object to which the method is called to the method, that is, pass it. THIS, you can call the object instance of the method via this.
The concept of this can also explain why the non-static method and element cannot be called in a static method. This is because there is no THIS in the static method, that is, we cannot get the handle of the object that calls the method. Since this object instance is not found How can we call the method and elements of the object instance in it?
Then why there is no THIS? The concept of static method can be used to understand this problem. Static method is a class method. It is a method for all object instances. It does not belong to a specific object instance, so it is impossible to use this to reflect This example. This is not the same as the non-static method. For a few more users in a local area, everyone has a client, but all access a public server. For each client, Its this this is the use of its users. For the server, it doesn't have this because it is a common, not for a specific customer. 4. Object initialization points: 1. The object is only created, The initialization is only used when using it, otherwise it will never initialize. 2. Object initialization has a certain order, whether in the definition, the placement position of each member is. First is a static member and object, then a non-static member And objects, finally run constructors. 3. Static members and objects have only one initialization process, which occurs during the first creation of objects or the first time use of static members and objects.
Taking a class named DOG as an example, its object instance initialization process is as follows: (1) When an object of a Dog is created for the first time, or when the Static method / static field of the Dog class is first accessed, the Java interpreter must find DOG .CLASS (Search in the classpath in advance). (2) After finding dog.class, all of its Static initialization modules will run. Therefore, STATIC initialization only occurs once - when the Class object is loaded first. (3) When you create a New Dog (), the build process of the DOG object will first assign enough storage space for a DOG object in the memory stack (HEAP). (4) This storage space is cleared to zero, and all the basic types in the Dog are set to their default values (5) all initialization that occurs when field definitions occur. (6) Execute the builder. As will be said in Chapter 6, this actually requires considerable operation, especially when involving inheritance.
5. The initialization array of arrays includes basic data type arrays and object arrays, where "Exception" errors are often present for the initialization of the object array. For example, the following program
The problem code is as follows: public userinfo [] getUsersInfo () {
Userinfo [] usersinfo = null;
IF (users.size ()! = 0) {usersinfo = new userInfo [users.size ()];
For (int i = 0; i Where UserInfo is defined as class userinfo {userinfo (string name, int type, int us; this.Name = name; this.Type = type; this.userid = userid;} string name; int type; int userid;} When running to the problem area marked in the program, the system displays nullpointerserexception, why is this? This is because Java is used when defining arrays. Usersinfo = New userinfo [users.size ()]; does not assign memory to array elements, it is just a handle array, and the objects in arrays have not been initialized. So each of the arrays Objects can be accessed after NEW. For example: a [] a = new a [2]; for (int i = 0; i <2; i ) a [i] = new a (); this can be A [i ] .someMethod () Therefore the above program should be changed Public userinfo [] getUsersinfo () { Userinfo [] usersinfo = null; IF (users.size ()! = 0) {usersinfo = new userInfo [users.size ()]; For (int i = 0; i