Nanfeng

Notes on software development, code, and curious ideas

Primitive Data Types in Java

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
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.

+