Java Arena
Step 2 of 6

Changing a value

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:

int lives = 3;
System.out.println("Lives: " + lives);
lives = 2;
System.out.println("Lives: " + lives);
Output
Lives: 3
Lives: 2

The = here means "put this value into the variable", not "is equal to". The old value is gone after the assignment.

Create once, change many times

The 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:

public class Main {
    public static void main(String[] args) {
        int points = 10;
        int points = 25;
        System.out.println(points);
    }
}
What javac prints
Main.java:4: error: variable points is already defined in method main(String[])
        int points = 25;
            ^
1 error

And 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.

Your task

Challenge 1 of 3

Change weather to rainy between the two prints, so the program prints:

Morning: sunny
Evening: rainy

Challenge 2

This program doesn't compile, because it creates the variable points twice. Fix it so it changes the value instead, and prints:

Points: 10
Points: 25

Challenge 3

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:

Price: 4.99
Sale price: 3.99