This note is designed to clarify one of the most confusing behaviours in Java for new programmers:
Why modifying a primitive parameter does nothing, but modifying an array does change the original.
Java always uses pass‑by‑value, but what gets copied depends on the type:
The two demonstrations below illustrate this difference.
intShow that changing an int parameter inside a method does not change the original variable.
public class PrimitiveDemo extends ConsoleProgram {
public void run() {
int number = 5;
System.out.println("Before increaseNum: " + number);
increaseNum(number, 3);
System.out.println("After increaseNum: " + number);
}
// Tries to add 'incr' to the parameter
public void increaseNum(int myNumber, int incr) {
myNumber = myNumber + incr; // modifies only the local copy
System.out.println("Inside increaseNum: " + myNumber);
}
}
Before increaseNum: 5
Inside increaseNum: 8
After increaseNum: 5
number is a primitive (int).5 into myNumber.myNumber does not change number in run().For primitives, methods receive a copy of the value.
Changing the copy does nothing to the original variable.
int[]Show that changing an element of an array inside a method does change the original array.
public class ArrayDemo extends ConsoleProgram {
public void run() {
int[] numbers = {5, 10, 15};
System.out.println("Before increaseFirst: " + numbers[0]);
increaseFirst(numbers, 3);
System.out.println("After increaseFirst: " + numbers[0]);
}
// Adds 'incr' to the first element of the array
public void increaseFirst(int[] myArray, int incr) {
myArray[0] = myArray[0] + incr; // modifies the shared array object
System.out.println("Inside increaseFirst: " + myArray[0]);
}
}
Before increaseFirst: 5
Inside increaseFirst: 8
After increaseFirst: 8
numbers is an array, which is not a primitive in Java.numbers and myArray point to the same array in memory.myArray[0] updates the underlying array, which numbers also sees.For arrays and objects, methods receive a copy of the reference.
Both variables point to the same array, so changes affect the original.
// PRIMITIVE
public void increaseNum(int myNumber, int incr) {
myNumber = myNumber + incr; // only local copy changes
}
// ARRAY
public void increaseFirst(int[] myArray, int incr) {
myArray[0] = myArray[0] + incr; // shared object changes
}
Final takeaway:
Understanding this behaviour is essential when designing helper methods that clean, transform, or update data stored in arrays.