Only value passing in Java? Thoroughly understand an article

Novice on the road, record their understanding.
In C + +, there are two ways for functions to pass values: Value Passing and reference passing. In Java, only values are passed, even if the object passed is of reference type (such as String). The so-called value transfer refers to transferring a copy of the argument to the function when calling the function, so that the operation in the function is a copy of the argument and will not modify the argument; The so-called reference passing refers to passing the address of the argument to the function when calling the function. In this way, the operation in the function is the argument object, and the modification inside the function will affect the argument.
Thus, the essential difference between value passing and reference passing is that value passing will produce a copy, while reference passing will not. Therefore, value passing will not change the value of the original argument, but reference passing will.
Three examples (basic data type, string and class) are described below:

//Basic data type
public static void main(String[] args)
{
    int a = 1;
    System.out.println("Before calling the function a The value of is:" + a);
    System.out.println("a The memory address of is:" + System.identityHashCode(a));
    fun(a);
    System.out.println("After calling the function a The value of is:" + a);
}

public static void fun(int b)
{
    b = 2;
    System.out.println("b The memory address of is:" + System.identityHashCode(b));
}
Before calling the function a The value of is: 1
a The memory address of is 1989780873
b The memory address of is 1480010240
 After calling the function a The value of is: 1
/Reference type: String
public static void main(String[] args)
{
    String a = "River Map";
    System.out.println("Before calling the function a The value of is:" + a);
    System.out.println("a The memory address of is:" + System.identityHashCode(a));
    fun(a);
    System.out.println("After calling the function a The value of is:" + a);
}

public static void fun(String b)
{
    b = "characters associated with the legendary Emperor Yu";
    System.out.println("b The memory address of is:" + System.identityHashCode(b));
}
Before calling the function a The value of is: river chart
a The memory address of is 1078694789
b The memory address of is 81628611
 After calling the function a The value of is: river chart
public class Practice
{
    //Reference types: classes and objects
    public static void main(String[] args)
    {
        Person a = new Person();
        a.age = 18;
        System.out.println("Before calling the function a The value of is:" + a.age);
        System.out.println("a The memory address of is:" + System.identityHashCode(a));
        System.out.println("a The memory address of the property is:" + System.identityHashCode(a.age));
        fun(a);
        System.out.println("After calling the function a The value of is:" + a.age);
    }

    public static void fun(Person b)
    {
        b.age = 0;
        System.out.println("b The memory address of is:" + System.identityHashCode(b));
        System.out.println("b The memory address of the property is:" + System.identityHashCode(b.age));
    }
}

class Person
{
    int age;
}
Before calling the function a The value of is: 18
a The memory address of is 1480010240
a The memory address of the attribute is 81628611
b The memory address of is 1480010240
b The memory address of the property is 1828972342
 After calling the function a The value of is: 0

For the basic data type, its value exists in the stack. When the parameter is passed, the formal parameter copies the value of the argument from the stack, but it is already two objects, so the argument value is not changed.

For a reference type (string), its reference is stored in the stack and its value is stored in the heap. Formal parameters copy their references from the stack, so you can access the values on the heap. b in the function changes the reference. At this time, b points to another piece of memory in the heap and cannot access the value in the heap pointed to by the argument reference, but it does not affect the argument value.


For a reference type (class), its reference exists in the stack and its value exists in the heap. Formal parameters copy their references from the stack, so you can access the values on the heap. b in the function changes the value of the attribute, but does not change its reference, so b can still access the value in the heap pointed to by the argument reference. The value in the stack cannot be changed, and the value in the heap can be changed, so the value of the final attribute age is changed.


We can change the following code slightly:

public class Practice
{
    //Reference types: classes and objects
    public static void main(String[] args)
    {
        Person a = new Person();
        a.age = 18;
        System.out.println("Before calling the function a The value of is:" + a.age);
        System.out.println("a The memory address of is:" + System.identityHashCode(a));
        System.out.println("a The memory address of the property is:" + System.identityHashCode(a.age));
        fun(a);
        System.out.println("After calling the function a The value of is:" + a.age);
    }

    public static void fun(Person b)
    {
        b = new Person();//Change the reference of b itself here
        b.age = 0;
        System.out.println("b The memory address of is:" + System.identityHashCode(b));
        System.out.println("b The memory address of the property is:" + System.identityHashCode(b.age));
    }
}

class Person
{
    int age;
}
Before calling the function a The value of is: 18
a The memory address of is 1480010240
a The memory address of the attribute is 81628611
b The memory address of is 1828972342
b The memory address of the attribute is 1452126962
 After calling the function a The value of is: 18

Obviously, the addresses of a and b are different. At this time, changing the attribute in b will no longer affect a.
Therefore, there is only value passing in Java.

Keywords: Java

Added by Azala on Wed, 02 Mar 2022 07:21:49 +0200