Java Method Parameter Passing: Pass by Value or Pass by Reference? A Comprehensive Guide

In Java learning, the question of “whether method parameters are passed by value or by reference” is a common point of confusion. Many beginners might think that since objects (reference types) are passed, it is “pass by reference”; however, in reality, all parameter passing in Java is essentially pass by value. Today, we’ll use simple examples to help you thoroughly understand this issue.

一、先明确概念:值传递 vs 引用传递

  • Pass by Value: The method receives a “copy” of the parameter. Modifying the parameter will not affect the original variable. For example, if you hand someone a note (the value), and they write on it, it won’t change the note in your hand.
  • Pass by Reference: The method receives the “reference address” of the parameter (similar to a “key”). Modifying the parameter will directly affect the original object. For example, if you give someone a key (the reference), and they use the key to open the original lock (the object) and modify its content, the content of the original lock will change.

Key Difference: Pass by value modifies the “copy”, while pass by reference modifies the “original object”. However, there is no strict “pass by reference” in Java—only pass by value exists.

二、基本类型参数:值传递的直接体现

In Java, basic types (such as int, double, boolean, etc.) are passed as copies of their values. Modifying the parameter will not affect the original variable.

Example: Swapping two int variables

public class ValuePassDemo {
    // Define a swap method
    public static void swap(int a, int b) {
        int temp = a;
        a = b;
        b = temp;
    }

    public static void main(String[] args) {
        int x = 10;
        int y = 20;
        swap(x, y); // Call the method
        System.out.println("x=" + x + ", y=" + y); // Output: x=10, y=20
    }
}

Why the result didn’t change?
The a and b in the swap method are “copies” of x and y. Swapping a and b only modifies the copies, leaving the original variables x and y unchanged.

三、引用类型参数:容易混淆的“陷阱”

In Java, reference types (such as objects, arrays, interfaces, etc.) are passed as copies of the reference address. Although the “address copy” itself is modified, since the copy and the original reference point to the same object, modifying the object’s properties will affect the original object.

Misconception 1: Modifying object properties affects the original object → mistaken for “pass by reference”

class Student {
    String name;
    public Student(String name) {
        this.name = name;
    }
}

public class ReferencePassDemo {
    // Method: Modify the name property of a Student object
    public static void changeName(Student s) {
        s.name = "小明"; // Modify object property
    }

    public static void main(String[] args) {
        Student s = new Student("小红");
        changeName(s); // Pass the object reference
        System.out.println(s.name); // Output: 小明 (original object property modified)
    }
}

The Truth:
The changeName method receives a copy of the Student object’s reference (the copy and the original reference s point to the same Student object). Modifying the name property actually changes the content of “the same object”, so the original object’s property will change. This happens because the “reference address copy” and the original reference point to the same object—not because it is “pass by reference”.

Misconception 2: Modifying the reference itself → original object is unaffected

If you modify the reference itself in the method (making the reference point to a new object), the original object will not change. This further proves it is “pass by value”:

public static void changeReference(Student s) {
    s = new Student("小刚"); // Modify the reference to point to a new object
}

public static void main(String[] args) {
    Student s = new Student("小红");
    changeReference(s); // Pass the original reference
    System.out.println(s.name); // Output: 小红 (original object unchanged)
}

Why the result didn’t change?
The Student s in changeReference is a “copy” of the original reference. Modifying the copy to point to a new object leaves the original reference s in main still pointing to “小红”. This is the essence of pass by value: the “reference address copy” is modified, not the original reference itself.

四、总结:Java永远是“值传递”

  • Basic Types: Pass a “copy of the value”. Modifying the copy does not affect the original variable.
  • Reference Types: Pass a “copy of the reference address”. Modifying the properties of the object pointed to by the copy will affect the original object (since they share the same object), but modifying the reference itself (to point to a new object) will not affect the original object.

Core Conclusion: All parameter passing in Java is pass by value. There is no “pass by reference”. The special nature of reference types lies in the “shared object through address copies”, not in the passing method itself being “pass by reference”.

一句话记住:

  • Pass by Value: Passes a “copy”; modifying the copy does not affect the original.
  • Reference Types: Passes a “copy of the address”; shares the object, and modifying properties affects the original.
    Java always uses “pass by value”!
Xiaoye