What is x after “x = x++”?

后端 未结 17 1350
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-21 06:26

What happens (behind the curtains) when this is executed?

int x = 7;
x = x++;

That is, when a variable is post incremented and assigned to

相关标签:
17条回答
  • 2020-11-21 06:42
    x = x++;
    

    is equivalent to

    int tmp = x;
    x++;
    x = tmp;
    
    0 讨论(0)
  • 2020-11-21 06:43

    A construct like x = x++; indicates you're probably misunderstanding what the ++ operator does:

    // original code
    int x = 7;
    x = x++;
    

    Let's rewrite this to do the same thing, based on removing the ++ operator:

    // behaves the same as the original code
    int x = 7;
    int tmp = x; // value of tmp here is 7
    x = x + 1; // x temporarily equals 8 (this is the evaluation of ++)
    x = tmp; // oops! we overwrote y with 7
    

    Now, let's rewrite it to do (what I think) you wanted:

    // original code
    int x = 7;
    x++;
    

    The subtlety here is that the ++ operator modifies the variable x, unlike an expression such as x + x, which would evaluate to an int value but leave the variable x itself unchanged. Consider a construct like the venerable for loop:

    for(int i = 0; i < 10; i++)
    {
        System.out.println(i);
    }
    

    Notice the i++ in there? It's the same operator. We could rewrite this for loop like this and it would behave the same:

    for(int i = 0; i < 10; i = i + 1)
    {
        System.out.println(i);
    }
    

    I also recommend against using the ++ operator in larger expressions in most cases. Because of the subtlety of when it modifies the original variable in pre- versus post-increment (++x and x++, respectively), it is very easy to introduce subtle bugs that are difficult to track down.

    0 讨论(0)
  • 2020-11-21 06:44

    Post Increment operator works as follows:

    1. Store previous value of operand.
    2. Increment the value of the operand.
    3. Return the previous value of the operand.

    So the statement

    int x = 7;
    x = x++; 
    

    would be evaluated as follows:

    1. x is initialized with value 7
    2. post increment operator stores previous value of x i.e. 7 to return.
    3. Increments the x, so now x is 8
    4. Returns the previous value of x i.e. 7 and it is assigned back to x, so x again becomes 7

    So x is indeed increased but since x++ is assigning result back to x so value of x is overridden to its previous value.

    0 讨论(0)
  • 2020-11-21 06:47

    The statement:

    x = x++;
    

    is equivalent to:

    tmp = x;   // ... this is capturing the value of "x++"
    x = x + 1; // ... this is the effect of the increment operation in "x++" which
               //     happens after the value is captured.
    x = tmp;   // ... this is the effect of assignment operation which is
               //     (unfortunately) clobbering the incremented value.
    

    In short, the statement has no effect.

    The key points:

    • The value of a Postfix increment/decrement expression is the value of the operand before the increment/decrement takes place. (In the case of a Prefix form, the value is the value of the operand after the operation,)

    • the RHS of an assignment expression is completely evaluated (including any increments, decrements and/or other side-effects) before the value is assigned to the LHS.

    Note that unlike C and C++, the order of evaluation of an expression in Java is totally specified and there is no room for platform-specific variation. Compilers are only allowed to reorder the operations if this does not change the result of executing the code from the perspective of the current thread. In this case, a compiler would be permitted to optimize away the entire statement because it can be proved that it is a no-op.


    In case it is not already obvious:

    • "x = x++;" is almost certainly a mistake in any program.
    • The OP (for the original question!) probably meant "x++;" rather than "x = x++;".
    • Statements that combine auto inc/decrement and assignment on the same variable are hard to understand, and therefore should be avoided irrespective of their correctness. There is simply no need to write code like that.

    Hopefully, code checkers like FindBugs and PMD will flag code like this as suspicious.

    0 讨论(0)
  • 2020-11-21 06:48

    x = x++;

    This is the post-increment operator. It should be understood as "Use the operand's value and then increment the operand".

    If you want the reverse to happen i.e "Increment the operand and then use the operand's value", you must use the pre-increment operator as shown below.

    x = ++x;

    This operator first increments the value of x by 1 and then assigns the value back to x.

    0 讨论(0)
  • 2020-11-21 06:51

    ++x is pre-increment -> x is incremented before being used
    x++ is post-increment -> x is incremented after being used

    int x = 7; -> x get 7 value <br>
    x = x++; -> x get x value AND only then x is incremented
    
    0 讨论(0)
提交回复
热议问题