疯狂java


您现在的位置: 疯狂软件 >> 新闻资讯 >> 正文

Java 参数传递都是值传递


 

 
Java 参数传递都是值传递,验证代码如下
 
public class ParamTransferTest {
     
    public static void swap(int a, int b) {
        int temp = a;
        a = b;
        b = temp;
        System.out.println("swap() a = " + a + " b = " + b);
    }
     
    public static void swap(DataWrap dataWrap) {
        int temp = dataWrap.a;
        dataWrap.a = dataWrap.b;
        dataWrap.b = temp;
         
        System.out.println("swap() datawrap = " + dataWrap + " Field a = " + dataWrap.a + " Field b = " + dataWrap.b);
    }
     
    public static void newObject(DataWrap dataWrap) {
        dataWrap = new DataWrap();
         
        System.out.println("newObject() datawrap = " + dataWrap + " Field a = " + dataWrap.a + " Field b = " + dataWrap.b);
    }
     
    public static void destoryObject(DataWrap dataWrap) {
        dataWrap = null;
         
        System.out.println("destoryObject() datawrap = " + dataWrap);
    }
     
    public static void swapObject(DataWrap a, DataWrap b){
        System.out.println("in swap before a = " + a + " b = " + b);
        DataWrap temp = a;
        a = b;
        b = temp;
        System.out.println("in swap after a = " + a + " b = " + b);
    }
     
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //1.PrimitiveTransferTest
        int a = 6;
        int b = 9;
         
        System.out.println("before swap, a = " + a + " b = " + b);
        swap(a, b);
        System.out.println("after swap, a = " + a + " b = " + b);
         
        //2.RefTransferTest
        DataWrap dataWrap = new DataWrap();
        dataWrap.a = a;
        dataWrap.b = b;
         
        System.out.println("before swap, dataWrap = " + dataWrap + " Field a = " + dataWrap.a + " Field b = " + dataWrap.b);
        swap(dataWrap);
        System.out.println("after swap, dataWrap = " + dataWrap + " Field a = " + dataWrap.a + " Field b = " + dataWrap.b);
         
        //3.in method create new object
        System.out.println("before newObject, dataWrap = " + dataWrap + " Field a = " + dataWrap.a + " Field b = " + dataWrap.b);
        newObject(dataWrap);
        System.out.println("after newObject, dataWrap = " + dataWrap + " Field a = " + dataWrap.a + " Field b = " + dataWrap.b);
         
        //4.in method destory object
        System.out.println("before destoryObject, dataWrap = " + dataWrap + " Field a = " + dataWrap.a + " Field b = " + dataWrap.b);
        destoryObject(dataWrap);
        System.out.println("after destoryObject, dataWrap = " + dataWrap + " Field a = " + dataWrap.a + " Field b = " + dataWrap.b);
         
        //5.swap object
        DataWrap dataWrapA = new DataWrap();
        DataWrap dataWrapB = new DataWrap();
        System.out.println("before swap, dataWrapA: " + dataWrapA + " dataWrapB = = " + dataWrapB);
        swapObject(dataWrapA, dataWrapB);
        System.out.println("after swap, dataWrapA: " + dataWrapA + " dataWrapB: " +dataWrapB);
    }
}
  DataWrap.java 
 
 
public class DataWrap {
    public int a;
    public int b;
}
  运行结果:
 
 
before swap, a = 6 b = 9
swap() a = 9 b = 6
after swap, a = 6 b = 9
before swap, dataWrap = DataWrap@659e0bfd Field a = 6 Field b = 9
swap() datawrap = DataWrap@659e0bfd Field a = 9 Field b = 6
after swap, dataWrap = DataWrap@659e0bfd Field a = 9 Field b = 6
before newObject, dataWrap = DataWrap@659e0bfd Field a = 9 Field b = 6
newObject() datawrap = DataWrap@2a139a55 Field a = 0 Field b = 0
after newObject, dataWrap = DataWrap@659e0bfd Field a = 9 Field b = 6
before destoryObject, dataWrap = DataWrap@659e0bfd Field a = 9 Field b = 6
destoryObject() datawrap = null
after destoryObject, dataWrap = DataWrap@659e0bfd Field a = 9 Field b = 6
before swap, dataWrapA: DataWrap@15db9742 dataWrapB = = DataWrap@6d06d69c
in swap before a = DataWrap@15db9742 b = DataWrap@6d06d69c
in swap after a = DataWrap@6d06d69c b = DataWrap@15db9742
after swap, dataWrapA: DataWrap@15db9742 dataWrapB: DataWrap@6d06d69c
  如上,简单数据类型就不多说了。让人疑惑的往往是在参数是引用的时候。
 
由代码中验证的第3.4.5.步可以看出实际上参数类型是引用的时候,也是值传递。在函数内部栈会创建自己的指向原引用对象的引用,所以传引用的时候,修改对象本身,执行完函数后,对象本身确实发生了变化。而如果只是修改引用本身,并不会对传入的引用造成影响,因为在函数内部修改的引用只是局部的。