Java Primitive Data Types: Ranges, Defaults, Literals and Examples

Java has eight primitive types. They store simple values directly rather than object references.

TypeSizeTypical purpose
byte8 bitsSmall signed integers, binary data
short16 bitsSmall signed integers
int32 bitsDefault integer type
long64 bitsLarge signed integers
float32 bitsSingle-precision floating point
double64 bitsDefault floating-point type
char16 bitsOne UTF-16 code unit
booleanJVM-dependent storagetrue or false

Integer types

All four integer types are signed two’s-complement values:

1
2
3
4
byte small = 100;
short count = 20_000;
int population = 1_000_000;
long distance = 9_000_000_000L;

The L suffix marks a long literal. Underscores can improve readability inside numeric literals.

Floating-point types

1
2
float ratio = 0.75F;
double average = 12.345;

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
2
3
char letter = 'A';
char newline = '\n';
char symbol = '\u03A9';

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
2
boolean enabled = true;
boolean finished = false;

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.