null 是一个对象吗? [英] Is null an Object?

查看:23
本文介绍了null 是一个对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

null 是 Java 中的 Object 吗?

Is null an Object in Java?

推荐答案

如果 null 是一个 Object,它将支持 java.lang.Object 的方法,例如 equals().但是,情况并非如此 - 对 null 的任何方法调用都会导致 NullPointerException.

If null were an Object, it would support the methods of java.lang.Object such as equals(). However, this is not the case - any method invocation on a null results in a NullPointerException.

这就是 Java语言规范不得不说这个话题:

And this is what the Java Language Specification has to say on this topic:

还有一个特殊的空类型,表达式 null 的类型,它具有无名.因为空类型没有名称,无法声明null 类型的变量或要强制转换到空类型.空引用是唯一可能的值null 类型的表达式.空引用总是可以转换为任何引用类型.在实践中,程序员可以忽略空类型并假装 null 只是一个可以是任何特殊文字引用类型.

There is also a special null type, the type of the expression null, which has no name. Because the null type has no name, it is impossible to declare a variable of the null type or to cast to the null type. The null reference is the only possible value of an expression of null type. The null reference can always be cast to any reference type. In practice, the programmer can ignore the null type and just pretend that null is merely a special literal that can be of any reference type.

我认为这可以归结为null 是特殊的".

I think this can be boiled down to "null is special".

这篇关于null 是一个对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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