Don't remember which exam i saw this one at...but i just can't seem to understand why this should happen.... It seems obvious that the foll two code should give an output: a=3, code 1:{ int a=2; int y= a++; System.out.println("a="+a); } code 2: { int a=2; a=a+1; System.out.println("a="+a); } However this code below outputs a=2. { int a=2; a= a++; System.out.println("a="+a); } Why is the post incremented value of 'a' not printed in the last code??? Help !!!
[This message has been edited by eskay kumar (edited July 23, 2000).]
Joseph Zhou
Ranch Hand
Joined: Aug 01, 2000
Posts: 129
posted
0
As you see, the expression a++ is 2, and will assign to a (using =). The = have the lowest precedence than others, so, it will be the last step to be carry out.
sunilkumar ssuparasmul
Ranch Hand
Joined: Dec 13, 2000
Posts: 142
posted
0
{ int a=2; a= a++; System.out.println("a="+a); }
SEE it happens like this 1. a= 2 is stored in some memory location 2. a is incremented and stored in some other location 3. while assigning back it takes the original memory location i.e of step 1 4 . ie y u get output=2 if u try giving some other variable like b=a++ u would get the desired answer thanks sunil
------------------ "Winners don't do different things They do things differently"
"Winners don't do different things<br /> They do things differently"