A tour of reading the original TIJ English books -- Chapter Five: Initialization & Cleanup,
Method overloading
| _ Distinguishing overloaded methods
If the methods hava the same name, how can Java know which method you mean? There's a simple rule: Each overloaded method must take a unique list of argument types.
| _ Overloading with primitives
We can know that the constant value 5 is treated as int, so if an overloaded method is available that takes as int, it is used. in all other cases, if you hava data type that is smaller than the argument in the method, that data type is promoted. char produces a sightly different effect, since if it doesn't find an exact char match, it is promoted to int.
Suppose the methods take narrower primitive values. if your argument is wider, then you must perform a narrowing conversion with a cast. if you don't do this, the compiler will issue an error message.
| _ Overloading on return values
void f() {}int f() {return 1;}
If we just want to call a method for its side effect, such:
f();
How can Java determine which f () shocould be called? And how coshould someone reading the code see it? Because of this sort of problem, you cannot use return value types to distinguish overloaded methods.
Default constructors
If you create a class that has no constructors, the compiler will automatically create a default constructor for you.
If you define any constructors (with or without arguments), the compiler will not synthesize one for you.
When you don't put in any constructors, it's as if the compiler says, "You are bound to need some constructor so let me make one for you. "But if you write a constructor, the compiler says," You're written a constructor so you know what you're re doing; if you didn't put in a default it's because you meant to leave it out."
The this keyword
| _ Calling constructors from constructors
While you can call one constructor using this, you cannot call two. In addition, the constructor call must be the first thing you do, or you'll get a compiler error message.
| _ The meaning of static
It means that there is no this for that participant method. you cannot call non-static methods from inside static method (although the reverse is possible), and you can call a static method for the class itself, without any object. in fact, that's primarily what a static method if.
Cleanup: finalization and garbage collection
When the garbage collector is ready to release the storage used for your object, it will first call finalize (), and only on the next garbage-collection pass will it reclaim the object's memory.
In Java, objects do not always get garbage collected. Or, put another way:
1. Your objects might not get garbage collected.
2. Garbage collection is not destruction.
3. Garbage collection is only about memory.
You might find that the storage for an object never gets released because your program never nears the point of running out of storage. if your program completes and the garbage collector never gets around to releasing the storage for any of your objects, that storage will be returned to the operating system en masse as the program exits. this is a good thing, because garbage collection has some overhead, and if you never do it, you never incur that expense.
| _ What is finalize ()?
It wowould seem that finalize () is in place because of the possibility that you'll do something Clike by allocating memory using a mechanic other than the normal one in Java. this can happen primarily though native methods, which are a way to call non-Java code from Java. C and C ++ are the only supported ages currently supported by native methods, but since they can call subprograms in other ages, you can adjust tively call anything. inside the non-Java code, C's malloc () family of function might be called to allocate storage, and unless you call free (), that storage will not be released, causing a memory leak. of course, free () is a C and C ++ function, so you 'd need to call it in a native method inside your finalize ().
After reading this, you probably get the idea that you won't use finalize () much (Joshua Bloch goes further: "finalizes are unpredictable, often dangerous, and generally unnecessary. "). you are correct; it is not the appropriate place for normal cleanup to occur. so where shocould normal cleanup be saved med?
| _ You must perform cleanup
If you want some kind of cleanup stored med other than storage release, you must still explicitly call an appropriate method in Java, which is the equivalent of a C ++ destructor without the convenience.
Remember that neither garbage collection nor finalization is guaranteed. If the JVM isn' t close to running out of memory, then it might not waste time recovering memory through garbage collection.
| _ How a garbage collector works
First, the JVM has multiple implementation forms. Different types of JVM implementations may differ, and the speed and stability are also different. Therefore, the following text should be taken into a dialectical view -:)
In some JVMs, Java stacks are very different from those in C ++. The heap in C ++ is like a large yard where each object occupies a place. The heap in Java is more like a conveyor belt, which speeds up the allocation of memory for objects, just as the stack frame in C ++ moves, with the movement of "heap pointer", the memory allocation speed is slightly inferior to that of the stack (for Java heap, there's a little extra overhead for bookkeeping, but it's nothing like searching for storage ).
A simple and slow garbage collection solution uses reference counting. Each object contains a reference counter. GC traverses the entire Object List. When a reference counter is found to be zero, the memory of the object is released. Management counter reference has a small constant overhead throughout the life cycle of the program. The disadvantage is that objects referenced cyclically by each other are not released because their reference counters are non-zero. This scheme is usually used to explain a possible garbage collection mechanism, but it seems that it has never been used by any specific JVM implementation.
A faster solution is based on the idea that non-dead objects must eventually be tracked by references that survive on stacks or static storage areas. This chain may traverse grass-roots objects, starting from all references in the stack and static storage areas, tracing the objects to which the reference points, and then tracking all references in the objects, so repeatedly, you can find all surviving objects. It is worth noting that it is no longer a problem to release the referenced objects cyclically. They cannot be found and become garbage automatically.
JVM adopts a suitable garbage collection solution and can switch based on different situations. The two main mechanisms are "stop-and-copy" and "mark-and-sweep ". The stop-and-copy scheme suspends the current program, leaves all the garbage, and only copies the surviving objects from one heap to another. The mark-and-sweep scheme has been adopted by the early Sun's JVM. Generally, it is slow, but when there is little or no garbage, the speed is very fast. The mark-and-copy solution also follows the logic of the reference tracing object from the stack or static storage area. Each time it discovers an object, the object is set with a tag, after the mark program is executed, sweep can be executed. During the sweep execution, the dead object is released. Because no copy occurs, if the collector decides to compress the fragment stack, it can only move the object.
JVM monitors GC efficiency. If all objects survive for a long time, it switches to mark-and-sweep mode to work. Similarly, if there are more fragments on the stack, It switches back to the stop-and-copy mode.
There are many additional acceleration methods in JVM, such as just-in-time (JIT) compiler and HotSpot technology. A long history Ah :-)
Constructor Initialization
There's one thing to keep in mind, however: You aren't precluding the automatic initialization, which happens before the constructor is entered.
All native data types and object references (excluding local variables, if they are used before initialization, a compilation error occurs: variable might not have been initialized) and those variables that are clearly initialized in the definition are pre-initialized to zero.
| _ Order of initialization
Within the class, the initialization sequence of the variable depends on the sequence defined in the class. Variable definitions may be dispersed between methods, but variables are initialized before the constructor is executed.
| _ Static data initialization
Static variable initialization occurs when the class object is created or when the first static access occurs.
To summarize the process of creating an object, consider a class called Dog:
1. Even though it doesn' t explicitly use the static keyword, the constructor is actually a static method. so the first time an object of type Dog is created, or the first time a static method or static field of class Dog is accessed, the Java interpreter must locate Dog. class, which it dose by searching through the classpath.
2. As Dog. class is loaded (creating a Class object), all of its static initializers are run. thus, static initialization takes place only once, as the Class object is loaded for the first time.
3. When you create a new Dog (), the construction process for a Dog object first allocates enough storage for a Dog object on the heap.
4. This storage is wiped to zero, automatically setting all the primitives in that Dog object to their default values (zero for number and the equivalent for boolean and char) and the references to null.
5. Any initializations that occur at the point of field definition are executed.
6. Constructors are executed. This might actually involve a fair amount of activity, especially when inheritance is involved.
| _ Explicit static initialization
In Java, You can initialize a set of static variables through static code blocks.
Syntax: static {...}
It will only run once -- the first time you make an object of that class or the first time you access a static member of that class (even if you never make an object of that class).
| _ Non-static instance initialization
Java provides a simple syntax {...} called instance initialization. It is used to initialize non-static variables of each object and is executed before the constructor.
Array initialization
Java throws runtime exceptions to prevent array access from going out of bounds. Although this takes some time, it does not close it. For the sake of network security and the output of programmers, Java designers have made a trade-off they think is worthwhile. Although you may write code in an array access mode that you think is more efficient, this is a waste of time, because automatic compilation and runtime optimization will achieve fast array access.
| _ Variable argument lists
Different from the array as a parameter, the number of parameters in the Variable Parameter List can be zero.
(END_XPJIANG ).