我在一本书中读到(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/