問題描述
我在一本書中讀到(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.
這篇關于理解 Java 中 null 和 'u000' 的區別的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!