我是 JAVA 新手,正在使用相等运算符。在尝试了几种操作数组合之后,我对 Java 中相等运算符(==)的兼容操作数的确切定义有点困惑。
int x = 23;
if (x == 23.3f) { // compiler accepts it. (may be because both are primitives)
int x = 23;
Double d = new Double(23.3);
if (x == d) { // compiler accepts it. (may be compatible pair of primitive and object reference)
int x = 23;
String s = "hello";
if (x == s) // compiler throws error - incompatible operands for == operator.
所以,在我看来,相等运算符的兼容操作数应该有一些正式的定义。请帮助我澄清这个疑问。