We When using binary operators with mixed operand sizes, be aware that some of the narrower operands may be promoted to a wider type, to match the type of the other operand. For example in the expression 'a' == 42, the field 'a' will be promoted to an int before the comparison is carried out.When using binary operators with operands of different types, be aware of the implicit casts.
In particular (section 5.6.2, "Binary Numeric Promotion", from the java specification):
| Wiki Markup |
|---|
The Java Language Specification \[[JLS 05|AA. Java References#JLS 05]\] section 5.6 "Numeric Promotions" describes numeric promotion as: |
- If any of the operands is of a reference type, unboxing conversion is performed. Then:
- If either operand is of type double, the other is converted to double.
- Otherwise, if either operand is of type float, the other is converted to float.
- Otherwise, if either operand is of type long, the other is converted to long.
- Otherwise, both operands are converted to type int.
...
Widening conversions resulting from integer promotions preserve the overall magnitude of the number. However, promotions in which the operands are converted from a numeric type such as an integer to
...
a float or a long to a double, are particularly pernicious (see the INT33-J rule for more details regarding this issue)
...
. These implicit casts can lead to an undesirable loss in precision.
These conversions can happen with the following operators : multiplicative operators (%, *, /), additive opeators operators (+, -) comparisons , comparison operators (<, >, <=, >=) and equality (==, !=) operators and the integer bitwise operators (&, |, ^) and can be particularly harmful in the case of a conversion from an integral type to a floating point type (see INT33-J for more details), due to widening conversion.
...
In the following example, a is promoted to a double before the + operator is applied.
| Code Block |
|---|
int a = some_value;
double b = some_other_value;
double c = a + b;
|
a is converted to double before the + operator is applied.
A more complex example As another example, consider:
| Code Block |
|---|
int a = some_value; char b = some_character; if( (a + b) == 0.0f) { //do something } |
this timeHere, b is first converted to int, then, so that the + operator is appliedcan be applied to operands of the same type. The result of (a+b) is then converted to a float, and the comparison operator is finally applied.
Note : these code are just examples to illustrate how integer promotions work, their compliant
Non-compliant code example
Noncompliant Code Example
Here is an example of what could happen :.
| Code Block | ||
|---|---|---|
| ||
class Test{
public static void main(String[] args){
int big = 1999999999;
double big_float = big;
float one = 1.0f;
System.out.println(big*one);
System.out.println(big_float);
}
}
|
...
Note : these output were generated on a Java Hotspot(TM) 64-bit server VM (version 1.5.0), and compiled with javac, version 1.5.0.
Risk assessment
If an operator is applied and some unexpected conversion occur, the result may be different from what the programmer and lead to some unexpected behavior and ultimately to a flaw or an abnormal terminationFailing to consider integer promotions while dealing with floating point and integer operands together, can result in loss of precision.
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
|---|---|---|---|---|---|
EXP08-J | low | probable | medium | P4 | L3 |
References
| Wiki Markup |
|---|
\[[JLS 05|AA. Java References#JLS 05]\] 5.6 "Numeric Promotions" |