如何在Java中实现null? [英] How is null implemented in Java?
问题描述
我知道null不是数据类型。
I know that null is not a data type.
但 null
只能分配给任何类型对象
和字符串
。
But null
can be only assigned for any type of Object
and String
.
示例:
Object o = null; // it tells that null is an object
String b = null; // it tells that null is of string type
这里 null
可以分配给对象
和 String
,但如果我想分配任何对象在String中我们必须使用类型转换。但是对于null,没有类型的情况。
如下所示
Here null
can be assigned to both an Object
and a String
, but if I want to put assign any object into String we have to use type cast. but for null there is no type case.
something like the following
String b =(String) o;
这就是为什么我怀疑哪种类型的数据类型为空。如何将null分配给任何对象或任何String。如何用Java实现 null
?
Thats why I have doubt that which type of data type is null. How null can be assigned to any object or any String. How is null
implemented in Java?
推荐答案
null
reference和keyword是Java内置结构,由编译器专门处理。然后编译器根据使用 null
的上下文发出相应的字节码指令。
The null
reference and keyword are Java built-in constructs and are treated specially by the compiler. The compiler then emits the corresponding bytecode instructions depending on the context which null
is used in.
注意这种行为与C或C ++ 03等旧语言不同,其中NULL指针只是一个零常量。
Note that this behavior is different from older languages such as C or C++03 where NULL pointer was simply a zero constant.
这篇关于如何在Java中实现null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!