Skip to lesson

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

TotalMarks.javaJava
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));    }}
Output
Term 1 total: 386
Term 2 total: 238

What this code does

  • int[] marks in the header means the method takes an int array; the call passes term1, then term2.
  • The for-each loop works for any size, so one method serves both arrays. A fixed size like i < 5 would break on term2.

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

TableArray.javaJava
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);        }    }}
Output
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.
  • main stores the returned array in t and 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.

main's variable marks and the parameter marks of addGrace both hold the address of the same array, 35 43 36. So when addGrace adds 3 to each box, main sees the new values.

Example 3 · The method changes the caller's array

AddGrace.javaJava
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]);    }}
Output
Before: 32
After: 35

What this code does

  • addGrace receives the address of the same array that main has.
  • So marks[i] += extra writes into main'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.

Left: after int[] same = marks; both names point to one array, so same[0] = 99 also changes marks[0]. Right: copy is a new array filled in a loop, so copy[1] = 55 changes only copy; marks keeps 20 at index 1.

Example 4 · Two names, one array, and a real copy

CopyArray.javaJava
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]);    }}
Output
marks[0] = 99
marks[1] = 20
copy[1] = 55

What this code does

  • same = marks copies only the address, so changing same[0] changes marks[0] too.
  • copy is a new array of the same length, filled in a loop, so changing copy[1] leaves marks alone.
The Arrays class (opens in a new tab) makes the same copy in one line with Arrays.copyOf.

Next step

Check your understanding

10 quick questions