Lesson · Methods
Parameter passing: everything is a copy
11 min readParameter Passing
Java is always pass-by-value. The value copied for an object parameter is the reference itself, which is why methods can change your object but never repoint your variable.
One rule, two consequences
Every argument a method receives is a copy. For a primitive, the copy is the number: the callee's reassignment is invisible outside. For an object, the copy is the reference: callee and caller now hold two references to one object. Mutations through either reference are visible to both; reassigning the callee's reference changes only the copy.
That single sentence answers every parameter-passing question. A swap method cannot work on primitives, because it swaps copies. A method can trim a StringBuilder it was handed, because both sides point at the same builder. And 'Java is pass-by-reference for objects' is the folklore that the swap test falsifies in twenty seconds.
Pass by value · three scenarios
Caller locals
Callee frame
no active call
Heap
nothing allocated
The caller holds two ints, x = 1 and y = 2, and calls swap(x, y). So far this is ordinary code; the interesting part is what the call does with the values.
Why swap cannot work, and what it teaches
void swap(int a, int b) swaps its own parameters and returns; the caller's variables are untouched. This is not a limitation to work around, it is a boundary the language draws, and the idiomatic response is to return something instead: int[] swapped = { b, a };, a record with two fields, or an in-place operation on a mutable structure you already hold.
The same test applied to objects: void clear(Person p) { p = null; } repoints the copy, and the caller's reference survives untouched. void rename(Person p) { p.name = ...; } mutates the shared object and the caller sees it. Same method shape, opposite outcomes, both consistent with by-value.
Mutation through parameters: power and discipline
The ability to mutate a caller's object is convenient and dangerous in equal measure. A method that fills a collection passed by the caller reads fine (Collections.sort(list)). A method that quietly mutates a parameter it does not own reads as a surprise, and the calling code has no clue from the signature. The defensive habit is either documenting the side effect by name (sortInPlace) or returning a new value and leaving the argument alone.
The defensive-copy rules from the encapsulation topic are the same rule seen from the other side: copying on input protects you from exactly the mutation-through-parameter behaviour just described.
Answering the classic question well
The interview version of this topic asks: 'Is Java pass-by-reference?' The complete answer names the mechanism and both consequences: values are always copied; for objects the value is the reference, so the callee shares the object but has its own variable. Being able to demonstrate with swap and rename on a whiteboard turns a vocabulary question into an understanding question.
A method cannot reassign your variable, ever
No method can make your reference point somewhere else, and no method can change your int. What it can do is change the object your reference points at, because both references address the same heap object. Copy the reference, share the object, never share the variable.
Interview angles
Is Java pass-by-value or pass-by-reference?+
Always pass-by-value. Primitives copy the value; objects copy the reference. The callee can mutate the shared object but cannot repoint the caller's variable. Swap on primitives proves it, rename on an object's field confirms the nuance.
Why does a swap method fail for ints?+
It swaps its own copies. The caller's variables were never part of the method's world. Return the values in a record or array, or operate on a mutable holder, when a swap is genuinely needed.
Can a method set a caller's object reference to null?+
No. Assigning null to the parameter changes only the callee's copy of the reference. The caller's variable, and the object both referenced, are unaffected.