From bytecode to analyze, i ++ i ++ distinguished

++ / - is a special arithmetic operators, it requires two operands in arithmetic operation performed in the operator, and the increment decrement operator is an operand

Prefix increment (++ a): for the first increment operation, then operation expression;

Postfix increment (a ++): expression operator to perform, and then increment operator.

        int x = 5, y=5;
        int a = 2*++x;
        int b = 2*y++;
        System.out.println(a+"=="+b);

Output: a = 12, b = 10; 

++ prefix, the first increment operation, = "a = 2 * 6

+ Suffix, the first calculation expression, = "b = 10;

  

 1     public void prefixAdd(){
 2         int i = 100;
 3         i = ++i;
 4         System.out.println(i);
 5     }
 6 
 7     public void postfixAdd(){
 8         int i = 100;
 9         i = i++;
10         System.out.println(i);
11     }
12 
13     public void varAdd(){
14         int i = 100;
15         int y = i++;
16         System.out.println(i+"=="+y);
17     }

 

Here is a comparison of the difference between nagging second and third methods. This need to see the distribution of the compiled bytecode to the underlying data.

 

 

 iload_1: reading from the local variable stack frame, 1 represents the index

istore_1: storing the local variables, index 1 denotes

iinc: adding a constant value of local variable

Prefix and postfix ++ ++ difference:

 

 

 

 When the value of the proceeds to the seventh row, istore_1 or storage 100, and local variables has been increased to overwrite 101 1. So when 11-line, again iload_1 or 100.

 

 

 

 

 

Line prefix + 3 increased local variables, while line 101 has been read 6, line 7 is again stored in the local variable 1 is 101, 11 is read 101.

 

Suffix ++: first read local variables, and then to increase local variables

Prefix ++: first, to increase local variables, and then read.

 

  The third method, using a variable value of the first iload_1 (i = 100) is stored in the local variables istore_2, the value of the local variable 1 is 101, and is not covered.

 

 

 

 

Therefore, the output:

101
100
101==100

i = i ++; ---> int y = i ++; this change can be better understood, i = i ++; Why i = 100.

 

Guess you like

Origin www.cnblogs.com/song27/p/12359702.html
I
"I"
I: