由于 Int “不符合”AnyRef,我不确定为什么它不会根据 Scala Reference 抛出 NullPointerException关于第 6.3 节:
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,操作系统为windows XP
最佳答案
在 Scala 中,Null
是一种类型,它具有单个值 null
。由于 null
是一个值,并且 Scala 中的每个值都是一个对象,因此您可以调用它的方法。
关于scala - 为什么 null.asInstanceOf[Int] 不抛出 NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8285916/