01 / 20

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.

Caller name = "Ada" Method param = ??? pass argument What exactly gets copied over?

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

STACK (LOCAL VARS) HEAP (OBJECTS) x (int) 42 y (double) 3.14 d (Dog) @0x3A Dog Object @0x3A name = "Rex" arr (int[]) @0x7F {1, 2, 3} @0x7F

The Golden Rule

PRIMITIVE int x = 5; Copies: 5 (the number) Method gets its own copy OBJECT Dog d = new Dog(); Copies: @0x3A (the ref) Both point to same object

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
BEFORE CALL x 10 n 10 copy AFTER n = n * 2 x 10 n 20 x is still 10: the original was never touched. n was a separate copy that lived only inside doubleIt.

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"!
STACK myDog @0x3A d @0x3A copy HEAP Dog @0x3A name = "Rex" → "Buddy" Both myDog and d point to the same object. Mutating the object through d is visible through myDog. The reference was copied, not the object itself.

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"
STACK myDog @0x3A d @0xBB HEAP Dog @0x3A name = "Rex" Dog @0xBB name = "Max" d = new Dog("Max") made d point elsewhere. myDog still points to the original object with name "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?

Question 2 of 5

After calling change(data), what is data[0]?

Question 3 of 5

After calling trySwap(x, y), what are x and y?

Question 4 of 5

Which statement best describes why reassigning a parameter inside a method does not affect the caller?

Question 5 of 5

What does the println output?