In the java code example, what is the reference and what is the value?
Categories:
Java Pass-by-Value: Understanding References and Values

Explore the fundamental concept of pass-by-value in Java, clarifying how primitive types and object references are handled in method calls.
One of the most common sources of confusion for new Java developers is understanding how arguments are passed to methods. Java is strictly "pass-by-value." This means that when you pass a variable to a method, a copy of that variable's value is made and passed to the method. However, the interpretation of "value" differs significantly between primitive types and object references, leading to common misconceptions about "pass-by-reference."
The Core Principle: Pass-by-Value
In Java, every argument passed to a method is passed by value. This means that the method receives a copy of whatever is passed to it. The key distinction lies in what that 'value' actually represents:
- For primitive types (e.g.,
int
,char
,boolean
,double
): The actual value of the primitive is copied. Changes made to the parameter inside the method do not affect the original variable outside the method. - For object types (e.g.,
String
,Object
, custom classes): The value of the variable is a reference (memory address) to the object. A copy of this reference is passed to the method. Both the original variable and the method parameter now point to the same object in memory. Changes made to the object's state via the copied reference inside the method will be visible outside the method, because both references point to the same object. However, if you reassign the parameter to a new object inside the method, the original variable outside the method will still point to its original object.
flowchart TD A[Call Method with Argument] --> B{Is Argument Primitive?} B -->|Yes| C[Copy Primitive Value] C --> D[Method operates on copied value] D --> E{Changes affect only copy} B -->|No| F[Copy Object Reference] F --> G[Method operates on object via copied reference] G --> H{Changes to object state visible outside} G --> I{Reassigning reference creates new link} I --> J{Original reference unaffected by reassignment}
Flowchart illustrating Java's pass-by-value mechanism for primitives and objects.
Primitives: Value is the Content
When you pass a primitive type to a method, the method receives a completely independent copy of that primitive's value. Any modifications to this copied value within the method will not reflect back to the original variable in the calling scope.
public class PrimitiveExample {
public static void modifyPrimitive(int number) {
System.out.println("Inside method (before change): " + number); // Prints 10
number = 20; // Modifies the *copy* of 'number'
System.out.println("Inside method (after change): " + number); // Prints 20
}
public static void main(String[] args) {
int originalNumber = 10;
System.out.println("Outside method (before call): " + originalNumber); // Prints 10
modifyPrimitive(originalNumber);
System.out.println("Outside method (after call): " + originalNumber); // Prints 10 (unchanged)
}
}
Demonstrates pass-by-value for primitive types.
Objects: Value is the Reference
For objects, the 'value' being passed is the memory address (reference) where the object resides. When this reference is copied and passed to a method, both the original variable and the method's parameter now hold references to the same object in memory. This means that if the method uses its copied reference to change the state of the object (e.g., call a setter method, modify a public field), those changes will be visible to the original variable because they are both looking at the same object.
However, if the method reassigns its parameter to point to a different object, this change only affects the method's local copy of the reference. The original variable outside the method will still point to the object it was initially referencing.
class MyObject {
String name;
public MyObject(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class ObjectReferenceExample {
public static void modifyObject(MyObject obj) {
System.out.println("Inside method (before change): " + obj.getName()); // Prints "Original"
obj.setName("Modified"); // Changes the state of the *original* object
System.out.println("Inside method (after change): " + obj.getName()); // Prints "Modified"
}
public static void reassignObject(MyObject obj) {
System.out.println("Inside reassign method (before reassign): " + obj.getName()); // Prints "Original"
obj = new MyObject("New Object"); // 'obj' now points to a *new* object locally
System.out.println("Inside reassign method (after reassign): " + obj.getName()); // Prints "New Object"
}
public static void main(String[] args) {
MyObject originalObject = new MyObject("Original");
System.out.println("Outside method (before modify call): " + originalObject.getName()); // Prints "Original"
modifyObject(originalObject);
System.out.println("Outside method (after modify call): " + originalObject.getName()); // Prints "Modified" (state changed)
System.out.println("\nOutside method (before reassign call): " + originalObject.getName()); // Prints "Modified"
reassignObject(originalObject);
System.out.println("Outside method (after reassign call): " + originalObject.getName()); // Prints "Modified" (original object unchanged)
}
}
Illustrates how object references are passed by value, affecting object state but not original reference reassignment.