{"id":"variables-types","slug":"variables-and-types","title":"Variables and their types","text":"A variable holds a value under a name. Every Java variable also has a **type**, which says what kind of value it can hold. Four types cover most of what you'll need for now:\n\n| Type | Holds | Examples |\n|---|---|---|\n| `int` | whole numbers | `3`, `-12`, `2026` |\n| `double` | numbers with a decimal point | `0.45`, `-3.5`, `2.0` |\n| `boolean` | true or false | `true`, `false` |\n| `String` | text | `\"apple\"`, `\"Hello!\"` |\n\nA variable is created by writing its type, its name and its first value:\n\n```java\nint apples = 12;\ndouble price = 0.45;\nboolean inStock = true;\nString fruit = \"apple\";\nSystem.out.println(fruit + \": \" + apples + \" at \" + price);\nSystem.out.println(\"In stock: \" + inStock);\n```\n\n```output\napple: 12 at 0.45\nIn stock: true\n```\n\nAs the last two lines show, `+` joins numbers and booleans into text too: `\"In stock: \" + inStock` becomes `In stock: true`.\n\n### The type decides what fits\n\nA variable can only hold values that fit its type. Putting a decimal number into an `int` would lose the part after the point, so Java refuses to compile it:\n\n```java\npublic class Main {\n    public static void main(String[] args) {\n        int price = 4.99;\n        System.out.println(price);\n    }\n}\n```\n\n```javac\nMain.java:3: error: incompatible types: possible lossy conversion from double to int\n        int price = 4.99;\n                    ^\n1 error\n```\n\nThe other way round is fine: `double distance = 5;` compiles, because a whole number fits in a `double` without losing anything (it becomes `5.0`).\n\nNote the capital `S` in `String`: it's a class, like `Scanner`, while `int`, `double` and `boolean` are built into the language and written in lower case.\n","task":"Fill in the three values so the program prints exactly `Mo ran 3 laps of 2.5 km`.\n","kind":"fill","seed":"public class Main {\n    public static void main(String[] args) {\n        int laps = [[3]];\n        double distance = [[2.5]];\n        String runner = \"[[Mo]]\";\n        System.out.println(runner + \" ran \" + laps + \" laps of \" + distance + \" km\");\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        int laps = 3;\n        double distance = 2.5;\n        String runner = \"Mo\";\n        System.out.println(runner + \" ran \" + laps + \" laps of \" + distance + \" km\");\n    }\n}\n","hints":["Compare the print line with the output: `runner` becomes `Mo`, `laps` becomes `3` and `distance` becomes `2.5`.","A String's value goes between the quotes, which are already there. Numbers are written without quotes."],"tests":[{"name":"Output","stdin":"","expect":"Mo ran 3 laps of 2.5 km","hidden":false}],"require":[],"forbid":[],"more":[{"task":"Choose the right type for each of the four variables, so the program compiles and prints `Oslo, 2026: -3.5 degrees, raining: false`.\n","kind":"fill","seed":"public class Main {\n    public static void main(String[] args) {\n        [[int‖long]] year = 2026;\n        [[double]] temperature = -3.5;\n        [[boolean]] raining = false;\n        [[String]] city = \"Oslo\";\n        System.out.println(city + \", \" + year + \": \" + temperature + \" degrees, raining: \" + raining);\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        int year = 2026;\n        double temperature = -3.5;\n        boolean raining = false;\n        String city = \"Oslo\";\n        System.out.println(city + \", \" + year + \": \" + temperature + \" degrees, raining: \" + raining);\n    }\n}\n","hints":["Look at each value: a whole number, a number with a decimal point, `false`, and text in quotes.","The types are `int`, `double`, `boolean` and `String`. Only `String` starts with a capital letter."],"tests":[{"name":"Output","stdin":"","expect":"Oslo, 2026: -3.5 degrees, raining: false","hidden":false}],"require":[],"forbid":[]},{"task":"Create four variables: a String `name` holding `Kai`, an int `age` holding `17`, a double `height` holding `1.82` and a boolean `student` holding `true`. Then print them with labels, using the variables (not the values typed into the text):\n\n```\nName: Kai\nAge: 17\nHeight: 1.82\nStudent: true\n```\n","kind":"code","seed":"public class Main {\n    public static void main(String[] args) {\n        // Four variables, then four lines of output.\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        // Four variables, then four lines of output.\n        String name = \"Kai\";\n        int age = 17;\n        double height = 1.82;\n        boolean student = true;\n        System.out.println(\"Name: \" + name);\n        System.out.println(\"Age: \" + age);\n        System.out.println(\"Height: \" + height);\n        System.out.println(\"Student: \" + student);\n    }\n}\n","hints":["One line per variable, for example `double height = 1.82;`","Print each with its label: `System.out.println(\"Age: \" + age);`"],"tests":[{"name":"Output","stdin":"","expect":"Name: Kai\nAge: 17\nHeight: 1.82\nStudent: true","hidden":false}],"require":[{"pattern":"String\\s+name\\s*=","message":"Create a String variable called `name`."},{"pattern":"int\\s+age\\s*=","message":"Create an int variable called `age`."},{"pattern":"double\\s+height\\s*=","message":"Create a double variable called `height`."},{"pattern":"boolean\\s+student\\s*=","message":"Create a boolean variable called `student`."}],"forbid":[{"pattern":"print(ln|f)?\\s*\\([^;]*\"[^\"]*(Kai|17|1\\.82|true)[^\"]*\"","raw":true,"message":"Print the variables; don't type their values into the printed text."}]}]}