At that year, the "Dragon" exposure of the international superstar is exposed, and everyone accuses him to sneak his wife Lin Fengjiao, forced him to come forward.
As a person who is self-satisfied with the world, "all men will be committed in all the world." I have never committed this mistake,
It is also often considered that they are not a man.
Although I didn't commit "all men who committed all the world", I have made "all programmers" all over the world.
Will make mistakes. " Regardless of the language, all programmers in the world must have committed this mistake, that is:
It is too dependent on the compiler, but I don't know what the compiler does.
In general, the higher-level programming language will provide the convenience of the more grammar, which is commonly known as the program.
Syntactic Sugar, I call it "Sweets on the Syntax". Although it is sweet, if you fail to understand the syntax
The essence connotation, it is likely that there is no sweetener, but it is suffering.
Not long ago, I received an email, the reader lists the Java programs below, saving me. After reading this program
I am sure this is another programmer who will be committed in all the world.
// Program 1
class Singleton {private static Singleton obj = new Singleton (); public static int counter1; public static int counter2 = 0; private Singleton () {counter1 ; counter2 ;} public static Singleton getInstance () {return obj;}} // program 2 public class mymain {public static void main (string [] args) {singleton obj = singleton.getinstance (); system.out.println ("obj.counter1 ==" obj.counter1); system.out.println "obj.counter2 ==" Obj.counter2);}}
The execution result is:
Obj.counter1 == 1
Obj.counter2 == 0
Have you been scared by this result? At first glance, you are likely to think that Counter1 and Counter2 must have
It will be equal, but the execution results are obviously not the case. In fact, the program 1 is compiled by the program should be equivalent to the following program 3
:
// Program 3 Class Singleton {Private Static Singleton Obj; Public Static Int Counter1; Public Static Int Counter2; Static {// This is Class Constructor // Before entering this CLASS CONSTRUctor, Class has been configured by JVM //, all STATIC Field will be set to 0, // So at this time, COUNTER1 and Counter2 are already 0, and Singleton is null obj = new singleton (); // The problem is generated by this line program // counter1 will not This is set to 0 counter2 = 0; // counter2 is set again 0 (actually more this one)} private singleton () {// This is Instance Constructor Counter1 ; counter2 ;} public static singleleton getInstance () {Return Obj This is because: When the Class has Static Field, it is set to set its value in the declaration office through "= ...".
The compiler will automatically move these narratives to the Class Constructionor. Similarly, when Class has instance
Field, and directly in the declaration of "= ...", the compiler will automatically sequence these narratives.
Move to the Instance Constructor in the INSTANCE CONSTRUctor.
This program has not initialized Static Field in Class Constructor (this time, Counter1 and Coun
NTER2 is 0), call Instance Constructor, and instance constructor will also go more
The value of Static Field makes Counter1 and Counter2 become 1. Then Instance Constructor is completed
Back to Class Constructor, set the value of counter2 to 0 (but
Counter1 remains unchanged). The last result: Counter1 is equal to 1, and counter2 is equal to 0.
To correct the procedure 1, the method has three:
- Method 1: Turn the declaration of Singleton Field to Counter1 and Counter2 Field.
This is the best practice.
- Method 2: During the declaration of Counter2 = 0, the "= 0" part is deleted. This method is only in hope
- Method 3: Move the initialization action to the Class Constructors, write itself, not dependence
The compiler is generated. This is the most insurance.
How to avoid committing a mistake of all programmers all over the world, I give you a Java programmer
Suggestions are:
- Reading Java Language Specification
- When having questions, use JavaP provided by J2SDK to reversely translate Java Bytecode, directly observe
Compiled results.
Below is the demonstration of I use Javap to reverse group translation programs:
C:. /> Javap -c -classpath Singleton Compiled from MyMain.java class Singleton extends java.lang.Object {public static int counter1; public static int counter2; public static Singleton getInstance (); static {};} Method Singleton ( 0 ALOAD_0 1 InvokeSpecial # 1
Therefore, C # initiaters is more likely to commit "all the mistakes all of the world will be committed". Many C # books will be one side
Introduce the result of the C # syntax, while introducing the results of MSIL (.NET in the middle language, similar java's Bytecode),
However, Java's book is far less.
Although it is "all the incorrects of all programmers around the world", but this does not mean that you have made this mistake, you can still
Cao Qitai, who loves to borrow money, is generally "looked up with chest and straightforward". As long as you have heart, the mistake of this category is still
Avoided.