Pass by value and pass by reference in Java

https://www.cnblogs.com/nnngu/p/8299724.html


When an object is passed as a parameter to a method, the properties of the object can be changed in this method, so is it passed by value or by reference? 
  Answer: It is passed by value. Parameter passing in the Java language is only by value. When an instance object is passed as a parameter to a method, the value of the parameter is a copy of the object's reference. Pointing to the same object, the contents of the object can change within the called method, but the object's reference (not a copy of the reference) never changes.

 

Java's parameter passing, whether it is a parameter of a basic data type or a reference type, is passed by value, not by reference!

 We can take a look at the definition of parameters passed by reference in Microsoft's documentation (as shown in the screenshot below):

 

1. Parameters of basic data types

Let's first look at an example of passing parameters of primitive data types by value:

TransferTest.java

public class TransferTest {
    public static void main(String[] args) {
        int num = 1;
        System.out.println( "before changeNum() method call: num = " + num);
        changeNum(num);
        System.out.println( "After the changeNum() method call: num = " + num);
    }

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

operation result:

 

The schematic diagram of this transfer process is as follows:

When num is passed as a parameter to the changeNum() method, the value 1 stored in the storage unit pointed to by num in the memory space is passed to the x variable in the changeNum() method, and this x variable is also allocated in the memory space. A storage unit is created, and at this time, the value of num is passed to the storage unit of x. After that, all operations on x in the changeNum() method are for the storage unit pointed to by x, and have nothing to do with the storage unit pointed to by num!

Therefore, after the changeNum() method is called, the value of the storage unit pointed to by num has not changed, which is called "pass by value"! The essence of pass- by- value is: the contents of the storage unit are passed, not the reference to the storage unit!

 

2. Parameters of reference type

Again, let's look at an example:

TransferTest2.java  

 1 public class TransferTest2 {
 2     public static void main(String[] args) {
 3         Person person = new Person();
 4         System.out.println(person);
 5         change(person);
 6         System.out.println(person);
 7     }
 8 
 9     public static void change(Person p) {
10         p = new Person();
11     }
12 }
13 
14 /**
15  * Person类
16  */
17 class Person {
18 
19 }

  

运行结果:

 

可以看出两次打印person的地址值是一样的,即调用完change() 方法之后,person变量并没有发生改变。

 

这个传递过程的示意图如下:

 

当执行到第3行代码时,程序在堆内存中开辟了一块内存空间用来存储Person类的实例对象,同时在栈内存中开辟了一个存储单元用来存储该实例对象的引用,即上图中person指向的存储单元。

当执行到第5行代码时,person作为参数传递给change()方法,需要注意的是:person将自己存储单元的内容传递给了change()方法的p变量!此后,在change()方法中对p的一切操作都是针对p所指向的存储单元,与person所指向的那个存储单元没有关系了!


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324812551&siteId=291194637