There is a code:
int i =0; i = i++; System.out.println(i);
Will output 0 to the console.
The question is why – 0 ?
Offtopabout such construction in
This code can output 0, 1 and, generally speaking, anything.
i = i++is
undefined behaviorby the standard.
There is a standard which defines such concept as sequence point. So, parsing this expression in the light of these same
sequence pointsinvolves a double change in the value of
sequence point'ами. Which by standard leads to undefined behavior.
You can read more here.
Javaeverything is simpler – the given code unambiguously corresponds to the following code. More – here.
int temp = i; // temp = 0 i++; // i = 1 i = temp; // i = 0