null 是 Java 中的一个类吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2590344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:56:29  来源:igfitidea点击:

Is null a class in java?

javanull

提问by Somerandomeguy

According to, http://www.freshvanilla.org:8080/display/www/Java+Interview+Questions

根据, http://www.freshvanilla.org:8080/display/www/Java+Interview+Questions

Under

在下面

Which class is the superclass of every class?

哪个类是每个类的超类?

nullseems to be the answer.

null似乎是答案。

I found that

我找到

 new Object().getClass().getSuperClass()

verifies the answer as correct. But can nullbe considered a class?

验证答案正确。但是也null算是一个类吗?

I see all primitive data types are represented as Classobjects from java.lang.Class's documentation.

我看到所有原始数据类型都表示为java.lang.Class 文档中的Class对象 。

回答by Mike Daniels

That is a screwy interview question. I am pretty sure the answer they are looking for is Object, not null. nullis not a class. What it means when getSuperClassreturns nullis "there is no superclass". nullis merely a special value that a reference can have. If nullwere a class, you would be able to instantiate objects of type null. It would also not make sense to assign nullto a reference of any other type.

这是一个棘手的面试问题。我很确定他们正在寻找的答案是Object,不是nullnull不是一个类。getSuperClass返回时的意思null是“没有超类”。null只是引用可以具有的特殊值。如果null是一个类,您将能够实例化类型为 的对象null。分配null给任何其他类型的引用也是没有意义的。

回答by Vivart

I think Object class is the answer.

我认为 Object 类就是答案。

from javadoc.

来自 javadoc。

new Object().getClass().getSuperClass()

If this Class represents either the Object class, an interface, a primitive type, or void, then null is returned.it doesn't mean that null is supperclass.

如果此类表示 Object 类、接口、基本类型或 void,则返回 null。这并不意味着 null 是超类。

If null is a class, i think it must be Null. null is not a class.

如果 null 是一个类,我认为它必须是 Null。null 不是一个类。

回答by stacker

In java java.lang.Object is the parent (root) of all other Objects and has no predecessor.

在 java 中 java.lang.Object 是所有其他对象的父(根)并且没有前身。

Null isn't an object at all it indicates that there is no object available (null reference).

Null 根本不是一个对象,它表示没有可用的对象(空引用)。

See also JLS Chapter 4

另见JLS 第 4 章

回答by Shyam

You can't cast the Null object to other primitive types and you don't inherit from it. The top level class in Java is Object.

您不能将 Null 对象强制转换为其他基本类型,也不能从它继承。Java 中的顶级类是 Object。

回答by Dean Harding

Remember that the Classclass is itselfa class. So when you call c.getClass()you're getting back an instanceof the Classclass. So because there is nosuper class of Object, the getSuperClass()method cannot return anything so it returns null.

请记住,Class本身就是一个类。所以,当你打电话c.getClass()你得到回一个实例中的Class类。所以因为没有 的超类Object,该getSuperClass()方法不能返回任何东西,所以它返回null

回答by user207421

Poorly worded question. Only the 'simple' answer is correct as an answer to the question. The others are just confusing.

措辞不好的问题。作为问题的答案,只有“简单”的答案才是正确的。其他的只是一头雾水。

回答by sam

Object is the super class of all the class and null is object references

Object 是所有类的超类,null 是对象引用