Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Is Java a value pass or a reference pass? There are pictures to prove it


Jun 01, 2021 Article blog


Table of contents


This article was reproduced by the public number: Java Chinese Community Author: Lei brother

Start by exposing the answer, in the Java language, the essence is only value delivery, but no reference delivery, interpretation and proof detailed in the body.

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

1. Value type

In layman's terms, the so-called value types refer to the eight underlying data types in Java:

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

 Is Java a value pass or a reference pass? There are pictures to prove it1

At the JVM level: The so-called value type refers to the type that generates the value directly in the stack (Java virtual machine stack) when assigning a value, as shown in the following illustration:

 Is Java a value pass or a reference pass? There are pictures to prove it2

2. Reference type

Reference types are data types other than value types, such as:

  • kind
  • interface
  • array
  • string
  • Packaging class (Integer, Double...

 Is Java a value pass or a reference pass? There are pictures to prove it3

At the JVM level, the so-called reference type refers to the type of data that is generated on the reference build stack at initialization and the value is generated on the heap, as shown in the following illustration:

 Is Java a value pass or a reference pass? There are pictures to prove it4

3. Value delivery

Pass By Value refers to a copy of the original content that is passed when method parameters are passed, so how the copy is modified does 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("调用方法前:" + age);
        intTest(age);
        System.out.println("调用方法后:" + age);
    }


    private static void intTest(int age) {
        age = 30;
        System.out.println("方法中修改为:" + age);
    }
}

The result of the program's execution is:

Before calling the method: 18

The method is modified to: 30

After calling the method: 18

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

4. Reference delivery

Pass By Reference refers to the method parameter itself when it is passed, so any modification of the argument affects the original content.

The implementation code that simulates "reference delivery" is as follows:

public class PassTest {
    public static void main(String[] args) {
        char[] name = {'编程', '师'};
        System.out.println("调用方法前:" + new String(name));
        paramTest(name);
        System.out.println("调用方法后:" + new String(name));
    }
    private static void paramTest(char[] n) {
        n[1] = '狮';
        System.out.println("方法中修改为:" + new String(n));
    }
}

The result of the program's execution is:

Before calling the method: Programmer

The method is modified to: Programming Lion

After calling the method: Programming Lion

From the above results, it can be seen that after modifying the parameters in the paramTest method, when the parameters are reprinted in the main method, we find that the value of the parameters has also changed, so it seems that we can conclude that there appears to be a "reference pass" in Java, but this is not the case, let's go on.

5. True and false "reference delivery"

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

public class PassByValue {
    public static void main(String[] args) {
        char[] name = {'磊', '哥'};
        System.out.println("调用方法前:" + new String(name));
        paramTest(name);
        System.out.println("调用方法后:" + new String(name));
    }
    private static void paramTest(char[] n) {
        n = new char[2]; // 添加此行代码
        n[1] = '神';
        System.out.println("方法中修改为:" + new String(n));
    }
}

The result of the program's execution is:

Before calling the method: Leigo

The method is modified to: God

After calling the method: Leigo

As you can see from the above results, when we add new char[] the paramTest method, the Reference Pass is suddenly passed? Why?

This is because, in the Java language, there is essentially only a value pass, which means that Java's arguments pass only a copy of it, not the parameters themselves.

The quoted "reference pass" in front actually just passes a copy of its reference, as shown in the following image:

 Is Java a value pass or a reference pass? There are pictures to prove it5

PS: The description of the Java heap in the Java Virtual Machine Specification is: "All object instances and arrays should be allocated on the heap."

So we new char[] can see that the n object has a new address and the original content has not been modified, and if you look at the idea of reference delivery, no matter how you perform the modification will change the original content, so we can more confirm that there is only a value pass in the Java language, as shown in the following image:

 Is Java a value pass or a reference pass? There are pictures to prove it6

summary

From the content of this article, we can conclude that only values are passed in the Java language, and that method parameters pass only copy information instead of the original content. We also know that the underlying data type is generated directly on the stack, while the object or array generates information on both the stack and the stack, and the references generated on the stack point directly to the data generated in the heap, as shown in the following image:

 Is Java a value pass or a reference pass? There are pictures to prove it7

That's W3Cschool编程狮 says about Whether Java is a value pass or a reference pass? There are pictures as evidence of the relevant introduction, I hope to help you.