Sunday, June 18, 2006

i=i++ produces the output "0" instead of "1".

The code
int i = 0;
i = i++;
System.out.println(i);
produces the output "0" instead of "1".

"i = i++" roughly translates to

int oldValue = i;
i = i + 1;
i = oldValue;

2 comments:

Jayaprabhakar said...

I think you will like the explanation that I have given here.

http://skeletoncoder.blogspot.com/2006/09/java-tutorials-i-i.html

This shows the exact Byte Codes, and illustrates step by step at the JVM level.

Jayaprabhakar said...


Java Tutorials: i = i++