Skip to content
Theory · Methods · lesson 3 of 8

Parameters are copies

A parameter is a variable of the method, and the method may change it. But what then happens to the variable passed in the call? Does it change too?

Back to the kitchen. Our appliance has one quirk: it never takes the food from your table. Before it starts, it puts an exact copy into the jar, and your food stays where it was. Whatever happens in the jar, everything on the table stays as it was.

Let's see what this looks like in code. The method addBonus adds 10 to its parameter, and the variable points in main stays the same:

JAVA
public class Main {
    static void addBonus(int score) {
        score = score + 10;
        System.out.println(score); // 60
    }

    public static void main(String[] args) {
        int points = 50;
        addBonus(points);
        System.out.println(points); // 50
    }
}

Let's go through the example line by line:

  1. int points = 50; sets up the variable points in main.
  2. The call addBonus(points) copies the value 50 into the parameter score.
  3. score = score + 10; changes the copy: score now holds 60.
  4. addBonus prints 60 and finishes, and score disappears with it.
  5. main prints points, which still holds 50.

Here the kitchen picture is off. You cannot copy real food, but Java copies the argument's value on every call. For a number this is fast, and you never notice it.

Pass-by-value

This way of passing is called pass-by-value: the method gets a copy of the argument's value, not the variable itself.

You have already met this copying in the theme on variables: after int b = a; the variable b lives on its own. The call addBonus(points) does the same as the line int score = points;, except that score is the method's parameter.

A parameter is an ordinary local variable of the method:

  • you can read it and change it with = and ++;
  • it appears on every call and disappears when the method finishes;
  • it has no link at all to the caller's variable.

There are two cases where a link only seems to exist.

The same name

Name the parameter points, like the variable in main, and there are still two variables. Each is visible only in its own method, and writing to one does not touch the other.

An expression as the argument

An argument can be an expression rather than a variable. For the call addBonus(points + 5) Java first computes 55 and copies the finished number into the parameter. The method does not know where that number came from.

Returning a new value

A method is linked to the calling code in two ways: arguments going in and the result coming out. For main to get a new number, the method returns it with return, and main itself puts the result into the variable:

JAVA
public class Main {
    static int withBonus(int score) {
        return score + 10;
    }

    public static void main(String[] args) {
        int points = 50;
        points = withBonus(points);
        System.out.println(points); // 60
    }
}

Let's go through main line by line:

  1. withBonus(points) gets a copy of the number 50 and returns 60.
  2. points = … puts that result into points.
  3. println prints 60: the variable changed, but main changed it itself, by assignment.

The appliance handed over the finished dish, and you put it in place of the old one yourself.

An interview question

Interviewers often ask this question: "Does Java pass arguments by value or by reference?" The answer has two parts:

  • always by value: the method gets a copy, not the variable itself;
  • so assigning to a parameter never changes the caller's variable.

The rule holds for objects too, the things a program creates from a class's description, only for them it is not the object itself that gets copied.

Common mistake

A method was written to swap two variables:

JAVA
public class Main {
    static void swap(int a, int b) {
        int t = a;
        a = b;
        b = t;
    }

    public static void main(String[] args) {
        int x = 1;
        int y = 2;
        swap(x, y); // ✗
        System.out.println(x + " " + y); // 1 2
    }
}

The program prints 1 2: the variables were not swapped. The method swap swapped its own copies a and b, and never reached x and y in main. In Java a method cannot change the caller's variables. Swap the values where the variables themselves live: right in main, through a third variable. Why such a swap does not work is another interview favorite.

Key points

  • A call copies the argument's value into the parameter: this is pass-by-value.
  • Changing a parameter changes only the copy, and the caller's variable stays the same.
  • Java always passes arguments by value, so swap(x, y) does not change x and y.
  • To get a new value, the method returns it and the caller assigns it: points = withBonus(points);.