在Scala中,“ null == last”和“ null eq last”有什么区别? [英] What's the difference between `null == last` and `null eq last`, in Scala?
问题描述
我在scala 2.7.7的内置类 MessageQueue.scala
中看到,在第164行附近:
I see in the built-in class MessageQueue.scala
of scala 2.7.7, around line 164, it's:
def extractFirst(p: Any => Boolean): MessageQueueElement = {
changeSize(-1) // assume size decreases by 1
val msg = if (null eq last) null
else {
...
}
}
我不明白 val msg = if(null eq last)null
很好,为什么它使用 eq
,但不是 null
。如果我写 if(last == null)null
,是否正确?有什么区别吗?
I don't understand val msg = if (null eq last) null
well, why it uses eq
, but not null
. If I write if (last==null) null
, is it correct? Is there any difference?
推荐答案
当 == 的任何一侧时是
null
或,如果的第一个操作数==
计算为为空,那么Scala将不会调用等于
。因此,在这种情况下,是的, x == null
与 x eq null
相同; 不会被调用 equals
方法。请注意以下情况。
When either side of the ==
is null
or if the first operand of ==
evaluates to null, then Scala will not invoke equals
. So, in this case, yes, x == null
is the same as x eq null
; the equals
method is not invoked. Pay attention to the cases below.
请考虑以下问题:
class X {
// this is just for testing
// any equals that returns true when the other object is null
// is arguably broken. thus even though it may be invoked
// the end semantics should remain the same
override def equals(x: Any) = true
}
var x = new X()
x == null // false -- compiler optimization?
null == x // false
var y = null
y == x // false -- see documentation below, y is null, x is not
x == y // true -- x does not evaluate to null, equals invokes
x eq y // false
并注意:
(new X()) == null
导致警告说新鲜物体将永远不等于(为空)。
Results in a warning saying a "a fresh object" will never be equal (to null).
我怀疑 x == y
发出的代码可能比 x == null
发出的代码略多/不同
I suspect there may be slightly more/different code emitted for x == y
than x == null
(in case the equals must be invoked), but have not checked.
快乐编码。
Scala语言规范的6.3节(空值)说:
Section 6.3 (The Null Value) of the Scala Language Specification has this to say:
null值的类型为scala.Null,因此与每个引用
类型都兼容。它表示参考值,该参考值引用特殊的空对象。此对象
在scala.AnyRef类中实现方法,如下所示:
The null value is of type scala.Null, and is thus compatible with every reference type. It denotes a reference value which refers to a special "null" object. This object implements methods in class scala.AnyRef as follows:
- [null] eq(x)和[ null] ==(x)如果参数x也是空对象,则返回true。
- ne(x)和!=(x)当true时返回true参数x也不是 null对象。
- isInstanceOf [T]始终返回false。
- asInstanceOf [T]返回 null如果T符合
scala.AnyRef,则对象本身,否则抛出NullPointerException。
- [null] eq( x ) and [null] ==( x ) return true iff the argument x is also the "null" object.
- ne( x ) and !=( x ) return true iff the argument x is not also the "null" object.
- isInstanceOf[T ] always returns false.
- asInstanceOf[T ] returns the "null" object itself if T conforms to scala.AnyRef, and throws a NullPointerException otherwise.
对任何其他成员的引用 null对象将引发
NullPointerException。
A reference to any other member of the "null" object causes a NullPointerException to be thrown.
这篇关于在Scala中,“ null == last”和“ null eq last”有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!