Understanding the difference between null and '\u000' in Java

后端 未结 4 1787
别那么骄傲
别那么骄傲 2020-12-02 13:36

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 e

相关标签:
4条回答
  • 2020-12-02 13:38

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

    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)

    and

    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)

    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.

    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.

    0 讨论(0)
  • 2020-12-02 13:43

    \u000 would be the unicode representation of a character with ASCII code 0, but has nothing to do with null as used by Java.

    0 讨论(0)
  • 2020-12-02 13:44

    '\u0000' decimal equivalent is 00 Char equivalent is NUL.

    From Character in Javadoc: http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Character.html

    static char MIN_VALUE 
          The constant value of this field is the smallest value of type char, '\u0000'. 
    

    This is the unicode value, that in CHAR is equivalent to NULL, ie it is the NULL CHARACTER, rather than being the literal NULL.

    0 讨论(0)
  • 2020-12-02 13:55

    null is a literal and you can see the specification which says in practice you can simply pretend that it's "merely a special literal that can be of any reference type".

    as @assylias said what you read was surely not out of java :)

    0 讨论(0)
提交回复
热议问题