It is generally believed that the predecessor + + is to first add the value of the variable 1, and then use the value plus 1 to participate in the operation, and then set + + is used to participate in the operation of the value, and then add the value of 1.
Let's look at the first example:
Package test;
public class Plus_test01 {public
static void Main (string[] args) {
int i = m;
i = i++;
System.out.println (i);
}
}
Guess what the result is?
Then look at the second one:
Package test;
public class plus_test02 {public
static void Main (string[] args) {
int k = m;
while (true) {
if (k++ > MB) {
//System.out.println (k);
break;
System.out.println (k);}}
Guess what the result is?
In fact, whether it's a predecessor + + or a back + +, you add the value of the variable to 1 before continuing the calculation. the real difference between the two is that the first + + is to add the value of the variable after 1, using the value of the variable, and then set + + is to first assign the variable to a temporary variable, then add 1 to the value of the variable, and then use that temporary variable for the operation.
For the following code fragment (predecessor + +):
int i=1;
int j=++i*5;
The actual second sentence is equivalent to:
I+=1; Will I plus 1
j=i*5; The value added after 1 is calculated, and the result is: 10
For the following code fragment (post + +):
int i=1;
int j=i++*5;
The second sentence is equivalent to:
int temp=i; Assign I to a temporary variable
I+=1; Will I plus 1
j=temp*5; Calculates a temporary variable with this result: 5
For the first example, the equivalent:
int temp=i;
I+=1;
I=temp; //
So the result should be unchanged, that is, 100.
The assembly code for the first example is:
public static void Main (java.lang.string[]);
Descriptor: ([ljava/lang/string;) V
flags:acc_public, acc_static
Code:
stack=2, locals=2, args_size=1
0:bipush
2:istore_1
3:iload_1
4:iinc 1, 1//local var, second plus 1
7:istore_1 / Save to local var
8:getstatic #16 //Field Java/lang/system.out:ljava/io/printstream;
The 11:iload_1//load parameter is the second in the stack, which is still the
12:invokevirtual #22 //Method Java/io/printstream.println: (I) V
15:return
For the second example, it is not difficult, the result is 101, pay attention to the process, you can not make such a mistake in the future. (Process is: First comparison temp=i,temp>100,, obviously not set up, will I+=1, jump to syso that sentence, print of course is 101, again cycle also have temp=i,temp>100, this is established, and then i+=1, directly out of circulation, Does not execute a while inside statement).
Assembly of the second example (only the main method is selected):
public static void Main (java.lang.string[]);
Descriptor: ([ljava/lang/string;) V
flags:acc_public, acc_static
Code:
stack=2, locals=2, args_size=1
0:bipush //100 pressure stack
2:istore_1// save to Second local Var (first local var is method parameter)
3:iload_1 // From the second local var load
4:iinc 1, 1 //adds 1 to the int value of the 2nd position of the locals Var, and the result is still in regional Var, the operand stack top 1 does not change
7: Bipush 100//100 pressure stack
9:if_icmple 15//compare operand stack top two int integer value, if the first is less than or equal to the second, then jumps to 15 rows
12:goto 25//Otherwise jump to 25 lines (ie operand stack top 1> operand stack top 2)
15:getstatic #2 //Field java/lang/system.out:ljava/io/ PrintStream;
18:iload_1////from the first local var load
19:invokevirtual #3 //Method Java/io/printstream.println: (I) V//Call this approach
22:goto 3//Jump back to 3 again, recycle
25:return//exit
A third example:
Package test;
public class plus_test03 {
static int proplus () {
int i =;
int j = ++i;
Return J; The
static int postplus () {
int i =;
int j = i++;
Return J; The
public
static void Main (string[] args) {
System.out.println (ProPlus ());//56
System.out.println (Postplus ());//55
}
}
Compilation of a third example:
static int ProPlus ();
Descriptor: () I
flags:acc_static
Code:
stack=1, locals=2, args_size=0
0:bipush 55//55 pressure Stack
2 : Istore_0 ///to store the top of the int-type stack to the first local var
3:iinc 0, 1//the first local var plus 1 6:iload_0//from the local var load
7:istore_1 //Save to second local var
8:iload_1 //stack top for second local var
9:ireturnstatic int postplus ();
Descriptor: () I
flags:acc_static
Code:
stack=1, locals=2, args_size=0
0:bipush 55
2:istore_0
3:iload_0 //load to stack
4:iinc 0, 1//First local var plus 1
7:istore_1
8:iload_1
9:ireturn
Visible, the difference between the predecessor + + and Post + + is the part of the blue above (//First local VAR plus 1), which is reversed. For a predecessor, the number in the local Var is added to 1 and then loaded into the stack, which is then loaded into the stack from the Stack local VAR, then adds 1 to the local Var, which is equivalent to leaving a backup.
Conclusion:
One. Front, and back + + is the value of the variable plus 1, rather than the predecessor + + first add 1 then operation, and then add + + first operation after adding 1.
Two. From the program, Post + + assigns a variable to a temporary variable, then adds a value of 1 to the variable, and then uses that temporary variable to participate in the operation.
Three. From the instructions, the Post + + in the implementation of value-added instruction (IINC) before the value of the variable into the stack, the implementation of value-added instructions, the use of the previous pressure into the stack value.
Hope that this article, a thorough understanding of the predecessor + + and after the operation of the difference between + +, thank you for your support for this site!