Errors made by all programmers AROUND THE WORLD ZZ

Source: Internet
Author: User
This is an article from Java-CN. Article

When Jackie Chan, an international star, came to light, they accused him of being sorry for his wife, Lin fengjiao.
Yes, 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,
Therefore, I 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.ProgramEmployee
」. No matter what language you use, 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.

In general, the higher the program language, the more convenient the syntax, to facilitate program writing, this is commonly known
Syntactic sugar, which I call "Sweet syntactically 」. Although it is sweet, if you fail to understand the syntax
The essence of the content, it is likely not taste sweet, but bitter.

Not long ago, I received an email listing the following Java programs and asking me for help. After reading this program
I'm sure this is another "mistake made by all programmers around the world 」.

// Program 1

Code:

--------------------------------------------------------------------------------

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? Check the programCode, You may think the counter1 and counter2 values are certain.
Will be equal, but the execution results are obviously not. In fact, after program 1 is compiled, the program should be equivalent to the following Program 3
:

Code:
--------------------------------------------------------------------------------

// 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 automatically moves these statements to class constructor in sequence. Similarly, when the class has an instance
Field, and when the value is set directly at the Declaration through the "=..." method, the compiler will automatically sort these statements in order.
Move to instance constructor.

In class constructor, this program has not initialized static field (at this time, counter1 and cou
If nter2 is 0), call instance constructor, And the instance constructor will change.
The value of the static field changes both counter1 and counter2 to 1. Then 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:

Code:
--------------------------------------------------------------------------------

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 are on one side
This section describes the C # syntax and the result of the compiled msil (. NET intermediate language, similar to Java's bytecode,
However, 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
Cao Qitai, who loves to borrow money, generally "looks up and up straight 」. As long as you are interested, this type of Errors still works.
Avoid.

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.