I tried "swap variable in java without temp" in Java and I found something that bothers me:
int a = 1, b = 2;
b^= a ^= b ^= a;
System.out.println(a + " vs " + b);
The output shows
2 vs 0
However if I separate the leftmost assignment as a individual statement:
int a = 1, b = 2;
a ^= b ^= a;
System.out.println(a + " vs " + b);
b^=a;
System.out.println(a + " vs " + b);
The output is
2 vs 3
2 vs 1
Now the output is as expected.
In C++, the evaluation is ensured from right to left. What the difference, in terms of language spec, tells Java could lead such expected result?