Primitive Data Types
A free lesson from Swipe Coding · Java, level 2 · updated October 2026
By the Swipe team, founded by Colin · how we make lessons
Every variable in Java needs a type, and the eight built-in primitive types are the most basic kinds of data a variable can hold. Unlike types you will meet later (like String), primitives are not objects — they store a simple value directly, which makes them fast and memory-efficient.
int quantity = 10; double price = 4.99; boolean inStock = true; char grade = 'A';
int stores whole numbers (no decimal point) and is the default choice for counting things — it can hold values from roughly -2.1 billion to 2.1 billion. double stores decimal numbers and is the default choice for anything with a fractional part, like prices or measurements. boolean can only ever be true or false, which makes it perfect for yes/no conditions. char stores exactly one character, written in single quotes like 'A' — never double quotes.
To store a single character like 'A', you use the ____ type.
Java also has smaller and larger whole-number types for when int isn't the right fit: byte and short save memory for tiny numbers, while long is used for whole numbers bigger than int can hold — you mark a long literal with an L at the end. float is a smaller, less precise alternative to double, rarely used unless memory is tight.
long population = 8_000_000_000L; byte smallNumber = 120;
Fields that belong to a class automatically get a default value if you don't initialize them — 0 for numeric types, false for boolean, and a null character for char. Local variables declared inside a method, however, do not get a default value: Java's compiler forces you to assign one before you can use the variable, which helps catch bugs early.
A local variable declared inside a method automatically gets a default value, like 0, if you never initialize it.
Which primitive type would you use to store whether a light switch is on or off?
Variables & Primitive Types
10 questions · pass with 60% — just like in the app.
What must every variable in Java have?
In Java, variable names are case-sensitive, so score and Score are different variables.
Java's naming convention for variables is ____, e.g. totalScore.
Which statement correctly declares and initializes an int variable named age?
You can declare a variable without giving it a value right away, as long as you assign one before using it.
The ____ type stores whole numbers and is the most common choice for counting things.
Which type would you use to store a value like 4.99?
A local variable inside a method automatically receives a default value if you don't initialize it.
A long literal in Java must end with the letter ____.
What happens when you reassign a variable that already holds a value?
Keep your streak going in Swipe Coding
Swipe through every level of Java on your iPhone — with XP, stars, streaks, a personal advisor and the full track beyond these free levels.
Get the Swipe apps →