3番目の変数を使用しないで、2つの変数間の値の伝達と交換を実現します.

2206 ワード

public class ChangeTest {  
02	        
03	    public void changeMethodA(int a, int b){  
04	        System.out.println("changeMethodA \ta:"+a+"\tb:"+b);  
05	        a = a + b - (b = a);  
06	       System.out.println("changeMethodA \ta:"+a+"\tb:"+b);  
07	    }  
08	        
09	    public void changeMethodB(int a, int b){  
10	        System.out.println("changeMethodB \ta:"+a+"\tb:"+b);  
11	        b = a + (a = b)*0;  
12	        System.out.println("changeMethodB \ta:"+a+"\tb:"+b);  
13	    }  
14	        
15	    public void changeMethodC(int a, int b){  
16	        System.out.println("changeMethodC \ta:"+a+"\tb:"+b);  
17	        a = a + b;  
18	        b = a - b;  
19	        a = a - b;  
20	        System.out.println("changeMethodC \ta:"+a+"\tb:"+b);  
21	    }  
22	        
23	    public void changeMethodD(int a, int b){  
24	        System.out.println("changeMethodD \ta:"+a+"\tb:"+b);  
25	        a = a * b;  
26	        b = a / b;  
27	        a = a / b;  
28	        System.out.println("changeMethodD \ta:"+a+"\tb:"+b);  
29	    }  
30	        
31	    public void changeMethodE(int a, int b){  
32	        System.out.println("changeMethodE \ta:"+a+"\tb:"+b);  
33	        a = a^b;  
34	        b = a^b;  
35	        a = a^b;  
36	        System.out.println("changeMethodE \ta:"+a+"\tb:"+b);  
37	    }  
38	        
39	    public static void main(String[] args) {  
40	        ChangeTest changeTest = new ChangeTest();  
41	        changeTest.changeMethodA(1, 10);  
42	        System.out.println("-----------------------------------");  
43	        changeTest.changeMethodB(2, 9);  
44	        System.out.println("-----------------------------------");  
45	        changeTest.changeMethodC(3, 8);  
46	        System.out.println("-----------------------------------");  
47	        changeTest.changeMethodD(4, 7);  
48	        System.out.println("-----------------------------------");  
49	        changeTest.changeMethodE(5, 6);  
50	    }  
51	  }