Guys,
I got the impression from atleast one post in here that the increment from a++ never gets writtent back to a, so I'm adding my comments.
Take note of the following two points from the JLS & Java's operator precedence
* 15.7.1 Evaluate Left-Hand Operand First - The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated.
* 15.14.2 Postfix Increment Operator ++ - The value of the postfix increment expression is the value of the variable before the new value is stored
* Operator precedence -
http://www.irixtech.com/java/tutorials/java-operator-precedence So, for the example, from the original post "a = a--", a-- puts 5 as the value in the expression & makes the final value of a as 4 per JLS 15.14.2. And now, since assignment will always happen in the end the final value of a ends up being 5 although a was 4 after a--.
Another example to consider is "a = a++ + a". In this case, per JLS 15.7.1 left-hand operands need to be fully evaluated before any part of the right-hand can be evaluated. So we evaluate a++, we get 5 in the expression & a has the value 6. Now we evaluate the right-hand operand which is simply substituting the value of a i.e. 6. Finally, the assignment is performed & a ends up being 11.
Take a minute to have a look at the said JLS articles, the JLS may seem geeky at times but all the answers are there.
JLS 15.7.1 JLS 15.14.2 HTH
Ashish Hareet
[ October 03, 2008: Message edited by: Ashish Hareet ]