1 Public classdemo_01 {2 Public Static voidMain (string[] args) {3 intA = 10;4 intb = 20;5 inti = 0;6i = i++;7b = a++;8 System.out.println (a);9 System.out.println (b);Ten System.out.println (i); One } A}
First Equals, then, OK, the answer is A=11,b=10,i???
At this time, my first reaction i=0? And then i++? That I equals 1?
In fact, the correct answer is i=0.
To explain the cause of this result in more popular words, let's first explain b=a++
Java code This statement, we look at the surface is the first execution equals, in the execution of the Gaga operation, but actually in the run, but it is not the case. In order to let us appear to be equal to the first operation, in the actual operation, will first put the value of a in a place, and then let a for the operation of + +, this time to get the result 11, and then assign 11 to a, the previously saved 10 assigned to B, also got the result we want.
If it is i=i+1 operation, the same, I now equals 0, first 0 to find a place to hide, execute i++, the result 1 is assigned to I, and then 0 to find in the assignment to I, so I the value of 0 is covered out, also got 0 results.
The above is a very popular explanation, that a little more professional explanation is (only to explain the i=i++), first, because this time I value is 0, so 0 advanced stack, and then perform 0+1 operation, the next step, get this result 1 into the stack, here, the operation of the stack end, and then start to assign value, first 1 out of the stack, At this time I=1, followed by 0 out of the stack, i=0, completed I is worth covering.
The final summary is that the b=a++ of such statements is not the same as the steps we take to prioritize calculations, which results in incorrect calculations for i=i++ results.
i=i++ in Java