Matthew Phillips
<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
The value of the postfix increment expression is the value of the variable before the new value is stored.
Originally posted by Valentin Crettaz:
Moreover, what is the advantage of writing i=i++ when only by doing i++ i's value would be incremented?[/QB]
<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
the most important thing here is that i++ evaluates to the value of i BEFORE the incrementation occurs.
So when evaluating i++, the current value of i (0) is stored somewhere (not relevant here), then the incrmetation proceeds (at which time i is 1) and then the assignment finally occurs, which has the effect of assigning the stored value (0) to i again. That's why i will always be 0.
The value of the prefix increment expression is the value of the variable after the new value is stored.
Rob
SCJP 1.4
Originally posted by Rob Ross:
Hey Valentin, is i++ and ++i an atomic operation? Ie, thread-safe?
<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
Rob
SCJP 1.4
<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes