int i = 0; i = i++ + m(i); when u use i++ value used to evaluate expression is 0 however value of i becomes 1 this value is used in the function call m(i) hence 1 is printed function returns 0 hence new i = 0+0 hence the output of 1,0
just to avoid confusion for anybody reading this: the output of
int i=0; int k=i++; System.out.println(k);
is 0 (at least on my system )
Joined: Sep 20, 2005
Thanks for the replies guys, I think I am close to deciphering this one.
int k=0; k= k++ + k++; System.out.println("k:"+k);
So why does this print out 1? Following what has been said, I would have thought:
int k = 0; k = k++ + k++; k = 0 + 0; k = 0;
But this doesn't happen. It in fact evaluates to 1. ...so it looks as if one of the k increments gets evaluated and one gets disregarded.
int k = 0; k = k++ + k++ + k++; I would have thought that this means: k = 0 + 0 + 0; k = 0;
... but in this example k becomes 3.
What I think is happening is this: int k = 0; k = k++ [this k evaluates to 0, but increments the next k] + k++ [this k evaluates to 1, and increments the next k] + k++; [this k evaluates to 2, but the increment is DISREGARDED because there is no further use of k in the line of code]
So we have: k = 0 + 1 + 2 = 3;
int k = 0; k = k++ + k++ + k++ + k++; evaluates to: k = 0 + 1 + 2 + 3; And thus: k = 6;
I'm just making assumptions here. I don't understand the reason why the last increment gets disregarded. So if someone has any more stable knowledge, please share it!