为什么 null.asInstanceOf[Int] 不抛出 NullPointerException? [英] why doesn't null.asInstanceOf[Int] throw a NullPointerException?

查看:56
本文介绍了为什么 null.asInstanceOf[Int] 不抛出 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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