Home > Java > Javagetting Started > What is the difference between pass by value and pass by reference in java

What is the difference between pass by value and pass by reference in java

青灯夜游
Release: 2023-02-02 15:27:40
Original
5958 people have browsed it

Difference: 1. Value transfer creates a copy, while reference transfer does not create a copy; 2. The original object cannot be changed in the function during value transfer, but the original object can be changed in the function during reference transfer. Passing by value means that a copy of the actual parameters is passed to the function when calling the function, so that if the parameters are modified in the function, the actual parameters will not be affected; passing by reference means that the actual parameters are copied when calling the function. The address is passed directly to the function, so modifications to the parameters in the function will affect the actual parameters.

What is the difference between pass by value and pass by reference in java

The operating environment of this tutorial: windows7 system, java8 version, DELL G3 computer.

actual and formal parameters

We all know that parameters can be defined when defining a method in Java. For example, the main method in Java, public static void main(String[ ] args), the args here are parameters. Parameters are divided into formal parameters and actual parameters in programming languages.

  • Formal parameters: are the parameters used when defining the function name and function body. The purpose is to receive the parameters passed in when calling the function.

  • Actual parameters: When calling a parameterized function, there is a data transfer relationship between the calling function and the called function. When calling a function in the calling function, the parameters in parentheses after the function name are called "actual parameters"

A simple example:

public static void main( String[ ] args) {
ParamTest pt = new ParamTest();
pt.sout( "Hollis");//实际参数为Hollis
}
public void sout( String name) {/!形式参数为name
system.out.println(name);
}
Copy after login

The actual parameters are The content that is actually passed when calling a method with parameters, and the formal parameters are the parameters used to receive the content of the actual parameters.

Value passing and reference passing

As mentioned above, when we call a parameterized function, the actual parameters will be passed to the formal parameters. However, in programming languages, there are two cases of transfer in this transfer process, namely transfer by value and transfer by reference. Let's take a look at how passing by value and passing by reference are defined and distinguished in programming languages.

Value passing refers to copying a copy of the actual parameters to the function when calling the function, so that if the parameters are modified in the function, the actual parameters will not be affected.
Passing by reference refers to passing the address of the actual parameters directly to the function when calling the function. Then the modification of the parameters in the function will affect the actual parameters.

With the above concepts, you can then write code and practice it. Let’s see whether it is value passing or reference passing in Java. So, the simplest piece of code came out:

public static void main( String[] args) {
     ParamTest pt = new ParamTest();
    int i = 10;
    pt.pass(i);
    System.out.println( "print in main , i is " +i);
}
public void pass(int j){
    j = 20;
    system.out.println( "print in pass , j is " + j);
}
Copy after login

In the above code, we modify the value of parameter j in the pass method, and then print the value of the parameter in the pass method and main method respectively. The output result is as follows:

print in pass , j is 20
print in main , i is 10
Copy after login

It can be seen that the modification of the value of i inside the pass method does not change the value of the actual parameter i. So, according to the above definition, someone came to the conclusion: Java's method passing is value passing.
However, some people soon raised questions (haha, so don’t jump to conclusions easily.). Then, they will move out the following code:

public static void main(String[ ] args) {
    ParamTest pt = new ParamTest();
    User hollis = new User();
    hollis.setName( "Hollis");
    hollis.setGender("Male");
    pt.pass(hollis);
    system.out.println( "print in main , user is " + hollis);}public void pass(User user) {
    user.setName( "hollischuang");
    System.out.println( "print in pass , user is " + user);}
Copy after login

is also a pass method, and the value of the parameter is also modified within the pass method. The output result is as follows:

print in pass , user is User{name='hollischuang', gender='Male '}
print in main , user is User{name='hollischuang' , gender='Male '}
Copy after login

After the pass method is executed, the value of the actual parameter is changed. According to the definition of passing by reference above, the value of the actual parameter is changed. Isn’t this called passing by reference? . Therefore, based on the above two pieces of code, someone came to a new conclusion: in Java methods, when passing ordinary types, it is passed by value, and when passing object types, it is passed by reference.
However, this statement is still wrong. If you don’t believe me, take a look at the following parameter transfer where the parameter type is an object:

public static void main( string[] args) {
    ParamTest pt = new ParamTest();
    string name = "Hollis";
    pt.pass(name ) ;
    System.out.println( "print in main , name is " + name);
}
public void pass(string name) {
    name = "hollischuang";
    system.out.println( "print in pass , name is " + name);
}
Copy after login

The output result of the above code is

print in pass , name is hollischuangprint in main , name is Hollis
Copy after login

What’s the explanation? An object is also passed, but the original parameter is The value has not been modified. Could it be that the transferred object has become a value transfer again?

Value transfer in Java

Above, we gave three examples to show the The results are different, which is why many beginners and even many advanced programmers are confused about Java's transfer types. In fact, what I want to tell you is that the above concept is not wrong, but there is a problem with the code example. Come on, let me outline the key points of the concept for you, and then give you a few truly appropriate examples.

Value passing refers to copying a copy of the actual parameters to the function when calling the function, so that if the parameters are modified in the function, the actual parameters will not be affected.
Passing by reference refers to passing the address of the actual parameters directly to the function when calling the function. Then the modification of the parameters in the function will affect the actual parameters.

So, let me summarize for you the key points of the difference between value passing and reference passing.

##Pass by valuePass by referenceFundamental differenceWill create a copyDoes not create a copyAllThe original object cannot be changed in the functionThe original object can be changed in the function

