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

TypeSizeRangeDefault
byte8 bits−128 to 1270
short16 bits−32 768 to 32 7670
int32 bits≈ ±2.1 billion0
long64 bits≈ ±9.2 × 10¹⁸0L
float32 bitsIEEE 754 single precision0.0f
double64 bitsIEEE 754 double precision0.0
char16 bitsUTF-16 code unit'\u0000'
booleanJVM-definedtrue / falsefalse

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.