Home > Java > javaTutorial > body text

How to pass value in java?

青灯夜游
Release: 2019-11-15 15:03:10
Original
3551 people have browsed it

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.

How to pass value in java?

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);
    }
}
Copy after login

Run this program, the output result is:

改变之前:张三,a2=5
改变之后:李四,a2=10
重新指向一个新对象后:王五
调用方法后:李四,a1=5
Copy after login

Result Analysis

How to pass value in java?

## Let’s use the above picture as an aid to analyze this paragraph Program, first we define a

User class, and then instantiate a User object in the test class, named user1, and assign it a value of name = 'Zhang San'.

At this time, in the memory, as shown in

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.

Next, we call the method

change() to try to change the name value of user1 to verify the value passed in java Way.

We pass

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.

As shown in

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.

And if it is passed by reference, it is as defined, as shown in

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.

Next, let’s analyze the output results. Whether it is passed by value or by reference, the output result in line 1 must be

张三, 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.

At this time, we redirect

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.

The real calling process is as shown in

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

a1

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

User

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!