Java Reference ยท Types and Literals
Type conversions and casting
Reference for Java type conversions: widening and narrowing of primitives, numeric promotion in expressions, reference casts and ClassCastException.
Java converts between types in three situations: automatically when no information is lost (widening), explicitly with a cast when it might be (narrowing), and implicitly in expressions where operands are promoted to a common type. Reference types follow a similar split: an upcast to a supertype is implicit, a downcast needs an explicit cast and is checked at run time.
For primitives, widening goes in this order: byte to short to int to long to float to double, and char to int. Note that long to float is allowed even though it can lose low-order digits.
double d = 5; // widening, implicit int i = (int) 3.9; // narrowing, explicit cast: 3 Object o = "text"; String s = (String) o; // reference downcast
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
targetType variable = (targetType) expression;
| Conversion | Example | Notes |
|---|---|---|
| Widening primitive | long l = intValue; |
Implicit; int/long to float and long to double can lose precision. |
| Narrowing primitive | (byte) intValue |
Integers keep the low bits; floating point truncates and saturates, NaN becomes 0. |
| Constant narrowing | byte b = 10; |
Implicit when the compile-time constant fits. |
| Upcast | Object o = "x"; |
Always safe, implicit. |
| Downcast | (String) o |
Checked at run time; ClassCastException on mismatch. |
| Boxing / unboxing | Integer i = 5; |
Implicit; see autoboxing. |
| String conversion | "" + 5 |
+ with a String converts the other operand. |
Usage notes
- Promotion rules in arithmetic: operands narrower than
intbecomeint; then if either isdoublethe result isdouble, elsefloat, elselong, elseint. - Cast the operand, not the result.
(double) (a / b)divides as integers first. Write(double) a / b. - Casts have high precedence:
(int) x + ycasts onlyx. Parenthesize:(int) (x + y). - No cast between boolean and numbers, and none between unrelated classes (the compiler rejects
(String) someInteger). - Parsing is not casting. To turn text into numbers use
Integer.parseInt(); to turn numbers into text useString.valueOf(). - Pattern matching
instanceof(Java 16) combines the test and the cast and avoids mostClassCastExceptions. - Generics: casts to a parameterized type such as
(List<String>) objare unchecked and generate warnings because of type erasure. charandshortare not assignable to each other without a cast, because their ranges differ in sign.
Related
- Autoboxing
- Cast operator
- instanceof
- int
- Tutorial: Type casting