Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - is `null` an instance of `object`?

Tags:

java

People also ask

IS null an instance of object in Java?

No, a null check is not needed before using instanceof. The expression x instanceof SomeClass is false if x is null . The Java 11 Language Specification expresses this concisely in section 15.20. 2, "Type comparison operator instanceof".

IS null an instance of object?

You can assign a null value to any reference variable. But the null value is not an instanceof Object or any other class. When you assign a reference variable null, it means that the reference variable doesn't refer to any object.

Does null extend object in Java?

No, null is not an object. It is a literal that means that a variable does not reference any object.

IS null == null in Java?

out. println("(Object)string == number: " + ((Object)string == number)); To conclude this post and answer the titular question Does null equal null in Java? the answer is a simple yes.


No, it is a reference. null is not an object

String s = null;

System.out.println(s instanceof Object); // false

In a word, no.

Peter Norvig's Java IAQ addresses this question in some detail (specifically "Q: Is null an Object?")


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

Java Language Specification


Null means that you don't have a reference to an object.

Object o = null;

o is a reference but there is no object referenced and no memory allocated for it.

o = new Object();

o is still a reference and holds the adress where the object is located in memory


No, null is not an object. It is a literal that means that a variable does not reference any object.