Search code examples
javascjppost-increment

x = x++ doesn't increment because the ++ is applied after the assignment?


From page 280 of OCP Java SE 6 Programmer Practice Exams, question 9:

int x = 3;
x = x++;
// x is still 3

In the explanation we can read that:

The x = x++; line doesn't leave x == 4 because the ++ is applied after the assignment has occurred.

I agree that x is 3, I understand post-incremenation.

I don't agree with the explanation. I would replace "after" with "before".

I thought it works like this:

  1. x is 3.
  2. x++ is executed. I see this post-increment operator as a function:

    int operator++() {
        int temp = getX();
        setX(temp + 1);
        return temp;
    }
    

    So, after x++ execution, x is 4, but the value returned from x++ expression is 3.

  3. Now, the assignment =. Simply write returned 3 to x.

So, in my eyes ++ is applied before the assignment has occurred. Am I wrong?


Solution

  • ...the ++ is applied after the assignment has occurred.

    Okay, hold on. This is actually confusing and perhaps suggests an incorrect behavior.

    You have the expression:

    x = ( x++ )
    

    What happens is (JLS 15.26.1):

    1. The expression on the left-hand side of the assignment x is evaluated (to produce a variable).
    2. The expression on the right-hand side of the assignment ( x++ ) is evaluated (to produce a value).
    3. The evaluation of the right-hand side is: x is post-incremented, and the result of the expression is the old value of x.
    4. The variable on the left-hand side, x, is assigned the value produced by the evaluation of the right-hand side, which is the old value of x.

    So the post-increment happens before the assignment.

    (Therefore, as we know, the value of x after executing the statement x = x++; is the same as the value of x before executing the statement, which is 3.)

    So, in my eyes ++ is applied before the assignment has occurred. Am I wrong?

    You are right.

    Technically, the way it is specified is that x++ is evaluated before its result is stored and during the evaluation of the assignment operator. So x++ can be interpreted as happening either before or during the assignment. Not after, so the book appears to be wrong either way.

    Just for the heck of it, we may also look at some bytecode:

    /*                          int x = 3;                           */
    iconst_3 // push the constant 3 on to the stack         : temp = 3
    istore_0 // pop the stack and store in local variable 0 : x = temp
    /*                          x = x++;                             */
    iload_0  // push local variable 0 on to the stack       : temp = x
    iinc 0 1 // increment local variable 0 by 1             : x = x + 1
    istore_0 // pop the stack and store in local variable 0 : x = temp
    

    The iinc happens before the istore.

    †: The parentheses have no effect on the evaluation, they are just there for clarity.