1.須要添加第三方變量(開發中經常使用):spa
class Hello2 { public static void main(String[] args) { int x = 10; int y = 5; int temp; temp = x ; x = y; y = temp; System.out.println("x = " + x + ",y = " + y); } }
結果:code
2.不須要第三方變量(1)(有弊端,有可能超過int的取值範圍):blog
class Hello2 { public static void main(String[] args) { int x = 10; int y = 5; x = x + y; y = x - y; x = x - y; System.out.println("x = " + x + ",y = " + y); } }
結果:開發
不須要第三方變量(2)(用^來作):class
class Hello2 { public static void main(String[] args) { int x = 10; int y = 5; x = x ^ y; y = x ^ y; x = x ^ y; System.out.println("x = " + x + ",y = " + y); } }
結果:變量