** Dup: What's the difference between X = X++; vs X++;? **
So, even though I know you would never actually do this in code, I'm still curious:
public static void main(String[] args) {
int index = 0;
System.out.println(index); // 0
index++;
System.out.println(index); // 1
index = index++;
System.out.println(index); // 1
System.out.println(index++); // 1
System.out.println(index); // 2
}
Note that the 3rd sysout
is still 1
. In my mind the line index = index++;
means "set index to index, then increment index by 1" in the same way System.out.println(index++);
means "pass index to the println method then increment index by 1".
This is not the case however. Can anyone explain what's going on?