Is Java value passing or reference passing?

In the Java} language, the essence is only value passing, but no reference passing. See the text for explanation and proof.

When it comes to value passing and reference passing, we have to mention two concepts: value type and reference type.

 

1, Value type

 

In a popular sense, the so-called value type refers to the eight basic data types in Java:

  • Integer type: byte, int, short, long
  • Floating point type: float, double
  • Character type: char
  • boolean type: boolean

 

 

From the perspective of JVM: the so-called value type refers to the type of value directly generated in the stack (Java virtual machine stack) during assignment, as shown in the following figure:

 

 

2, Reference type

Reference types refer to data types other than value types, such as:

  • class
  • Interface
  • array
  • character string
  • Wrapper classes (Integer, Double...)

 

From the perspective of JVM, the so-called reference types refer to those data types that generate references on the stack and values on the heap during initialization, as shown in the following figure:

 

 

3, Value transfer

Pass By Value means that when a method passes a parameter, it passes a copy of the original content. Therefore, how to modify the copy will not affect the original content.

The implementation code is as follows:

public class PassTest {
    public static void main(String[] args) {
        int age = 18;
        System.out.println("Before calling the method:" + age);
        intTest(age);
        System.out.println("After calling the method:" + age);
    }

    private static void intTest(int age) {
        age = 30;
        System.out.println("The method is modified as:" + age);
    }
}

The execution result of the program is:

Before calling method: 18

The method is modified as: 30

After calling the method: 18

From the above results, it can be seen that modifying parameters in the method does not affect the original content. We call this parameter transfer method value transfer.

 

4, Reference passing

Pass By Reference refers to that when a method passes a parameter, the parameter itself is passed. Therefore, any modification of the parameter will affect the original content.

The implementation code of simulating "reference passing" is as follows:

public class PassTest {
    public static void main(String[] args) {
        char[] name = {'Leisurely', 'brother'};
        System.out.println("Before calling the method:" + new String(name));
        paramTest(name);
        System.out.println("After calling the method:" + new String(name));
    }
    private static void paramTest(char[] n) {
        n[1] = 'god';
        System.out.println("The method is modified as:" + new String(n));
    }
}

 

 

The execution result of the program is:

Before calling the method: Lei Ge

The method is modified as: Leishen

After calling the method: Lei Shen

It can be seen from the above results that after modifying the parameters in the paramTest method and printing the parameters in the main method, it is found that the value of the parameters also changes. Then it seems that we can draw a conclusion that there seems to be "reference passing" in Java, but the facts are not the same. Let's continue.

 

5, True or false "reference passing"

Let's add a line to the above code, as follows:

public class PassByValue {
    public static void main(String[] args) {
        char[] name = {'Leisurely', 'brother'};
        System.out.println("Before calling the method:" + new String(name));
        paramTest(name);
        System.out.println("After calling the method:" + new String(name));
    }
    private static void paramTest(char[] n) {
        n = new char[2]; // Add this line of code
        n[1] = 'god';
        System.out.println("The method is modified as:" + new String(n));
    }
}

The execution result of the program is:

Before calling the method: Lei Ge

In the method, it is modified as: God

After calling the method: Lei Ge

From the above results, we can see that when we add new char [] to the paramTest method, "reference passing" suddenly changes to value passing? Why?

This is because in the Java language, there is only value passing in essence, that is to say, Java parameters will only pass its copy, not the parameter itself.

In fact, the quoted "reference passing" just passes its reference copy, as shown in the following figure:

 

 

PS: the description of Java heap in Java virtual machine specification is: "all object instances and arrays should be allocated on the heap".

Therefore, after calling new char [], we can see that the n # object has a new address, but the original content has not been modified. If we follow the idea of reference passing, no matter how to modify it, the original content will be changed. Therefore, we can further confirm that there is only value passing in the Java language, as shown in the following figure:

 

Vi. summary

Through the content of this article, we can draw a conclusion: in the Java language, there is only value passing, and the method will only pass the copy information rather than the original content when passing parameters. We also know that the basic data type will be directly generated on the stack, while the object or array will generate information on both the stack and the heap, and the reference generated on the stack will directly point to the data generated in the heap, as shown in the following figure:

 

 

Original source:

CSDN wechat official account, multi graph proof, is Java value transfer or reference transfer? https://mp.weixin.qq.com/s/hwRrRnayu8TNgoL2X1_EOA , 2020-09-03

 

 

 

 

 

 

 

 

 

Tags: Java

Posted by invarbrass on Thu, 19 May 2022 19:52:57 +0300