日期:2014-05-19  浏览次数:20638 次

交换两个int参数的值和两个integer的值有什么区别
交换两个int参数的值和两个integer的值有什么区别?
下面的代码我运行是可以的,不知道两者有什么区别。
还有会不会和jdk的版本有关系呢?
Java code

public class B {
    public static void main(String[] args) {
        c(5,3);
        d(new Integer(2),new Integer(6));
    }
    
    public static void c(int a,int b){
        System.out.println(a);
        System.out.println(b);
        int c=0;
        c=b;
        b=a;
        a=c;
        System.out.println(a);
        System.out.println(b);
    }
    
    
    public static void d(Integer a,Integer b){
        System.out.println(a);
        System.out.println(b);
        Integer c=0;
        c=b;
        b=a;
        a=c;
        System.out.println(a);
        System.out.println(b);
    }
}



------解决方案--------------------
Java code

    public static void main(String[] args) {
        int d = 5;
        int e = 6;
        c(d,e);
        System.out.println(d);
        System.out.println(e);
        
        Integer a = new Integer(6) ;
        Integer b =new Integer(3) ;
        d(a, b);
        System.out.println(a);
        System.out.println(b);
    }
    
    public static void c(int a,int b){
        System.out.println(a);
        System.out.println(b);
        int c=0;
        c=b;
        b=a;
        a=c;

    }
    
    
    public static void d(Integer a,Integer b){
        System.out.println(a);
        System.out.println(b);
        Integer c = null;
        c = b;
        b = a;
        a = c;

    }

------解决方案--------------------
不太明白楼主想要什么啊?如楼上所说,交换只是发生在方法内部,个人看法如下:
1.没有实际区别,只是定义变量上有差异。
2.和JDK版本没有关系。
------解决方案--------------------
没有区别
------解决方案--------------------
我猜lz是想看到的知识点是传参是传地址,还是传值的备份的问题吧。