System.out.println(" answer is " + (5.0==5L));
This returns true
!
It should return a false
value because two different types are being compared.
Even though the double
is compared to a long
value!
The two operands are going through binary numeric promotion as per JLS section 5.6.2 in order to get to a single type for both operands.
The rules are like this:
- If any operand is of a reference type, it is subjected to unboxing conversion (§5.1.8).
- 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 todouble
.- ...
- ...
Your second operand is of type double
, so the long
value is implicitly converted to double
, then the two double
values are compared - and they're equal.
See more on this question at Stackoverflow