Beginner's Day 88 - > is Java passed by reference?

Posted by douceur on Wed, 22 Sep 2021 04:07:55 +0200

To understand this problem, we should understand two concepts: what is passing by value and what is passing by reference

call by value: refers to copying and passing the value of the actual parameter to the function when calling the function, so that if the parameter is modified in the function, the actual parameter will not be affected

call by reference: it means that the address of the actual parameter (pointer in c language) is directly passed to the function when calling the function, so the modification of the parameter in the function will affect the actual parameter

Let's take a look at two examples, hoping to help the partners understand this problem

Reference data type

public class BianLiFeng {
    public static void main(String[] args) {
        People people = new People("yht");
        System.out.println("before:"+people.getName());
        change(people);
        System.out.println("after:"+people.getName());
    }
    public static void change(People people){
        people.setName("qcby");
        System.out.println("change:"+people.getName());
    }
}
class People{
    public String name;
    public People(String name){
        this.name=name;
    }
    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name=name;
    }
}

The code structure should be very clear. You can think about what the code will output first

result


According to the results, we can draw a conclusion that when the passed method parameter type is the reference data type, the method can modify the parameters
We can understand that when we create a new object, we will create an object in the heap and point the reference in the stack to the instance in the heap. When we pass the people object to the function, we actually pass the address of the object to the function, that is, we pass a copy of the argument, but the copy and the argument point to the same address in the heap at the same time, If we modify the content of the address through the copy, of course, the value of the argument will be changed, but we can't change the address in the heap pointed to by the argument.

Basic data type

 public static void main(String[] args) {
        int x=66;
        System.out.println("before:"+x);
        change(x);
        System.out.println("after:"+x);
    }
    public static void change(int x){
        x=23;
        System.out.println("change:"+x);
    }

This code, friends, let's think about what will be output first.

result


In fact, it's also easy to understand. When we pass the basic data type to the function, we still pass a copy of the argument to the function. Therefore, if you modify the value of a copy, it must have no impact on my argument.

Revalidation

public static void main(String[] args) {
        People people = new People("yht");
        System.out.println("before:"+people.getName());
        change(people);
        System.out.println("after:"+people.getName());
    }
    public static void change(People people){
        people=new People("qcby");
        System.out.println("change:"+people.getName());
    }
}
class People{
    public String name;
    public People(String name){
        this.name=name;
    }
    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name=name;
    }

Please take a closer look at this code. What's the difference between this code and the above reference type verification? Yes, this time we let the incoming parameter point to a new address in the function, that is, let the parameter point to a new address. As a result, if you want to understand, you really understand whether java is passed by value or by reference

result


Yes, when we try to modify the address of the reference type parameter in the function, it can't be modified, because java is passed by value. It passes a copy of the knowledge argument, but we can modify the content in the address, because the copy and the argument point to the same address, but we can't change the address of the argument if we want to change it.

I hope these three examples can help us solve this problem~

Topics: Java Back-end