Primitive type
Java has 8 primitive types. Unlike object references, a primitive variable holds its value directly in memory. They are fast, non-null, and come with fixed sizes defined by the Java Language Specification.
The 8 primitives
| Type | Size | Range | Default |
|---|---|---|---|
byte | 8 bits | −128 to 127 | 0 |
short | 16 bits | −32 768 to 32 767 | 0 |
int | 32 bits | ≈ ±2.1 billion | 0 |
long | 64 bits | ≈ ±9.2 × 10¹⁸ | 0L |
float | 32 bits | IEEE 754 single precision | 0.0f |
double | 64 bits | IEEE 754 double precision | 0.0 |
char | 16 bits | UTF-16 code unit | '\u0000' |
boolean | JVM-defined | true / false | false |
Primitive vs wrapper
Every primitive has a wrapper class: int / Integer, long / Long, boolean / Boolean. Wrappers are objects; they can be null and live in collections (List<Integer>, not List<int>).
Autoboxing
Integer boxed = 42; // auto-box from int
int unboxed = boxed; // auto-unbox
Beware of silent NullPointerException when unboxing a null Integer.