1. java-hell.de
  2. Topics
  3. Variables
Topic 2 of 11

Learn Java Variables – Exercises Online

Variables are the drawers of your program. You'll meet int, double, String and boolean, glue text together and overwrite values – each exercise takes just a few minutes.

The exercises

What you'll learn in this topic

  1. Your First Variable
  2. A Variable for Text
  3. Decimal Numbers
  4. Putting Text Together
  5. Overwriting a Variable
  6. True or False
Exercise 1 of 6

Your First Variable

A variable is like a labeled box. You give it a name and put a value inside.

Here's what that looks like:

int age = 25;

That means: create a box called age that stores a whole number (int), and put 25 in it.

The variable already exists. Change its value from 25 to 30.

Starter code
void main() {
    int age = 25;
    IO.println(age);
}
Background
How variables work in memory

When you write int age = 25;, Java sets aside exactly 4 bytes (32 bits) of memory and stores the value 25 there as a binary number.

The name age is just a label for you — the compiled bytecode works with a numbered slot in the method's stack frame instead. The name only survives as optional debug info.

int can store values from about -2.1 billion to +2.1 billion. For bigger numbers: long (8 bytes).

Exercise 2 of 6

A Variable for Text

For text you use the type String (capital S!):

String name = "Anna";

Change the name so that Max is printed.

Starter code
void main() {
    String name = "Anna";
    IO.println(name);
}
Background
String vs. int — reference vs. value

int stores the value directly. String stores a reference (a pointer) to an object in the so-called heap memory.

That's why you compare strings with .equals() instead of ==: == only compares the references (do both point to the same object?), not the content.

Exercise 3 of 6

Decimal Numbers

For decimal numbers there's double:

double price = 4.99;

Important: Java always uses a dot as the decimal separator — so 4.99, never 4,99.

Change the price to 9.99.

Starter code
void main() {
    double price = 4.99;
    IO.println(price);
}
Background
Floating-point numbers and their quirks

double uses 8 bytes and stores numbers in the IEEE 754 floating-point format. That means: some decimal numbers can't be represented exactly!

For example: 0.1 + 0.2 gives you 0.30000000000000004 in Java. That's why money amounts are usually handled with BigDecimal.

Exercise 4 of 6

Putting Text Together

With + you can join text and variables:

String name = "Java";
IO.println("Hello " + name);

Result: Hello Java

Replace the ??? so that I am 25 years old is printed.

Starter code
void main() {
    int age = 25;
    IO.println(???);
}
Background
String concatenation

When you write "Text" + number, Java automatically converts the number into a string (string conversion) and glues the two together.

Strings are immutable, so every concatenation produces a new string object. Inside a loop that adds up fast and gets slow. In that case pros use a StringBuilder, or for formatted output String.format("I am %d years old", age).

Exercise 5 of 6

Overwriting a Variable

You can change the value of a variable at any time:

int x = 5;
x = 10;  // x is now 10

Note: When you change it, you don't write int in front again — the type only appears when the variable is created.

Set points to 100 before it gets printed.

Starter code
void main() {
    int points = 0;
    // Set points to 100
    IO.println(points);
}
Background
Declaration vs. assignment

int x = 5; is a declaration with initialization — the type is fixed and a starting value is set. x = 10; is a plain assignment.

If you write int x = 10; again, Java thinks you want to create a new variable — and complains, because x already exists. So: state the type only once!

Exercise 6 of 6

True or False

The type boolean knows only two values: true or false.

boolean isSunny = true;
boolean isRaining = false;

Change the value so that true is printed.

Starter code
void main() {
    boolean javaIsCool = false;
    IO.println(javaIsCool);
}
Background
Booleans and logic

In theory a boolean needs just 1 bit. The JVM doesn't actually pin down its size, though — in practice it usually takes a whole byte (in arrays) or even 4 bytes (as a local variable), because that's faster to work with.

Boolean values are the foundation of every decision in a program. You can combine them with && (AND), || (OR) and ! (NOT).

Java uses short-circuit evaluation: with a && b, b isn't even checked if a is already false.

Enough reading. Your turn.

In the tutorial you write the code yourself, hit Run and instantly see whether your program works. No JDK, no IDE, no bullshit.

Practice Variables in your browser