In depth explanation of i = i + + and i = + + i in java
- 2021-08-12 02:40:01
- OfStack
public class Count {
public static void main(String[] args) {
int i = 0;
i = i++ ;
System.out.println(i);
}
}
The i output from the above code is 0. If i = i + + is replaced by i = + + i, it will output 1 again. This is because i + + is assigned first and then calculated, but why assign first?
public static void main(String[] args) {
int i = 0;
i++ ;
}
public static void main(java.lang.String[]);
Code:
0: iconst_0 Will int Type 0 Push to the top of the stack (find 0 )
1: istore_1 Put the top of the stack int Type value is stored in the first 2 Local variables (variables are i )
2: iinc Will specify the int Type variable increases the specified value ( Such as i++, i--, i+=2 Etc )
5: return
The above figure shows the bytecode of i + +, 1 touch 1 sample of + + i and i + +, 0 and 1 are the most basic values for i, and iinc is to increase i by itself.
But what if you add 1 i = i + +?
// i = i++;
public static void main(java.lang.String[]);
Code:
0: iconst_0 Will int Type 0 Push to the top of the stack
1: istore_1 Put the top of the stack int Type value is stored in the first 2 Local variables
2: iload_1 Will be 2 A int Type local variables are pushed to the top of the stack
3: iinc Will specify the int Type variable increases the specified value ( Such as i++, i--, i+=2 Etc )
6: istore_1 Put the top of the stack int Type value is stored in the first 2 Local variables
7: return
0 and 1 are still assigned to i as 0, 2 is to push the current value of i to the top of the stack, then iinc increases i of the local variable table, and 6 is to assign the value of the top of the stack to i. Note that when 2 is pushed to the top of the stack, it is 0, so it changes back to 0.
// i = ++i
public static void main(java.lang.String[]);
Code:
0: iconst_0 Will int Type 0 Push to the top of the stack
1: istore_1 Put the top of the stack int Type value is stored in the first 2 Local variables
2: iinc Will specify the int Type variable increases the specified value ( Such as i++, i--, i+=2 Etc )
5: iload_1 Will be 2 A int Type local variables are pushed to the top of the stack
6: istore_1 Put the top of the stack int Type value is stored in the first 2 Local variables
7: return
When i = + + i, iinc goes one step ahead, indicating that before the value of i is pressed to the top of the stack, it is self-increasing, so the value of the top of the stack is 1. At this time, it is self-increasing when it is assigned to i in the local variable table.
Summary: The iinc operation is directed at the local variable table, Without going through the operation stack, the so-called pre-assignment of i + + means that i stores the original value into the operand stack before self-increment from the bytecode level. Therefore, if b = i + + is executed, the reason why b is equal to the original value of i is because the value of the operand stack is the value before self-increment, while the self-increment operation of + + i is advanced, so after b = + + i, the value of b and i are 1.
Summarize