7

Understanding the difference between ++i and i++, the below example still feels counter-intuitive.
Could someone please explain the order of operations and assignments in the following example?

    int i = 0;
    i = i++;
    System.out.println(i); // 0

Namely on line two, why is i not incremented after the assignment?

Etheryte
  • 24,589
  • 11
  • 71
  • 116

1 Answers1

13

The simple way to see it is like this:

Step 1: Your int i = 0; line, which (of course) does this:

i = 0

Then we come to the i = i++; line, where things get interesting. The right-hand side of the = is evaluated, and then assigned to the left-hand side. So let's look at the right-hand side of that, i++, which has two parts:

Step 2:

temporary_holder_for_value = i

The value of i is read and stored away in a temporary location (one of the virtual machine registers, I expect). Then the second part of i++ is done:

Step 3:

i = i + 1

Now we're done with the right-hand side, and we assign the result to the left-hand side:

Step 4:

i = temporary_holder_for_value

The key is that last step. Basically, everything to the right of the = is done first, and the result of it is then assigned to the left. Because you used a post-increment (i++, not ++i), the result of the expression on the right takes i's value before the increment. And then the last thing is to assign that value to the left-hand side.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875