java - What is x after "x = x++"?

ID : 10246

viewed : 15

Tags : javaoperatorspost-incrementjava

Top 5 Answer for java - What is x after "x = x++"?

vote vote

91

x = x++; 

is equivalent to

int tmp = x; x++; x = tmp; 
vote vote

84

x does get incremented. But you are assigning the old value of x back into itself.


x = x++; 
  1. x++ increments x and returns its old value.
  2. x = assigns the old value back to itself.

So in the end, x gets assigned back to its initial value.

vote vote

71

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.

vote vote

63

int x = 7; x = x++; 

It has undefined behaviour in C and for Java see this answer. It depends on compiler what happens.

vote vote

59

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.

Top 3 video Explaining java - What is x after "x = x++"?

Related QUESTION?