Is null an Object?

If null were an Object, it would support the methods of java.lang.Object such as equals(). However, this is not the case - any method invocation on a null results in a NullPointerException.

And this is what the Java Language Specification has to say on this topic:

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.

I think this can be boiled down to "null is special".


According to the Java spec, null is a type that can be assigned to an object variable (as a value as noted in the comment). You cannot instantiate or create variables of this type though, you must use the literal null provided by the compiler.


Absolutely not: null instanceof Object returns false.


JRL (and others) wrote:

No it's not, ...

As often, it depends from where you look at it, who you believe more.

According to the JLS, yes, it is. Especially if you rephrase the question to: „Is the null literal of type Object?”. In addition to JLS 4.1 cited by Michael Borgwardt above:

See JLS 3.10.7:

A null literal is always of the null type.

and JLS 4.10:

The subtypes of a type T are all types U such that T is a supertype of U, and the null type.

or JLS 4.10.2:

The direct supertypes of the null type are all reference types other than the null type itself.

[Emphases by me.]

According to Eclipse 2019-09's compiler it's not:

true.toString();  // Cannot invoke toString() on the primitive type boolean
null.toString();  // Cannot invoke toString() on the primitive type null

According to OpenJDKs 12.0.1 javac it is:

true.toString();  // error: boolean cannot be dereferenced
null.toString();  // error: <null> cannot be dereferenced

Where the angle brackets imply that null is of an other than a primitive type. And according to JLS 4.1:

There are two kinds of types in the Java programming language: primitive types (...) and reference types (...).

if it's not the one it's the other.


Claudiu wrote:

null is kind of ugly.

Au contraire, null is beautiful. What would you suggest as default value for a reference type variable instead? An arbitrary bit combination? Welcome to access violation or, even worse, pointer hell!


Joachim Sauer wrote:

null is a type and a value.

There are actually three items in conjunction with null (see also JLS 3.10.7):

  1. The (otherwise unnamed) null type.
  2. The null literal.
  3. The null reference value. (Commonly abbreviated as null value or simply null.)

(1) Note that, according to JLS 4.10.2 cited above, the null type uses multiple inheritance not only for interfaces but for classes as well. Which we all know is not possible for us application programmers.

(2) The null literal might be imagined as a variable being defined as:

JVM_global final null_type null = new null_type();

Note also JLS 3.9:

A variety of character sequences are sometimes assumed, incorrectly, to be keywords:

  • null is not a keyword, but rather the null literal (§3.10.7).

Concerning null instanceof <any type>

With JLS 4.10.2 in mind („the null type is a subtype of every type”) null instanceof <any type> should be supposed to evaluate to true, shouldn't it? At first sight, yes, but JLS 15.20.2 gives the insight answer:

[...] the result of the instanceof operator is true if the value of the RelationalExpression is not null [...]. Otherwise the result is false.

[Emphases by me.]

Ask yourself what makes more sense (from an application programmer's point of view):

  • Giving false and thus indicating that a reference expression is not of a type exposed to us, i.e. indicating it's not referencing anything useful to us

  • or giving true, thus informing us that the expression evaluates to a special reference, the null reference, referencing an "object" we don't know whether it even exists and which is of the special null type which has no name, is not exposed to us but via the null literal, is a subtype of any type including multiple inheritance and is to be ignored anyway? Consider also the more practical example:

       class Car implements Vehicle {  
       ...
       Vehicle car = null;
       ...
       boolean b = car instanceof Car;  // True? There's not even an instance
       ...                              // which could be of type Car.
    

Which also leads to:

Why is instanceof not a proper way to say something about null's Object-ness?

It's called instanceof not sameorsubtypeof. That means we are comparing an instance's type with a type, not two types. Now null means: „There is no instance” and if there is no instance there's no instance's type. It's obvious that comparing nothing with something is supposed to lead to false.

Or in a "more" real world example:

  • I have a real-size picture of an apple (=reference type) in my hands with »Big Apple« (=reference type name) written on it.
  • There's a table (=heap) in front of me.
  • If there is an apple (=instance) on the table there is a cord (=reference) connected to it.
  • I hold the other end of this cord in my hand (=reference variable) .
  • I trace the apple along the cord and compare it with my picture (=instanceof).
  • If the apple is of the same size or bigger than the picture the writing »Big Apple« applies to it (=true).
  • If it's smaller, then not (=false).
  • If there is no apple on the table (=no instance) and, hence, no cord exists (=null) the writing doesn't apply either (=false). Because: Is no apple a big apple? No, it's not.


As Michael sums up: "null is special" indeed.


Null is the lack of an object.