All programmers in the world will make mistakes.

Source: Internet
Author: User

When an international superstar's "dragon" was revealed, the crowd accused him of being sorry for his wife and forced him to hold a press conference to confess to the world that he had made "all men in the world will make mistakes ". I have never made such a mistake and often think that I am not a man.

Although I have never made the mistake that all men in the world will make, I have made the 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 will be to facilitate program writing. This is commonly known as syntactic sugar, which I call as "the sweetness of syntax ". 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 that all programmers in the world will make ".

Procedure 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;

}

}
Procedure 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:

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,

Singleton is null.

Obj = new Singleton ();

// The problem is caused by this program.

// Counter1 will not be set to 0 here

Counter2 = 0;

// Counter2 is set to 0 again (in fact, this is an extra action)

}

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 (but counter1 remains unchanged ). 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" part of the counter2 = 0 announcement. This approach is only expected.

Method 3: Move the initialization action to the class constructors and write it by yourself without relying on the compiler. This is the safest practice.

How to avoid making mistakes that all programmers in the world will make? My suggestions for Java programmers 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 and directly observe the compiled result.

Related Article

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.