Support Online
Skip to main content

Why is Pass by Value Essential in Java?

It is often discussed how data is passed in method calls in Java.
So, Does Java really go "by reference" or "by value"?
In this guide, we will prove with examples that Java always uses pass by value**.

🧠 Technical Summary

Main topic: Java's way of passing arguments to methods (Pass by Value)
Problem: “Java pass by reference?” confusion around the question
Steps:

  1. Learn the differences between pass by value and pass by reference
  2. Create class Balloon
  3. Test reference change with method swap()
  4. Examine the value update with the changeValue() method

Purpose:
Understanding why Java is not a “true pass by reference” by understanding that even object references are copied as values.


🔍 Argument Passing in Java: Value or Reference?

Java always uses pass by value.
This rule applies to both primitive types (int, boolean) and object references.

Concept Difference

GenreDescriptionImpact
Pass by ValueThe value of the variable is copied. Changes within the method do not affect the original.The original variable is preserved
Pass by ReferenceThe address (reference) of the variable is transferred.The method can directly modify the original object

In Java, a variable does not hold the object, but the address of the object in memory**.
A copy of this address is sent to the method — so although it looks like a “pass by reference”, it is not.


🎈 1. Balloon Class (Auxiliary Object)

// Bu sınıf, renk bilgisini tutan basit bir nesneyi temsil eder.
public class Balloon {

private String color;

public Balloon(String c) {
this.color = c;
}

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}
}

🔁 2. Pass by Value Proof with Swap() Method

The swap() method attempts to swap the references of two objects, but actually only swaps their reference copies.


public class Test {

public static void main(String[] args) {
Balloon kirmizi = new Balloon("Kirmizi"); // Bellek adresi: 50
Balloon mavi = new Balloon("Mavi"); // Bellek adresi: 100

swap(kirmizi, mavi);
System.out.println("Swap sonrası:");
System.out.println("kirmizi rengi = " + kirmizi.getColor());
System.out.println("mavi rengi = " + mavi.getColor());

changeValue(mavi);
System.out.println("changeValue sonrası:");
System.out.println("mavi rengi = " + mavi.getColor());
}

public static void swap(Object o1, Object o2) {
Object temp = o1; // temp = 50
o1 = o2; // o1 = 100
o2 = temp; // o2 = 50
}
}

Expected Output:


Swap sonrası:
kirmizi rengi = Kirmizi
mavi rengi = Mavi

🧩 Why Did This Happen?

In swap(), the variables o1 and o2 just hold copies of the references.

Even though these copies are swapped, the external red and blue variants continue to point to the same addresses. ➡️ Result: Original objects are not affected.


🎨 3. Value Update with changeValue() Method

Now let's try changing the content of the same object.


private static void changeValue(Balloon balloon) {
balloon.setColor("Kirmizi"); // Bellek 100'deki nesnenin rengini değiştirir
balloon = new Balloon("Yesil"); // Yeni nesne (Bellek 200)
balloon.setColor("Mavi"); // 200 adresindeki nesneyi değiştirir
}```

Expected Output:

```java

changeValue sonrası:
mavi rengi = Kirmizi
```java



after changeValue:

blue color = red

📘 Explanation: the balloon parameter is a copy of the address of the blue variable (100).

The first line sets the color of the original object to “Red”.

When a new Balloon("Green") object (200) is created, the balloon now moves to the new address.

However, blue still shows the old address (100). ➡️ That is, the original object is affected by the change in the first row only.


🧠 4. Memory Logic in Java

VariableMemory AddressColor ValueStatus
red50RedUnchanged
blue100RedOnly the first change took effect
balloon (inside method)200BlueSwitched to new object

Java copies the address value of the reference in memory.
Therefore, the reference change within the method is not reflected outside.


📘 Pass by Value Behavior Summary Table

Data TypeHow to Transfer in JavaDescription
Primitive types (int, boolean)With valueActual value is copied
Object types (class, array)With reference valueThe address of the reference is copied

❓ Frequently Asked Questions (FAQ)

  1. Why is Java not Pass by Reference?

Because a copy of the address of that object is passed to the methods, not the object itself.

  1. Why does swap() fail?

It only changes the reference copies, the original addresses remain the same.

  1. Why is changeValue() only effective on the first line?

The first line changes the same object, after which the connection is broken because the new object is created.

  1. Is the situation different in other languages ​​(C++, Python)?

Yes. In C++, the reference can be passed directly, while in Python, the reference effect is seen in mutable types.

  1. Why is this principle important?

It helps you understand the cause of “unexpected changes” in Java codes and prevents side effects.


🏁 Conclusion

With these examples, you learned that Java always works with pass by value logic. Even object references are copied as values; that is, a method does not access the original object but its address copy.