Java self-increment operations

Source: Internet
Author: User

Remember when the university began to learn C language, the teacher said: Since the increase there are two forms, respectively, i++ and ++i,i++ is the first assignment after the addition of 1,++i is the first add 1 after the assignment, so that the understanding of many years did not appear to be the problem, until the following code, I doubt that my understanding is wrong:

public class Client

{

public static void Main (string[] args)

{

int count = 0;

for (int i=0;i<10;i++)

{

count=count++;

}

System.out.println ("count=" +count);

}

}

How many count is the output of this program? is count self-added 10 times? The answer equals 10? Can be very sure to tell you that the answer is wrong! The result of the run is count equals 0. Why is it? count++ is an expression that has a return value, and its return value is the value of count since it was added, which is what Java does for self-addition: first copy the value of count (note is a value, not a reference) to a temporary variable area, then add 1 to the count variable, and finally return the value of the temporary variable area. The procedure for processing the first cycle of the program is as follows: Step 1 The JVM copies the Count value (whose value is 0) to the temporary variable area. Step 2 count value plus 1, when the value of Count is 1. Step 3 Returns the value of the temporary variable area, noting that the value is 0 and has not been modified. Step 4 Returns the value assigned to count, at which point the count value is reset to 0.

The "countcount=count++" statement can be interpreted as follows:

public static int Mockadd (int count)

{

Save the initial value first

int temp =count;

Do a self-increment operation

Countcount = count+1;

Return original value

return temp;

}

So the value of count after the first loop is still 0, the other 9 cycles are the same, and eventually you will find that the value of count is always unchanged and remains in its original state. In this example, the author of the code intended to expect count to be self-increasing, so it is assumed that the assignment was made to itself, and that it was never intended to fall into the Java self-increment trap. The solution is simple, just modify "count=count++" to "count++". This problem has different implementations in different locales: C + + is equivalent to "count=count++" and "count++", while in PHP it retains the same processing as Java. Each language to the self-increasing implementation of different, the reader is interested in more than one language to test, think about the principle. Next time if you see someone on the T-shirt printed on "i=i++", do not despise him, remember, can be interpreted in different languages clearly this sentence is absolutely not simple, should show "like surging River" like the admiration, psychological meditation "tall man, the peerless man."

Java self-increment operations

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.