java - 理解Java中null和 '\u000'的区别

标签 java null char

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

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

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

最佳答案

语言规范是定义 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. --Link to documentation (Section 4.1)

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 引用 本身 - 只是另一个指针。空引用的值并不真正相关,可能取决于实现者,但可能是零或其他不能与另一个对象地址混淆的值。

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

关于java - 理解Java中null和 '\u000'的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12195628/

相关文章:

java - 如何在 Spring Security 中使用内置的 X-Auth-Token 而不是 Spring Session Http Cookie

java - 分配的最终对象字段在构造函数中可能仍然为 null 是真的吗?

计算二维网格中的字符数 C

c - 为什么分配 `struct` 会出错?

java - 在实例化的 boolean 数组上获取 NPE

java - 使用 Graph API 从 azure AD 获取用户属性

java - Java 填充数组时如何检测 BufferedInputStream 是否结束?

f# - 选项类型和可为空类型之间有什么区别?

typescript - 如何将空检查拉入 typescript 中的功能

c++如何运行其内容存储在char数组中的.exe文件?