我们上面看过的几个pass的例子中,都只关注了实际参数内容是否有改变。如传递的是User对象,我们试着改变他的name属性的值,然后检查是否有改变。其实,在实验方法上就错了,当然得到的结论也就有问题了。

为什么说实验方法错了呢?这里我们来举一个形象的例子。再来深入理解一下值传递和引用传递,然后你就知道为啥错了。

你有一把钥匙,当你的朋友想要去你家的时候,如果你直接把你的钥匙给他了,这就是引用传递。这种情况下,如果他对这把钥匙做了什么事情,比如他在钥匙上刻下了自己名字,那么这把钥匙还给你的时候,你自己的钥匙上也会多出他刻的名字。

你有一把钥匙,当你的朋友想要去你家的时候,你复刻了一把新钥匙给他,自己的还在自己手里,这就是值传递。这种情况下,他对这把钥匙做什么都不会影响你手里的这把钥匙。

但是,不管上面那种情况,你的朋友拿着你给他的钥匙,进到你的家里,把你家的电视砸了。那你说你会不会受到影响?而我们在pass方法中,改变user对象的name属性的值的时候,不就是在“砸电视”么。

还拿上面的一个例子来举例,我们真正的改变参数,看看会发生什么?

public static void main(String[ ] args){
    ParamTest pt = new ParamTest();
    User hollis = new User();
    hollis.setName( "Hollis");
    hollis.setGender("Male" );
    pt.pass(hollis);
    system.out.println("print in main , user is " + hollis);
    public void pass(User user) {
        user = new User();
        user.setName( "hollischuang");
        user.setGender( "Male");
        system.out.println( "print in pass , user is " + user);
Copy after login

上面的代码中,我们在pass方法中,改变了user对象,输出结果如下:

print in pass , user is User{name='hollischuang ' , gender='Male '}
print in main , user is User{name='Hollis', gender= 'Male '}
Copy after login

我们来画一张图,看一下整个过程中发生了什么,然后我再告诉你,为啥Java中只有值传递。

What is the difference between pass by value and pass by reference in java

稍微解释下这张图,当我们在main中创建一个User对象的时候,在堆中开辟一块内存,其中保存了name和gender等数据。然后hollis持有该内存的地址ex123456(图1)。当尝试调用pass方法,并且hollis作为实际参数传递给形式参数user的时候,会把这个地址ex123456交给user,这时,user也指向了这个地址(图2)。然后在pass方法内对参数进行修改的时候,即user = newUser();,会重新开辟一块 eX456789的内存,赋值给user。后面对user的任何修改都不会改变内存eX123456的内容(图3)。

上面这种传递是什么传递?肯定不是引用传递,如果是引用传递的话,在user=new User()的时候,实际参数的引用也应该改为指向eX456789,但是实际上并没有。

通过概念我们也能知道,这里是把实际参数的引用的地址复制了一份,传递给了形式参数。所以,上面的参数其实是值传递,把实参对象引用的地址当做值传递给了形式参数。

我们再来回顾下之前的那个“砸电视”的例子,看那个例子中的传递过程发生了什么。

What is the difference between pass by value and pass by reference in java

同样的,在参数传递的过程中,实际参数的地址eX1213456被拷贝给了形参,只是,在这个方法中,并没有对形参本身进行修改,而是修改的形参持有的地址中存储的内容。

所以,值传递和引用传递的区别并不是传递的内容。而是实参到底有没有被复制一份给形参。在判断实参内容有没有受影响的时候,要看传的的是什么,如果你传递的是个地址,那么就看这个地址的变化会不会有影响,而不是看地址指向的对象的变化。就像钥匙和房子的关系。

那么,既然这样,为啥上面同样是传递对象,传递的String对象和User对象的表现结果不一样呢?我们在pass方法中使用name = “hollischuang”;试着去更改name的值,阴差阳错的直接改变了name的引用的地址。因为这段代码,会new一个String,在把引用交给name,即等价于name =new String(“hollischuang”);。而原来的那个”Hollis”字符串还是由实参持有着的,所以,并没有修改到实际参数的值。

What is the difference between pass by value and pass by reference in java

所以说,Java中其实还是值传递的,只不过对于对象参数,值的内容是对象的引用。

总结

无论是值传递还是引用传递,其实都是一种求值策略(Evaluation strategy)。在求值策略中,还有一种叫做按共享传递。其实Java中的参数传递严格意义上说应该是按共享传递。

Passing by sharing means that when a function is called, a copy of the address of the actual parameter is passed to the function (if the actual parameter is on the stack, the value is copied directly). When operating parameters inside a function, you need to copy the address to find the specific value before operating. If the value is on the stack, then because it is a direct copy of the value, operations on the parameters within the function will not affect external variables. If the original copy is the address of the original value in the heap, then you need to find the corresponding location in the heap based on the address before performing the operation. Because a copy of the address is passed, the operation on the value within the function is visible to the external variable.

To put it simply, transfer in Java is by value, and this value is actually a reference to the object.
Passing by sharing is actually just a special case of passing by value. So we can say that passing in Java is passing by sharing, or that passing in Java is passing by value.

So operating parameters inside the function will not affect external variables. If the original copy is the address of the original value in the heap, then you need to find the corresponding location in the heap based on the address before performing the operation. Because a copy of the address is passed, the operation on the value within the function is visible to the external variable.

To put it simply, transfer in Java is by value, and this value is actually a reference to the object.

Passing by sharing is actually just a special case of passing by value. So we can say that passing in Java is passing by sharing, or that passing in Java is passing by value.

For more programming-related knowledge, please visit: Programming Teaching! !

The above is the detailed content of What is the difference between pass by value and pass by reference in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template