为什么 null.asInstanceOf[Int] 不抛出 NullPointerException? [英] why doesn't null.asInstanceOf[Int] throw a NullPointerException?
问题描述
由于 Int不符合"AnyRef,我不确定它为什么不根据 Scala 参考 6.3 节:
Since Int "does not conform to" AnyRef, I am not sure why it doesn't throw a NullPointerException according to Scala Reference on Section 6.3 :
asInstanceOf[T ] 如果 T 符合scala.AnyRef,否则抛出 NullPointerException
asInstanceOf[T ] returns the "null" object itself if T conforms to scala.AnyRef, and throws a NullPointerException otherwise
null.asInstanceOf[Double]
、null.asInstanceOf[Boolean]
、null.asInstanceOf[Char]
也没有.
PS:我的 Scala 库是 2.9.0.1 版和 OS Windows XP
PS: My scala library is of version 2.9.0.1 and OS windows XP
推荐答案
在 Scala 中 Null
是一种类型,它只有一个值 null
.由于 null
是一个值,Scala 中的每个值都是一个对象,因此您可以对其调用方法.
In Scala Null
is a type, which has a single value null
. Since null
is a value and every value in Scala is an object, you can call methods on it.
这篇关于为什么 null.asInstanceOf[Int] 不抛出 NullPointerException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!