Variables and Primitive Types
AvailableLesson 1 of 60 · Syntax · Easy
The big idea
A variable stores a value so your program can use it later. In Java, every variable starts with its type, followed by a name, an equals sign =, a value, and a semicolon ; at the end: Java keeps code organized inside a class block (public class Solution { ...
Think of it like this: Think of a Java variable like a labeled box with its shape printed on the side: `int` says the box holds a whole number, `x` is the label, and `5` goes inside.
Read the lesson
A variable stores a value so your program can use it later.
In Java, every variable starts with its type, followed by a name, an equals sign =, a value, and a semicolon ; at the end:
int age = 20; // a whole number
double price = 9.99; // a decimal number
boolean ok = true; // true or false
Java keeps code organized inside a class block (public class Solution { ... }). We already added that wrapper for you — just write your variable line inside the curly braces.
Quick check
Which line correctly creates a whole-number variable named age set to 20 in Java?
Your challenge
Inside Solution, create a whole-number variable named x set to 5 (type int x = 5;).
More learning tools(compare languages, visual guide, related lessons)
Compare with another language
Similar to javascript.variables-types (85% overlap). Variables & primitive types: compare let/const (JS), auto/int (C++), := (Go), let mut (Rust), and column types (SQL).
Visual walkthrough
A diagram, a step-by-step animation, and short captions for Variables and Primitive Types.
Learn this first
Nothing needed - you can start here.