Analysis of i=i++ problems in Java

Source: Internet
Author: User

http://www.ticmy.com/?p=43

Focus: The execution of the local variable table and the operand stack.

Use Javac to compile and then use JAVAP-C test to decompile this class to see its bytecode, as follows (only the main method is extracted):

public static void Main (java.lang.string[]);

Code:

0:iconst_0

1:istore_1

2:iload_1

3:iinc 1, 1

6:istore_1

7:getstatic #2; Field Java/lang/system.out:ljava/io/printstream;

10:iload_1

11:invokevirtual #3; Method java/io/printstream.println: (I) V

14:return

Here, I start analysis from line No. 0 (analysis "" means the stack, the bottom of the stack is on the left, the top is on the right):

0: Press the constant 0 into the stack, the contents of the stack: "0"

1: Pop the element at the top of the stack, that is, 0, and save it to the local variable area index to 1 (that is, the variable i). Stack content: ""

2: Press the value of the local variable area index to 1 (that is, the variable i) into the stack, the stack content: "0"

3: Add one to the value of the local variable area index of 1 (that is, constant I), at which point the local variable area index is 1 (that is, the value of i) is 1. Stack content: "0"

6: The top element of the stack pops up and is saved to the local variable area index of 1 (that is, I), at which I becomes 0. Stack content: ""

7: Gets the class variable represented by index 2 in the constant pool, which is System.out. Stack element: ""

10: Press the value of the local variable area index to 1 (that is, I) into the stack. Stack element: "0"

11: Call the constant pool index to 3 method, that is, System.out.println

14: Return to Main method

=================================================================

Java uses the intermediate cache variable mechanism:

i=i++; equivalent to:

Temp=i ( i on the right side of the equals sign)

i=i+1; ( I on the right side of the equals sign )

I=temp; ( I on the left of the equals sign )

and I=++i; is equivalent to:

i=i+1;

Temp=i;

I=temp;

=================================================================

15:iload_2// The variable i value 0 of the local variable area index 2 is pressed into the operand stack /c10>

16:iinc 2, 1//Add 1 to the value of the local variable area 2 index , and the operand stack does not change

19:istore_2// 0 popup at the top of the stack , assigned to the i of the local variable area index 2

=================================================================

The local variable table of main and fremin I at the respective ( runtime's current stack frame <StackFrame>) <local Variable in table>, two variables are different. main call fremin after the end of the stack, but did not change the main stack frame of the local variable table I value.  

i=i++;i++ 's Assembly instruction is (iinc 1,1), he only changes the value of the local variable table , but when i= assignment, the stack top of the operand stack is still 0 , save to the local variable table, and then cover the slot1 to 0, so get 0.

iconst_0//0

Istore_1//slot 1:i=0

Iload_1// remove 0

Iinc//slot 1:i=1

istore_1//Slot 1 = 0 overwrite

Iload_1//println 's 0

Output.

=================================================================

Analysis of i=i++ problems in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.