//两数交换不用中间变量
public class TwoChange {
public static void main(String[] args) {
int a = 3, b = 5;
//第一种方式采用加减法实现
/* a = a + b;
b = a - b;
a = a - b;*/
//第二种采用异或运算实现^,底层采用的是二进制异或运算,相同为0,不同为1,性能最好
a = a ^ b;
b = a ^ b;
a = a ^ b;
System.out.println("a=" + a + " " + "b=" + b);
}
}
结果:a=5 b=3