Hi I got a small question conserning the following code: code 1: <code> int i = 5; int y = i++; System.out.println("i:"+i); //output i:6 System.out.println("y:"+y); //output y:5 </code> We use post incrementation when we want to initialize y, which is why i is 6 and y 5. Observ the following code. code2: <code> int i=5; i = i++; System.out.println("i:"+i); //output 5 int t = i++; System.out.println("i:"+i); //output 6 </code> Can anyone please discribe why i isn't incremented after i = i++. Thanks in advantage. /Svend Rost
execution happens this way according to precedence: 1- right hand side of the = is evaluated ==> 5 2- i is incremented AFTER putting its current value in the RHS evaluation due to unary postfix ++ ==> 6 3- right hand side is assigned to the left hand side of the = operator remember right hand side was evaluated before i was incremented, so right hand side still equals 5 although i is incremented. after the assignment, i gets back the 5.... got it? [ November 12, 2002: Message edited by: Alfred Kemety ]