The difference between java i=i++ and j=j++

The difference between i=i++;j=j++

i=i++------------The sequence of this statement in java should be like this (tmp=i;i++;tmp==i)

When the java compiler encounters i++ and i- - , it will re-allocate a memory space for the variable operation to store the original value, and after completing the assignment operation, this memory will be released. Let's first take a look at if is the case of j=i++ :

 

The original value of i is stored in the newly opened memory, and finally this value will be assigned to j , so that after j=i++ , j will get the value of i, and i will be added by itself, so after releasing the memory, the original storage The places where j and i will get the values ​​will be: j ( the value at this time is equal to the initial i value ) and i ( the value of i after self-addition ) .

Take a look at the case of i=i++ :

 

 

The original value of i is stored in the newly developed memory, self-addition is performed to the i at the beginning, and finally the i in the latter memory is assigned to the i at the beginning .

 

 

Let's take a look at the results of decompilation of these two programs

This is i=i++;

 

 

 

1: iconst_0    integer constant value 0 is pushed onto the stack

2: istore_1    stores the top-of-the-stack integer value into the second local variable

3: iload_1 pushes   the second integer local variable onto the stack

4: iinc 1, 1  specifies that the integer variable increases the specified value

5: istore_1  stores the top-of-the-stack integer value into the second local variable

 j = i++;

 

After decompiling it looks like this

7: iconst_0  integer constant value 0 is pushed onto the stack

8: istore_1  stores the top-of-the-stack integer value into the second local variable

9: iconst_0  integer constant value 0 is pushed onto the stack

10: istore_2  stores the top-of-the-stack integer value into the third local variable

11: The third integer local variable of iload_2 is pushed   onto the stack

12: iinc 2, 1  specifies that the integer variable increases the specified value

15: istore_1  stores the top-of-the-stack integer value into the second local variable

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326398644&siteId=291194637