理解 Java 中 null 和 'u000' 的区别

2022-01-12 00:00:00 null char java

我在一本书中读到(Bruce Eckel 的 Thinking in Java,第 4 版,第 47 页) null 等于 'u000'代码>.然后我想知道 'u000' 到底是什么意思.

I read in a book (Thinking in Java by Bruce Eckel, 4th edition, page 47) that null is equal to 'u000'. And then I was wondering what exactly does 'u000' really mean.

根据我的理解,null 是什么都没有或没有任何东西.而 'u000' 与这个定义相矛盾.

As per my understanding null was nothing or absence of anything. And 'u000' comes in contradiction with this definition.

谁能澄清这个关于 null'u000' 的问题?

Can anyone clarify this issue about null and 'u000'?

推荐答案

语言规范是定义null的地方,它说

The language specification is where null is defined, and it says

还有一种特殊的 null 类型,即表达式 null 的类型,它没有名称.因为 null 类型没有名字,所以不可能声明一个 null 类型的变量或强制转换为 null 类型.空引用是空类型表达式的唯一可能值.空引用始终可以转换为任何引用类型.在实践中,程序员可以忽略 null 类型,只是假装 null 只是一个可以是任何引用类型的特殊文字.--文档链接(第 4.1 节)

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. --Link to documentation (Section 4.1)

null 类型有一个值,即 null 引用,由文字 null 表示,它由 ASCII 字符组成.空文字始终是空类型.--文档链接(第 2.3 节)

The null type has one value, the null reference, represented by the literal null, which is formed from ASCII characters. A null literal is always of the null type. --Link to documentation (Section 2.3)

听起来像是一个循环的定义,但 null 的值是 null 引用 本身 - 只是另一个指针.空引用的值并不真正相关,可能取决于实现者,但可能是零或其他不能与另一个对象地址混淆的值.

Rather a circular sounding definition, but the value of null is the null reference itself - just another pointer. The value of the null reference isn't really relevant and is presumably up to the implementor, but zero or some other value that can't be confused with another object address is likely.

这里可能会引起混淆,因为有一个名为空字符的字符值,其值为u0000.这是 char 类型的默认值.

Confusion may be caused here because there is a character value called the null character with value u0000. This is the default value for type char.

相关文章