The JLS (§15.21.1) says:
If the operands of an equality operator are both of numeric type, or one is of numeric type and the other is convertible (§5.1.8) to numeric type, binary numeric promotion is performed on the operands (§5.6.2).
Note that binary numeric promotion performs value set conversion (§5.1.13) and may perform unboxing conversion (§5.1.8).
So the problem you describe occurs only if both operands are boxed types (Double
, Integer
, etc.). Whether the operand type is boxed or not depends on how the operand is declared. If it's a variable, field or parameter, it depends on the type used to declare the name; if it's a method call, it depends on the return type used when the method is declared; if it's the result of some other operation like +
, it should already be unboxed; if a typecast has been applied, the cast will tell you what the type is.
As others have said, you usually shouldn't use ==
to compare doubles or floats due to rounding errors.