What is null in Java?

后端 未结 14 1370
陌清茗
陌清茗 2020-11-21 20:49

What is null?

Is null an instance of anything?

What set does null belong to?

How is it represented in the me

14条回答
  •  南方客
    南方客 (楼主)
    2020-11-21 21:20

    Is null an instance of anything?

    No. That is why null instanceof X will return false for all classes X. (Don't be fooled by the fact that you can assign null to a variable whose type is an object type. Strictly speaking, the assignment involves an implicit type conversion; see below.)

    What set does 'null' belong to?

    It is the one and only member of the null type, where the null type is defined as follows:

    "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." JLS 4.1

    What is null?

    See above. In some contexts, null is used to denote "no object" or "unknown" or "unavailable", but these meanings are application specific.

    How is it represented in the memory?

    That is implementation specific, and you won't be able to see the representation of null in a pure Java program. (But null is represented as a zero machine address / pointer in most if not all Java implementations.)

提交回复
热议问题