Passing Arrays to Methods in Java
Fast recall
Quick revision
Core answer
Whether you understand Java's pass-by-value semantics and why arrays behave like they are passed by reference even though they technically are not.
Saying Java passes arrays by reference. Java is ALWAYS pass-by-value. The confusion is that the value happens to be a reference value, so the method can reach the same heap object through it.
Distinguish the two scenarios clearly: modifying arr[0] inside a method DOES affect the caller's array (same heap object), but doing arr = new int[]{...} inside a method does NOT affect the caller (you replaced your local copy of the reference, not the original).
Interview-ready guide
Interview answer
Java always passes arguments by value. With an array, Java copies the array reference into the parameter, so both variables initially refer to the same array. That is why changing arr[0] inside the method is visible to the caller.
If the method assigns arr = new int[]{1, 2}, only its local parameter changes; the caller still has its original reference. That final test is why this is pass-by-value rather than pass-by-reference.
Passing Arrays to Methods in Java
Direct Answer
Java is always pass-by-value. For an array parameter, the copied value is an array reference, so the caller and method initially refer to the same array. The method can change that array's elements. Reassigning the parameter changes only the method's local copy of the reference and does not redirect the caller's variable.
Code Example
These two methods look almost identical but behave completely differently, and running them is the fastest way to internalise the rule. Try it as a Java 17 console app and watch the two print statements.
public class ArrayPassDemo {
// Modifies elements — the change IS visible to the caller
static void doubleAll(int[] arr) {
for (int i = 0; i < arr.length; i++) {
arr[i] = arr[i] * 2; // reaches through the copied reference to the shared array
}
}
// Reassigns the parameter — the caller's variable is NOT affected
static void tryReplace(int[] arr) {
arr = new int[]{1, 2, 3}; // only changes the local copy of the reference
// the caller's original array is completely untouched
}
public static void main(String[] args) {
int[] scores = {90, 85, 78};
doubleAll(scores);
System.out.println(scores[0]); // 180 — change visible, same heap object
tryReplace(scores);
System.out.println(scores[0]); // 180 — unchanged, reassignment did not escape
}
}
The contrast is the whole answer: doubleAll reaches through the shared array reference and the change sticks, while tryReplace only rebinds its local copy of the address, so the caller's scores never moves.
Common Mistakes & Pitfalls
- Calling it pass-by-reference — the word interviewers wait for is "value". Java copies the reference by value; it never passes the variable itself.
- Expecting reassignment to escape —
arr = new int[]{...}inside a method looks like it should replace the caller's array, but it only rebinds the local parameter; the caller is untouched. - Forgetting that shared state cuts both ways — a helper that sorts or zeroes the array in place silently mutates the caller's data. If the caller still needs the original, pass
arr.clone()orArrays.copyOf(arr, arr.length). - Assuming a method can "return" through the parameter — to give the caller a different array you must
returnit and reassign at the call site; writingarr = ...inside the method accomplishes nothing visible outside.
Check your understanding
Answer these without looking back at the article.
- Is Java pass-by-value or pass-by-reference for primitive types?
- What happens when you pass a String to a method in Java?
- How would you write a method that returns a new array instead of modifying the original?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.