简体   繁体   English

如果Scala的==调用等于,那么为什么不抛出异常?

[英]If Scala's == calls equals, why doesn't this throw an exception?

scala> val x:String = null
x: String = null

scala> x == "Hey"
res0: Boolean = false

This should translate to x.equals("Hey")? 这应该转换为x.equals(“ Hey”)?

As per the SLS: 根据SLS:

6.3 The Null Value 6.3空值

The null value is of type scala.Null, and is thus compatible with every reference type. null值的类型为scala.Null,因此与每种引用类型都兼容。 It denotes a reference value which refers to a special “null” object. 它表示参考值,该参考值引用特殊的“空”对象。 This object implements methods in class scala.AnyRef as follows: 该对象实现类scala.AnyRef中的方法,如下所示:

• eq(x ) and ==(x ) return true iff the argument x is also the “null” object. •如果参数x也是“空”对象,则eq(x)和==(x)返回true。

• ne(x ) and !=(x ) return true iff the argument x is not also the “null” object. •如果参数x也不是“ null”对象,则ne(x)和!=(x)返回true。

• isInstanceOf[T ] always returns false. •isInstanceOf [T]始终返回false。

• asInstanceOf[T ] returns the default value (see §4.2) of type T . •asInstanceOf [T]返回类型T的默认值(请参见§4.2)。

• ## returns “0”. •##返回“ 0”。 A reference to any other member of the “null” object causes a NullPointerException to be thrown. 对“ null”对象的任何其他成员的引用将引发NullPointerException。

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

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