问题
I have found quote from jls:
The equality operators may be used to compare two operands that are convertible (§5.1.8) to numeric type, or two operands of type boolean or Boolean, or two operands that are each of either reference type or the null type. All other cases result in a compile-time error.
But this code
String str= "";
Number num = 1;
System.out.println(str == num);
every operand is reference!
said that it is incompatible types.
Where did in jls say that these types should be compatible ?
回答1:
In section 15.21.3 (Reference Equality Operators == and !=):
It is a compile-time error if it is impossible to convert the type of either operand to the type of the other by a casting conversion (§5.5). The run-time values of the two operands would necessarily be unequal.
来源:https://stackoverflow.com/questions/23805024/when-can-i-use-operator