Swipe
SwipeSwipe
Swipe Apps

Paths

Motivation

Information

Variables & Primitive Types · Lesson 25 of 28

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

🤖
Eight little types carry almost all the data in Java. 🧮

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';
Four commonly used primitive types: a whole number, a decimal number, a true/false value, and a single character.
🤖
Four everyday types, four different jobs. 🔡

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.

Fill in the blank

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;
long needs an L suffix for its literal value; byte is useful for very small numbers where saving memory matters.
🤖
Local variables don't get the same free pass as fields — keep that in mind here. 🤔

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.

True or false

A local variable declared inside a method automatically gets a default value, like 0, if you never initialize it.

Quick check

Which primitive type would you use to store whether a light switch is on or off?

Chapter quiz

Variables & Primitive Types

10 questions · pass with 60% — just like in the app.

Quick check

What must every variable in Java have?

True or false

In Java, variable names are case-sensitive, so score and Score are different variables.

Fill in the blank

Java's naming convention for variables is ____, e.g. totalScore.

Quick check

Which statement correctly declares and initializes an int variable named age?

True or false

You can declare a variable without giving it a value right away, as long as you assign one before using it.

Fill in the blank

The ____ type stores whole numbers and is the most common choice for counting things.

Quick check

Which type would you use to store a value like 4.99?

True or false

A local variable inside a method automatically receives a default value if you don't initialize it.

Fill in the blank

A long literal in Java must end with the letter ____.

Quick check

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 →