...
There are 22 possible narrowing primitive conversions in Java. According to the JLS, Section 5 Java Language Specification, §5.1.3, "Narrowing Primitive Conversions" ,
short
tobyte
orchar
char
tobyte
orshort
int
tobyte
,short
, orchar
long
tobyte
,short
,char
, orint
float
tobyte
,short
,char
,int
, orlong
double
tobyte
,short
,char
,int
,long
, orfloat
...
Integer type ranges are defined by the Java Language Specification, Section 4JLS, §4.2.1, "Integral Types and Values", [JLS 2005] and are also described in guideline "NUM00-J. Detect or prevent integer overflow."
The table below presents the rules for narrowing primitive conversions of integer types. In the table, for an integer type T
, n
represents the number of bits used to represent the resulting type T
(precision).
...
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
:
- if If
T
islong
, andV
can be represented as along
, thelong
valueV
is produced,. - or if If
V
can be represented as anint
, then theint
valueV
is produced.
Otherwise:
- the The value is negative infinity or a value too negative to be represented, and
Integer.MIN_VALUE
orLong.MIN_VALUE
is produced,. - or the The value is positive infinity or a value too positive to be represented,
Integer.MAX_VALUE
orLong.MAX_VALUE
is produced.
2. If T
is byte
, char
, or short
, the result of the conversion is the result of a narrowing conversion to type T
of the result of the first step
See the JLS, Section 5§5.1.3, "Narrowing Primitive Conversions" for more information.
Other Conversions
Narrower primitive types may can be cast to wider types without affecting the magnitude of numeric values. See the JLS, Section 5§5.1.2, " Widening Primitive Conversion" for more information. Conversion from int
or long
to float
, or long
to double
may can lead to loss of precision (loss of least significant bits). No runtime exception occurs despite this loss.
Note that conversions from float
to double
or double
to float
can also lose information about the overall magnitude of the converted value. See guideline "NUM09-J. Use the strictfp modifier for floating point calculation consistency across platforms" for additional information.
...
Code Block | ||
---|---|---|
| ||
class CastAway { public static void workWith(int i) //check if i is within byte range if ((i < Byte.MIN_VALUE) || (i > Byte.MAX_VALUE)) { throw new ArithmeticException("Value is out of range"); } byte b = (byte) i; // work with b } } |
Noncompliant Code Example (Floating-
...
Point Conversion to Integer)
The narrowing primitive conversions in this noncompliant code example suffers suffer from loss in the magnitude of the numeric value, as well as a loss of precision.
...
The minimum and maximum float
values are converted to minimum and maximum int
values (0x80000000
and 0x7fffffff
, respectively) respectively. The resulting short
values are the lower 16 bits of these values (0x0000
and 0xffff
). The resulting final values (0 and -1) might could be unexpected.
Compliant Solution (Floating-
...
Point to Integer Conversion)
This compliant solution range checks both the i
and j
variables before converting to the resulting integer type.
...
Code Block | ||
---|---|---|
| ||
double i = Double.MIN_VALUE; double j = Double.MAX_VALUE; if ((i < Float.MIN_VALUE) || (i > Float.MAX_VALUE) || (j < Float.MIN_VALUE) || (j > Float.MAX_VALUE)) { throw new ArithmeticException ("Value is out of range"); } float b = (float) i; float c = (float) j; //other operations |
Exceptions
NUM15-EX0EX1: Java's narrowing conversions are both well-defined and portable; knowledgeable programmers can intentionally apply such conversions in contexts where their output is both expected and reasonable. Consequently, narrowing conversions are permitted when the code contains comments that document both the use of narrowing conversions and that the potential for truncation has been anticipated. A suitable comment might read: "// Deliberate narrowing cast of i; possible truncation OK"
...
Casting a numeric value to a narrower type can result in information loss related to the sign and magnitude of the numeric value. ConsequentlyAs a result, data can be misrepresented or interpreted incorrectly.
...
C Secure Coding Standard: "INT31-C. Ensure that integer conversions do not result in lost or misinterpreted data, "
C Secure Coding Standard: "FLP34-C. Ensure that floating point conversions are within range of the new type"
C++ Secure Coding Standard: "INT31-CPP. Ensure that integer conversions do not result in lost or misinterpreted data, "
C++ Secure Coding Standard: "FLP34-CPP. Ensure that floating point conversions are within range of the new type"
MITRE CWE: CWE-681 "Incorrect Conversion between Numeric Types,"
MITRE CWE: CWE-197 "Numeric Truncation Error"
Bibliography
...
...
NUM14-J. Do not rely on the default string representation of floating-point values 03. Numeric Types and Operations (NUM) NUM16-J. Convert integers to floating point for floating point operations