Primitive Data Types
You’ve already used int, double, String, and boolean to store values. But Java actually has eight built-in types for storing raw data, and understanding them properly saves you from some genuinely confusing bugs. This lesson covers all eight, with special attention to the traps that catch people off guard.
Why types exist
Every variable in Java has a type, and the type does two things:
- It tells Java how much space to reserve for the value.
- It determines what operations are valid on it. You can multiply two
intvalues, but you can’t multiply a name and a price.
Keeping types distinct lets Java catch whole categories of mistakes at compile time, before your program ever runs.
The eight primitive types
Java has exactly eight primitive types. You know four of them already. Here’s the full picture:
// The four you've already seenint age = 25; // whole numbersdouble price = 9.99; // decimal numbersboolean isLoggedIn = true; // true or falseString name = "Maya"; // text — covered separately below// The restchar grade = 'A'; // a single characterlong population = 8000000000L; // very large whole numbersfloat temperature = 98.6f; // decimal, less precise than doubleshort year = 2024; // smaller whole numbers (rarely used)byte b = 127; // very small numbers (rarely used)int and double cover most situations. boolean and char come up regularly. long, float, byte, and short exist for specific needs, you’ll encounter them in library code more often than you’ll write them yourself.
Java’s eight primitive types, grouped by category. The four in color are the ones you’ll use in almost every program.
One thing to notice: String is not in this list. It’s capitalized, unlike the others, and it behaves differently. The eight types here are called primitives, they store a raw value directly. String is an object, you’ll see exactly what that means in a later chapter.
Integers: int and long
int stores whole numbers with no decimal point. It handles values from about negative two billion to positive two billion, which covers most everyday counts, ages, scores, and years.
int score = 0;int year = 2024;int temperature = -5;int maxUsers = 1000000;When you need a number larger than two billion (a world population, a file size in bytes) use long. The L suffix on the literal tells Java you mean a long value, not an int. Without it, Java reads the number as an int, sees that it doesn’t fit, and refuses to compile.
long worldPopulation = 8000000000L;long bytesOnDisk = 500000000000L;The overflow trap
If you add 1 to the largest possible int, you don’t get an error. The value silently wraps around to the most negative number possible. Run this to see it:
Java doesn’t warn you. The value just wraps. This is called integer overflow, and it causes real bugs. If there’s any chance a value might exceed two billion, use long.
Floating-point numbers: double and float
double stores decimal numbers. Use it by default whenever you need a fractional value.
double price = 14.99;double pi = 3.14159265358979;double absoluteZero = -273.15;float is similar but less precise. You mark a float literal with an f suffix, without it, Java treats any decimal literal as a double and will refuse the assignment. The memory savings are negligible in modern Java, so just use double.
float roughEstimate = 3.14f; // the f suffix is requiredThe floating-point imprecision trap
Decimal fractions can’t always be represented exactly in binary. This leads to a result that surprises almost every new developer:
Not 0.3. The slight error is a fundamental property of floating-point arithmetic, not a Java quirk. For most calculations (physics, statistics, measurements) the error is negligible. For money, never use double. Java has a BigDecimal class for exact decimal arithmetic, but that’s a later topic.
Booleans: boolean
A boolean holds exactly one of two values: true or false. You’ve seen this already. One thing Java enforces strictly: you can’t use a number where a boolean is expected. In some other languages, 0 means false and 1 means true. Not in Java. A boolean is a boolean, nothing else.
boolean isActive = true;boolean hasDiscount = false;Characters: char
char stores a single character. Character literals use single quotes, not double quotes.
char initial = 'J';char plus = '+';char zero = '0';"A" and 'A' are different things in Java. Double quotes produce a String. Single quotes produce a char. Mixing them up is a common early mistake.
char arithmetic
Java stores characters internally as numbers, specifically, Unicode code points. So arithmetic on char values is legal, and the results can be surprising:
When you add an int to a char, the result is an int. So 'A' + 1 gives you 66, not 'B'. You can convert it back to a char with a cast, covered in a later lesson. For now, just know this connection exists.
Checking the limits
Java exposes the minimum and maximum value of each numeric type as named constants. Useful to know they exist:
These come from the wrapper classes: Integer, Double, Long, and so on. Each primitive type has a corresponding wrapper class that provides useful constants and utility methods. You’ll meet them properly in a later lesson. For now, just know they’re the source of these constants.
Choosing the right type
A simple rule covers most situations:
- Whole numbers? Use
int. Switch tolongif the value might exceed two billion. - Decimal numbers? Use
double. - True or false? Use
boolean. - A single character? Use
char.
byte, short, and float are there when you need them (low-level protocols, graphics, legacy APIs) but you’ll reach for them rarely.
Checkpoint
Which of the following best describes what a type tells Java?
You declare long fileSize = 5000000000; without the L suffix. What happens?
What does this print?
System.out.println(0.1 + 0.2);