...
| Code Block |
|---|
int a = some_value;
char b = some_character;
if( (a + b) == 0.0f){
//do something
}
|
this time, b is first converted to int, then, the + operator is applied. The result of (a+b) is then converted to float, and the comparison operator is finally applied.
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;
System.out.println( (big * one) == big_float);
}
}
|
The output is false. big is first converted to a float, and loses some precision. The result of the multiplication is then converted to double, and compared to big_float, which hasn't lost precision, since it's a wider type than float. The two operands are therefore different, and the comparison will return false.
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 termination.
...