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

如上,简单数据类型就不多说了。让人疑惑的往往是在参数是引用的时候。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/3d60398e328c82e7499f14ee45e2e0a0.html