This is an unintuitive (but not "weird"!) behaviour when using post-increment.
The statement j = j++
does this:
- Evaluate the LHS
- In this case, nothing special happens because you simply named a variable, but this may not always be the case, e.g.
foo() = j++
- In this case, nothing special happens because you simply named a variable, but this may not always be the case, e.g.
- Evaluate the RHS
- Take the current value of
j
(0), and remember it as the result; - Increment
j
(to get 1);
- Take the current value of
- Assign the RHS to the LHS
- recall that the RHS evaluates to that "remembered" value of
j
(0).
- recall that the RHS evaluates to that "remembered" value of
The result is a no-op.
The key here is that the entire RHS is evaluated and the post-increment performed, before the final assignment.
http://www.ecma-international.org/ecma-262/5.1/#sec-11.3.1
http://www.ecma-international.org/ecma-262/5.1/#sec-11.13.1