null 是一个对象吗?

发布于 2024-08-14 17:26:44 字数 38 浏览 4 评论 0原文

null 是 Java 中的对象吗?

Is null an Object in Java?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

阳光下慵懒的猫 2024-08-21 17:26:44

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

这就是 Java 语言规范< /a> 关于这个话题不得不说:

还有一种特殊的 null 类型,
表达式 null 的类型,其中有
没有名字。因为null类型没有
名称,无法声明
null 类型的变量或要强制转换的变量
到 null 类型。空引用
是唯一可能的值
null 类型的表达式。空值
引用始终可以转换为任何
参考类型。 在实践中,
程序员可以忽略 null 类型

并假装 null 只是一个
可以是任何特殊文字
参考类型。

我认为这可以归结为“null 是特殊的”。

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.

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

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.

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

最偏执的依靠 2024-08-21 17:26:44

根据 Java 规范null 是一种可以分配给对象变量的类型(作为注释中注明的值)。但您无法实例化或创建这种类型的变量,您必须使用编译器提供的文字 null

According to the Java spec, null is a type that can be assigned to an object variable (as a value as noted in the comment). You cannot instantiate or create variables of this type though, you must use the literal null provided by the compiler.

清君侧 2024-08-21 17:26:44

绝对不是:null instanceof Object 返回 false。

Absolutely not: null instanceof Object returns false.

小清晰的声音 2024-08-21 17:26:44

JRL(和其他人)写道:

不,不是,...

通常,这取决于您从哪里看待它,您更相信谁。

根据 JLS 的说法,是的。特别是如果您将问题改写为:“null 文字是 Object 类型吗?”。
除了 JLS 4.1 上面由 Michael Borgwardt 引用:

参见 JLS 3.10.7

空文字始终是空类型。

JLS 4.10

类型 T 的子类型都是 U 类型,因此 T 是 U 的超类型、和 null 类型。

JLS 4.10.2

null 类型的直接超类型都是除 null 类型本身之外的引用类型

[我强调。]

根据 Eclipse 2019-09 的编译器不是

true.toString();  // Cannot invoke toString() on the primitive type boolean
null.toString();  // Cannot invoke toString() on the primitive type null

根据 OpenJDKs 12.0.1 javac 它是

true.toString();  // error: boolean cannot be dereferenced
null.toString();  // error: <null> cannot be dereferenced

角度在哪里括号暗示 null 不是原始类型。并根据 JLS 4.1

Java 编程语言中有两种类型:原始类型 (...) 和引用类型 (...)。

如果不是这个,那就是另一个。


克劳迪乌写道:

null 有点难看。

相反,null 很漂亮。您建议将什么作为引用类型变量的默认值?任意位组合?欢迎来到访问冲突,或者更糟糕的是,指针地狱!


约阿希姆·索尔写道:

null 是一种类型和一个值。

实际上有三个项与null相关(另请参见JLS 3.10.7):(

  1. 否则未命名)null 类型
  2. null 文字
  3. 空引用值。 (通常缩写为null value或简单地null。)

(1) 请注意,根据JLS 4.10.2 上面引用的,null 类型使用多个继承不仅适用于接口,也适用于类。众所周知,这对于我们应用程序员来说是不可能的。

(2) null 文字 可以想象为一个变量,定义如下:

JVM_global Final null_type null = new null_type();

另请注意 JLS 3.9

有时会错误地假定各种字符序列为关键字:

  • null 不是关键字,而是 null 文字 (§3.10.7)。

null instanceof

关于 JLS 4.10.2 的 请记住(“null 类型 是每种类型的子类型”)null instanceof 应该计算为 true ,不应该吗?乍一看,是的,但是 JLS 15.20.2 给出了深刻的答案:

[...] 如果<的值,则instanceof运算符的结果true em>RelationalExpression 不是 null [...]。 否则结果为


[我强调。]

