Home >Java >javaTutorial >How to pass value in java?
We all know that there are basically two ways to pass by value: pass by value and pass by reference. So in JAVA, is it pass by value or pass by reference? The following article will introduce it to you, I hope it will be helpful to you.
Value passing: refers to copying a copy of the actual parameters and passing them to the formal parameters when calling the function, so that the formal parameters can be modified in the function Will not affect the actual parameter value.
Pass by reference: refers to passing the address of the actual parameter directly to the formal parameter when calling the function. Then the modification of the parameter in the function will affect the actual parameter. value.
How to pass value in java?
Java's value-passing method: value-passing (all changes are only limited to the method body, outside the method body, any modification operations are no longer valid). [Recommended learning: java course]
We can use a program to verify that only value is passed in Java
/** * 验证java中只有值传递 */ 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 result is:
改变之前:张三,a2=5 改变之后:李四,a2=10 重新指向一个新对象后:王五 调用方法后:李四,a1=5
Result Analysis
## Let’s use the above picture as an aid to analyze this paragraph Program, first we define aUser class, and then instantiate a
User object in the test class, named
user1, and assign it a value of
name = 'Zhang San'.
Figure 1, instantiating an object is equivalent to opening a piece of memory in the heap, and the memory address is
017 , at this time, the reference of this object is
user1, and the memory address is
001. It saves the address of the object in the memory, which means it points to the object.
change() to try to change the
name value of
user1 to verify the value passed in java Way.
user1 as an actual parameter to the
change() method, and the formal parameter
user2 accepts this actual parameter, which is reflected here. The difference between the two methods of passing parameters is revealed. If passed by value, then it is as defined.
Figure 2,
user2 is a copy of
user1, which means that when passing parameters,
user1 (itself a reference to an object), made a copy named
user2, which is also a reference to an object, and
user1 and
user2Points to the same object at this time.
Figure 5, when passing parameters,
user1 is directly passed to the formal parameter , just changed the name to
user2, but essentially
user1 and
user2 are actually the same. It is a reference to an object.
张三, because they all point to the same object. For the output in line 2, we still can't tell which method is used, because the same object is changed, and the value will also change; the key lies in the output in line 3 and line 4.
user2 to a new object and assign a value to this object
name = '王五', if it is passed by reference , then
user1 will also change the pointer to point to the new object. The output result after calling the method in the last line will be the same as the third line,
王五, but in fact the output It’s
李思. This shows that
user1 and
user2 are not actually the same.
Figure 2~
Figure 4, so that
user2 points to a new object,# The object pointed to by ##user1
has not changed, it is still the original object. For basic type parameters, the value of
has not changed at the end, indicating that a2
is one of a1
when the method is executed. copy. For reference type parameters, such as the
object, when calling the method, its reference user1
is actually used as the actual parameter, then it is passed to the form The reference will be a copy of the reference reference user2
, although this is two references (such as the relationship between a1
and a2
). But it points to the same object, and all operations are for the same object.
EndWe can know through the above analysis.
There is only one way of passing by value in Java, but for reference types, the parameters passed are references to objects.
The above is the detailed content of How to pass value in java?. For more information, please follow other related articles on the PHP Chinese website!