Java has eight primitive types. They store simple values directly rather than object references.
| Type | Size | Typical purpose |
|---|---|---|
byte |
8 bits | Small signed integers, binary data |
short |
16 bits | Small signed integers |
int |
32 bits | Default integer type |
long |
64 bits | Large signed integers |
float |
32 bits | Single-precision floating point |
double |
64 bits | Default floating-point type |
char |
16 bits | One UTF-16 code unit |
boolean |
JVM-dependent storage | true or false |
Integer types
All four integer types are signed two’s-complement values:
1 | byte small = 100; |
The L suffix marks a long literal. Underscores can improve readability inside numeric literals.
Floating-point types
1 | float ratio = 0.75F; |
Floating-point values follow IEEE 754 and cannot represent every decimal fraction exactly. Use BigDecimal for exact decimal arithmetic such as financial calculations.
char and Unicode
1 | char letter = 'A'; |
A char is one UTF-16 code unit, not necessarily a complete user-perceived character. Supplementary Unicode characters require a surrogate pair or code-point APIs.
boolean
1 | boolean enabled = true; |
Java does not implicitly convert numbers to Boolean values.
Defaults and local variables
Instance and static fields receive defaults: numeric primitives become zero, char becomes \u0000, and boolean becomes false. Local variables do not receive automatic defaults and must be assigned before use.
Each primitive has a wrapper class such as Integer, Long, Double, and Boolean, useful for generics, nullable values, and utility methods.