方式一:
public static void main(String[] args) { int a = 3; int b = 4; int temp = a; a = b; b = temp; System.out.println("a=" + a + ",b=" + b); // a=4,b=3 }
方式二:
public static void main(String[] args) { int a = 3; int b = 4; a = a + b - (b = a); System.out.println("a=" + a + ",b=" + b); // a=4,b=3 }
方式三:
public static void main(String[] args) { int a = 3; int b = 4; a = a + b; b = a - b; a = a - b; System.out.println("a=" + a + ",b=" + b); // a=4,b=3 }
方式四:
public static void main(String[] args) { int a = 3; int b = 4; a = a ^ b; b = a ^ b; a = a ^ b; System.out.println("a=" + a + ",b=" + b); // a=4,b=3 }