Learn/Java/2. Values and variables
Lesson 2 of 15
Sign in

Java, lesson 2

Values and variables

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.

The everyday types

Java has eight primitive types that hold plain values. You will mostly use these four:

TypeHoldsExample
intwhole numbers up to about ±2.1 billion42, -7
long

2 exercises to try what you just read.

Exercise 1

Movie ticket

Press Ctrl Enter or Check to test your code.

Exercise 2

A year in milliseconds

Press Ctrl Enter or Check to test your code.

much larger whole numbers
8_000_000_000L
doublenumbers with a decimal point3.14, 1.5e3
booleantrue or falsetrue

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.

Overflow: when an int is too small

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:

var: let the compiler work out the type

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.

Constants with final

Mark a variable final when it must never change. By convention, constants that are fixed for the whole program use UPPER_SNAKE_CASE:

Naming

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.

Further reading

  • Variables (dev.java)
  • Primitive types (dev.java)
  • Using var (dev.java)

Declare five variables with these exact values and types, then print them in the format below:

  • title, a String: Space Cats
  • year, an int: 2026
  • price, a double: 12.5
  • in3D, a boolean: true
  • row, a char: F

Expected 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 compile
int big = 2_000_000_000;
System.out.println(big * 2);       // -294967296  (wrapped!)
System.out.println(big * 2L);      // 4000000000
var city = "Lisbon";   // String
var count = 12;        // int
final int MAX_PLAYERS = 4;
final double TAX_RATE = 0.2;
// MAX_PLAYERS = 5;   // compile error: cannot assign a value to final variable
Title: Space Cats
Year: 2026
Price: 12.5
3D: true
Row: F
Seconds per year: 31536000
Milliseconds per year: 31536000000