Objective
These days in the consolidation of the basic knowledge of Java content, the value of the transfer is not a special understanding, so looked up some information and online related blogs, their own summary, and finally organized into a blog.
Value passing
value passing means that a copy of the actual argument is passed to the parameter when the function is called, so that modifications to the parameter in the function will not affect the value of the actual parameter.
Reference delivery
reference passing means that when a function is called, the address of the actual argument is passed directly to the formal parameter, and the modification of the parameter in the function will affect the value of the actual parameter.
We can use a program to verify Java
that only values are passed in
/** * 验证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 and the output is:
改变之前:张三,a2=5改变之后:李四,a2=10重新指向一个新对象后:王五调用方法后:李四,a1=5
Results analysis
Here we think of the auxiliary, to analyze the program, first we define a User
class, and then instantiate an object in the test class User
, named user1
, and assign a value name = '张三'
, at this time in memory as shown in 图1
, the instantiation of an object is equivalent to open up a piece of memory in the heap, The memory address is, at this point, the object is referred to, the 017
user1
memory address is 001
, it holds the object in memory address, that is, point to the object. Next, we call the method to change()
try to change user1
the name
value in this way to verify java
the value of the pass.
We will user1
pass this argument to the method as the argument, and here we can see the difference between the change()
user2
two ways of communicating the arguments. If it is passed by value, then as defined, as 图2
shown, user2
is user1
a copy, that is, when passing parameters, will user1
(itself is a reference to an object), copied a copy, named user2
, it is also a reference to an object, user1
and user2
this point points to the same object. And if it is a reference pass, as defined, as 图5
shown, when passing a parameter, it is user1
passed directly to the formal parameter, just a change of name is called user2
, but essentially user1
and user2
actually is the same. It is a reference to an object.
Next, the results of the output are analyzed, whether by value or by reference, the result of the 1th line output must be 张三
, because all points to the same object. For the 2nd line of output, we still cannot tell which way, because it is changing the same object, the value will change, the key is the 3rd line of output and the 4th line of output, at this point, we will user2
re-point to a new object, and assign the value to this object, name = '王五'
if it is 引用传递
the way, Then the user1
same will change the point, point to the new object, the last line to call the method after the output will be the same as the 3rd line 王五
, but the fact that the output is 李四
, this shows that user1
and actually is not the user2
same. The actual invocation process, as shown in the ~, will cause the object to point to 图2
图4
user2
a new object user1
without changing or the original object.
For basic type parameters, the value of A1
has not changed at last, stating that A2
is a copy of A1
when executing a method. For a parameter of a reference type, such as a User
object, when the method is called, it is actually referring to user1
as the actual argument, then a copy reference to the reference is passed to the parameter user2
, although this is a two-copy reference (like A1
and A2
). But it points to the same object, and all operations are to the same object.
Take an example to illustrate all this, if you have a key to your room and engrave your name on it, this process is like giving int
the initialization value of a type a1
5
. Your friend has a very good relationship with you and wants the key to your room, and you don't give him your key right away, but you copy a new key that opens the door to your room. And your friend engraved his name on the new key. This process is like calling change()
a method, a1
copying a copy of the assignment a2
, at this time, the modification a2
and a1
no relationship, your friend engraved his name on the new key will not affect your hand that the original key. The key is that both keys can open your room, just like user1
and user2
all point to the same object. At this point your friend opened your room with this new key and smashed the TV in your room. This process is like renaming a name 李四
. When you open your room with your key, you will inevitably see a scene like this-the TV was smashed. It's like calling a method and user1
turning it into a 李四
. In the process of invoking the method, user2
it finally re-points to a new object, which is like your friend's copy of the key you copied to him again processing, at this time can not open your room door, but can open his own room, He opened his room with the key and smashed his TV. This does not affect the TV in your room, which means that user1
the last name will not become 王五
. This is the java
value passed in. Of course, if it is a reference pass, then there will be only one key in this example, and the final result will be different.
End
We can tell by the above analysis. Java
only the value is passed in this way, but for reference types, the passed argument is a reference to the object.