采用位异或方式将两个变量数值调换
1 package debug; 2 3 class Demo2 { 4 public static void main(String[] args){ 5 int a = 1; 6 int b = 2; 7 System.out.println("a:" + a + ",b:" + b); 8 9 //方法一,采用异或方法 10 a = a ^ b; 11 b = a ^ b; 12 a = a ^ b; 13 System.out.println("a:" + a + ",b:" + b); 14 15 //方法二,一句话方法 16 b = (a + b) - (a = b); 17 System.out.println("a:" + a + ",b:" + b); 18 } 19 }