Fundamentals 10 min read

Is Java Pass‑by‑Value or Pass‑by‑Reference? A Deep Dive into Method Parameter Passing

This article demystifies Java's method‑parameter passing by analyzing code examples with primitives, strings, and objects, illustrating how the JVM copies values—including reference addresses—so that arrays appear mutable while reassigning a String or object reference does not affect the original variable.

Senior Brother's Insights
Senior Brother's Insights
Senior Brother's Insights
Is Java Pass‑by‑Value or Pass‑by‑Reference? A Deep Dive into Method Parameter Passing

Background and Interview Question

The article is part of a Java interview series and tackles the long‑standing debate: does Java use value passing or reference passing when invoking methods? It starts with a sample method that prints an array and a String before and after calling a helper method.

@Test
public void test1() {
    String[] arr = {"关注", "程序", "新视界"};
    String name = "二师兄";
    System.out.println("before change : arr=" + Arrays.toString(arr));
    System.out.println("before change : name=" + name);
    change(arr, name);
    System.out.println("after change : arr=" + Arrays.toString(arr));
    System.out.println("after change : name=" + name);
}

public void change(String[] arr, String name) {
    arr[0] = "公众号";
    name = new String("Steven");
    System.out.println("in change method : arr=" + Arrays.toString(arr));
    System.out.println("in change method : name=" + name);
}

Running this code shows that the array element changes persist after the method call, while the String variable remains unchanged, prompting the question of whether the first parameter is passed by reference and the second by value.

Value Passing vs. Reference Passing

Pass‑by‑value copies the actual argument into the parameter; modifications inside the method do not affect the original argument. Pass‑by‑reference passes the argument’s address, so changes to the parameter affect the original object.

The essential distinction is whether the called method receives a fresh copy of the argument’s value or a direct handle to the original memory location.

Primitive Types and String Passing

Many developers mistakenly think that the type (primitive vs. object) decides the passing mode. In reality, Java always passes a copy of the argument’s value. For primitives, the value itself is copied. For objects (including String), the *reference* value is copied.

@Test
public void test2() {
    int a = 10;
    change(a);
}
public void change(int b) {
    b = 2;
}

After change returns, a is still 10, confirming value passing for primitives.

@Test
public void test3() {
    String name = "Tom";
    change(name);
}
public void change(String str) {
    str = new String("Steven");
}

Inside change, the reference to the original String is copied; reassigning str points the local copy to a new object, leaving the original name unchanged. This still follows value passing—the copied value happens to be a reference.

Reference Types (Objects) and Their Mutability

Arrays and custom objects are reference types. The following example shows that mutating the object’s internal state via a method call does affect the original object, even though the reference itself was passed by value.

@Test
public void test4() {
    User user = new User();
    user.setName("Tom");
    change(user);
}
public void change(User paramUser) {
    paramUser.setName("Steven");
}
class User {
    private String name;
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
}

After change, user.getName() returns "Steven" because the method operated on the same heap object whose reference was copied into paramUser. The reference value itself was not altered.

To demonstrate that reassigning the parameter does **not** affect the caller, the method is altered:

public void change(User paramUser) {
    paramUser = new User();
    paramUser.setName("Steven");
}

Now the original user still holds the name "Tom" because the new object is only referenced by the local copy.

Conclusion

Across primitives, String, arrays, and custom objects, Java consistently passes arguments **by value**. For objects, the value being copied is the reference (address) to the heap object, which explains why mutating the object’s fields is visible to the caller while reassigning the parameter is not. Understanding this JVM memory model resolves many interview‑style confusions about Java’s parameter passing semantics.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

referenceprimitivepass-by-valuemethod-parametersmemory-model
Senior Brother's Insights
Written by

Senior Brother's Insights

A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.