Widening, narrowing and promotion
What you will learn
A conversion changes how a value is represented. Some conversions happen automatically, but others require a cast. The type of each operand also determines the type in which Java performs arithmetic. A cast after an operation cannot repair information that has already been lost.
public class Main { public static void main(String[] args) { int units = 70_000; int price = 40_000; long tooLate = units * price; long total = (long) units * price; System.out.println(tooLate); System.out.println(total); }}Expected output:
-14949672962800000000The int multiplication wraps before widening; converting units first makes the multiplication long.
How it works
For ordinary numeric widening, int can become long or double implicitly. Widening is not always exact: large long values cannot all be represented exactly as double. Narrowing from long to int or from double to int normally requires (int) expression. A narrowing integral cast keeps the low bits; floating-point to int truncates toward zero for in-range values, maps NaN to zero and clamps out-of-range values to the int endpoints. A representable compile-time constant can initialize byte, short or char without a cast in assignment contexts. For binary arithmetic, byte, short and char usually promote to int; if an operand is double, float or long, the common numeric type follows that order. Boxing converts primitives to corresponding wrapper references (Integer boxed = 7); unboxing extracts a primitive (int count = boxed). Unboxing null throws NullPointerException. Wrapper-to-wrapper == is reference identity, so do not depend on cached wrapper identity; use value comparison when appropriate.
public class Main { public static void main(String[] args) { byte first = 100; byte second = 30; int sum = first + second; System.out.println(sum); System.out.println((byte) sum); System.out.println((int) -9.8); }}Expected output:
130-126-9Byte addition is performed as int. Casting 130 to byte keeps eight bits, and casting a finite fractional value to int discards its fraction.
Check a boundary or unusual input
public class Main { public static void main(String[] args) { Integer boxed = 42; int count = boxed; long exact = 9_007_199_254_740_993L; double rounded = exact; System.out.println(count + 1); System.out.println((long) rounded); System.out.println((int) Double.NaN); }}Expected output:
4390071992547409920Unboxing supplies an int. The large long rounds during conversion to double. NaN has a defined zero result when narrowed to int.
Common mistakes
Do not add a cast only to silence a compiler error. Ask whether the discarded information is acceptable. long total = (long) (a * b) still performs int multiplication first. byte n = a + b is invalid for nonconstant byte variables because their sum has type int.
Try it yourself
An inventory has 90,000 cartons of 50,000 grams each. Print the correct long mass in grams and the whole-number portion of a measured 12.75 kilograms as an int. Repeat with 0 cartons and -12.75 for the conversion demonstration.
Save each example independently as Main.java. Compile with javac Main.java and run with java Main. Local Java practice only; automatic ByteMind Java grading is not established.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Widening, narrowing and promotion — knowledge check
Apply widening, narrowing and promotion to examples and boundary conditions.
What is the type of the sum of two nonconstant byte variables?
Which expression avoids int overflow before widening the product of int a and b?
What is `(int) -3.9`?
What happens when a null Integer is unboxed to int?
Which widening conversion may lose precision?