Lesson · Java Language Fundamentals
Type conversion: the widening ladder and the casts back down
12 min readType Conversion
Java converts numbers by strict rules: widening is automatic, narrowing needs a cast, and expressions promote operands before doing arithmetic. The traps all live in the conversions you did not write.
The widening ladder
Numeric types form a partial order: byte widens to short, int, long, float and double; short, char and int climb the same way upward. Conversion along the arrows is automatic and never throws, because every value of the smaller type is representable in the larger one. This is the conversion Java performs silently, and most of the time it is exactly what you want.
One asterisk belongs on the word lossless. int to float and long to float can lose precision for values above 2^24 and 2^53 respectively, even though they are widening conversions. The type gets bigger, the guarantee does not: only the exponent grew enough to hold the magnitude.
The widening ladder
byte
8-bit integer
short
16-bit integer
char
16-bit UTF-16 unit
int
32-bit integer
long
64-bit integer
float
32-bit IEEE 754
double
64-bit IEEE 754
widen = automatic
narrow = explicit cast
Widening runs along the arrows for free: every value of a smaller type fits in the larger. Narrowing goes the other way and needs an explicit cast.
Expressions promote before they compute
Binary arithmetic does not look at the variable on the left. It looks at both operands, promotes them to a common type, and computes in that type. The rules are a fixed ladder: if either operand is double the result is double; otherwise if either is float it is float; otherwise if either is long it is long; otherwise both are promoted to int.
That last clause is the one people forget. byte + byte is an int, char + char is an int, and a short is treated as an int the moment it enters arithmetic. The result then has to fit back into whatever receives it, and that fit is the compiler's business, not arithmetic's.
Narrowing: casts and their two failure modes
Going down the ladder requires an explicit cast, and casts do not check anything. Integer to smaller integer truncates the bit pattern: (byte) 130 wraps to -126 because 130 minus 256 is -126. The same rule scales: (short) 40000 becomes -25536.
Floating point to integer behaves differently. The fractional part is discarded toward zero, so (int) 3.99 is 3 and (int) -3.99 is -3. If the magnitude does not fit, the result clamps to the type's maximum or minimum instead of wrapping, so casting an enormous double to int gives Integer.MAX_VALUE.
And then there is the cast you did not write. Compound assignment hides one: b += 1 compiles for a byte while b = b + 1 does not, because the compound form is defined as b = (byte) (b + 1). The implicit narrowing is why overflow in b += 1 is silent at 127 but a compile error in the explicit form.
Where conversion bites in practice
Three patterns cause most real bugs. Integer division: 1 / 2 is 0 because both operands are ints, so averages need a floating operand (sum / 2.0). Overflow before widening: long total = a * b; does not protect the multiplication; if a and b are ints, the product is computed as an int, wrapped if large, and only the wreckage is widened. Write long total = (long) a * b; or make one operand a long literal. Silent precision loss when storing: assigning a long to a float or a large long to a double compiles, and quietly rounds.
long total = a * b; does not save the multiplication
The assignment target plays no part in how the expression is computed. If a and b are ints, a * b happens in int arithmetic and can overflow before total ever sees it. Cast one operand first: long total = (long) a * b; so the multiplication itself runs at 64 bits.
Interview angles
Is widening always lossless?+
For integral types, yes. int to float and long to float are formally widening but can lose precision above 2^24 and 2^53, because the mantissa is smaller than the integer range. The category name is about the spec's ordering, not an exactness promise.
Explain the hidden cast in compound assignment.+
b += 1 on a byte is specified as b = (byte) (b + 1): the addition promotes to int, and the compound form narrows back without asking. That is why it compiles where b = b + 1 fails, and why it can wrap silently at the type's limit.
Why does 1 / 2 equal 0?+
Both operands are ints, so the division is integer division and the fractional part is discarded. Make either operand floating point (1.0 / 2) or cast before dividing. This is the most common conversion bug in fresh code.
How do casts behave differently for integers and floating point?+
Narrowing integer to smaller integer keeps the low bits and wraps: (byte) 130 is -126. Narrowing floating point to integer truncates toward zero, and out-of-range magnitudes clamp to MIN_VALUE or MAX_VALUE instead of wrapping.