How is null implemented in Java?

前端 未结 2 1657
悲哀的现实
悲哀的现实 2021-01-18 00:55

I know that null is not a data type.

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

Example:

相关标签:
2条回答
  • 2021-01-18 01:34
    Object o = null; // it tells that null is an object
    String b = null; // it tells that null is of string type
    

    Neither of those comments is correct. All that's happening here is that references are being set to null.

    0 讨论(0)
  • 2021-01-18 01:45

    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.

    Note that this behavior is different from older languages such as C or C++03 where NULL pointer was simply a zero constant.

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