Basic

Variables and Data Types

Declare variables and understand Java's built-in data types.

What you will see

7.0

About 2 min of reading

Try It

Declare your first variables, watch Java's types hold them, and see the compiler reject a mismatch, all in a tiny program you can compile in seconds.

1

Start with the smallest runnable Java program: a class with a main method. Declare an int, a double, and a boolean, then print each one. Watch how each type prints exactly what you stored.

class Main { public static void main(String[] args) { int age = 30; double height = 1.75; boolean active = true; System.out.println(age); System.out.println(height); System.out.println(active); } }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
30 1.75 true
Every variable's type must be declared explicitly in Java. You cannot reassign age to a string later, the compiler stops you before the program even runs.
2

Reassign the variable to a new value of the same type and print it. The name stays, only the value changes, proving the variable is a labeled box you can refill.

class Main { public static void main(String[] args) { int score = 85; score = 90; System.out.println(score); } }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
90
The type cannot change, but the value can. Try reassigning score to "high" to see the compiler reject it, a common first mistake.
3

Work with text. Java's String is not a primitive; you build it by concatenating with the plus sign. The spaces inside the string literals matter, they show up in the output exactly as written.

class Main { public static void main(String[] args) { String name = "Ada"; String greeting = "Hello, " + name; System.out.println(greeting); } }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Hello, Ada
String is a reference type, not a primitive. Concatenation with + creates a new String each time because Strings are immutable.
4

Cast a double to an int with (int). Watch Java truncate toward zero, it drops the decimal instead of rounding, which is a subtle trap when the number is large.

class Main { public static void main(String[] args) { double x = 3.9; int y = (int) x; System.out.println(y); } }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
3
(int) 3.9 gives 3, not 4. Casting loses data on purpose, so do it only when you mean to drop the decimal part.
5

Mix an int with a double in one expression. Java silently promotes the int to double so the result is a double, the wider type always wins in a mixed operation.

class Main { public static void main(String[] args) { int a = 5; double b = 2.0; double c = a + b; System.out.println(c); } }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
7.0
The reverse is not automatic: you cannot assign a double straight into an int variable without an explicit cast.

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

A value is created, stored in a typed slot, and later read or reassigned.
1 Declare a variable with a type
↓
2 Assign a compatible literal or expression
↓
3 Read the value in expressions or output
↓
4 Reassign a new value of the same type
Why this works
The eight primitives
TypeSizeUse
byte8-bitsmall integers
short16-bitintegers
int32-bitintegers (default)
long64-bitlarge integers
float32-bitdecimals
double64-bitdecimals (default)
char16-bitsingle character
boolean1-bittrue/false

Use int and double unless you have a reason to choose otherwise. Add L/F suffixes for long/float literals: 3.14F, 100L.

Automatic type promotion

When you combine types, Java promotes to the wider type:

int a = 5;
double b = 2.0;
double c = a + b;   // 7.0 (int promoted to double)

The reverse is not automatic, assigning a double to an int requires a cast:

double x = 3.9;
int y = (int) x;   // 3 (truncated, not rounded)

Casting loses data, so do it intentionally.

Examples

Declaring primitive variables

Declaring primitive variables

Java is statically typed: every variable has a type you must declare.

int age = 30;
double height = 1.75;
char grade = 'A';
boolean active = true;

int holds whole numbers, double decimals, char single characters, boolean true/false. The compiler checks every type at build time.

Naming and reassigning

Variables hold values

int score = 85;
score = 90;   // reassign
// score = "high";  // compile error: wrong type
System.out.println(score);   // 90

Once declared, a variable's type cannot change. You can reassign a new value of the same type.

String is not a primitive

Working with text

String name = "Ada";
String greeting = "Hello, " + name;
System.out.println(greeting);   // Hello, Ada

String is a reference type (a class), not a primitive. You concatenate with +. Strings are **immutable*: every operation creates a new String.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

Next Lesson
Control Flow: Conditionals
Control Flow: Conditionals →