Is Java "pass-by-reference" or "pass-by-value"?

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Java is strictly "pass-by-value," not "pass-by-reference." This concept is often misunderstood because of how Java handles object references.

Understanding Pass-by-Value in Java

In Java, when you pass a variable to a method, you are actually passing a copy of the variable's value. This distinction is straightforward for primitive types, but it can be confusing when dealing with objects due to the following reasons:

  • Primitive Types: For primitive data types (like int, char, double, etc.), the actual values are stored directly in the variables. When a primitive type is passed to a method, a copy of the value is made, and any changes to this value in the method do not affect the original value.
public class Main { public static void main(String[] args) { int x = 10; modifyValue(x); System.out.println(x); // Outputs: 10 } public static void modifyValue(int value) { value = 20; } }

In this example, x remains 10 even after being passed to modifyValue because only a copy of x is modified.

  • Object References: For objects, the variable actually holds a reference (or address) to the object in memory, not the object itself. When an object is passed to a method, the value of the reference is copied. Both the original reference and the copied reference point to the same object in memory. Therefore, while you cannot change the reference itself (to point to a different object), you can modify the object's state that both references point to.
public class Main { public static void main(String[] args) { StringBuilder sb = new StringBuilder("Hello"); appendWorld(sb); System.out.println(sb); // Outputs: Hello World } public static void appendWorld(StringBuilder builder) { builder.append(" World"); } }

In this example, the StringBuilder object sb is modified inside appendWorld. Although sb is passed by value, the method receives a copy of the reference pointing to the same StringBuilder object. Changes made through the builder reference inside the method reflect in the original object.

Key Points

  • Java does not allow method parameters to alter the original reference to point to a different object. You can modify the object itself but not the "pointing" of the reference variable passed.
  • Java's approach avoids some pitfalls associated with true pass-by-reference semantics, but it requires understanding that objects are accessed via references, and passing a reference itself is done by value.

Conclusion

In Java, all function argument passing is done by value — for both primitive types and objects. The confusion typically arises from the behavior with objects, where modifications to the object itself within a method affect the original object, while the reference itself (the pointer to the object) cannot be altered to point to a different object.

TAGS
System Design Interview
CONTRIBUTOR
Design Gurus Team

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
How to self-learn front-end?
Is ServiceNow a product-based company?
How do you manage inter-service communication in microservices?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Image
Grokking Data Structures & Algorithms for Coding Interviews
Image
Grokking Advanced Coding Patterns for Interviews
Image
One-Stop Portal For Tech Interviews.
Copyright © 2024 Designgurus, Inc. All rights reserved.