How is null implemented in Java?

I know that null is not a data type.

But null can only be assigned to any type of Object and String .

Example:

 Object o = null; // it tells that null is an object String b = null; // it tells that null is of string type 

Here null can be assigned to either Object or String , but if I want to put any object in String, we must use the cast type. but for null there is no type. something like the following

 String b =(String) o; 

This is why I doubt that the data type is NULL. How null can be assigned to any object or any String. How is null implemented in Java?

+6
source share
2 answers

The reference and the null keyword are built-in Java constructs and are specially handled by the compiler. The compiler then issues the appropriate bytecode instructions, depending on the context in which null used.

Note that this behavior is different from older languages ​​such as C or C ++ 03, where the NULL pointer was just a null constant.

+8
source
 Object o = null; // it tells that null is an object String b = null; // it tells that null is of string type 

None of these comments are correct. All that happens here is that the links are set to null.

+2
source

Source: https://habr.com/ru/post/892974/


All Articles