[ZT] errors made by all programmers in the world-cai xuexiao

Source: Internet
Author: User
[ZT] errors made by all programmers in the world-cai xuexiao

When Jackie Chan, an international star, came to light, they accused him of being sorry for his wife, Lin fengjiao, and forced him to hold a press conference, to the rest of the world, he made the mistake that all men in the world will make 」. I have never made such a mistake and often think that I am not a man.

Although I have never made a mistake that all men in the world will make, I have made a mistake that all programmers in the world will make 」. No matter which language is used, all programmers in the world must have made this mistake: it is too dependent on the compiler, but does not know what the compiler has done.

Generally, the higher the programming language, the more syntactically convenient it is to facilitate program writing. This is commonly known as syntactic sugar, and I call it "Sweet syntactically 」. Although it is sweet, if you fail to understand the essence of the syntax, it is very likely that you will not have a taste of the sweetness, but suffer.

Not long ago, I received an email listing the following Java programs and asking me for help. After reading this program, I am sure this is another "mistake made by all programmers in 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 ever been shocked by this result? At first glance, you may think that the values of counter1 and counter2 are equal, but the execution results are obviously not the same. In fact, the program after program 1 is compiled should be equivalent to program 3 below:

// Program 3
Class Singleton {
Private Static Singleton OBJ;
Public static int counter1;
Public static int counter2;
Static {// This is the class Constructor
// Before entering this class constructor, the class has been
// Configure the memory. All static fields are set to 0 first,
// Therefore, counter1 and counter2 are both 0 and Singleton is null.
OBJ = new Singleton (); // all problems are generated by this line of Program
// Counter1 will not be set to 0 here
Counter2 = 0; // counter2 is set to 0 again (in fact, this is another option)
}
Private Singleton () {// This is instance Constructor
Counter1 ++;
Counter2 ++;
}
Public static Singleton getinstance (){
Return OBJ;
}
}

This is because when the class has a static field and its value is directly set at the Declaration through the "=..." method, the compiler will automatically move these statements to the class constructor in sequence. Similarly, when the class has an instance Field and its value is directly set at the Declaration through the "=..." method, the compiler will automatically move these statements to the instance constructor in sequence.

In class constructor, this program calls instance constructor before initializing static field (at this time, both counter1 and counter2 are 0, the instance constructor will change the value of the static field so that both counter1 and counter2 become 1. After instance constructor is executed, return to class constructor and set counter2 to 0 (
Counter1 ). The final result is: counter1 equals 1, and counter2 equals 0.

To correct procedure 1, there are three methods:

-Method 1: Adjust the declaration of Singleton field to counter1 and counter2 fields.
This is the best practice.
-Method 2: delete the "= 0" section in the counter2 = 0 announcement. This approach is only expected
-Method 3: Move the initialization action to the class constructors and write it on your own without dependency.
Generated by the compiler. This is the safest practice.

How to avoid making mistakes that all programmers in the world will make?
The suggestions are as follows:
-Familiar with Java Language Specification
-If you have any questions, use the javap provided by j2sdk to reverse group the Java bytecode.
The compiled result.

Below is a demonstration of anti-group translation program 1 using javap:

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 <method java. Lang. Object ()>
4 getstatic #2 <field int counter1>
7 iconst_1
8 iadd
9 putstatic #2 <field int counter1>
12 getstatic #3 <field int counter2>
15 iconst_1
16 iadd
17 putstatic #3 <field int counter2>
20 return

Method Singleton getinstance ()
0 getstatic #4 <field Singleton OBJ>
3 areturn

Method static {}
0 new #5 <class Singleton>
3 DUP
4 invokespecial #6 <method Singleton ()>
7 putstatic #4 <field Singleton OBJ>
10 iconst_0
11 putstatic #3 <field int counter2>
14 return

In fact, Java's syntactic sugar is not much, and C #'s syntactic sugar is actually ubiquitous,
Therefore, C # beginners are more likely to make "mistakes made by all programmers around the world 」. Many C # books will introduce the C # syntax while introducing the compiled msil (. net intermediate language, similar to Java's bytecode), but few Java books do this.

Although it is "the mistake that all programmers in the world will make", it does not mean that after you make this mistake, you can still borrow money from Cao Qitai. Generally, you can "Look up and feel confident 」. This type of errors can still be avoided if you are interested.

Author: Cai xuexiao
Article Source: sleepless 2.0
Published on: 03/10/2003


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.