Java Arena
Step 1 of 6

Variables and their types

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:

Type Holds Examples
int whole numbers 3, -12, 2026
double numbers with a decimal point 0.45, -3.5, 2.0
boolean true or false true, false
String text "apple", "Hello!"

A variable is created by writing its type, its name and its first value:

int apples = 12;
double price = 0.45;
boolean inStock = true;
String fruit = "apple";
System.out.println(fruit + ": " + apples + " at " + price);
System.out.println("In stock: " + inStock);
Output
apple: 12 at 0.45
In stock: true

As the last two lines show, + joins numbers and booleans into text too: "In stock: " + inStock becomes In stock: true.

The type decides what fits

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

public class Main {
    public static void main(String[] args) {
        int price = 4.99;
        System.out.println(price);
    }
}
What javac prints
Main.java:3: error: incompatible types: possible lossy conversion from double to int
        int price = 4.99;
                    ^
1 error

The other way round is fine: double distance = 5; compiles, because a whole number fits in a double without losing anything (it becomes 5.0).

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

Your task

Challenge 1 of 3

Fill in the three values so the program prints exactly Mo ran 3 laps of 2.5 km.

Challenge 2

Choose the right type for each of the four variables, so the program compiles and prints Oslo, 2026: -3.5 degrees, raining: false.

Challenge 3

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

Name: Kai
Age: 17
Height: 1.82
Student: true