Primitive Data Types
Java has eight primitive types for simple values: whole numbers, decimals, single characters and true/false. They're fast and compact, and each has a fixed size and range. Knowing them helps you pick the right type and avoid surprises like overflow and rounding errors.
The eight primitives
| Type | Size | Holds | Example |
|---|---|---|---|
byte | 8 bits | −128 to 127 | byte b = 100; |
short | 16 bits | −32,768 to 32,767 | short s = 20000; |
int | 32 bits | about ±2.1 billion | int i = 1_500_000; |
long | 64 bits | about ±9.2 quintillion | long l = 8_000_000_000L; |
float | 32 bits | ~7 significant digits | float f = 2.5f; |
double | 64 bits | ~15–16 significant digits | double d = 2.5; |
char | 16 bits | one UTF-16 character | char c = 'A'; |
boolean | — | true or false | boolean ok = true; |
In everyday code, use int for whole numbers, long when they may exceed two billion, double for decimals, and boolean for flags.
Limits and overflow
void main() { System.out.println(Integer.MAX_VALUE); System.out.println(Integer.MAX_VALUE + 1); System.out.println(Long.MAX_VALUE); }
2147483647 -2147483648 9223372036854775807
Going past the maximum doesn't cause an error — the value silently wraps around to the minimum. Use long for large counts, or Math.addExact, which throws an exception on overflow.
long and float literals
void main() { long population = 8_100_000_000L; float temperature = 21.5f; System.out.println(population); System.out.println(temperature); }
8100000000 21.5
Whole-number literals are int by default, so large ones need an L suffix. Decimal literals are double by default, so float values need an f.
Decimals aren't exact
void main() { System.out.println(0.1 + 0.2); System.out.println(1.0 - 0.9); }
0.30000000000000004 0.09999999999999998
double stores numbers in binary, and values like 0.1 can't be represented exactly. That's fine for measurements and science, but for money use BigDecimal (lesson 11) or whole cents in a long.
char
void main() { char letter = 'J'; char next = (char) (letter + 1); System.out.println(letter); System.out.println(next); System.out.println((int) letter); }
J K 74
A char uses single quotes and is stored as a number (its Unicode code), so you can do arithmetic on it. Strings use double quotes: 'A' is a char, "A" is a String.
boolean
void main() { boolean isAdult = 20 >= 18; boolean hasTicket = false; System.out.println(isAdult); System.out.println(isAdult && hasTicket); }
true false
Unlike some languages, Java's boolean isn't a number: you can't use 0 or 1 in its place.
Primitives vs reference types
Everything that isn't one of these eight is a reference type: String, arrays, and every class. A primitive variable holds the value itself; a reference variable holds a reference (an address) to an object elsewhere in memory. The difference matters when copying and comparing — lessons 9 and 23 return to it. Each primitive also has a wrapper class (Integer, Double…) used with collections (lesson 38).
Default values
Fields in classes get default values (0, 0.0, false, '\u0000', or null for references). Local variables inside methods get no default and must be assigned before use, as you saw in lesson 5.
Try it yourself
Print the range of each integer type.
byte: -128 to 127 short: -32768 to 32767 int: -2147483648 to 2147483647 long: -9223372036854775808 to 9223372036854775807