** 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?
this is a duplicate question.
EDIT: I can’t seem to find the original 😛 oh well
a = a++ uses the postincrement, which your compiler interprets as:
EDIT 2: What's the difference between X = X++; vs X++;?