Java, lesson 2
A variable is a named box that holds a value. In Java every variable has a type, and the type never changes. You declare a variable by writing the type, then the name, then (usually) a starting value:
The compiler checks types before the program runs. That catches a whole class of mistakes early, such as accidentally storing text where a number is expected.
Java has eight primitive types that hold plain values. You will mostly use these four:
| Type | Holds | Example |
|---|---|---|
int | whole numbers up to about ±2.1 billion | 42, -7 |
long |
2 exercises to try what you just read.
| much larger whole numbers |
8_000_000_000L |
double | numbers with a decimal point | 3.14, 1.5e3 |
boolean | true or false | true |
char holds one character in single quotes, like 'Z'. The others (byte, short, float)
are for special cases.
String is not a primitive. It is a class, which is why it starts with a capital letter, but you
can create one simply by writing text in double quotes.
Underscores inside number literals are just for readability: 1_000_000 is one million. A whole
number literal is an int unless you add an L suffix to make it a long.
An int cannot hold more than 2,147,483,647. If a calculation goes past that, Java does not
complain; the value silently wraps around to a negative number. When numbers might get big
(money in cents, milliseconds, populations), use long. If any value in a calculation is a
long, the whole calculation happens in long:
For local variables you can write var instead of the type, and Java infers it from the value on
the right. The variable is still strongly typed; you just did not have to spell it out.
var needs a starting value, and it only works for local variables inside methods. Use it when
the type is obvious from the right-hand side.
Mark a variable final when it must never change. By convention, constants that are fixed for
the whole program use UPPER_SNAKE_CASE:
Names are case sensitive and use camelCase for variables: totalScore, not total_score or
TotalScore. A name can contain letters, digits, _ and $, but cannot start with a digit or be
a keyword like class or int. Pick names that say what the value means: price beats p.
Declare five variables with these exact values and types, then print them in the format below:
title, a String: Space Catsyear, an int: 2026price, a double: 12.5in3D, a boolean: truerow, a char: FExpected output:
The program reads no input.
The starter computes how many seconds and milliseconds are in a 365-day year using final
constants, but the millisecond line prints a strange negative number. Fix it so both lines are
correct. Keep using the constants.
Expected output:
int lives = 3;
double speed = 2.5;
String player = "Robin";
boolean gameOver = false;
char grade = 'A';
lives = lives - 1; // you can change the value later...
// lives = "three"; // ...but not the type: this would not compileint big = 2_000_000_000;
System.out.println(big * 2); // -294967296 (wrapped!)
System.out.println(big * 2L); // 4000000000var city = "Lisbon"; // String
var count = 12; // intfinal int MAX_PLAYERS = 4;
final double TAX_RATE = 0.2;
// MAX_PLAYERS = 5; // compile error: cannot assign a value to final variableTitle: Space Cats
Year: 2026
Price: 12.5
3D: true
Row: FSeconds per year: 31536000
Milliseconds per year: 31536000000