Pass By Value vs Pass By Reference in Java: Complete Guide with Internal Working and Examples
One of the most common Java interview questions is: "Does Java support Pass By Value or Pass By Reference?" Many developers, including experienced programmers, get confused when answering…
One of the most common Java interview questions is:
“Does Java support Pass By Value or Pass By Reference?”
Many developers, including experienced programmers, get confused when answering this question because object modifications inside methods appear to behave like Pass By Reference.
However, the truth is:
Java is always Pass By Value.
There is no Pass By Reference mechanism in Java.
Understanding this concept is extremely important because it helps you understand method calls, object references, memory management, collections, Spring applications, and many advanced Java concepts.
In this article, we will understand:
- What is Pass By Value?
- What is Pass By Reference?
- How Java handles primitive types
- How Java handles objects
- Why objects create confusion
- Internal memory working
- Multiple program examples
- Interview questions and answers
By the end of this article, you will be able to confidently explain this topic in any Java interview.
What is Pass By Value?
In Pass By Value, a method receives a copy of the variable’s value.
Any modifications made inside the method affect only the copied value.
The original variable remains unchanged.
Example
public class Main {
static void changeValue(int num) {
num = 100;
}
public static void main(String[] args) {
int number = 10;
changeValue(number);
System.out.println(number);
}
}
Output
10
Internal Working
Initially:
number = 10
When method is called:
changeValue(number);
Java creates a copy:
number = 10
num = 10
Memory representation:
Main Method
number = 10
After method call:
changeValue()
num = 10
Now:
num = 100;
Only the copied variable changes:
number = 10
num = 100
Method finishes execution.
The copied variable is removed from memory.
The original variable remains:
10
What is Pass By Reference?
In Pass By Reference, a method receives direct access to the original variable.
Any modifications made inside the method affect the original variable.
Example in C++:
void change(int &x)
{
x = 100;
}
When executed:
int num = 10;
change(num);
Output:
100
Because the method receives the actual variable, not a copy.
Java does NOT support this behavior.
Why Developers Get Confused in Java
Let’s see an object example.
Program
class Employee {
String name;
}
public class Main {
static void modify(Employee emp) {
emp.name = "John";
}
public static void main(String[] args) {
Employee employee = new Employee();
employee.name = "Prakash";
modify(employee);
System.out.println(employee.name);
}
}
Output
John
Many developers see this output and conclude:
“Java is passing objects by reference.”
This conclusion is incorrect.
Understanding References
Consider:
Employee employee = new Employee();
Java creates an object in heap memory.
Suppose the object is located at:
Address 1000
Memory:
Stack Memory
employee
|
|
V
Heap Memory
Employee Object
Address 1000
The variable employee does not store the object.
Instead, it stores a reference.
employee = 1000
What Happens During Method Call?
Method call:
modify(employee);
Java copies the reference value.
Before method call:
employee = 1000
After method call:
employee = 1000
emp = 1000
Memory:
Stack
employee ----+
|
emp ---------+
|
V
Heap
Employee Object
Both references point to the same object.
Modifying Object State
Inside method:
emp.name = "John";
The object itself is modified.
Since both references point to the same object:
employee -> Object
emp -> Object
The change becomes visible everywhere.
Output:
John
Important Point
The object changed.
The reference did not.
Java still passed a copy of the reference.
Therefore Java remains Pass By Value.
Proof That Java Is Not Pass By Reference
Let’s try replacing the object.
class Employee {
String name;
}
public class Main {
static void change(Employee emp) {
emp = new Employee();
emp.name = "John";
}
public static void main(String[] args) {
Employee employee = new Employee();
employee.name = "Prakash";
change(employee);
System.out.println(employee.name);
}
}
Output
Prakash
Internal Working
Initially:
employee -----> Object1
Reference value:
employee = 1000
Method call:
employee = 1000
emp = 1000
Now:
emp = new Employee();
Java creates another object:
Object2
Address 2000
Now memory becomes:
employee -----> Object1
emp ----------> Object2
The copied reference now points somewhere else.
Original reference remains unchanged.
Therefore:
employee.name
Still prints:
Prakash
This is the strongest proof that Java does not support Pass By Reference.
Primitive Example
public class Main {
static void increment(int num) {
num++;
}
public static void main(String[] args) {
int value = 5;
increment(value);
System.out.println(value);
}
}
Output:
5
Because primitive values are copied.
String Example
public class Main {
static void change(String str) {
str = "Java";
}
public static void main(String[] args) {
String name = "Spring";
change(name);
System.out.println(name);
}
}
Output:
Spring
Reason:
String references are also passed by value.
The copied reference points to a new String object.
Original reference remains unchanged.
Array Example
public class Main {
static void update(int[] arr) {
arr[0] = 100;
}
public static void main(String[] args) {
int[] numbers = {10,20,30};
update(numbers);
System.out.println(numbers[0]);
}
}
Output:
100
Because both references point to the same array object.
Real Interview Explanation
Suppose interviewer asks:
“How are objects passed in Java?”
Correct answer:
“Objects are not passed by reference. Java passes a copy of the object reference. Since both references point to the same object, modifications to the object’s state are visible outside the method. However, replacing the reference inside the method does not affect the original reference.”
This answer immediately demonstrates a strong understanding of Java internals.
Common Interview Questions
Question 1
Is Java Pass By Value or Pass By Reference?
Answer:
Java is always Pass By Value.
Question 2
Are objects passed by reference in Java?
Answer:
No.
A copy of the reference is passed.
Question 3
Can a method change an object’s state?
Answer:
Yes.
Because both references point to the same object.
Question 4
Can a method replace the original object reference?
Answer:
No.
Only the copied reference can be replaced.
Question 5
Can primitive variables be modified through method calls?
Answer:
No.
Only copies are modified.
Final Conclusion
The biggest misconception in Java is believing that objects are passed by reference.
Java never passes variables by reference.
Java always creates a copy:
- Primitive types → copy of actual value
- Objects → copy of reference value
Because object references point to the same object, object modifications are visible outside the method, which creates the illusion of Pass By Reference.
Remember this interview statement:
Java is strictly Pass By Value. For primitive types, the value is copied. For objects, the reference value is copied. Java never passes the original variable itself.