Is there really a reference to passing parameters in Java methods?

type of data

Data classes in Java can be divided into two categories: primitive types and reference types.

Basic types include integer (byte, short, int, long), floating point (float, double), character (char), Boolean (boolean).

Reference types include classes, interfaces, and arrays.

Many articles introducing Java will say that Java methods have two ways of passing parameters: value passing and reference passing.

Are there really two ways to pass parameters?

Let's first look at how basic types and reference types are stored in memory.

basic type

The basic type variable will directly store the value of the variable in memory, for example: int a = 1;
then the memory unit corresponding to a will store 1.
The Beauty of Java Programming
(Image source: Wang Zheng's "The Beauty of Java Programming" https://www.xzgedu.com/detail/p_625cfab5e4b01a4851f30d8a/6)

reference type

The reference type variable does not store the object itself, but the first address of the memory block where the object is located.
Student stu = new Student(1,3);
As a variable of reference type, stu stores the first address of the Student object in memory.
insert image description here
(Image source: Wang Zheng's "The Beauty of Java Programming" https://www.xzgedu.com/detail/p_625cfab5e4b01a4851f30d8a/6)

method parameter

If the parameter of the method is a basic type, then when the method is called, the value of the variable in memory will be copied to the parameter of the method, that is, the change(a) method will copy the value 1 of a in memory to the variable x.

Because x is a local variable of the method change, it is stored in the change method stack, and will not affect the a variable stored in the main method stack, so the output result is 1.

 public static void main(String[] args) {
    
    
        int a = 1;
        change(a);
        System.out.println(a); // 输出结果为1
    }

    public static void change(int x) {
    
    
        x = 2;
    }

If the method parameter is a reference type, then when the method is called, the value of the variable in memory will also be copied to the parameter of the method, that is, the change(stu) method will copy the value of stu in memory, which is the first value of the Student object . Address , copy a copy to the variable stux.

In this way, the values ​​of the variable stu and the variable stux in the memory are the same, and both are the first address of the Student object, that is to say, the values ​​of the variable stu and the variable stux point to the same Student object.

Therefore, in the change method, the variable stux modifies the attribute age of the object pointed to by it, that is, the attribute age of the object pointed to by the variable stux is modified, because they point to the same object.

 public static void main(String[] args) {
    
    
        Student stu = new Student(1,20);
        change(stu);
        System.out.println(stu.age); // 输出结果为18
    }

    public static void change(Student stux) {
    
    
        stux.age = 18;
    }

    static class Student {
    
    
        int id;
        int age;
        public Student(int id,int age) {
    
    
            this.id = id;
            this.age = age;
        }
    }

Summarize

To sum up, regardless of whether the parameter type of the method is a basic type or a reference type, they pass the value of the variable in memory, but the value of the basic type in memory is the value itself, and the reference type stores the first address of the object in memory That's all.

Therefore, in the Java language, method parameter passing, whether it is a basic type or a reference type, is passed by value, and the value in the variable is passed.

So why does C++ pass by reference?

Because C++ can obtain the address of a variable through "&" reference, it can pass the address of the variable to the function.

Randomly find a picture from the Internet to explain:
insert image description here
C++ reference transfer, the address of the variable itself is passed: 0x7fff5cc109de

Java transfers the value in the variable: 10
(if it is a variable of reference type, then the value in the variable is the first address of the object, no matter what, the value in the variable is passed)

So Java does not pass by reference, only by value.

Guess you like

Origin blog.csdn.net/zhanyd/article/details/124629099