Java parameter passing is "by value" or a "pass by reference"?

Java programming language is one of the biggest puzzles: java is passed by value is passed by reference . I often ask this question the interviewer in the interview, but there are still many interviewers understanding of the problem is not quite right.
Here Insert Picture Description
There are many interviewers understand it this way:

  • If the transmission type based on the type of data, the value of transfer press,
  • If the transmission type is based, press reference.

This understanding correct? They can even write sample code to test their ideas, let us take a look together how most people is to verify the "basic types passed by value, non-base type pass by reference" The idea is:

Basic data type passed as a parameter

/**
 * 基础类型数据作为参数传递
 * @Author: danding
 * @Date: 2019/11/5
 */
public class TestParams {

    public static void main(String[] args){
        int x = 6;
        System.out.println("x的初始值为:" + x);
        add(x);
        System.out.println("x的最终值为:" + x);
    }

    public static void add(int x){
        x = x + 1;
        System.out.println("add 方法中的x值为:" + x);
    }

}

operation result:

x的初始值为:6
add 方法中的x值为:7
x的最终值为:6

Non-base type as an argument
First, we define a class

/**
 * 定义一个女朋友的类
 * (简陋了点,只有年龄,但不影响我们使用呀)
 * @Author: danding
 * @Date: 2019/11/5
 */
public class GrilFriend {
    private int age;

    public GrilFriend(int age) {
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Then we will create an instance and passed as a parameter

/**
 * 基础类型数据作为参数传递
 * @Author: danding
 * @Date: 2019/11/5
 */
public class TestParams {

    public static void main(String[] args){
        GrilFriend gril = new GrilFriend(18);
        System.out.println("女朋友的初始年龄为:" + gril.getAge());
        add(gril);
        System.out.println("女朋友的最终年龄为:" + gril.getAge());
    }

    private static void add(GrilFriend friend){
        friend.setAge(friend.getAge()+1);
        System.out.println("女朋友在方法中的年龄为:" + friend.getAge());
    }

}

operation result:

女朋友的初始年龄为:18
女朋友在方法中的年龄为:19
女朋友的最终年龄为:19

When the non-base type as an argument, the value is actually modified.

This time many students to verify the above two examples, and that they come to their own conclusions:

If you pass the basic data type is the type of value transfer press, otherwise passed by reference.

In this description, this understanding is wrong, wrong, wrong . Here we are concerned that in Java parameter passing in the end is passed by value or pass by reference?

First, that the correct answer: the Java parameter passing, whether it is the basic data type or types of parameter references are passed by value, not by reference pass!

First, we should understand the meaning of the pass by value or pass by reference.

  • Passed by value : a method parameter value is copied to another variable, then pass the object to copy, it is referred to as passed by value.
  • Passed by reference : passing a reference to the actual parameters or alias to a method, referred to as the reason passed by reference.

Dehen bad you bad old man, I believe you are a ghost, I'm going to explain it to you this bad review .....

Listen to the old lady (Oh, no, small series) slowly come ...

When an object is passed as a parameter to a method, in this method you can change the properties of this object, then here in the end is a "pass-by-value" or a "pass by reference"?

A: Yes, passed by value. Java language only pass parameters "by value." When a method is passed to the object instance as a parameter, the parameter value is a copy of the referenced object. Point to the same object, the content object may change in the method is called, but the object reference (not a reference copy) will never change.

The basis of the type of parameter passing

The above example has been verified, as passed by value, this we should not have any objection.

Non-base-type transmission parameters

Our focus for the next type of object passed as a parameter

First look at the transfer of examples:

public class TestParams {

    public static void main(String[] args){
        Person p1 = new Person();
        System.out.println(p1);
        change(p1);
        System.out.println(p1);
    }

    private static void change(Person p2){
        p2 = new Person();
    }

}

class Person{

}

operation result

Person@677327b6
Person@677327b6

After the print can be seen that the two address values ​​are the same person, i.e. complete call change () method, person variable not changed.

This transfer process schematic is as follows:

When performing the Person p1 = new Person (); time code, a program memory space to open up an instance of the Person class objects stored in the heap memory, a memory cell simultaneously opened in the stack memory used to store the object reference instance, i.e. points to the figure above the memory cell person.

When performing the change (p1); when the code is, person passed as a parameter to change () method should be noted that: person to transfer their contents to the storage unit change () method of the variables P2! Since then, the change () method in all operations of the storage unit for p2 is p2 points to, nothing to do with the memory cell pointed to a person!

This time the students have to say, that the girlfriend example above, the girlfriend of age is not yet been modified in the process? If you pass a copy of it you should not modify the age girlfriend do?

If we code into memory girlfriend in the example of FIG walk again, you should understand the truth of them.
The so-called reference copy, but it points or real objects, modify or property on the real object.

I hope the above explanation can eliminate all doubt, just remember that the Java parameter passing, whether it is the basic data type or types of parameter references are passed by value, not passed by reference! . When you understand the heap and stack memory and storage space different objects and locality of reference when, will be more clear, detailed description of the program, read the Java Heap Stack VS .


"A short step, a thousand miles," hope in the future you can: have a dream horse habitat everywhere! Come on, Junior!

No public concern: "Java confidant" , updated daily Java knowledge Oh, look forward to your arrival!

  • Send " Group ", progress with 100,000 programmers.
  • Send " interview " to receive information BATJ interview, video interview Raiders.
  • Send " Fun algorithm " to receive "Fun algorithm" series of video tutorials.
  • Do not send " 1024 " ...
    Daily benefits

Guess you like

Origin www.cnblogs.com/java-friend/p/11797656.html