Interpret i++ and ++i with Java bytecode

Source: Internet
Author: User

Explaining the i++ and ++i-tutuhatec columns with Java bytecode-blog channel-csdn.net

Look at 4 topics first:

①int i = 0;

i = i++;

②int i = 0;

i = ++i;

③int i = 0;

int j = 0;

j = i++ + i++;

④int i = 0;

int j = 0;

j = i++ + i++ + i++;

What are the I and j in each question?

With the MyEclipse test, the results are ①i = 0,②i = 1,③i = 2,j = 1,④i = 3,j = 3.

i++ and ++i problems that plague many people. There are a variety of explanations on the Internet and in books. Now you can determine how the two statements are executed by parsing the bytecode.

Let's start with the meaning of the bytecode we're going to use today.

Bytecode

Stack

Before->after

Description

Iconst_0

->0

Loads the int value 0 onto the stack

Istore_1

Value->

Store int value into variable 1

Istore_2

Value->

Store int value into variable 2

Iinc

No Change

Increment local variable #index by signed byte const

Iload_1

->value

Loads an int value from variable 1

Iadd

Value 1,value 2->result

Adds 2 INTs together

Note the two points needing attention:

The ①iinc operation is parameterized, but omitted here, abbreviated to IINC, which corresponds to the self-add operation, and the operation does not have any changes to the stack;

The result is only preserved in the stack after the ②iadd operation.

Next is the four-segment program's main byte code:

①iconst_0②iconst_0③iconst_0④iconst_0

Istore_1 istore_1 istore_1 Istore_1

Iload_1 iinc iconst_0 Iconst_0

Iinc iload_1 istore_2 istore_2

Istore_1 istore_1 iload_1 Iload_1

Iinc Iinc

Iload_1 iload_1

Iinc Iinc

Iadd Iadd

Istore_2 iload_1

                                                                                   iinc

                                                                                   iadd

Istore_2

Now explain ①. The first step is to put an int constant 0 in the stack, and the second step assigns it to the first variable, our I; The third step is to deposit the value of the first variable i into the stack, and the fourth step is self-adding in the space of I, and the value of the third step in the stack does not change. The fifth step is to assign the value of the third step to the stack to the first variable i. In other words, I was actually self-added, but was overwritten by my own original value. From here we can see that the self-add operation has a higher priority than the assignment operation.

Look at ② again. Compared with ①, the difference is that the ++i is the first self-added, and then the value of the self-added to the stack, so the last assignment to I is 1, not 0.

Where's ③? First, ③ has one more variable than the first two, but that's not the point. Looking down, the value of the first variable i (0) is deposited into the stack, I self-add, and the value of the first variable I (1) is deposited in the stack, and then added. At this point, there are two values in the stack, 0 and 1, the result of adding the operation is 1, and then assigning the value to the second variable J. This is a reversal of what we were told before, plus the priority of operations is higher than self-heightening. This is what bothers us, in fact, as long as the value is read into the stack, the next step is self-added, only to get two parameters, only to add the operation.

Look at ④. Front and ③, just to do the first add operation, the result (1) is saved in the stack, is not assigned to J, and then read the value of I (2) to the Stack,i self-addition, the addition of two parameters, 1 and 2, the second operation of the result is 3, assigned to J.

By this point, the problem has been solved.

Interpret i++ and ++i with Java bytecode

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.