{"id":"calculating-tracing","slug":"tracing-values","title":"Tracing values","text":"`=` copies a value into a variable, once. After `a = b;`, the variable `a` holds the same number as `b`, but the two aren't linked: changing `b` later doesn't change `a`.\n\n```java\nint b = 5;\nint a = b;\nb = 8;\nSystem.out.println(a);\nSystem.out.println(b);\n```\n\n```output\n5\n8\n```\n\nWhen a program gets confusing, trace it by hand: go through it line by line and write down what each variable holds after each line.\n\n| After line | a | b |\n|---|---|---|\n| `int b = 5;` | | 5 |\n| `int a = b;` | 5 | 5 |\n| `b = 8;` | 5 | 8 |\n\nTracing is how programmers check their own thinking, and it's a skill worth practicing on paper, without running anything.\n","task":"What does this program print? Trace it and type each line.\n","kind":"predict","seed":"public class Main {\n    public static void main(String[] args) {\n        int first = 10;\n        int second = 3;\n        first = second;\n        System.out.println(first);\n        System.out.println(second);\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        int first = 10;\n        int second = 3;\n        first = second;\n        System.out.println(first);\n        System.out.println(second);\n    }\n}\n","hints":["`first = second;` copies the value of `second`, which is 3, into `first`.","`second` itself doesn't change."],"tests":[{"name":"Output","stdin":"","expect":"3\n3","hidden":false}],"require":[],"forbid":[],"lines":["3","3"],"more":[{"task":"And this one? Watch the change to `b` after the copy.\n","kind":"predict","seed":"public class Main {\n    public static void main(String[] args) {\n        int a = 4;\n        int b = a * 2;\n        a = b;\n        b = b + 1;\n        System.out.println(a);\n        System.out.println(b);\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        int a = 4;\n        int b = a * 2;\n        a = b;\n        b = b + 1;\n        System.out.println(a);\n        System.out.println(b);\n    }\n}\n","hints":["`b` starts as `4 * 2`, which is 8. Then `a` gets a copy of 8.","`b = b + 1` changes only `b`: it becomes 9, while `a` stays 8."],"tests":[{"name":"Output","stdin":"","expect":"8\n9","hidden":false}],"require":[],"forbid":[],"lines":["8","9"]},{"task":"Three values this time. Trace carefully: the order of the lines matters.\n","kind":"predict","seed":"public class Main {\n    public static void main(String[] args) {\n        int x = 2;\n        int y = x + 3;\n        int z = y * x;\n        x = z - y;\n        y = y + x;\n        System.out.println(x);\n        System.out.println(y);\n        System.out.println(z);\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        int x = 2;\n        int y = x + 3;\n        int z = y * x;\n        x = z - y;\n        y = y + x;\n        System.out.println(x);\n        System.out.println(y);\n        System.out.println(z);\n    }\n}\n","hints":["After the first three lines: x is 2, y is 5 and z is 10.","Then `x = z - y` makes x 5, and `y = y + x` uses that new x: 5 + 5."],"tests":[{"name":"Output","stdin":"","expect":"5\n10\n10","hidden":false}],"require":[],"forbid":[],"lines":["5","10","10"]}]}