Home > Java > javaTutorial > Detailed analysis of the difference between value passing and reference passing in Java

Detailed analysis of the difference between value passing and reference passing in Java

WBOY
Release: 2024-01-30 08:11:06
Original
796 people have browsed it

Detailed analysis of the difference between value passing and reference passing in Java

In-depth analysis of the difference between value passing and reference passing in Java

In Java programming, we often encounter the situation of passing parameters, and the parameter passing There are two ways: passing by value and passing by reference. These two delivery methods have different characteristics and application scenarios in Java.

Pass-by-Value refers to passing the value of the actual parameter to the formal parameter when the function is called. Modifications to the formal parameters within the function will not affect the value of the actual parameter. Pass-by-Reference means that when a function is called, the reference of the actual parameter is passed to the formal parameter. Modifications to the formal parameters within the function will affect the actual parameters.

In order to better understand the difference between value passing and reference passing, let's look at a simple sample code:

public class PassByValueDemo {

    public static void main(String[] args) {
        int num1 = 10;
        int num2 = 20;
        System.out.println("交换前:num1=" + num1 + ", num2=" + num2);
        swap(num1, num2);
        System.out.println("交换后:num1=" + num1 + ", num2=" + num2);

        Person person1 = new Person("Tom");
        Person person2 = new Person("Jerry");
        System.out.println("交换前:person1=" + person1 + ", person2=" + person2);
        swap(person1, person2);
        System.out.println("交换后:person1=" + person1 + ", person2=" + person2);
    }

    public static void swap(int a, int b) {
        int temp = a;
        a = b;
        b = temp;
    }

    public static void swap(Person p1, Person p2) {
        Person temp = p1;
        p1 = p2;
        p2 = temp;
    }
}

class Person {
    private String name;

    public Person(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }
}
Copy after login

In the above code, we pass the swap method To exchange two numbers and the values ​​of two Person objects. In the case of value passing, we expect the swap method to be able to exchange numbers and values ​​of Person objects. However, after running the code, we will find that only the values ​​of the numbers have been exchanged, and the value of the Person object has not changed.

This is because value transfer in Java copies the value of the actual parameter to the formal parameter, and modifies the formal parameter inside the function without affecting the original actual parameter. Therefore, in the swap(int a, int b) method, the values ​​of a and b are exchanged without affecting the values ​​of num1 and The value of num2.

For the exchange in the swap(Person p1, Person p2) method, we expect to be able to exchange the values ​​​​of person1 and person2. However, since reference passing in Java is to pass the reference of the actual parameter to the formal parameter, the formal parameter and the actual parameter point to the same object. During the exchange process, we only exchanged the references of the formal parameters p1 and p2, but did not modify the actual parameters person1 and person2 points to the object, so the exchange does not take effect.

The Person object can be correctly exchanged with the following code:

public static void swap(Person p1, Person p2) {
    String temp = p1.getName();
    p1.setName(p2.getName());
    p2.setName(temp);
}
Copy after login

In the above code, by calling getName() and setName () method to obtain and modify the attribute values ​​​​of the Person object to achieve correct object exchange.

To sum up, value passing and reference passing are two different parameter passing methods in Java. Passing by value does not modify the value of the actual parameter, while passing by reference modifies the object pointed to by the actual parameter. Understanding and correctly using these two delivery methods is very important for Java programming.

The above is the detailed content of Detailed analysis of the difference between value passing and reference passing in Java. For more information, please follow other related articles on the PHP Chinese website!

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