View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

Pass By Value and Call By Reference in Java

Updated on 04/03/20254,280 Views

In Java, pass by value means that a copy of the original variable is passed to a method, while call by reference passes the actual reference of the variable. The main problem arises when you need to understand how these concepts affect data manipulation in methods. 

In this guide, you'll uncover what is pass by value in Java and how call by reference in Java works. By the end, you'll better understand both, improving your coding approach.

Improve your Java programming skills with our Software Development courses — take the next step in your learning journey! 

Java's Strict Pass By Value Model

When you're working with methods in Java, it's important to understand what is pass by value in Java. Simply put, Java uses Pass By Value for method calls, meaning it always passes a copy of the variable's value to the method. 

Some developers assume Java supports Call By Reference, but it actually passes object references by value. Here's why this misconception exists.

What is Pass By Value in Java?

In Java, when you pass a variable to a method, Java doesn't give the method access to the original variable directly. Instead, it creates a copy of the variable's value and sends that copy to the method. Changing the value inside the method doesn't affect the original variable outside the method.

Here’s a simple example to illustrate this: 

public class PassByValueExample {
public static void main(String[] args) {
int num = 5;
System.out.println("Before method call: " + num); // Outputs 5
modifyValue(num);
System.out.println("After method call: " + num); // Outputs 5
}

public static void modifyValue(int num) {
num = 10;
}
}

Output: 

Before method call: 5
After method call: 5

Explanation:

  • In this example, the variable num is passed to the modifyValue method.
  • In Java, arguments are passed by value, so a copy of 5 is passed to the method, not the original variable.
  • Inside the method, the copy of num is changed to 10, but this does not affect the original variable in the main method.
  • After the method call, the value of num in main remains 5 because only the copy of the value was modified, not the original variable.

In this example, you can see that the original value of num outside the method doesn’t change even though we modified num inside the method. 

This is because Java passed a copy of num to the method, not the actual variable.

Why Java Doesn’t Support True Call By Reference

In some languages like C++, there's a concept called Call By Reference. This means that when you pass a variable to a method, the method can directly modify the original variable because it’s working with the actual reference to that variable, not just a copy.

However, in Java, Call By Reference is not how things work. Java passes a copy of the reference, meaning the reference itself cannot be modified inside the method, but object properties can be changed. 

So, while you can modify the object’s internal properties, you can’t change the reference to point to a new object.

Let’s look at an example to clarify: 

public class PassByValueWithObject {
public static void main(String[] args) {
Person person = new Person("Ajay");
System.out.println("Before method call: " + person.name); // Outputs Ajay
changeReference(person);
System.out.println("After method call: " + person.name); // Outputs Ajay
}

public static void changeReference(Person person) {
person = new Person("Neha"); // This changes the reference, but does not affect the original object
}
}

class Person {
String name;
Person(String name) {
this.name = name;
}
}

Output: 

Before method call: Ajay
After method call: Ajay

Explanation:

  • In this example, the person object is created with the name "Ajay" and passed to the changeReference method.
  • Java still follows Pass By Value, even with object references. The reference to the Person object is passed by value, meaning the method receives a copy of the reference, not the original one.
  • Inside the changeReference method, a new Person object is created with the name "Neha". However, this change only affects the local copy of the reference inside the method. The original person reference in the main method still points to the original object with the name "Ajay".
  • Therefore, after the method call, the name of the person object remains "Ajay" because the method did not alter the original reference in the main method.

In this case, the reference to the Person object is passed to the method, but the original reference in the main method is not changed since Java passes the reference by value. The Person object’s name remains "Ajay" after the method call.

Also Read: Exploring the 14 Key Advantages of Java: Why It Remains a Developer's Top Choice in 2025

Now that we've seen how Java handles objects with Pass By Value, let’s clear up some common myths about Call By Reference and why Java doesn't follow that path.

Objects in Java and Pass By Value

While Java is strictly Pass By Value, things behave a bit differently when working with objects. When you pass an object to a method, you're actually passing a copy of the reference to the object, not the object itself. 

This is still Pass By Value, but it can be confusing because it allows modifications to the object's internal state.

Let’s break it down:

  • The method receives a copy of the reference (not the actual reference), so you can modify the object’s fields or properties.
  • However, you cannot change the object that the reference points to—meaning you can't reassign the reference to point to a different object.

Code Example

public class PassByValueWithObject {
public static void main(String[] args) {
Person person = new Person("Alice");
System.out.println("Before method call: " + person.name); // Outputs Alice
modifyPerson(person);
System.out.println("After method call: " + person.name); // Outputs Bob
}

public static void modifyPerson(Person person) {
person.name = "Bob"; // Modifying the object's state
}
}

class Person {
String name;
Person(String name) {
this.name = name;
}
}

Output: 

Before method call: Alice
After method call: Bob

Explanation:

  • In the modifyPerson method, we modify the name property of the Person object. Since the reference to the object was passed by value, the method can change the object's internal state, but not the reference itself (e.g., you can’t change person to point to a new Person object).
  • After the method call, the original person object is updated, showing that the object's state was changed, not the reference.

 Also Read: Types of Variables in Java: Java Variables Explained

Whether you’re modifying simple properties or handling more complex object structures, understanding how Java handles references and values helps you write efficient and bug-free code.

