Fundamentals 8 min read

Is Java Pass‑by‑Value or Pass‑by‑Reference? The Definitive Explanation

This article clarifies the long‑standing confusion about whether Java uses pass‑by‑value or pass‑by‑reference by defining both concepts, explaining the roles of actual and formal parameters, illustrating with primitive and object examples, and showing why Java is technically pass‑by‑value even for reference types.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Is Java Pass‑by‑Value or Pass‑by‑Reference? The Definitive Explanation

01. Value Passing vs Reference Passing

Before deciding whether Java uses pass‑by‑value or pass‑by‑reference, we must understand the definitions of the two concepts.

Pass‑by‑value means the argument is copied and passed to the method, so modifications to the parameter do not affect the original argument. Pass‑by‑reference means the argument’s address is passed directly, so changes to the parameter affect the original argument.

02. Actual Parameters and Formal Parameters

An actual parameter (argument) is the value supplied when calling a method; a formal parameter is the variable declared in the method signature that receives the argument.

Formal parameter: defined in the method declaration, used to receive the passed‑in value. Actual parameter: the value placed inside the parentheses when invoking the method.

Example with the classic main(String[] args) program:

Here args is the formal parameter, while the string "hello world" is the actual argument.

Another example using a custom method sop(String name):

In this case the literal "沉默王二" is the actual argument, and name inside sop is the formal parameter.

03. Primitive Types Are Passed by Value

Java’s primitive types (int, double, etc.) are always passed by value. The following code demonstrates that changing the formal parameter does not affect the actual argument:

The method receives a copy of age (18); modifying the parameter to 28 leaves the original value unchanged, as shown by the output.

04. Are Reference Types Passed by Value?

The confusion arises with reference types. Although the object itself is not copied, the reference (the memory address) is passed by value. Consider the following illustration:

In main() we create an object cmower with name = "沉默王二" and pass it to sop(cmower). Inside sop we change cmower.name to "沉默王三". The output shows the name has changed, leading many to think Java uses reference passing.

However, when we add an extra variable old = cmower and compare old == cmower, the result is true, confirming that the reference itself was copied (passed by value) while the object it points to was mutated.

Thus, Java is strictly pass‑by‑value; for reference types the "value" being passed is the reference.

05. Summary

When a primitive type is used as a parameter, its value is copied directly. When a reference type is used, the variable holds the object's memory address, and that address (the reference) is copied. Both cases are pass‑by‑value, but the value for objects is the reference.

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.

Javaprogramming fundamentalsParameter PassingReference Typespass-by-value
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

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.