DevNotes
Support
Core Java · Chapter 13

Pass by Value in Java

Why Java is always pass-by-value, and what really happens when you pass primitives, objects, Strings and arrays.

◷ 6 min read§ 20 sections? 11 interview questions{ } 20 code examples

#Introduction

One of the most confusing and frequently asked Java interview questions is:

Is Java Pass by Value or Pass by Reference?

Many developers answer: "Primitive types are Pass by Value and Objects are Pass by Reference."

❌ This is incorrect. The correct answer is:

Java is 100% Pass by Value.

Whether you pass a primitive variable or an object, Java always passes a copy of the value. The confusion arises because, for objects, the value being copied is the reference (memory address), not the object itself.

#What is Pass by Value?

Pass by Value means a copy of the variable's value is passed to the method.

Any modification inside the method affects only the copied value. The original variable remains unchanged.

#Pass by Value with Primitive Types

Java
public class Demo {

    static void change(int x) {
        x = 100;
    }

    public static void main(String[] args) {
        int number = 10;
        change(number);
        System.out.println(number);
    }
}
Output
10

Memory Representation

Before callnumber = 10
Inside methodx = 10 → x = 100
After callnumber = 10
Only the copied variable changes; the original stays 10

#Pass by Value with Objects

Java
class Employee {
    String name;
}
Java
Employee emp = new Employee();
emp.name = "John";

Method:

Java
static void update(Employee e) {
    e.name = "David";
}

Calling:

Java
update(emp);
Output
David

Many people think Java passed the object by reference. It did not. Java passed a copy of the reference.

Memory Diagram

emp0x100e0x100 (copy)
Employee @0x100name = John → David
Both references hold 0x100, so they point to the same object

Changing object state with e.name = "David"; updates the shared object.

#Reassigning the Reference

Java
static void change(Employee e) {
    e = new Employee();
    e.name = "Alice";
}

Main:

Java
Employee emp = new Employee();
emp.name = "John";
change(emp);
System.out.println(emp.name);
Output
John

Why? Because only the copied reference changed. The original reference still points to the original object.

Memory Representation

emp0x100
Employee @0x100name = John
Before
emp0x100
Employee @0x100name = John
e0x200
Employee @0x200name = Alice
Inside the method: the copy is re-pointed
emp0x100
Employee @0x100name = John
After the method returns: the object at 0x200 is eligible for garbage collection

#Primitive vs Object

PrimitiveObject
Value copiedReference value copied
Independent valuesShared object
Original unchangedObject state may change

Java is Pass by Value in both cases.

#String Example

Java
static void change(String s) {
    s = "Spring";
}
Java
String str = "Java";
change(str);
System.out.println(str);
Output
Java

Reason: Strings are immutable. A new String object is created, and the original reference is unaffected.

#Array Example

Java
static void modify(int[] arr) {
    arr[0] = 99;
}
Java
int[] numbers = {10, 20, 30};
modify(numbers);
System.out.println(numbers[0]);
Output
99

Array contents changed because both references point to the same array.

Reassigning Array

Java
static void reset(int[] arr) {
    arr = new int[]{1, 2, 3};
}

The original array remains unchanged. Only the copied reference changed.

#Wrapper Class Example

Java
static void increment(Integer x) {
    x++;
}

Main:

Java
Integer value = 10;
increment(value);
System.out.println(value);
Output
10

Reason: wrapper classes are immutable. x++ creates a new Integer, and the original reference is unchanged.

#Mutable Object Example

Java
static void append(StringBuilder sb) {
    sb.append(" Boot");
}
Java
StringBuilder builder = new StringBuilder("Spring");
append(builder);
System.out.println(builder);
Output
Spring Boot

Reason: StringBuilder is mutable. Both references point to the same object.

#Why the Confusion?

People observe:

Java
employee.name = "David";

and think Java passed the object itself. Actually, Java copied the reference value. Both references point to the same object. The object changes, not the reference.

#Real Project Examples

Service Layer

Java
updateEmployee(employee);

The service modifies the employee object. The caller sees updated values because both references point to the same object.

DTO

Java
request.setUsername("John");

The method changes object state. The original object reflects the changes.

Collections

Java
addEmployee(list);

The method adds elements. The caller sees the updated list.

#Interview Trick Question

#Pass by Value vs Pass by Reference

Pass by ValuePass by Reference
Copy of value is passedOriginal variable is passed
Original variable cannot be reassignedOriginal reference can change
JavaLanguages like C++ (reference parameters)

Java supports only Pass by Value.

#Common Interview Questions

Q1Is Java Pass by Value or Pass by Reference?

Java is always Pass by Value.

Q2Why do object changes reflect outside the method?

Because the copied reference and original reference point to the same object.

Q3Can a method change the caller's object reference?

No. Only the copied reference can be reassigned.

Q4Why does changing a String inside a method not affect the caller?

Because String is immutable.

Q5Can arrays be modified inside methods?

Yes. Arrays are mutable.

Q6Can wrapper objects be modified?

No. Wrapper classes are immutable.

Q7Why is StringBuilder modified successfully?

Because it is mutable.

Q8What exactly is passed for objects?

A copy of the object reference.

Q9Can Java simulate Pass by Reference?

Not directly. You can modify the state of mutable objects, but you cannot change the caller's reference variable.

Q10What is the correct interview answer?
Java is always Pass by Value. For objects, the value being passed is a copy of the reference.

#Common Mistakes

  • Saying Java supports Pass by Reference.
  • Confusing object modification with reference modification.
  • Assuming reassigning a parameter changes the caller's variable.
  • Forgetting that immutable objects behave differently.

#Best Practices

  • Remember that Java is always Pass by Value.
  • Use mutable objects only when modification is intended.
  • Prefer immutable objects where possible.
  • Clearly document methods that modify object state.
  • Be careful when passing collections or mutable DTOs.

#Quick Revision

Primitive
Copy Value
No Change
Object
Copy Reference
Same Object
State Can Change
Reference Cannot Change

#Interview Cheat Sheet

ScenarioResult
Primitive passedCopy of value
Object passedCopy of reference
Modify object stateVisible outside
Reassign parameterNot visible outside
String modificationNo change
StringBuilder modificationVisible outside

#Chapter Summary

After completing this chapter, you should be able to:

  • Explain why Java is always Pass by Value.
  • Differentiate between copying a primitive value and copying an object reference.
  • Understand why object state changes are visible while reference reassignments are not.
  • Explain the behaviour of Strings, Arrays, Wrapper Classes, and StringBuilder when passed to methods.
  • Confidently answer one of the most common Java interview questions with diagrams and examples.