简体   繁体   English

在哪里可以找到“==”等于操作数的 Java 文档?

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

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.所以,我相信这应该打印出错误,但我有一个错误,无与伦比的类型:java.lang.Integer 和 java.lang.Double。 When comparing addresses, why would type matter?比较地址时,为什么类型很重要? It's not the same address, so print false.不是同一个地址,所以打印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) );

The place you find this defined is the specification , which says:你发现这个定义的地方是规范,它说:

15.21.3. 15.21.3. Reference Equality Operators == and !=引用相等运算符==!=

If the operands of an equality operator are both of either reference type or the null type, then the operation is object equality.如果相等运算符的操作数都是引用类型或空类型,则该操作是对象相等。

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).如果无法通过强制转换(第 5.5 节)将任一操作数的类型转换为另一个操作数的类型,则会出现编译时错误。 The run-time values of the two operands would necessarily be unequal (ignoring the case where both values are null).两个操作数的运行时值必然不相等(忽略两个值都为空的情况)。

At run time, the result of == is true if the operand values are both null or both refer to the same object or array;在运行时,如果操作数值都为 null 或都指向同一个对象或数组,则==的结果为真; otherwise, the result is false.否则,结果为假。

The result of != is false if the operand values are both null or both refer to the same object or array;如果操作数值都为 null 或都指向同一个对象或数组,则!=的结果为 false; otherwise, the result is true.否则,结果为真。

While == may be used to compare references of type String, such an equality test determines whether or not the two operands refer to the same String object.虽然==可用于比较 String 类型的引用,但此类相等性测试确定两个操作数是否引用同一个 String 对象。 The result is false if the operands are distinct String objects, even if they contain the same sequence of characters (§3.10.5).如果操作数是不同的 String 对象,则结果为 false,即使它们包含相同的字符序列(第 3.10.5 节)。 The contents of two strings s and t can be tested for equality by the method invocation s.equals(t).可以通过方法调用 s.equals(t) 测试两个字符串 s 和 t 的内容是否相等。

(their emphasis) (他们的重点)

Equality of objects is about more than where they are in memory.对象的相等性不仅仅是它们在内存中的位置。 Comparing one type of object to another type of object is never going to yield a true result (see their emphasized part), so rather than have an == that will never be true, the compiler tells you that it's an error.将一种类型的对象与另一种类型的对象进行比较永远不会产生真正的结果(请参阅他们强调的部分),因此编译器会告诉您这是一个错误,而不是永远不会为真的==

The compiler does this with other errors that can be detected at compilation time, such as code you can't reach.编译器会使用其他可以在编译时检测到的错误来执行此操作,例如您无法访问的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM