Java was originally passed by value?

Let me talk about the conclusion first, Java is value passing.

01

In Java, all parameters are passed by value, that is, the method gets a copy of all parameter values, not the original parameters. When we pass a reference to an object as a parameter to a method, we are actually passing a copy of the reference to that object.

Here is an example to illustrate this:

public class Test {

    public static void main(String[] args) {
        int x = 10;
        changeValue(x);
        System.out.println("x = " + x); // 输出结果:x = 10
        
        String str = "hello";
        changeValue(str);
        System.out.println("str = " + str); // 输出结果:str = hello
        
        Person person = new Person("Jack");
        changeValue(person);
        System.out.println("person.name = " + person.getName()); // 输出结果:person.name = Rose
    }
    
    public static void changeValue(int x) {
        x = 5;
    }
    
    public static void changeValue(String str) {
        str = "world";
    }
    
    public static void changeValue(Person person) {
        person.setName("Rose");
    }
}

class Person {
    private String name;

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

In the above code, we define three methods respectively changeValue, and maincall these three methods in the method.

  • The first changeValuemethod receives a intparameter of a type and modifies it to 5, but mainthe output result in the method is still the original value 10, indicating that the passed parameters are passed by value.

  • The second changeValuemethod receives a Stringparameter of a type and modifies it to world, but mainthe output result in the method is still the original value hello, indicating that the passed parameter is passed by value.

  • The third changeValuemethod receives a Personparameter of type, modifies its name to Rose, and mainoutputs the name of the object in the method as Rose, indicating that the passed parameter is passed by value. But because a copy of the object's reference is passed instead of the original parameter, we can access and modify the object's properties through the reference.

02

Then why do many people think that Java is passed by reference, such as the passing of reference type parameters?

I believe this is also the question of many people.

This illusion is likely due to the fact that the object itself is passed by reference in Java. In Java, when we declare an object and assign it to a variable, we actually create a reference to that object. When we pass the variable to a method, we are actually passing the reference, the address to the object. Therefore, the state of the object can be modified through this reference inside the method.

However, it should be noted that although the reference itself is passed, the modification of the object itself will not affect the original object, because only the address is passed instead of the object itself. If we reassign the incoming parameter to a new object inside the method, the new object will only exist inside the method and will not affect the original object.

Therefore, we can say that Java is passed by value, but for parameters of reference type, the value of the reference is passed.

Ok, let's look at an example.

Suppose we have a Person class with two attributes named name and age. Now there is a method called changeAge. In this method, we receive a Person object and an integer as parameters, and then assign this integer to the age property of the Person object. code show as below:

public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public int getAge() {
        return age;
    }
}

public class Main {
    public static void main(String[] args) {
        Person p1 = new Person("Tom", 20);
        int newAge = 30;
        changeAge(p1, newAge);
        System.out.println(p1.getAge()); // 输出 20
    }

    public static void changeAge(Person person, int newAge) {
        person = new Person(person.getName(), newAge);
    }
}

In the above code, we have created a Person object p1 with an age of 20. We then call the changeAge method, passing p1 and a new age value of 30 to the method. In the changeAge method, we obtain the p1 object through the incoming person parameter, and reassign it to a new Person object, where the age is the incoming newAge parameter.

Finally, we print out the age of the p1 object, the expected output should be 30. But the actual output is 20. This is because in the changeAge method, we only modified the object pointed to by the incoming person reference, but did not modify the value of p1 itself. When the changeAge method returns, p1 still points to the original Person object, whose age is still 20.

Therefore, we can see that in Java, although the parameter of the reference type passes the value of the reference, this is not equivalent to passing by reference. For the incoming reference type parameter, modifying it inside the method will only affect the object pointed to by the reference, not the original object.

03

Seeing this, I know that some friends must not be able to sit still.

Who said that age cannot be changed, I can change it by writing like this!

    public static void changeAge(Person person, int newAge) {
        person.setAge(newAge);
    }

I won't talk too much nonsense, just tell you the conclusion:

Person is still passed by value, and the referenced value (memory address) is passed, so it looks like a passed by reference.

It's as if you can open the door with a key assigned, but even if the effect is the same, what you have in your hand is the assigned key instead of the original key.

Summarize

For basic data types, it must be passed by value, there is nothing to say.

For reference types, it is still passing by value, and the value (memory address) of the reference is passed, so it looks like passing by reference.

Guess you like

Origin blog.csdn.net/weixin_39570751/article/details/130866149