Java Arena
Step 5 of 5

Parameters are copies

When a method is called, each parameter gets a copy of the argument's value. The method can change its parameter, but that changes only its own copy:

public class Main {
    public static void main(String[] args) {
        int score = 10;
        doubleIt(score);
        System.out.println("In main: " + score);
    }

    public static void doubleIt(int score) {
        score = score * 2;
        System.out.println("In the method: " + score);
    }
}
Output
In the method: 20
In main: 10

Both variables are called score, but they're two different variables: each method has its own. The name used in the call doesn't matter either. Only the value is passed on, so doubleIt(score), doubleIt(10) and doubleIt(5 + 5) all do the same.

To get a value back out of a method, the method has to return it. That's the next module. (For objects, such as lists, there's more to the story; the module on primitive and reference variables tells it.)

Your task

Challenge 1 of 3

What does this program print? Trace it line by line and type each line.

Challenge 2

What does this program print? Watch which value goes to which parameter.

Challenge 3

What does this program print? The method changes its parameter in a loop.