public class Main
{
public static void main(String[] args)
{
int a,b,c,d;
a=1;b=2;
a^=b^=a^=b;
System.out.println(a+" "+b);
c=1;d=2;
c^=d;
d^=c;
c^=d;
System.out.println(c+" "+d);
}
}
I use a^b^=a^=b to swap a and b. However the output of this program is
0 1
2 1
I am a new one in Java, and I don't know why a is 0. Is it a bug of my java runtime environment? Or there are something special in Java I don't know? Here is java -version.
java version "1.7.0_65"
OpenJDK Runtime Environment (fedora-2.5.2.5.fc20-x86_64 u65-b17)
OpenJDK 64-Bit Server VM (build 24.65-b04, mixed mode)
Sorry it's my first question on stack overflow ... if I forgot some rules, tell me please, thank you.