Summary of method parameter modification in java

catalog

Overall code

String and basic type

Object, container class, array

summary

Overall code

package test.t05new;




import java.util.ArrayList;
import java.util.Arrays;

public class Test9 {
    public static void main(String[] args){
        //test String
        String str = "value";
        TestClass tc = new TestClass();
        System.out.println("str before is "+str+" ,memory address is "+System.identityHashCode(str));
        tc.ChangeVlaue(str);
        System.out.println("str now is "+str+" ,memory address is "+System.identityHashCode(str));
        System.out.println("--------------------------------------");
        //test int
        int num = 0;
        System.out.println("num before is "+num+" ,memory address is "+System.identityHashCode(num));
        tc.ChangeVlaue(num);
        System.out.println("num now is "+num+" ,memory address is "+System.identityHashCode(num));
        System.out.println("--------------------------------------");
        //test objectclass
        TestClass testClass = new TestClass();
        System.out.println("testClass before is "+testClass+";num: "+testClass.num+" ,memory address is "+System.identityHashCode(testClass));
        tc.ChangeVlaue(testClass);
        System.out.println("testClass now is "+testClass+";num: "+testClass.num+" ,memory address is "+System.identityHashCode(testClass));
        System.out.println("--------------------------------------");
        //test ArrryList
        ArrayList<String> liststr = new ArrayList<String>();
        liststr.add("str1");
        System.out.println("liststr before is "+liststr+" ,memory address is "+System.identityHashCode(liststr));
        tc.ChangeVlaue(liststr);
        System.out.println("liststr now is "+liststr+" ,memory address is "+System.identityHashCode(liststr));
        System.out.println("--------------------------------------");
        //test Arrry
        int[] intArray = new int[3];
        intArray[0]=1;
        System.out.println("liststr before is "+Arrays.toString(intArray)+" ,memory address is "+System.identityHashCode(intArray));
        tc.ChangeVlaue(intArray);
        System.out.println("liststr now is "+Arrays.toString(intArray)+" ,memory address is "+System.identityHashCode(intArray));
    }
}

class TestClass{
    public TestClass(){
        System.out.println("TestClass constructor ");
    }
    int num = 0;
    
    public void ChangeVlaue(String str){
        str = "changed value";
    }
    public void ChangeVlaue(int num){
        num ++;
    }
    public void ChangeVlaue(TestClass testClass){
        testClass.num ++;
        testClass = new TestClass();
        testClass.num ++;
    }
    public void ChangeVlaue(ArrayList<String> liststr) {
        liststr.add("str2");
        liststr = new ArrayList<>();
        liststr.add("str3");
    }
    public void ChangeVlaue(int[] intArray) {
    	intArray[1] = 2;
    	intArray = new int[3];
        intArray[2]=3;
    }
}

 

Return results

TestClass constructor 
str before is value ,memory address is 366712642
str now is value ,memory address is 366712642
--------------------------------------
num before is 0 ,memory address is 1829164700
num now is 0 ,memory address is 1829164700
--------------------------------------
TestClass constructor 
testClass before is test.t05new.TestClass@7852e922;num: 0 ,memory address is 2018699554
TestClass constructor 
testClass now is test.t05new.TestClass@7852e922;num: 1 ,memory address is 2018699554
--------------------------------------
liststr before is [str1] ,memory address is 1311053135
liststr now is [str1, str2] ,memory address is 1311053135
--------------------------------------
liststr before is [1, 0, 0] ,memory address is 118352462
liststr now is [1, 2, 0] ,memory address is 118352462

String and basic type

As you can see, in this case, the modification of the passed method parameter has no effect on the caller

Object, container class, array

As you can see, in this case, the address of the memory has not changed, that is, it is the same object, but the contents of the three have changed

summary

The Java programming language only passes parameters by value. When an object instance is passed to a method as a parameter, the value of the parameter is a copy of the object's reference (that is, an address of the object).

Pointing to the same object, the content of the object can be changed in the called method (modified according to the address to the destination), but the reference of the object (not the copy of the reference) will never change (the address of the caller will never change).

Java parameters, whether they are primitive or reference types, pass copies (another way is to pass values, but it's better to say that passing copies is usually relative to addresses).

If the parameter type is the original type, a copy of the parameter, that is, the value of the original parameter, is passed. This is the same as the previous passed value. Changing the value of the copy in the function does not change the original value

If the parameter type is a reference type, the copy of the reference parameter is passed, and the copy stores the address of the parameter. If the address of this copy is not changed in the function, but the value in the address, then the change in the function will affect the parameters passed in. If the address of the replica is changed in the function, such as new, then the replica points to a new address. At this time, the passed in parameter still points to the original address, so the value of the parameter will not be changed.

Keywords: Java Programming

Added by eddjc on Mon, 15 Jun 2020 10:56:52 +0300