Part 02 / Topic 2 of 9
The eight primitive types
Java has exactly eight primitive types: byte, short, int, long (whole numbers), float, double (decimals), char (one character) and boolean (true/false). String is not in this list — it is a class, used constantly but not covered until Stage 2.
| Type | Size | Range (whole numbers) | Typical use |
|---|---|---|---|
byte |
8 bits | -128 to 127 | rarely used directly; raw bytes |
short |
16 bits | -32,768 to 32,767 | rarely used directly |
int |
32 bits | -2,147,483,648 to 2,147,483,647 | the default whole-number type |
long |
64 bits | about ±9.2 × 10¹⁸ | big counters, IDs; literal needs L |
float |
32 bits | ~7 correct decimal digits | rarely used; literal needs f |
double |
64 bits | ~15 correct decimal digits | the default decimal type |
char |
16 bits | one UTF-16 character | single characters |
boolean |
1 bit (conceptually) | true / false |
conditions, flags |
long population = 1_428_600_000L; // bigger range, needs the L suffix
float weightKg = 2.5f; // needs the f suffix
char currencySymbol = '₹';
Output (examples/02_primitive_types.java):
120
30000
100234
1428600000
2.5
2450.75
₹
false
-128
0.33333334
0.3333333333333333
1000000
Two things to notice in that output:
- Overflow is silent.
byte maxByte = 127; byte overflowed = (byte) (maxByte + 1);gives-128, not an error. Abyte(orint,long, …) just wraps around past its range. floatloses precision thatdoublekeeps.1.0f / 3.0fprints0.33333334(8 digits);1.0 / 3.0prints0.3333333333333333(16 digits). Usedoubleunless you have a specific reason not to (large arrays of numbers, interop with older APIs).
Underscores in a number literal (1_000_000) are only for human readers; the compiler ignores them.
🔍 Going deeper:
intis the default for whole-number literals anddoublefor decimal literals — write3and Java treats it asint, write3.0and it isdouble. This is why100 / 3is33, not33.33: both sides are alreadyintbefore any variable gets involved.
Full program: examples/02_primitive_types.java.
Done here? The practice set for Part 2 — including the solutions — is on the part page.