Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

There are 22 possible narrowing primitive conversions in Java. According to the JLS, §5.1.3, "Narrowing Primitive Conversions" [JLS 2005]:

  • short to byte or char
  • char to byte or short
  • int to byte, short, or char
  • long to byte, short, char, or int
  • float to byte, short, char, int, or long
  • double to byte, short, char, int, long, or float

...

Integer type ranges are defined by the JLS, §4.2.1, "Integral Types and Values" [JLS 2005], and are also described in rule NUM00-J. Detect or prevent integer overflow.

...

See the JLS, §5.1.3, "Narrowing Primitive Conversions,"[JLS 2005] for more information.

Other Conversions

Narrower primitive types can be cast to wider types without affecting the magnitude of numeric values. See the JLS, §5.1.2, Widening Primitive Conversion" [JLS 2005], for more information. Conversion from int or long to float or from long to double can lead to loss of precision (loss of least significant bits). No runtime exception occurs despite this loss.

...

Bibliography

...