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

Jan 30, 2024 am 08:11 AM
java pass by reference Pass by value

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Square Root in Java Square Root in Java Aug 30, 2024 pm 04:26 PM

Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Armstrong Number in Java Armstrong Number in Java Aug 30, 2024 pm 04:26 PM

Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

See all articles