问问自己什么更有意义(从应用程序程序员的角度来看):

  • 给出 false ,从而表明引用表达式不是暴露给我们的类型,即表明它没有引用任何对我们有用的东西

  • 或给出true,从而告知我们知道表达式的计算结果是一个特殊的引用,空引用,引用一个“对象”,我们不知道它是否存在以及它是特殊的空类型它没有名字,不会暴露给我们,但通过空文字,是任何类型的子类型,包括多重继承,并且无论如何都会被忽略?还请考虑更实际的示例:

     类 Car 实现 Vehicle {  
       ...
       车辆car=null;
       ...
       布尔 b = 汽车实例; // 真的?连个例子都没有
       ... // 可以是 Car 类型。
    

这也导致:

为什么 instanceof 不是描述 null 的对象性的正确方法?

它被称为instanceof,而不是sameorsubtypeof。这意味着我们正在将实例的类型与一个类型进行比较,而不是两个类型。现在 null 的意思是:“没有实例”,如果没有实例,则没有实例的类型。很明显,将任何东西与某物进行比较应该会导致错误

或者在“更”现实世界的示例中:

  • 我手里有一张真实大小的苹果图片(=参考类型),上面有“大苹果”(=参考类型名称)写在上面。
  • 我面前有一张桌子(=堆)。
  • 如果桌子上有一个苹果(=实例),则有一根绳子(=参考)连接到它。
  • 我手里拿着这根绳子的另一端(=参考变量)。
  • 我沿着绳子追踪苹果并将其与我的图片进行比较 (=instanceof)。
  • 如果苹果与图片大小相同或更大,则适用“大苹果”文字 (=true)。
  • 如果它更小,则不是 (=false)。
  • 如果桌子上没有苹果(=没有实例),因此不存在绳子(=null),则书写也不适用(=假)。因为:没有苹果是大苹果吗?不,不是。

正如迈克尔总结的那样:“null 确实很特别”。

JRL (and others) wrote:

No it's not, ...

As often, it depends from where you look at it, who you believe more.

According to the JLS, yes, it is. Especially if you rephrase the question to: „Is the null literal of type Object?”.
In addition to JLS 4.1 cited by Michael Borgwardt above:

See JLS 3.10.7:

A null literal is always of the null type.

and JLS 4.10:

The subtypes of a type T are all types U such that T is a supertype of U, and the null type.

or JLS 4.10.2:

The direct supertypes of the null type are all reference types other than the null type itself.

[Emphases by me.]

According to Eclipse 2019-09's compiler it's not:

true.toString();  // Cannot invoke toString() on the primitive type boolean
null.toString();  // Cannot invoke toString() on the primitive type null

According to OpenJDKs 12.0.1 javac it is:

true.toString();  // error: boolean cannot be dereferenced
null.toString();  // error: <null> cannot be dereferenced

Where the angle brackets imply that null is of an other than a primitive type. And according to JLS 4.1:

There are two kinds of types in the Java programming language: primitive types (...) and reference types (...).

if it's not the one it's the other.


Claudiu wrote:

null is kind of ugly.

Au contraire, null is beautiful. What would you suggest as default value for a reference type variable instead? An arbitrary bit combination? Welcome to access violation or, even worse, pointer hell!


Joachim Sauer wrote:

null is a type and a value.

There are actually three items in conjunction with null (see also JLS 3.10.7):

  1. The (otherwise unnamed) null type.
  2. The null literal.
  3. The null reference value. (Commonly abbreviated as null value or simply null.)

(1) Note that, according to JLS 4.10.2 cited above, the null type uses multiple inheritance not only for interfaces but for classes as well. Which we all know is not possible for us application programmers.

(2) The null literal might be imagined as a variable being defined as:

JVM_global final null_type null = new null_type();

Note also JLS 3.9:

A variety of character sequences are sometimes assumed, incorrectly, to be keywords:

  • null is not a keyword, but rather the null literal (§3.10.7).

Concerning null instanceof <any type>

With JLS 4.10.2 in mind („the null type is a subtype of every type”) null instanceof <any type> should be supposed to evaluate to true, shouldn't it? At first sight, yes, but JLS 15.20.2 gives the insight answer:

[...] the result of the instanceof operator is true if the value of the RelationalExpression is not null [...]. Otherwise the result is false.

[Emphases by me.]

Ask yourself what makes more sense (from an application programmer's point of view):

  • Giving false and thus indicating that a reference expression is not of a type exposed to us, i.e. indicating it's not referencing anything useful to us

  • or giving true, thus informing us that the expression evaluates to a special reference, the null reference, referencing an "object" we don't know whether it even exists and which is of the special null type which has no name, is not exposed to us but via the null literal, is a subtype of any type including multiple inheritance and is to be ignored anyway? Consider also the more practical example:

       class Car implements Vehicle {  
       ...
       Vehicle car = null;
       ...
       boolean b = car instanceof Car;  // True? There's not even an instance
       ...                              // which could be of type Car.
    

Which also leads to:

Why is instanceof not a proper way to say something about null's Object-ness?

It's called instanceof not sameorsubtypeof. That means we are comparing an instance's type with a type, not two types. Now null means: „There is no instance” and if there is no instance there's no instance's type. It's obvious that comparing nothing with something is supposed to lead to false.

Or in a "more" real world example:

  • I have a real-size picture of an apple (=reference type) in my hands with »Big Apple« (=reference type name) written on it.
  • There's a table (=heap) in front of me.
  • If there is an apple (=instance) on the table there is a cord (=reference) connected to it.
  • I hold the other end of this cord in my hand (=reference variable) .
  • I trace the apple along the cord and compare it with my picture (=instanceof).
  • If the apple is of the same size or bigger than the picture the writing »Big Apple« applies to it (=true).
  • If it's smaller, then not (=false).
  • If there is no apple on the table (=no instance) and, hence, no cord exists (=null) the writing doesn't apply either (=false). Because: Is no apple a big apple? No, it's not.

As Michael sums up: "null is special" indeed.

べ映画 2024-08-21 17:26:44

Null 是指缺少对象。

Null is the lack of an object.

萌逼全场 2024-08-21 17:26:44

不,它不是一个物体。

No, it is not an object.

往日 2024-08-21 17:26:44

不,它不是类的实例,也不是类的实例。这是对任何事物的引用。

编辑:尚未阅读规范,因此上述内容可能不是 100% 准确。

No, it's not an instance of a Class nor a Class. It's a reference to nothing.

Edit: haven't read the spec so the above may not be 100% accurate.

冷…雨湿花 2024-08-21 17:26:44

正如4.1 类型和值的种类<一章中所述根据 Java 语言规范,null 是一种只有一个值的类型,即 null 引用(由文字 null 表示):

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

不过,您可能想阅读空对象模式(我不推荐)。请参阅 C2 WikiWikipedia 了解有关此模式的更多信息。

As explained in the chapter 4.1 The Kinds of Types and Values of the Java Language Specification, null is a type which has one value, the null reference (and is represented by the literal 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.

You might want to read about the Null Object Pattern (that I don't recommend) though. See the C2 Wiki or Wikipedia for more on this pattern.

城歌 2024-08-21 17:26:44

不,不是一个对象,因为 null instanceof Object 总是返回 false,而且只有一个 null,而不是每个类都有一个。

No, is not an object as null instanceof Object will always return false also there is only one null, not one for each class.

如果没结果 2024-08-21 17:26:44

根据 Java 规范

还有一个特殊的空文字
可以用作任何值
参考类型。可以指定 null
任何变量,除了变量
原始类型。很少有你
可以使用超出的空值
测试其存在。所以,
null 经常在程序中用作
标记来指示某个对象是
不可用。

According to the Java Spec,

There's also a special null literal
that can be used as a value for any
reference type. null may be assigned
to any variable, except variables of
primitive types. There's little you
can do with a null value beyond
testing for its presence. Therefore,
null is often used in programs as a
marker to indicate that some object is
unavailable.

身边 2024-08-21 17:26:44

Java 通过引用来处理对象。 Null 是 Java 面向对象的崩溃,因为它使你低于面向对象的水平。不,它不是一个对象,而是一个引用的值。它与对象范例无关,但与支持对象的 Java 管道有关。

Java handles objects via references. Null is a breakdown of OO-ness of Java, since it drops you below OO level. No it is not an object it is a VALUE of a reference. And it has nothing to do with object paradigms, but relates to plumbing of Java, that enables objects.

栀梦 2024-08-21 17:26:44

null 是 java.lang.Object 的实例吗?不。null

是一个对象吗?取决于“is”的定义。

Is null an instance of java.lang.Object? No.

Is null an object? depends on the definition of "is".

独行侠 2024-08-21 17:26:44
Object foo = null;
System.out.println(foo.toString()); 

第一行显示 null 可以分配给类型 Object,但第二行将证明它肯定不是 Object 并最终导致java.lang.NullPointerException

Object foo = null;
System.out.println(foo.toString()); 

The first line shows null can be assigned to type Object, but the second line will demonstrate it is certainly not an Object and eventually results in a java.lang.NullPointerException

我是男神闪亮亮 2024-08-21 17:26:44

不,null 不是一个对象。它是一个引用类型,其值不引用任何对象,因此内存中没有 null 的表示。

No ,null is not an object.It is a reference type and its value does not refer to any object and so there is no representation of null in memory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文