{"id":"methods-copies","slug":"parameters-are-copies","title":"Parameters are copies","text":"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:\n\n```java\npublic class Main {\n    public static void main(String[] args) {\n        int score = 10;\n        doubleIt(score);\n        System.out.println(\"In main: \" + score);\n    }\n\n    public static void doubleIt(int score) {\n        score = score * 2;\n        System.out.println(\"In the method: \" + score);\n    }\n}\n```\n\n```output\nIn the method: 20\nIn main: 10\n```\n\nBoth 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.\n\nTo 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.)\n","task":"What does this program print? Trace it line by line and type each line.\n","kind":"predict","seed":"public class Main {\n    public static void main(String[] args) {\n        int number = 5;\n        addTen(number);\n        System.out.println(number);\n    }\n\n    public static void addTen(int number) {\n        number = number + 10;\n        System.out.println(\"Inside: \" + number);\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        int number = 5;\n        addTen(number);\n        System.out.println(number);\n    }\n\n    public static void addTen(int number) {\n        number = number + 10;\n        System.out.println(\"Inside: \" + number);\n    }\n}\n","hints":["The method prints first, with its own copy changed to 15.","`main`'s `number` was never changed: it's still 5."],"tests":[{"name":"Output","stdin":"","expect":"Inside: 15\n5","hidden":false}],"require":[],"forbid":[],"lines":["Inside: 15","5"],"more":[{"task":"What does this program print? Watch which value goes to which parameter.\n","kind":"predict","seed":"public class Main {\n    public static void main(String[] args) {\n        int x = 1;\n        int y = 2;\n        show(y, x);\n    }\n\n    public static void show(int x, int y) {\n        System.out.println(\"x = \" + x);\n        System.out.println(\"y = \" + y);\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        int x = 1;\n        int y = 2;\n        show(y, x);\n    }\n\n    public static void show(int x, int y) {\n        System.out.println(\"x = \" + x);\n        System.out.println(\"y = \" + y);\n    }\n}\n","hints":["The first argument goes to the first parameter, whatever the names are.","The call is `show(y, x)`, so the method's `x` gets 2 and its `y` gets 1."],"tests":[{"name":"Output","stdin":"","expect":"x = 2\ny = 1","hidden":false}],"require":[],"forbid":[],"lines":["x = 2","y = 1"]},{"task":"What does this program print? The method changes its parameter in a loop.\n","kind":"predict","seed":"public class Main {\n    public static void main(String[] args) {\n        int n = 0;\n        countUp(n);\n        System.out.println(\"n is still \" + n);\n    }\n\n    public static void countUp(int n) {\n        while (n < 3) {\n            n++;\n            System.out.println(n);\n        }\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        int n = 0;\n        countUp(n);\n        System.out.println(\"n is still \" + n);\n    }\n\n    public static void countUp(int n) {\n        while (n < 3) {\n            n++;\n            System.out.println(n);\n        }\n    }\n}\n","hints":["Inside the method, `n` starts at 0 and goes up by one before each print.","The loop prints 1, 2 and 3. Then `main` prints its own `n`, which is still 0."],"tests":[{"name":"Output","stdin":"","expect":"1\n2\n3\nn is still 0","hidden":false}],"require":[],"forbid":[],"lines":["1","2","3","n is still 0"]}]}