I did not read the books in sequence. I read the books first if I was interested.
- In a confusing constructor case, Let's guess what the following program prints?
Public class confusing {
Private confusing (Object O ){
System. Out. println ("object ");
}
Private confusing (double [] darray ){
System. Out. println ("Double array ");
}
Public static void main (string [] ARGs ){
New confusing (null );
}
}
You may think that one of the two overload functions should be called randomly. Unfortunately, you will find that the double array is repeatedly output. It turns out that only the second function is called. Why? This is related to Java's parsing of overloaded functions. Java's parsing of overloaded functions is divided into two steps. First, it finds all the constructors or methods that can be obtained and applied, step 2: Find the most accurate method from these methods. In this example, the range of parameters that the 1st functions can accept is greater than that of the second function. Therefore, for null, the second function is more "precise ". This tells us that when writing an overloaded function, make sure that the parameter types accepted by all overloaded versions are as incompatible as possible!
2. Null and void are interesting examples:
Public class null {
Public static void greet (){
System. Out. println ("Hello world! ");
}
Public static void main (string [] ARGs ){
(Null). Greet ();
}
}
Can this be compiled? You may want to ask, haha, you can print the classic Hello world. Essentially, (null). Greet (); is equivalent to null. Greet (); Because greet is a static method, that is, a class method, it has nothing to do with a specific object instance! Although you specify the expression null here, this value is ignored. This method is easy to cause confusion. When calling a class method (when using a static method), try to use the class name to call it!
3. Several examples in this chapter detail the initialization sequence of variables in the class. You need to avoid the following situations:
A. Be careful about the class initialization sequence, especially when you are not sure, write the appropriate test.
B. Do not call methods that may be overwritten in constructors. These methods may be overwritten in the subclass, resulting in unpredictable behavior.
C. Special attention should be paid to the sequence, static fields, and even final static fields for static variable initialization, which may be used before they are correctly initialized.
4. Look at a program, dynamic binding and static binding.
Class dog {
Public static void bark (){
System. Out. Print ("woof ");
}
}
Class basenji extends dog {
Public static void bark (){}
}
Public class bark {
Public static void main (string ARGs []) {
Dog woofer = new dog ();
Dog nipper = new basenji ();
Woofer. Bark ();
Nipper. Bark ();
}
}
Ah, you probably thought it was dynamic binding at the beginning, polymorphism !! Print only one woof! But that's not the case. basenji, a poor puppy, is also calling. (according to the book, this dog is in Africa and never calling. It cannot be imagined that there are endless dogs in the world ). The problem is that bark is a static method, and there is no dynamic binding mechanism for calling static methods! Static methods are called only based on the caller's compilation period type. Both woofer and nipper are declared as dog, and their compilation period types are the same. You may ask, the bark () method in the sub-class basenji does not overwrite the bark method in the parent class, so what is going on? The answer is that it hides the bark method in the parent class. Static methods cannot be overwritten. They can only be hidden. To make this poor puppy return to the "normal" state, remove the static tag before the method.
5. Features of the instanceof OPERATOR:
A. Null instanceof string returns false, that is, false if the left operand is null.
B. In the compilation phase, instantceof checks whether the left operand is the child type of the right operand. If not, it cannot pass
C. After the compilation phase is passed, it does not mean that the left operand is the type or child type of the right operand. When you perform the transformation operation, you will make a runtime judgment. For example, if the transformation fails, classcastexception is thrown.
6. We often encounter such a requirement to track the number of instances created by a class. A typical method is to increment a private static variable in its constructor, such:
Public class creator {
Public static void main (string [] ARGs ){
For (INT I = 0; I <100; I ++)
Creature = new creature ();
System. Out. println (Creature. numcreated ());
}
}
Class creature {
Private Static long numcreated = 0;
Public creature (){
Numcreated ++;
}
Public static long numcreated (){
Return numcreated;
}
}
You try to run this program and expect it to print 100. Unfortunately, it does not, because you cannot even compile it ..... Why? The error message is as follows:
E:/book/Java/javapuzzlers/src/COM/denny_blue/puzzlers/classy/creator. Java: 18: Not a statement
Creature = new creature ();
E:/book/Java/javapuzzlers/src/COM/denny_blue/puzzlers/classy/creator. Java: 18: require ';'
Creature = new creature ();
2 error
Strange, isn't it? In fact, it is very simple: creature Creature = new creature (); this is a local variable declaration statement, while the Java language specification (jls14.4) does not allow a local variable declaration statement as a statement in, if the while or do statements are executed cyclically, they can only appear in one block. Therefore, the simple solution is to add {} to this sentence, or
For (INT I = 0; I <100; I ++)
New creature ();
Compiled ,. Now we can print 100 normally. Another problem is that if multiple threads create objects in parallel, we need to synchronize the counter code and the access counter code:
Class creature {
Private Static long numcreated = 0;
Public creature (){
Synchronized (Creature. Class ){
Numcreated ++;
}
}
Public static synchronized long numcreated (){
Return numcreated;
}
}
If you use JDK 5 or an updated version, you can use the newly introduced actiomiclong. This is really a good thing. I have little knowledge about this newly introduced and sent package in JDK 5. It can bypass synchronization requirements when faced with concurrency:
Import java. util. Concurrent. Atomic. atomiclong;
/**
*
* @ Author Dennis
*/
Public class creature {
Private Static atomiclong numcreated = new atomiclong ();
/** Creates a new instance of creature */
Public creature (){
Numcreated. incrementandget ();
}
Public static long numcreated (){
Return numcreated;
}
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 708983