Why does 5.0 == 5L return me a true?

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!

Jon Skeet
people
quotationmark

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 to double.
    • ...
  • ...

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.

people

See more on this question at Stackoverflow