The eight primitives
| Type | Size | Use |
|---|---|---|
byte | 8-bit | small integers |
short | 16-bit | integers |
int | 32-bit | integers (default) |
long | 64-bit | large integers |
float | 32-bit | decimals |
double | 64-bit | decimals (default) |
char | 16-bit | single character |
boolean | 1-bit | true/false |
Use int and double unless you have a reason to choose otherwise. Add L/F suffixes for long/float literals: 3.14F, 100L.
Automatic type promotion
When you combine types, Java promotes to the wider type:
int a = 5;
double b = 2.0;
double c = a + b; // 7.0 (int promoted to double)
The reverse is not automatic, assigning a double to an int requires a cast:
double x = 3.9;
int y = (int) x; // 3 (truncated, not rounded)
Casting loses data, so do it intentionally.