How Well Do You Understand Pass By Value in Java? 10 MCQs

Test your knowledge of Pass By Value and Java’s method passing! From basic concepts to deeper understanding, let's see how well you know how Java handles method calls.

1. What is the main characteristic of Pass By Value in Java?

A) The original variable is passed to the method

B) A copy of the variable’s value is passed to the method

C) The reference to the variable is passed to the method

D) Only primitive types can be passed by value

2. What happens when a primitive type is passed to a method in Java?

A) The method gets the original variable

B) The method gets a copy of the value

C) The method cannot modify the variable

D) The method gets a reference to the original variable

3. In Java, what is passed when an object is passed to a method?

A) A reference to the object

B) The object itself

C) A copy of the reference to the object

D) A copy of the object

4. Which of the following is true when modifying an object inside a method in Java?

A) The object reference can be reassigned

B) The object’s internal state can be changed

C) Both the object reference and internal state can be modified

D) The method cannot access the object’s properties

5. Why do people sometimes think Java uses Call By Reference with objects?

A) Because Java allows object references to be passed by value

B) Because the internal state of objects can be modified

C) Because object references can be reassigned inside the method

D) Because Java passes objects directly to methods

6. What happens when you try to reassign an object reference passed to a method in Java?

A) It changes the original reference in the calling method

B) It creates a new object in the calling method

C) It modifies the internal state of the original object

D) It does nothing to the original reference

7. Which statement is true about Pass By Value in Java with primitive types?

A) The method can modify the original value

B) A copy of the original value is passed to the method

C) The method cannot access the original value

D) Primitive types are passed by reference in Java

8. Which of the following methods in Java will cause a change in the object’s internal state?

A) Modifying a primitive type variable passed to a method

B) Modifying an object’s field passed to a method

C) Reassigning an object reference passed to a method

D) Returning an object from a method

9. How does Java handle object references in method calls?

A) By passing the reference by value

B) By passing the reference by reference

C) By passing a copy of the object

D) By passing the object directly

10. Which of these statements is a common misconception about Java’s method calling mechanism?

A) Java uses Pass By Value for both primitive types and objects

B) Java uses Call By Reference for both primitive types and objects

C) Objects can be modified inside methods because of Pass By Value

D) Pass By Value and Call By Reference mean the same in Java

Also Read: Abstract Class and Methods in Java: Key Concepts, Examples and Best Practices

You can deepen your understanding of Java’s method passing by tackling real-world coding challenges and exploring more advanced Java concepts. This will help you master Pass By Value and improve your overall coding efficiency.

How Can upGrad Help You Master Java’s Method Passing?

upGrad provides comprehensive learning opportunities to help you master Java’s method passing concepts, including Pass By Value and working with object references. 

With hands-on projects and expert-led modules, you’ll learn how to manage complex data structures, apply object-oriented principles effectively, and understand how Java handles method calls in real-world applications.

Below are some relevant upGrad courses:

You can also receive personalized career guidance with upGrad to help you take the next step, or visit your nearest upGrad center to begin practical, hands-on training today!

Similar Reads:

FAQs

1. What is the difference between Pass By Value and Call By Reference in Java?

Pass By Value in Java means a copy of the variable is passed to the method. Call by reference does not apply in Java, as Java passes a reference by value, meaning the reference itself cannot be changed inside the method.

2. Can we modify the original value of a variable passed to a method in Java?

No, what is pass by value in Java means that only a copy of the value is passed, so changes made inside the method won’t affect the original variable.

3. Does Java pass object references by value or by reference?

Java passes the object reference by value. Pass By Value applies to the reference itself, meaning the object’s internal state can be modified, but the reference cannot point to a new object.

4. Why does it seem like Java uses Call By Reference for objects?

This confusion arises because the object’s internal properties can be modified. However, Pass By Value is still at play since the reference itself is passed by value.

5. Can I change the reference of an object inside a method in Java?

No, since Java passes the reference by value, you cannot change the reference to point to a new object inside the method.

6. What happens when a primitive type is passed to a method in Java?

The value of the primitive type is passed by copy, meaning changes inside the method will not affect the original value.

7. How do I pass an object by reference in Java?

Java doesn’t support Call By Reference in Java. Instead, the reference is passed by value. You can modify the object's internal state but cannot change its reference.

8. Does Pass By Value apply to arrays in Java?

Yes, arrays are objects in Java, and while their references are passed by value, you can modify the contents of the array since the reference points to the same object.

9. How does Java handle immutability with Pass By Value?

If an object is immutable, like a String, Pass By Value still applies. Any attempt to modify the object will create a new instance, not change the original one.

10. Can Pass By Value lead to performance issues in Java?

Generally, Pass By Value in Java does not cause performance issues, but copying large objects or data can result in overhead. This is more of a concern with deep copies of complex objects.

11. How does Pass By Value impact method behavior in Java?

Pass By Value means that methods cannot change the original value of variables passed to them, providing safer and more predictable behavior in Java.

image

Take the Free Quiz on Java

Answer quick questions and assess your Java knowledge

right-top-arrow
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
advertise-arrow

Free Courses

Explore Our Free Software Tutorials

upGrad Learner Support

Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918045604032

Disclaimer

1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.

2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.