{"id":"references-primitives","slug":"primitive-variables","title":"Primitive variables","text":"Every Java variable is one of two kinds, and the kind decides what `=` copies, what a method can change, and what `==` compares. This module is about the difference, starting with the simpler kind.\n\n### The primitive types\n\nJava has eight **primitive types**. Four of them you've used all along:\n\n| Type | Holds | Examples |\n|---|---|---|\n| `int` | whole numbers, from about -2.1 billion to 2.1 billion | `42`, `-7` |\n| `double` | numbers with a decimal point | `3.75`, `-0.5` |\n| `boolean` | true or false | `true`, `false` |\n| `char` | one character, in single quotes | `'A'`, `'?'` |\n\nYou'll rarely need the other four: `long` holds whole numbers too big for an `int`, `float` is a less precise decimal number, and `byte` and `short` hold whole numbers with small ranges. All eight names start with a small letter.\n\nEvery other type is a **reference type**: `String`, `Scanner`, `ArrayList`, arrays such as `int[]`, and every class you write. Those are the next step.\n\n### A primitive variable holds its value\n\nA primitive variable holds the value itself. Picture a box with a number in it. Assigning one variable to another copies the value into the other box, and after that the two boxes have nothing to do with each other:\n\n```java\nint planned = 12;\nint done = planned;\ndone = done - 5;\nplanned++;\nSystem.out.println(\"Planned: \" + planned);\nSystem.out.println(\"Done: \" + done);\n```\n\n```output\nPlanned: 13\nDone: 7\n```\n\n- `int done = planned;` copies the value 12 into `done`. It doesn't tie `done` to `planned` in any way.\n- Changing `done` doesn't touch `planned`, and changing `planned` doesn't touch `done`.\n\nThe same goes for a value worked out from other variables. `int total = a + b;` stores the sum as it is at that moment, and if `a` changes later, `total` keeps the old sum.\n\n### Swapping two values\n\nAssigning puts a new value in the box, and the old one is gone. That's why swapping the values of two variables takes three steps and a third variable:\n\n```java\nint left = 3;\nint right = 8;\nint temp = left;\nleft = right;\nright = temp;\nSystem.out.println(left + \" \" + right);\n```\n\n```output\n8 3\n```\n\nWith only `left = right;` and `right = left;`, the first line overwrites the 3, and both variables end up 8. `temp` keeps a copy of the old value until it's needed.\n","task":"What does this program print? Keep track of each variable's value on paper, line by line.\n","kind":"predict","seed":"public class Main {\n    public static void main(String[] args) {\n        int first = 7;\n        int second = first;\n        first = first * 2;\n        second++;\n        int sum = first + second;\n        first = 1;\n        char grade = 'B';\n        char copy = grade;\n        grade = 'A';\n        System.out.println(first);\n        System.out.println(second);\n        System.out.println(sum);\n        System.out.println(grade + \" \" + copy);\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        int first = 7;\n        int second = first;\n        first = first * 2;\n        second++;\n        int sum = first + second;\n        first = 1;\n        char grade = 'B';\n        char copy = grade;\n        grade = 'A';\n        System.out.println(first);\n        System.out.println(second);\n        System.out.println(sum);\n        System.out.println(grade + \" \" + copy);\n    }\n}\n","hints":["`int second = first;` copies 7 into `second`. Doubling `first` afterwards doesn't change `second`, and `second++` makes it 8.","`sum` is worked out once, from 14 and 8. Setting `first` to 1 later doesn't change it. The chars work the same way: `copy` keeps the `'B'` it was given."],"tests":[{"name":"Output","stdin":"","expect":"1\n8\n22\nA B","hidden":false}],"require":[],"forbid":[],"lines":["1","8","22","A B"],"more":[{"task":"In a game, the player starts with some lives. The program reads the starting number, then one event per line until `end`: `hit` takes a life and `heart` gives one. The first line and the two prints at the end are ready. Declare `lives` as a copy of `start`, and change only `lives` in the loop, so that `start` still holds the starting number at the end. For the input `3`, `hit`, `hit`, `heart`, `hit` and `end`, one per line, the program prints:\n\n```\nLives at the start: 3\nLives now: 1\n```\n","kind":"code","seed":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int start = Integer.valueOf(scanner.nextLine());\n        // Declare lives here, then read the events.\n\n        System.out.println(\"Lives at the start: \" + start);\n        System.out.println(\"Lives now: \" + lives);\n    }\n}\n","solution":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int start = Integer.valueOf(scanner.nextLine());\n        int lives = start;\n        while (true) {\n            String event = scanner.nextLine();\n            if (event.equals(\"end\")) {\n                break;\n            }\n            if (event.equals(\"hit\")) {\n                lives--;\n            } else if (event.equals(\"heart\")) {\n                lives++;\n            }\n        }\n        System.out.println(\"Lives at the start: \" + start);\n        System.out.println(\"Lives now: \" + lives);\n    }\n}\n","hints":["`int lives = start;` copies the starting number. From then on the two variables are separate, so changing `lives` leaves `start` as it was.","In a `while (true)` loop, read a line and `break` at `end`. For `hit` do `lives--;`, for `heart` do `lives++;`."],"tests":[{"name":"Test 1","stdin":"3\nhit\nhit\nheart\nhit\nend\n","expect":"Lives at the start: 3\nLives now: 1","hidden":false},{"name":"Test 2","stdin":"5\nend\n","expect":"Lives at the start: 5\nLives now: 5","hidden":true},{"name":"Test 3","stdin":"1\nheart\nheart\nhit\nheart\nend\n","expect":"Lives at the start: 1\nLives now: 3","hidden":true},{"name":"Test 4","stdin":"4\nhit\nhit\nhit\nend\n","expect":"Lives at the start: 4\nLives now: 1","hidden":true}],"require":[{"pattern":"\\bint\\s+lives\\s*=\\s*start\\s*;","message":"Start `lives` as a copy of the starting number: `int lives = start;`."}],"forbid":[]},{"task":"The program reads two whole numbers, the ends of a range, and prints every number from the smaller one up to the larger one. The user may type the ends in either order. The loop is ready, and it counts from `low` up to `high`: when `low` is the larger number, swap the values of `low` and `high` before the loop, with a third variable as in the example. For the input `6` and `3` the program prints:\n\n```\n3\n4\n5\n6\n```\n","kind":"code","seed":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int low = Integer.valueOf(scanner.nextLine());\n        int high = Integer.valueOf(scanner.nextLine());\n        // Swap the values here when low is larger than high.\n\n        for (int i = low; i <= high; i++) {\n            System.out.println(i);\n        }\n    }\n}\n","solution":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int low = Integer.valueOf(scanner.nextLine());\n        int high = Integer.valueOf(scanner.nextLine());\n        if (low > high) {\n            int temp = low;\n            low = high;\n            high = temp;\n        }\n        for (int i = low; i <= high; i++) {\n            System.out.println(i);\n        }\n    }\n}\n","hints":["Swap only when it's needed: `if (low > high) { ... }`.","Inside the `if`, three lines: `int temp = low;`, then `low = high;`, then `high = temp;`."],"tests":[{"name":"Test 1","stdin":"6\n3\n","expect":"3\n4\n5\n6","hidden":false},{"name":"Test 2","stdin":"2\n5\n","expect":"2\n3\n4\n5","hidden":true},{"name":"Test 3","stdin":"4\n4\n","expect":"4","hidden":true},{"name":"Test 4","stdin":"-1\n-3\n","expect":"-3\n-2\n-1","hidden":true}],"require":[{"pattern":"\\b(low\\s*=\\s*high|high\\s*=\\s*low)\\s*;","message":"Swap the values with a third variable, as in the example."},{"pattern":"for\\s*\\(\\s*int\\s+i\\s*=\\s*low\\s*;\\s*i\\s*<=\\s*high\\s*;","message":"Keep the loop as it is: it counts from `low` up to `high`."}],"forbid":[]}]}