What is null in Java?

后端 未结 14 1381
陌清茗
陌清茗 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:14

    null is special value, it is not instance of anything. For obviously reason it cannot be instanceof anything.

    0 讨论(0)
  • 2020-11-21 21:14

    There are two major categories of types in Java: primitive and reference. Variables declared of a primitive type store values; variables declared of a reference type store references.

    String x = null;
    

    In this case, the initialization statement declares a variables “x”. “x” stores String reference. It is null here. First of all, null is not a valid object instance, so there is no memory allocated for it. It is simply a value that indicates that the object reference is not currently referring to an object.

    0 讨论(0)
  • 2020-11-21 21:17

    What is null?

    It is nothing.

    Is null an instance of anything?

    No as it is nothing It can't be instance of any thing.

    What set does null belong to?

    No any set

    How is it represented in the memory?

    If some reference points to it like:

    Object o=new Object();
    

    In heap memory some space assigned to new created object. And o will point to that assigned space in memory.

    Now o=null;

    This means now o will not point to that memory space of object.

    0 讨论(0)
  • Short and precise answer which answers all your questions formally from JLS:

    3.10.7. The Null Literal

    The null type has one value, the null reference, represented by the null literal null, which is formed from ASCII characters.

    A null literal is always of the null type.

    Only a reference of type which is assigned to null is allocated. You don't assign any value (object) to the reference. Such allocation is specific to JVM how much reference will take and in which memory area it will be allocated.

    0 讨论(0)
  • 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.)

    0 讨论(0)
  • 2020-11-21 21:21

    No it's not the instance of anything, instanceof will always be false.

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