For working professionals
For fresh graduates
More
6. JDK in Java
7. C++ Vs Java
16. Java If-else
18. Loops in Java
20. For Loop in Java
45. Packages in Java
52. Java Collection
55. Generics In Java
56. Java Interfaces
59. Streams in Java
62. Thread in Java
66. Deadlock in Java
73. Applet in Java
74. Java Swing
75. Java Frameworks
77. JUnit Testing
80. Jar file in Java
81. Java Clean Code
85. Java 8 features
86. String in Java
92. HashMap in Java
97. Enum in Java
100. Hashcode in Java
104. Linked List in Java
108. Array Length in Java
110. Split in java
111. Map In Java
114. HashSet in Java
117. DateFormat in Java
120. Java List Size
121. Java APIs
127. Identifiers in Java
129. Set in Java
131. Try Catch in Java
132. Bubble Sort in Java
134. Queue in Java
141. Jagged Array in Java
143. Java String Format
144. Replace in Java
145. charAt() in Java
146. CompareTo in Java
150. parseInt in Java
152. Abstraction in Java
153. String Input in Java
155. instanceof in Java
156. Math Floor in Java
157. Selection Sort Java
158. int to char in Java
163. Deque in Java
171. Trim in Java
172. RxJava
173. Recursion in Java
174. HashSet Java
176. Square Root in Java
189. Javafx
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!
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.
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, 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.
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 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.
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:
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:
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.
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.
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:
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.
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.
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.
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.
No, since Java passes the reference by value, you cannot change the reference to point to a new object inside the method.
The value of the primitive type is passed by copy, meaning changes inside the method will not affect the original value.
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.
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.
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.
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.
Pass By Value means that methods cannot change the original value of variables passed to them, providing safer and more predictable behavior in Java.
Take the Free Quiz on Java
Answer quick questions and assess your Java knowledge
Author
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
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.