Wednesday, August 24, 2005 06:05 PM
This is one of those stupid idiosyncracies that only people that write compilers need to worry about. ie, make sure your compiler does it the same way that all other Java compilers do it.
This code is ambiguous and nobody should ever use it in the real world. The only people that do, are testing compilers or demonstrating that they know the language better than anybody else. In a real world situation, you would never write code like that, and if you found some written like that you would correct it. Normal programmers would write i=5; i++; or i=5; i=i; or i=5; i=i+1; or i=6; so that the meaning is perfectly clear.
i=i++ could be interpreted two different ways. It is complicated because one statement has two assignments to the same variable. i++ returns the original value of i, then adds one to i and stores it back into i.
Interpretation that returns 6:
Interpretation that returns 5:
The only difference between the two interpretations is the order of the two assignments back to i.