Originally posted by ryan burgdorfer:
Lam,
Your explanation is correct, except for the part about assigning the post-incremented x to y. Try compiling/running this, you will see:
<pre>
public class testPost
{
public static void main( String[] args )
{
int x = 1 ;
int y = x++ ;
System.out.println( y ) ;
}
}</pre>
Hi Ryan,
Sorry!, I don't quite get what you want to say! I believe that I mentioned of the fact that if instead of letting x = x++, if one assign x = y++, y will increase its value by one.
In your example, you assign y = x++. That is a flip from what I said. And actually it does no matter what variables we use. The problem is this:
int x = 2;
x = x++;
(expectation x will be 3 - the answer is 2)
When I said about let x = y++, this is what I mean:
int y = 2;
int x = y++;
(expectation x is still 2 but y should be 3)
Similarly if we
exchange x and y, we will get your code:
int x = 2;
int y = x++;
(expectation y will be 2 but x would be 3)
The last two cases fool people to into thinking that x = x++ would inccrement x by one. And they were suprised that it would not and retain its original! In short, people thought that post-increment would eventually force an increment to the variable that the ++operator apllied to. But that is not always as shown by x = x++, a very specific case where the LHS and the RHS deal with the same variable..post-increament operator's operation would be overriden!
Once again, I do not catch what you want to convey to me. If you can be specific, I would certainly try to entertain.
Regards,
Lam