Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: temp save

Conversions of numeric types to narrower types can result in lost or misinterpreted data if the value of the wider type is outside the range of values of the narrower type.

Every primitive numeric type in Java has a limited range that is strictly defined by the [JLS 2005]. There are 22 possible narrowing primitive conversions in Java. According to the JLS, Section 5.1.3, "Narrowing Primitive Conversions"

...

Integer type ranges are defined by the Java Language Specification, Section 4.2.1, "Integral Types and Values," [JLS 2005] and are also described in NUM00-J. Ensure that integer operations do not result in overflow.

...

When integers are cast to narrow data types, the magnitude of the numeric value and the corresponding sign can be affected. Consequently, data can be lost or misinterpreted.

Floating-point

...

to Integer Conversion

Floating-point conversion to an integral type T is a two step procedure:

1. When converting a floating-point value to an int or long and the value is a NaN, an int or long, a zero value is produced. Otherwise, if the value is not infinity, it is rounded towards zero to an integer value V:

...