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
x = x++;
is equivalent to
int tmp = x;
x++;
x = tmp;
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.
Post Increment operator works as follows:
So the statement
int x = 7;
x = x++;
would be evaluated as follows:
So x is indeed increased but since x++ is assigning result back to x so value of x is overridden to its previous value.
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:
Hopefully, code checkers like FindBugs and PMD will flag code like this as suspicious.
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.
++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