Foreword
Value transfer
Value transfer means when the actual parameters of the function call to copy a parameter passed to, so that the function of the parameter changes will not affect the value of the actual parameter.
Passed by reference
Reference transfer means when calling the function address of the actual argument passed directly to the parameter, the parameters for the modification will affect the value of the actual parameter in the function.
We can use a program to verify Java
only the value transfer
/**
* 验证java中只有值传递
* Dmego 2018-8-27
*/
class User{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class TestValue {
public static void change(User user2,int a2){
System.out.println("改变之前:"+user2.getName()+",a2="+a2);
user2.setName("李四"); //改变 user2 的 name 值
a2 = 10; //改变 a2 的值
System.out.println("改变之后:"+user2.getName()+",a2="+a2);
user2 = new User(); //将 user2 重新指向一个新对象
user2.setName("王五");
System.out.println("重新指向一个新对象后:"+user2.getName());
}
public static void main(String[] args){
User user1 = new User();
user1.setName("张三"); //初始化 user1 的 name 为张三
int a1 = 5; //初始化 a1 的值为 5
change(user1,a1); //调用方法验证传值方式
System.out.println("调用方法后:"+user1.getName()+",a1="+a1);
}
}
Run this program, the output is:
改变之前:张三,a2=5
改变之后:李四,a2=10
重新指向一个新对象后:王五
调用方法后:李四,a1=5
Result analysis
Let auxiliary graph above, to analyze this procedure, we first define a User
class, then the test in a class is instantiated User
object name user1
, and assign it name = '张三'
, this time in memory as 图1
shown instantiated an object is equivalent to the heap opened up a memory, the memory address 017
, this time the object is referenced user1
memory address 001
, which holds the address of the object in memory, which is the point of the object. Took over, we call the method change()
to try to change user1
the name
value of this validation java
by value in.
We will user1
as an argument passed to change()
a method, parameters user2
to accept the arguments, here embodied in the two different ways of transmission parameters. If it is passed by value, so as defined above, as 图2
shown, user2
is user1
a copy, that is to say at the time of passing parameters, user1
(itself a reference to an object), a copy called user2
it It is also a reference to the object, and user1
and user2
at this time point to the same object. And if it is passed by reference, also as defined above, as 图5
shown, when the transmission parameters, is directly user1
transmitted to the parameter, only a name is changed user2
, but essentially user1
and user2
in fact the same. It is a reference to an object.
Next to analysis of the output, whether it is passed by value or by reference, the results of the first output line is constant 张三
, since both refer to the same object. For the second line of output, we still can not determine which way, because the same object is changed, the value will change; the key is to line 3 and 4 output lines output at this time, we will be user2
redirected to a new objects, and assigned to the object name = '王五'
, if it is 引用传递
the way it user1
will also change to point, point to the new object, the result output after the last row will call the method and the third line is the same 王五
, but the fact is output 李四
, this suggests user1
and user2
is actually not the same. True calling process as 图2
~ 图4
shown, this will make user2
the point to a new object, user1
the object pointed to does not change, or the original object.
For basic types of parameters, the a1
value does not change the last described method when executed, a2
it is a1
a copy. For reference types of parameters for such User
an object, a method call, in fact, is its reference user1
as an actual argument, then passed to the parameter will be a copy of the reference cited user2
, although it is two references ( like a1
the a2
relationship). But point to the same object, all of these operations are also in terms of the same object.
Finally, an example to illustrate the image of all of this, if you have a key to your room and in your name engraved on it, the process is like for a int
type of a1
initialization value 5
. Your friends and your relationship is very good, you want your room key, then you do not put your keys directly to him, but a copy of the new key, this key is also able to open the door of your room. And your friends in this the new key engraved with his name. This process is like calling change()
method to a1
copy an assignment to a2
, then modify a2
and a1
nothing to do, your friend engraved his name on the new keys in your hand that will not affect the original key. The key is that the keys can open your room, like user1
and user2
point to the same object. At this point your friend used to get a new key to open your room, your room TV smashed. This process is like renamed 李四
. Then you take your room key to open you will definitely see such a scene - the TV smashed. Like after calling the method user1
becomes 李四
. During the method call, the last user2
redirected to a new object, which is like your friend will you give him a copy of the keys have been processed again, this time not to open the door of your room, but able to open his own room he opened his room with this key and then smashed his own TV which does not affect the TV in your room, that is to say the last user1
name and does not become 王五
. This is java
the value of the transfer. Of course, if it is passed by reference, then the case from beginning to end will be only one key, the final results will be different.
end
We can know by the above analysis. Java
Only values are passed this kind of way, but for reference types, the parameter passing is a reference to an object of Bale.