ashwinsakthi ashwinsakthi - 3 months ago 22
Java Question

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

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.

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

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

Answer

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.