The difference between i++ and ++i (explanatory examples)

 

The popular interpretation of i++ is to assign first and then increment . In fact, the value assigned here is the value taken from the operand stack, that is to say, the value of i is pushed onto the stack first.

And self-increment is the self-increment of the value of the local variable table.

And ++i is the opposite, it is first incremented and then assigned , that is, the local variable table is incremented, and then the value of the local variable table is pushed onto the stack.

 

Next, you can understand and digest through the following five sample questions: Reprinted in: You can understand i++ and ++i in detail at a glance

Example 1 

int i = 0;
i = i++; 
System.out.println("i = " + i); 

Example 2 

int a = 2; 
int b = (3 * a++) + a;
System.out.println(b);

Example 3 

int a = 2;
 int b = a + (3 * a++);
System.out.println(b);

Example 4 

int i = 1;
int j = 1;
int k = i++ + ++i + ++j + j++; 
System.out.println(k);

Example 5 

int a = 0;
int b = 0;
a = a++;
b = a++;
System.out.println("a = " + a + ", b = " + b);

Sample answer

Example 1: 0
Example 2: 9
Example 3: 8
Example 4: 8
Example 5: a = 1, b = 0

 
 

Example 1 detailed explanation 

int i = 0;
i = i++; 
System.out.println("i = " + i);  // 结果:0

First look at i++, according to the principle "increase first, then return to the value before the increment", after i increment, i = 1, but then return to the value 0 before the increment, then the expression becomes i = 0, 0 When no value is assigned to i, the value of i is 1, but when 0 is assigned to i, the value of i becomes 0 again. Therefore, the code of i = i++ is useless work, because the value of i is still the same in the end. 
 

Example 2 in detail 

int a = 2;
 int b = (3 * a++) + a;
System.out.println(b);   // 结果:9

int b = (3 * a++) + a; after a++, a = 3, and return the value 2 before the increment, so the expression at this time is: 
int b = (3 * 2) + a; the value of a at this time It is already 3, and the expression becomes: 
int b = (3 * 2) + 3; so b = 9 
 

Example 3 detailed explanation 

int a = 2; 
int b = a + (3 * a++);
System.out.println(b); // 结果:8

This question is almost the same as Example 2, just changed the order, why the result is different? This requires the use of "expression principle": a variable is also an expression, the addition and subtraction of multiple expressions The calculations are carried out from left to right 
int b = a + (3 * a++); according to ordinary people’s thinking, 3 * a++ is calculated first, a is first incremented by a=3, and then the value 2 before the increment is returned, so this When the expression becomes: 
int b = a + (3 * 2); at this time the value of a is 3, and the expression becomes: 
int b = 3 + (3 * 2); the result b = 9 
we say one Variables are also expressions. The addition and subtraction operations of multiple expressions are carried out from left to right. You may not have a deep understanding of this theory, but you can understand it if I change the code a bit, as follows: 
int b = (a * 1) + (3 * a++) This code is the same as int b = a + (3 * a++), there is no difference, but by looking at (a *1) you can easily know that you have to count a * 1 first For this expression, the result of the expression is 2. 
Therefore, although the preceding a in int b = a + (3 * a++) is just a variable, it is also an expression. The expression a and the expression (3 * a++) are added together. The calculations are all carried out from left to right, so first calculate the expression a, the calculation result of a expression is 2, so the expression becomes: 
int b = 2 + (3 * a++) Then a is incremented and returned from Increase the previous value by 2, so the expression becomes: 
int b = 2 + (3 * 2); so the result is 8. At this time the value of a is 3 
 

Example 4 detailed explanation 

int i = 1;
int j = 1;
int k = i++ + ++i + ++j + j++; 
System.out.println(k);  // 结果:8

With the detailed explanation of the previous three examples, I believe you can answer this one by yourself. You can answer it yourself first, and see if the result is 8. If it is not, then look at my explanation below: The 
principle of expression says that there are multiple expressions. Addition and subtraction operations are carried out from left to right, the expressions here are: i++, ++i, ++j, j++, which are all additions, then we can calculate these 4 expressions from left to right and it is OK , As follows: 
1. First calculate i++, after i++, the value of i is 2, and return the value 1 before ++, so the entire expression can become: 
1 + ++i + ++j + j++; // this The value of i is 2 
2. Then calculate ++i. After ++i, the value of i is 3, and 3 is returned, so the entire expression can become: 
1 + 3 + ++j + j++; // At this point The value of i is 3 
3. Then calculate ++j. After ++j, the value of j is 2, and it returns 2, so the entire expression can become: 
1 + 3 + 2 + j++; // The value of j at this time It is 2 
4, and then calculate j++. After j++, the value of j is 3 and returns 2, so the entire expression can become: 
1 + 3 + 2 +2; // The result is 8, and the value of j is 3 
 

Example 5 in detail 

int a = 0;
int b = 0;
a = a++;
b = a++;
System.out.println("a = " + a + ", b = " + b); // a = 1, b = 0

When it comes to question 5, it seems that there is no difficulty. Everyone should be able to solve it, but for the completeness of the article, I will break it down. You should calculate it yourself first, and then look at my decomposition: 
a = a++; After a++, the value of a is 1 and returns 0, so the value of a is changed from 1 back to 0 b = a++; after a++, the value of a is 1 and returns 0, and 0 is assigned to b, so b is 0, and a is still 1! !
 

 

Guess you like

Origin blog.csdn.net/qq_44624536/article/details/115125198