...
There are 22 possible narrowing primitive conversions in Java. According to the Java Language Specification, §5§5.1.3, "Narrowing Primitive Conversions" ,
...
Integer type ranges are defined by the JLS, §4§4.2.1, "Integral Types and Values", [JLS 2005] and are also described in rule NUM00-J. Detect or prevent integer overflow.
...
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, §5§5.1.3, "Narrowing Primitive Conversions" for more information.
...
Narrower primitive types can be cast to wider types without affecting the magnitude of numeric values. See the JLS, §5§5.1.2, Widening Primitive Conversion" for more information. Conversion from int
or long
to float
, or long
to double
can lead to loss of precision (loss of least significant bits). No runtime exception occurs despite this loss.
...
INT31-C. Ensure that integer conversions do not result in lost or misinterpreted data | ||||
| FLP34-C. Ensure that floating point conversions are within range of the new type | |||
INT31-CPP. Ensure that integer conversions do not result in lost or misinterpreted data | ||||
| FLP34-CPP. Ensure that floating point conversions are within range of the new type | |||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="29cab801869f73f9-c3a66578-46314655-98c6b9e9-87488ae3e86cc3f44577be1d"><ac:plain-text-body><![CDATA[ | [ISO/IEC TR 24772:2010 | http://www.aitcnet.org/isai/] | "Numeric Conversion Errors [FLC]" | ]]></ac:plain-text-body></ac:structured-macro> |
CWE ID 681, "Incorrect Conversion between Numeric Types" | ||||
| CWE ID 197, "Numeric Truncation Error" |
Bibliography
...
NUM11-J. Do not rely on the default string representation of floating-point values 03. Numeric Types and Operations (NUM) NUM13-J. Convert Beware of precision loss when converting primitive integers to floating-point for floating-point operations