Arrays and methods
25 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)A method can take an array as a parameter and can return an array. The method works on the same array as the caller, not on a copy, so the changes it makes are still there after the call.
Passing an array to a method
Term 1 has 5 marks and term 2 has 3, and both need a total. Write the array type in the parameter, int[] marks, and call the method with the array's name alone: total(term1).
Example 1 · One method for any number of marks
public class TotalMarks { static int total(int[] marks) { int sum = 0; for (int m : marks) { sum += m; } return sum; }
public static void main(String[] args) { int[] term1 = {78, 85, 62, 90, 71}; int[] term2 = {80, 88, 70}; System.out.println("Term 1 total: " + total(term1)); System.out.println("Term 2 total: " + total(term2)); }}Term 1 total: 386 Term 2 total: 238
What this code does
int[] marksin the header means the method takes an int array; the call passesterm1, thenterm2.- The for-each loop works for any size, so one method serves both arrays. A fixed size like
i < 5would break onterm2.
Returning an array
A tutor app needs the first 5 rows of a times table as numbers it can reuse, not just printed. Make int[] the return type (opens in a new tab): build the array with new inside, fill it and return it.
Example 2 · A method that returns an array
public class TableArray { static int[] table(int n) { int[] result = new int[5]; for (int i = 0; i < result.length; i++) { result[i] = n * (i + 1); } return result; }
public static void main(String[] args) { int[] t = table(6); for (int x : t) { System.out.println(x); } }}6 12 18 24 30
What this code does
- The return type
int[]says the method gives back an int array;return result;hands over the array it built. mainstores the returned array intand can use it like any array it made itself.
The method changes the caller's array
A method adds 3 grace marks, returns nothing, and yet main sees the new marks. An array variable holds a reference: the address of the boxes in memory, like a house address written on a slip of paper.
Example 3 · The method changes the caller's array
public class AddGrace { static void addGrace(int[] marks, int extra) { for (int i = 0; i < marks.length; i++) { marks[i] += extra; } }
public static void main(String[] args) { int[] marks = {32, 40, 33}; System.out.println("Before: " + marks[0]); addGrace(marks, 3); System.out.println("After: " + marks[0]); }}Before: 32 After: 35
What this code does
addGracereceives the address of the same array thatmainhas.- So
marks[i] += extrawrites intomain's boxes, and the change is still there after the call, although the method returns nothing.
Java still passes a copy, as in Pass-by-value in Java (opens in a new tab). For an int it copies the number; for an array it copies the address, and both addresses lead to the same boxes.
Sharing is not copying
You want a backup of the marks before changing them, so you write int[] same = marks;. That copies only the address, so both names share one array. A real copy is a new array, filled element by element.
Example 4 · Two names, one array, and a real copy
public class CopyArray { public static void main(String[] args) { int[] marks = {10, 20, 30}; int[] same = marks; same[0] = 99; System.out.println("marks[0] = " + marks[0]); // 99, not 10 int[] copy = new int[marks.length]; for (int i = 0; i < marks.length; i++) { copy[i] = marks[i]; } copy[1] = 55; System.out.println("marks[1] = " + marks[1]); System.out.println("copy[1] = " + copy[1]); }}marks[0] = 99 marks[1] = 20 copy[1] = 55
What this code does
same = markscopies only the address, so changingsame[0]changesmarks[0]too.copyis a new array of the same length, filled in a loop, so changingcopy[1]leavesmarksalone.
Arrays.copyOf.Next step
Check your understanding
10 quick questions