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.
The operating environment of this tutorial: windows7 system, java8 version, DELL G3 computer.
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); }
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.
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); }
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
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);}
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 '}
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); }
The output result of the above code is
print in pass , name is hollischuangprint in main , name is Hollis
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?
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 reference | ||
---|---|---|
Will create a copy | Does not create a copy | |
The original object cannot be changed in the function | The original object can be changed in the function |