Where can I find the Java documentation for "==" equals operand?

flo*_*cca -1 java

I believe "==" is for reference comparison (address comparison). So, I believe this should print out false, but I have an error, Incomparable types: java.lang.Integer and java.lang.Double. When comparing addresses, why would type matter? It's not the same address, so print false. Again, I'm just looking for the documentation. It's a hypothetical question.

Integer seven = 7;
Double sevenPointTwo = 7.2;
System.out.println("seven == sevenPointTwo is " + (seven == sevenPointTwo) );
Run Code Online (Sandbox Code Playgroud)

T.J*_*der 5

您找到定义的地方是规范,它说:

15.21.3。参考平等运营商==!=

如果相等运算符的操作数均为引用类型或null类型,则该操作为对象相等。

如果无法通过强制转换将任何一个操作数的类型转换为另一个操作数的类型,则将发生编译时错误(第5.5节)。两个操作数的运行时值必然不相等(忽略两个值均为null的情况)。

在运行时,==如果操作数值都为null或都引用相同的对象或数组,则结果为true;否则,结果为true。否则,结果为假。

!=如果操作数值都为null或都引用相同的对象或数组,则结果为false;否则,结果为false。否则,结果为true。

尽管==可以用来比较String类型的引用,但是这种相等性测试确定两个操作数是否引用相同的String对象。如果操作数是不同的String对象,则结果为false,即使它们包含相同的字符序列(第3.10.5节)。可以通过方法s.equals(t)来测试两个字符串s和t的内容是否相等。

(重点)

对象的平等远不止于它们在内存中的位置。将一种类型的对象与另一种类型的对象进行比较永远不会产生真实的结果(请参见其重点部分),因此==编译器会告诉您这是错误,而不是具有永远不会成立的结果。

编译器使用其他在编译时可以检测到的错误来执行此操作,例如您无法访问的代码。