Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

null

char

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'?

like image 641
ashwinsakthi Avatar asked Aug 30 '12 11:08

ashwinsakthi


People also ask

What is the use of \0 in Java?

So to get the value of any character digit, you can just remove the '0', ie 48. If you don't remove '0' (48) the total sum will be over by 48 * numberOfDigits . See an ascii table to locate digits in it. Note that '0' is the character 0 , not the string "0" containing the character '0' .

What is the difference between Null and 0 in Java?

null means that a variable contains a reference to a space in memory that does not contain an object. 0 is a numeric data type with a value of 0. Nothing doesn't really exist, however I think you may be viewing this as an empty String "" which is simply a String data type that does not contain a value.

WHAT IS null character in Java?

The null character is often represented as the escape sequence \0 in source code , string literals or character constants.


2 Answers

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.

like image 193
waldyr.ar Avatar answered Sep 28 '22 09:09

waldyr.ar


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

like image 35
Ridcully Avatar answered Sep 28 '22 10:09

Ridcully