class Test {
public static void main(String[] args) {
int i=1;
double d=1.0;
if(i==d)
System.out.println("True") // whether any type casting happens.
else
System.out.println("false");
}
}
Asked
Active
Viewed 45 times
2

Eran
- 387,369
- 54
- 702
- 768
-
https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html see 5.1.2, widening conversions – pvg Dec 20 '15 at 09:08
1 Answers
1
Here's a relevant quote from the JLS :
When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order:
...
2. Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:- If either operand is of type double, the other is converted to double.
In your case i
is converted to double
in order to perform the comparison.

Eran
- 387,369
- 54
- 702
- 768