Is null an Object?

Is null an Object in Java?

60842 次浏览

No, it is not an object.

No, it's not an instance of a Class nor a Class. It's a reference to nothing.

Edit: haven't read the spec so the above may not be 100% accurate.

Null is the lack of an object.

Absolutely not: null instanceof Object returns false.

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.

No, is not an object as null instanceof Object will always return false also there is only one null, not one for each class.

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

As explained in the chapter 4.1 The Kinds of Types and Values of the Java Language Specification, null is a type which has one value, the null reference (and is represented by the literal null):

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.

You might want to read about the Null Object Pattern (that I don't recommend) though. See the C2 Wiki or Wikipedia for more on this pattern.

According to the Java Spec,

There's also a special null literal that can be used as a value for any reference type. null may be assigned to any variable, except variables of primitive types. There's little you can do with a null value beyond testing for its presence. Therefore, null is often used in programs as a marker to indicate that some object is unavailable.

Java handles objects via references. Null is a breakdown of OO-ness of Java, since it drops you below OO level. No it is not an object it is a VALUE of a reference. And it has nothing to do with object paradigms, but relates to plumbing of Java, that enables objects.

Object foo = null;
System.out.println(foo.toString());

The first line shows null can be assigned to type Object, but the second line will demonstrate it is certainly not an Object and eventually results in a java.lang.NullPointerException

Is null an instance of java.lang.Object? No.

Is null an object? depends on the definition of "is".

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.

No ,null is not an object.It is a reference type and its value does not refer to any object and so there is no representation of null in memory.