...
There are 22 possible narrowing primitive conversions in Java. According to the JLS, §5.1.3, "Narrowing Primitive Conversions" [JLS 2005]:
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 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.
...
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 |
VOID INT31-CPP. Ensure that integer conversions do not result in lost or misinterpreted data | |
| VOID FLP34-CPP. Ensure that floating point conversions are within range of the new type |
Numeric Conversion Errors [FLC] | |
CWE-681. Incorrect conversion between numeric types | |
| CWE-197. Numeric truncation error |
Bibliography
...