a ^= b;
b ^= a;
a ^= b;
1最常用的:
public static void main(String[] args) {
int a = 5;
int b = 4;
a = a^b;
b = b^a;
a = a^b;
System.out.print(a + ":" + b);
}
这个是最快的
其次不是很好但是可行的
public static void main(String[] args) {
int a = 5;
int b = 4;
a = a+b;
b = a-b;
a = a-b;
System.out.print(a + ":" + b);
}和
public static void main(String[] args) {
int a = 5;
int b = 4;
a = a * b;
b = a / b;
a = a / b;
System.out.print(a + ":" + b);
}
这2种可能会内存溢出,不建议使用
最后一种,可能大家没看过的,可能是最难理解的:
public static void main(String[] args) {
int a = 5;
int b = 4;
b = a + 0 * (a = b);
System.out.print(a + ":" + b);
}