歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

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.步可以看出實際上參數類型是引用的時候,也是值傳遞。在函數內部棧會創建自己的指向原引用對象的引用,所以傳引用的時候,修改對象本身,執行完函數後,對象本身確實發生了變化。而如果只是修改引用本身,並不會對傳入的引用造成影響,因為在函數內部修改的引用只是局部的。

Copyright © Linux教程網 All Rights Reserved