Lesson · Java Language Fundamentals
Primitive types: exact sizes, silent overflow
12 min readPrimitive Data Types
Eight primitives, each with a size the spec pins down. Where the ranges come from, why integer overflow never throws, and why 0.1 + 0.2 refuses to be 0.3.
Eight types with fixed sizes
Java's primitive sizes are the same on every platform, which is a deliberate break from C where int could be 16, 32 or 64 bits depending on the machine. Portability is the point: a serialised int means the same bytes everywhere.
- byte 8 bits, ranges from -128 to 127. For raw bytes and tight arrays.
- short 16 bits, roughly plus or minus 32 thousand. Rare, mostly for legacy formats.
- int 32 bits, about plus or minus 2.1 billion. The default for whole numbers.
- long 64 bits, about plus or minus 9.2 quintillion. Needs the L suffix on literals.
- float 32-bit IEEE 754, around 7 significant digits.
- double 64-bit IEEE 754, around 15 to 16 significant digits. The default for decimals.
- char 16 bits, unsigned, a UTF-16 code unit from 0 to 65535, not a full character.
- boolean true or false. The JVM spec does not fix its size, so never reason about it in bytes.
Ranges come from bits, and overflow is silent
Whole numbers use two's complement: the leftmost bit carries the sign, and every increment past the maximum wraps to the minimum. Integer.MAX_VALUE + 1 is Integer.MIN_VALUE, with no exception, no warning, and no way to opt out at runtime.
There is one asymmetry worth memorising. Constant expressions are evaluated at compile time, so int x = 2147483647 + 1; is a compile error: the compiler sees the overflow and refuses. The same arithmetic on variables runs at runtime and wraps silently. Compile-time safety stops exactly where variables begin.
byte · 8 bits · two's complement
value
0
A byte is eight bits. The leftmost is the sign bit. Step through and watch what happens when the value runs past its maximum.
Floating point: fast, approximate, IEEE 754
float and double store binary fractions, and most decimal fractions have no exact binary form. That is the whole explanation for the famous result: 0.1 + 0.2 prints 0.30000000000000004, and comparing it with 0.3 using == is false. If exact decimal arithmetic matters (prices, ledgers, invoicing), use BigDecimal, or hold money as a long count of the smallest unit.
Two more standard behaviours follow from the spec. Division by zero on doubles does not throw: it produces Infinity, -Infinity, or NaN. And NaN is not equal to anything, including itself, so the test is Double.isNaN(x) rather than x == Double.NaN. Finally, -0.0 exists and equals 0.0, while 1.0 / -0.0 is negative infinity, a fact that occasionally surfaces in graphics and physics code.
Choosing a type without overthinking
Whole numbers default to int. Reach for long when a value can exceed 2.1 billion: timestamps in milliseconds, file sizes, IDs from systems that grow. Picking long for identifiers early is cheap insurance, because widening it later touches every signature that ever carried the value.
Decimals default to double when the value is a measurement: coordinates, temperatures, statistics. If the value is money or another exact decimal contract, it is not really a decimal number at all, and BigDecimal or a minor-units long is the honest representation. float earns its place in large numeric arrays where halving memory and doubling SIMD throughput matter, or when an API hands you one. char shows up mostly through strings; direct use is for character-level logic. boolean needs no guidance.
Silent overflow is the default for variables
2147483647 + 1 as a constant fails compilation because the compiler folds it. Store the same value in a variable, increment it, and the result is -2147483648 with no error. If a counter can plausibly pass two billion, make it a long before the incident, not after.
Interview angles
Why are Java primitive sizes fixed, unlike C's?+
The language spec pins byte, short, int, long, float, double and char to exact widths so that behaviour, serialisation and arithmetic are identical everywhere. C leaves sizes implementation-defined for flexibility and platform fit; Java traded that flexibility for WORA.
Explain two's complement in one answer.+
The leftmost bit is interpreted as a negative weight instead of a sign flag, so 10000000 is -128 rather than -0. Adding past the maximum wraps to the minimum naturally, the hardware implements add and subtract identically, and there is exactly one representation of zero.
Why is 0.1 + 0.2 not 0.3?+
Binary floating point cannot represent 0.1 or 0.2 exactly; each is a rounded binary fraction. Their sum inherits the rounding error, so == comparison against 0.3 fails. Use BigDecimal or integer minor units when exact decimal values are required.
int, long or double: quick rules?+
int for ordinary counts and small IDs; long when values can pass 2.1 billion (time, bytes, growing IDs); double for measured quantities; BigDecimal or long-cents for money. Never double for currency, and never assume int for an ID that grows.