{"id":"variables-changing","slug":"changing-values","title":"Changing a value","text":"A variable's value can change while the program runs. To give an existing variable a new value, write its name, `=` and the value, without the type in front:\n\n```java\nint lives = 3;\nSystem.out.println(\"Lives: \" + lives);\nlives = 2;\nSystem.out.println(\"Lives: \" + lives);\n```\n\n```output\nLives: 3\nLives: 2\n```\n\nThe `=` here means \"put this value into the variable\", not \"is equal to\". The old value is gone after the assignment.\n\n### Create once, change many times\n\nThe type is written only once, when the variable is created. Writing it again creates a second variable with the same name, which Java doesn't allow:\n\n```java\npublic class Main {\n    public static void main(String[] args) {\n        int points = 10;\n        int points = 25;\n        System.out.println(points);\n    }\n}\n```\n\n```javac\nMain.java:4: error: variable points is already defined in method main(String[])\n        int points = 25;\n            ^\n1 error\n```\n\nAnd the type stays the same for the variable's whole life: an `int` variable can't later hold text. `age = \"twenty\";` fails with `incompatible types: String cannot be converted to int`.\n","task":"Change `weather` to `rainy` between the two prints, so the program prints:\n\n```\nMorning: sunny\nEvening: rainy\n```\n","kind":"code","seed":"public class Main {\n    public static void main(String[] args) {\n        String weather = \"sunny\";\n        System.out.println(\"Morning: \" + weather);\n        // Change weather to rainy here.\n        System.out.println(\"Evening: \" + weather);\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        String weather = \"sunny\";\n        System.out.println(\"Morning: \" + weather);\n        // Change weather to rainy here.\n        weather = \"rainy\";\n        System.out.println(\"Evening: \" + weather);\n    }\n}\n","hints":["The variable already exists, so leave out the type: `weather = ...;`","`weather = \"rainy\";` goes between the two print lines."],"tests":[{"name":"Output","stdin":"","expect":"Morning: sunny\nEvening: rainy","hidden":false}],"require":[{"pattern":"String\\s+weather","max":1,"message":"Keep the one `String weather` variable and give it a new value; don't create a second one."}],"forbid":[{"pattern":"print(ln|f)?\\s*\\([^;]*rainy","raw":true,"message":"Print the variable in the second line too; don't type `rainy` into the printed text."}],"more":[{"task":"This program doesn't compile, because it creates the variable `points` twice. Fix it so it changes the value instead, and prints:\n\n```\nPoints: 10\nPoints: 25\n```\n","kind":"code","seed":"public class Main {\n    public static void main(String[] args) {\n        int points = 10;\n        System.out.println(\"Points: \" + points);\n        int points = 25;\n        System.out.println(\"Points: \" + points);\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        int points = 10;\n        System.out.println(\"Points: \" + points);\n        points = 25;\n        System.out.println(\"Points: \" + points);\n    }\n}\n","hints":["The error says the variable is already defined. The second line that starts with `int points` is the problem.","Remove `int` from that line, so it changes the existing variable."],"tests":[{"name":"Output","stdin":"","expect":"Points: 10\nPoints: 25","hidden":false}],"require":[{"pattern":"int\\s+points","max":1,"message":"Create `points` only once."}],"forbid":[]},{"task":"A price with cents doesn't fit in an `int`, so this program doesn't compile. Choose the right type for `price` so it prints:\n\n```\nPrice: 4.99\nSale price: 3.99\n```\n","kind":"code","seed":"public class Main {\n    public static void main(String[] args) {\n        int price = 4.99;\n        System.out.println(\"Price: \" + price);\n        price = 3.99;\n        System.out.println(\"Sale price: \" + price);\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        double price = 4.99;\n        System.out.println(\"Price: \" + price);\n        price = 3.99;\n        System.out.println(\"Sale price: \" + price);\n    }\n}\n","hints":["The error says `possible lossy conversion from double to int`: the value has a decimal part, but the variable is an `int`.","Numbers with a decimal point go in a `double`."],"tests":[{"name":"Output","stdin":"","expect":"Price: 4.99\nSale price: 3.99","hidden":false}],"require":[],"forbid":[]}]}