Type Casting
Sometimes a value of one type is needed as another: an int in a calculation with a double, or a double stored in an int. Java converts automatically when nothing can be lost, and requires an explicit cast when information might be.
Widening: automatic
void main() { int items = 7; long bigItems = items; double precise = items; System.out.println(bigItems); System.out.println(precise); }
7 7.0
Converting to a larger type is safe, so Java does it automatically: byte → short → int → long → float → double.
Narrowing: explicit cast
void main() { double price = 9.99; int whole = (int) price; System.out.println(whole); }
9
Going to a smaller type may lose information, so you must write the target type in parentheses. Casting a double to int truncates — it drops the fraction, it doesn't round. Without the cast it's a compile error:
void main() { double price = 9.99; int whole = price; }
Main.java:3: error: incompatible types: possible lossy conversion from double to int
Rounding instead of truncating
void main() { double value = 9.99; System.out.println((int) value); System.out.println(Math.round(value)); System.out.println((int) -9.99); System.out.println(Math.round(-9.5)); }
9 10 -9 -9
Math.round rounds to the nearest whole number (halves round up, towards positive infinity) and returns a long.
Casting can overflow
void main() { int big = 300; byte small = (byte) big; System.out.println(small); }
44
300 doesn't fit in a byte, so only the lowest 8 bits are kept. A cast tells the compiler "I know what I'm doing" — make sure you do.
Integer division
void main() { int a = 7, b = 2; System.out.println(a / b); System.out.println((double) a / b); System.out.println(a / (double) b); System.out.println((double) (a / b)); }
3 3.5 3.5 3.0
Dividing two ints gives an int. Cast one operand before dividing to get a decimal result; casting the result afterwards is too late.
Numbers and text
void main() { String text = "42"; int number = Integer.parseInt(text); double d = Double.parseDouble("3.75"); String back = String.valueOf(number + 1); String also = "" + d; System.out.println(number * 2); System.out.println(d); System.out.println(back + also); }
84 3.75 433.75
Casting can't turn text into a number. Use Integer.parseInt, Double.parseDouble and friends; go the other way with String.valueOf or by concatenating with a string. Parsing invalid text throws a NumberFormatException:
void main() { int n = Integer.parseInt("forty-two"); }
Exception in thread "main" java.lang.NumberFormatException: For input string: "forty-two"
at Main.main(Main.java:2)Promotion in expressions
void main() { byte x = 10, y = 20; int sum = x + y; char c = 'A'; System.out.println(sum); System.out.println(c + 1); System.out.println((char) (c + 1)); }
30 66 B
In arithmetic, byte, short and char are promoted to int, and if either operand is long, float or double the result takes the larger type.
Try it yourself
Calculate an average score as a decimal, then show it rounded and truncated.
Average: 85.66666666666667 Rounded: 86 Truncated: 85