Is Java ‘pass-by-reference’ or ‘pass-by-value’?

Pravinkumar Singh
Javarevisited
Published in
3 min readJun 14, 2023

--

The age-old question that sparks confusion and debate amongst Java developers is whether Java uses ‘pass-by-reference’ or ‘pass-by-value’. i am sure you must have encountered this question in your interviews.

Let’s find out.

Pass-by-Value vs. Pass-by-Reference

Before we dive into Java’s behaviour, let’s first understand what ‘pass-by-value’ and ‘pass-by-reference’ mean:

  • Pass-by-value: It means the value of a variable is passed to a method and when a method is called, a new copy of the variable is created in the method’s scope. Any changes made to this variable within the method don’t affect the original variable.
  • Pass-by-reference: When a method is called, a reference (memory address) to the original variable is passed to the method. Any changes made to the variable within the method affect the original variable as well.

Java’s Behavior: Pass-by-Value or Pass-by-Reference?

Java is strictly a pass-by-value language. However, confusion often arises due to the way Java handles objects and their references. When we pass an object to a method, we aren’t passing the actual object (like we would in ‘pass-by-reference’) — instead, we’re passing the reference value (memory address) of the object. Given that, any changes made to the object within the method will affect the original object.

In simple terms:

  • Primitive data types (int, float, char, etc.) are passed by value.
  • Object references are also passed by value. However, because the value being passed is the reference (memory address) of the object, it might feel like it’s being passed by reference.

Let’s illustrate this with a couple of code examples.

Example 1: Primitive Data Types

public class PassByValueDemo {

public static void main(String[] args) {
int number = 10;
System.out.println("Before: " + number); // Output: Before: 10
modifyNumber(number);
System.out.println("After: " + number); // Output: After: 10
}

public static void modifyNumber(int value) {
value = value * 2;
System.out.println("Modified: " + value); // Output: Modified: 20
}
}

In this example, the number variable is a primitive data type (int). When we pass this variable to the modifyNumber() method, only a copy of its value is passed. As a result, the original variable remains unaffected.

Example 2: Objects

public class PassByValueDemo {

public static void main(String[] args) {
Point point = new Point(2, 3);
System.out.println("Before: " + point); // Output: Before: (2, 3)
modifyPoint(point);
System.out.println("After: " + point); // Output: After: (20, 30)
}

public static void modifyPoint(Point point) {
point.setX(point.getX() * 10);
point.setY(point.getY() * 10);
System.out.println("Modified: " + point); // Output: Modified: (20, 30)
}
}

class Point {
private int x;
private int y;

public Point(int x, int y) {
this.x = x;
this.y = y;
}

public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
}

@Override
public String toString() {
return "(" + x + ", " + y + ")";
}
}

In this example, when we pass the point object to the modifyPoint() method, only a copy of the object's reference value (memory address) is passed. The original object is modified in the method, resulting in the change being reflected in the output. However, it's crucial to understand that the object's reference itself is passed by value, meaning the object's memory address cannot be changed.

Conclusion

Java is a pass-by-value language in both cases — primitive data types as well as objects. When passing objects to methods, it’s important to remember that their reference values (memory addresses) are being passed by value.

Hope you liked it

Peace!

--

--