Arrays as Parameters and Return Values (Bonus)
Arrays as Parameters and Return Values
What You'll Learn
- 4.6.A: Pass an array as a method parameter.
- 4.6.B: Return an array from a method.
- Explain why passing an array passes a reference, not a copy.
- Predict whether changes inside a method affect the original array.
- Write methods that take arrays as input and return new arrays as output.
Key Vocabulary
| Term | Definition |
|---|---|
| pass by value | How Java passes all arguments — a copy of the value is given to the method. For primitives, this means changes inside the method don't affect the original variable. |
| reference | A variable that stores the memory address of an object or array, not the object itself. When you pass an array, you pass a copy of its reference — both variables point to the same array in memory. |
| alias | Two variables that refer to the same object in memory. Modifying the object through one alias is visible through the other. |
Passing an Array as a Parameter (4.6.A)
When you pass a primitive like int to a method, Java copies the value. Changes inside the method have no effect on the original variable.
public static void addFive(int x) {
x = x + 5; // only changes the local copy
}
int num = 10;
addFive(num);
System.out.println(num); // still prints 10
Arrays work differently. An array variable stores a reference — a memory address pointing to the array's data. When you pass an array, Java copies the reference, not the array itself. Both the original variable and the parameter now point to the same array in memory.
public static void doubleAll(int[] arr) {
for (int i = 0; i < arr.length; i++) {
arr[i] = arr[i] * 2;
}
}
int[] scores = {10, 20, 30};
doubleAll(scores);
System.out.println(scores[0]); // prints 20 — the original array was modified
📌 The Key Rule
Modifying elements of an array parameter (arr[i] = ...) changes the original array. Reassigning the parameter variable itself (arr = new int[5]) does not — that only changes what the local variable points to, leaving the original untouched.
Visualizing the Reference
| scores | → | [10, 20, 30] memory address 0x4A2 |
← | arr (parameter) |
Both scores and arr point to the same memory location. Any change to arr[i] inside the method is a change to scores[i] outside it.
Reassigning the Parameter Has No Effect
public static void tryToReplace(int[] arr) {
arr = new int[]{99, 99, 99}; // only changes local variable
}
int[] data = {1, 2, 3};
tryToReplace(data);
System.out.println(data[0]); // still prints 1
Inside tryToReplace, arr is redirected to a brand new array. But data back in the calling code still points to the original {1, 2, 3} array — the reassignment only affected the local copy of the reference.
⚠️ AP Exam Trap: Element Change vs. Reassignment
This is one of the most tested concepts involving arrays on the AP exam. The rule in two sentences: changing elements (arr[i] = x) affects the caller's array. Reassigning the parameter (arr = new int[3]) does not. Every AP question on this topic is testing whether you know the difference.
Returning an Array from a Method (4.6.B)
A method can return an array just like it returns any other value. Declare the return type with brackets: int[], String[], etc.
public static int[] makeDoubled(int[] arr) {
int[] result = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
result[i] = arr[i] * 2;
}
return result;
}
Usage:
int[] original = {5, 10, 15};
int[] doubled = makeDoubled(original);
System.out.println(doubled[1]); // prints 20
System.out.println(original[1]); // still prints 10 — original unchanged
This pattern — create a new array inside the method, fill it, return it — is the standard way to produce a transformed version of an array without modifying the original.
✅ Example: Returning a Filtered Array
// Returns a new array containing only the positive values
public static int[] positives(int[] arr) {
int count = 0;
for (int x : arr) {
if (x > 0) count++;
}
int[] result = new int[count];
int idx = 0;
for (int x : arr) {
if (x > 0) {
result[idx] = x;
idx++;
}
}
return result;
}
Two passes: first count how many qualify (to size the result array), then fill it. This two-pass filter pattern appears on AP FRQs.
Writing Complete Array Methods
Combining parameters and return values gives you full method design flexibility:
// Takes an array, returns a new array with each element replaced
// by the sum of itself and the next element.
// Last element wraps around to add arr[0].
public static int[] rollingSum(int[] arr) {
int[] result = new int[arr.length];
for (int i = 0; i < arr.length - 1; i++) {
result[i] = arr[i] + arr[i + 1];
}
result[arr.length - 1] = arr[arr.length - 1] + arr[0];
return result;
}
Summary
- Java always passes by value. For arrays, the value passed is a reference (memory address).
- Modifying array elements inside a method (
arr[i] = x) does change the original array. - Reassigning the parameter variable (
arr = new int[3]) does not change the original array. - Methods can return arrays by declaring a bracket return type:
public static int[] method(...). - The standard pattern for a non-destructive transform: create a new array inside the method, fill it, return it.
Practice Questions
public static void addTen(int[] arr) {
for (int i = 0; i < arr.length; i++) {
arr[i] += 10;
}
}
int[] nums = {1, 2, 3};
addTen(nums);
System.out.println(nums[0]);
arr[i] modifies nums[i] — they point to the same memory. nums[0] was 1, then += 10 makes it 11.public static void replace(int[] arr) {
arr = new int[]{99, 99, 99};
}
int[] vals = {1, 2, 3};
replace(vals);
System.out.println(vals[0]);
arr = new int[]{99, 99, 99} reassigns the local parameter variable — it now points to a new array. But vals in the calling code still points to the original {1, 2, 3}. The original is never touched. prints 1.int
void
int[]
int[][]
int[]. The brackets are part of the type declaration. int would return a single integer. void returns nothing. int[][] is a 2D array (Lesson 4.11).public static int[] doubled(int[] arr) {
int[] result = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
result[i] = arr[i] * 2;
}
return result;
}
int[] original = {3, 6, 9};
int[] copy = doubled(original);
System.out.println(original[1] + " " + copy[1]);
result — it never modifies original. original[1] stays 6. copy[1] is 6 * 2 = 12. Output: 6 12.Mastery: Arrays as Parameters and Return Values
public static void mystery(int[] arr) {
arr[0] = arr[0] + arr[arr.length - 1];
arr = new int[]{0, 0, 0};
}
int[] data = {4, 8, 12};
mystery(data);
System.out.println(data[0] + " " + data[1]);
arr[0] = 4 + 12 = 16 — this modifies the original array because it's an element change. Then arr = new int[]{0, 0, 0} — this reassigns the local parameter, so it has no effect on data. Final state of data: {16, 8, 12}. Output: 16 8.public static int[] reverse(int[] arr) {
int[] result = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
result[i] = arr[arr.length - 1 - i];
}
return result;
}
public static int[] reverse(int[] arr) {
for (int i = 0; i < arr.length; i++) {
arr[i] = arr[arr.length - 1 - i];
}
return arr;
}
public static void reverse(int[] arr) {
int[] result = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
result[i] = arr[arr.length - 1 - i];
}
}
public static int[] reverse(int[] arr) {
int[] result = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
result[i] = arr[i];
}
return result;
}
arr[0] is overwritten, the original value is lost for filling result[arr.length-1]. C returns void — the result is lost. D copies in the same order — not reversed.public static int[] shiftLeft(int[] arr) {
int[] result = new int[arr.length];
for (int i = 0; i < arr.length - 1; i++) {
result[i] = arr[i + 1];
}
result[arr.length - 1] = arr[0];
return result;
}
int[] a = {10, 20, 30, 40};
int[] b = shiftLeft(a);
System.out.println(b[0] + " " + b[3]);
20 10.public static void zeroOut(int[] arr) {
arr = new int[arr.length];
}
public static void zeroOut(int[] arr) {
for (int i = 0; i < arr.length; i++) {
arr[i] = 0;
}
}
public static int[] zeroOut(int[] arr) {
return new int[arr.length];
}
public static void zeroOut(int[] arr) {
for (int x : arr) {
x = 0;
}
}
public static int[] add(int[] a, int[] b) {
int[] result = new int[a.length];
for (int i = 0; i < a.length; i++) {
result[i] = a[i] + b[i];
}
return result;
}
int[] x = {1, 2, 3};
int[] y = {4, 5, 6};
int[] z = add(x, y);
System.out.println(z[1] + " " + x[1]);
add creates a new array result — neither x nor y is modified. z[1] = x[1] + y[1] = 2 + 5 = 7. x[1] is still 2. Output: 7 2.Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]