Pass-by-Value &
Object References
Understand how Java passes primitives and object references to methods: the single rule that governs every method call.
Java
AP Computer Science A
What Is "Passing" an Argument?
When you call a method like greet(name), the value of name is handed to the method's parameter. But how that handoff works determines whether the method can change your original data.
Two Kinds of Variables
Primitive Variables
Store the actual data directly:
int, double, boolean, char, etc.Reference Variables
Store a memory address (reference) that points to an object on the heap:
String, arrays, any class.The Memory Model
The Golden Rule
Primitives: Copy the Data
static void doubleIt(int n) {
n = n * 2; // changes local copy only
}
int x = 10;
doubleIt(x);
System.out.println(x); // prints 10, NOT 20
Objects: Copy the Reference
static void rename(Dog d) {
d.setName("Buddy"); // modifies the OBJECT
}
Dog myDog = new Dog("Rex");
rename(myDog);
System.out.println(myDog.getName()); // "Buddy"!
Reassignment Doesn't Reach Back
static void replace(Dog d) {
d = new Dog("Max"); // reassigns local copy of ref
}
Dog myDog = new Dog("Rex");
replace(myDog);
System.out.println(myDog.getName()); // still "Rex"
Mutation vs Reassignment
Mutation
Using the reference to change the object's fields. Both the caller and method see the change because they share the same object.
Reassignment
Pointing the parameter to a different object. Only the local copy of the reference changes: the caller's variable is unaffected.
How to Trace a Method Call
1
Identify the argument type. Is it a primitive (
int, double, etc.) or a reference (object/array)?2
Draw the copy. The method parameter gets a copy of the value: the raw number for primitives, the address for references.
3
Trace the method body. Does the method mutate the object's fields (visible to caller) or reassign the parameter (invisible to caller)?
4
Check the caller's variable. After the method returns, what does the original variable still hold?
Example 1: Swapping Primitives
static void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
}
int x = 3, y = 7;
swap(x, y);
System.out.println(x + " " + y); // 3 7 (unchanged!)
Example 2: Array Mutation
static void doubleFirst(int[] arr) {
arr[0] = arr[0] * 2; // mutates the array object
}
int[] nums = {5, 10, 15};
doubleFirst(nums);
System.out.println(nums[0]); // 10 (changed!)
Example 3: The String Gotcha
static void shout(String s) {
s = s.toUpperCase(); // creates a NEW String, reassigns s
}
String msg = "hello";
shout(msg);
System.out.println(msg); // "hello" (not "HELLO"!)
Common Mistakes
✕ "Java is pass-by-reference for objects"
Java copies the reference by value. Reassigning the parameter does not affect the caller. True pass-by-reference would.
✕ Expecting a swap method to work
Since Java copies values, you cannot swap two primitives (or two references) via a method. There is no way to reach back and change the caller's variable.
✕ Confusing mutation with reassignment
Calling
obj.setX() mutates the shared object (visible). Writing obj = new Obj() is reassignment (invisible to caller).Summary Table
| Primitive Arg | Object/Array Arg | |
|---|---|---|
| What is copied? | The actual data (e.g., 42) | The reference (e.g., @0x3A) |
| Modify inside method? | No effect on caller | Mutation visible to caller |
| Reassign parameter? | No effect on caller | No effect on caller |
| Can swap via method? | No | No (references are copied) |
| Immutable types (String)? | N/A | All "changes" create new objects |
Question 1 of 5
What does Java copy when you pass an object variable to a method?
Java copies the value of the reference (the address). The method receives its own copy of the address, which points to the same object on the heap.
Question 2 of 5
Code:
static void change(int[] arr) {
arr[0] = 99;
arr = new int[]{1, 2, 3};
}
int[] data = {10, 20, 30};
change(data);
// What is data[0]?
▼ Tap to read code
After calling change(data), what is data[0]?
arr[0] = 99 mutates the shared array object, so the caller sees 99. The next line arr = new int[]{1,2,3} is a reassignment of the local parameter only: it does not change the caller's data variable.
Question 3 of 5
Code:
static void trySwap(String a, String b) {
String temp = a;
a = b;
b = temp;
}
String x = "hi";
String y = "bye";
trySwap(x, y);
// What are x and y?
▼ Tap to read code
After calling trySwap(x, y), what are x and y?
The method only swaps its local copies of the references. The caller's variables x and y are completely unaffected. You cannot swap variables in Java via a method call.
Question 4 of 5
Which statement best describes why reassigning a parameter inside a method does not affect the caller?
Java is pass-by-value. The method parameter is a new variable holding a copy of the caller's value. Reassigning that copy makes it point somewhere else, but the caller's original variable still holds its original value.
Question 5 of 5
Code:
static void mystery(int x, int[] arr) {
x = x + 10;
arr[0] = arr[0] + 10;
}
int num = 5;
int[] vals = {5, 15, 25};
mystery(num, vals);
System.out.println(num + " " + vals[0]);
▼ Tap to read code
What does the println output?
num is a primitive: the method changes only its local copy, so num stays 5. vals is an array (object): arr[0] + 10 mutates the shared array, so vals[0] becomes 15. Output: 5 15.