129

Is null an Object in Java?

jason
  • 236,483
  • 35
  • 423
  • 525
Registered User
  • 3,050
  • 5
  • 26
  • 32
  • 46
    No it's not, and for those who wonder why such a question, there are languages where null is indeed an object like any, for example Ruby. – JRL Dec 12 '09 at 17:44
  • 8
    In Scala, which is very meticulous about types, there is a type `Null` (a *trait*, actually) having a single instance `null`. – Carl Smotricz Dec 12 '09 at 20:11
  • 1
    @JRL: Just out of curiosity, I wonder how that influences how JRuby works... (I'm not familiar enough with that level of implementation to know for sure.) – Benjamin Oakes Feb 03 '10 at 14:49
  • To add to @JRL's comment - Ruby's rough equivalent to `null` is `nil`, which is an instance of [NilClass](http://www.ruby-doc.org/core-2.1.2/NilClass.html) – Eliot Sykes Sep 10 '14 at 11:14
  • Or javascript, in which `null instanceof Object` is false but `typeof(null)` returns `'object'`. Is it an object in that case? Who knows. – Don Hatch Dec 03 '16 at 10:05

14 Answers14

175

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

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • 1
    So an object in Java is only an object if it is equal to or subclasses `java.lang.Object`. Practically - yes. You can't create a class that is not a subclass of java.lang.Object, but I never thought about it from a more philosophical point of view – Andreas Dolk Dec 12 '09 at 22:23
  • Curiously, of all things, the [API doc for `NullPointerException`](http://docs.oracle.com/javase/7/docs/api/java/lang/NullPointerException.html) makes mention of a »`null` object« … :) But then, it was also okay to talk about »null pointers« … – Lumi Feb 03 '14 at 13:44
  • You said: "The null reference can always be cast to any reference type". So, can we return null(typecasted-if allowed) in a method with an object reference(return) type? – Srichakradhar Feb 12 '14 at 08:54
  • 1
    @Srichakradhar: yes, we can! Which you can find out more easily by trying it in code than by asking here. – Michael Borgwardt Feb 12 '14 at 09:19
  • @GeroldBroser yep! :) – Carl Smotricz Nov 07 '15 at 20:56
  • 1
    "The null reference is the only possible value of an expression of null type". It is time to read the Specifications doc. :) – sofs1 Oct 21 '16 at 23:23
  • Please, change link to http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.1 – kolobok Apr 16 '17 at 17:30
  • 1
    Just like the square root of -1. `i` is special. – Gaurav Mall Aug 25 '19 at 09:15
  • @GauravMall It's not that special. `i²` is `-1` again. ;) – Gerold Broser Nov 13 '20 at 17:12
33

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.

Dana the Sane
  • 14,762
  • 8
  • 58
  • 80
  • 8
    null is a type and a value. – Joachim Sauer Dec 12 '09 at 17:50
  • 5
    The `null` name is an "instance" of the `null` type, apparently. – strager Dec 12 '09 at 18:51
  • `null` is not a variable, it's a literal: [JLS, 3.10.7. The Null Literal](http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.7) – Gerold Broser Nov 05 '15 at 20:37
  • Re "_`null` is a type that can be assigned_": 1) `null` (in code) is the _null literal_, not the _null type_ 2) Types cannot be assigned to variables in Java in general, see [JLS 4.1: "_There are \[...\] two kinds of data values that can be stored in variables \[...\]: primitive values (§4.2) and reference values (§4.3)._"](http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.1) It's [the null reference](http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.7) that can be assigned via the `null` literal. ...cont'd... – Gerold Broser Aug 15 '17 at 00:29
  • 3) _object_ in "_object variable_", compared to class variable, usually doesn't refer to the type but to the lifetime. When referring to the type it's usually called _variable of a reference type_ or, for short, _reference (type) variable._ I'm aware of 4 types in the Java API that expose instance/object variables (there may be more): [arrays with its `length`](http://docs.oracle.com/javase/specs/jls/se8/html/jls-10.html#jls-10.3) and the subclasses of [`java.awt.geom.Point2D`](http://docs.oracle.com/javase/8/docs/api/java/awt/geom/Point2D.html). – Gerold Broser Aug 15 '17 at 00:45
29

Absolutely not: null instanceof Object returns false.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
13

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.

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
12

Null is the lack of an object.

Tommy Carlier
  • 7,951
  • 3
  • 26
  • 43
  • But the null type is assignable to every reference type. – Tom Hawtin - tackline Dec 12 '09 at 17:56
  • There is no "null type"; null is a specific value of non-primitive expressions (i.e. references; there is no "reference type" in Java either, to my understanding). – Tommy McGuire Dec 12 '09 at 18:01
  • 3
    Tommy: JLS 4.1: "There is also a special null type, the type of the expression null, which has no name." – Ken Dec 12 '09 at 18:05
  • @TommyMcGuire Re »_there is no "reference type" in Java_« see [JLS 4.1: "_There are two kinds of types in the Java programming language: primitive types (§4.2) and reference types (§4.3)._"](http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.1) – Gerold Broser Aug 14 '17 at 23:19
11

No, it is not an object.

Erich Kitzmueller
  • 36,381
  • 5
  • 80
  • 102
9

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.

cherouvim
  • 31,725
  • 15
  • 104
  • 153
6

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.

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
4

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

4

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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
4

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.

Pavel Zaitsev
  • 578
  • 1
  • 7
  • 12
3

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

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

irreputable
  • 44,725
  • 9
  • 65
  • 93
2
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

Mehraj Malik
  • 14,872
  • 15
  • 58
  • 85
Matt Stephenson
  • 8,442
  • 1
  • 19
  • 19
1

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.

Sainath S.R
  • 3,074
  • 9
  • 41
  • 72
SparkOn
  • 8,806
  • 4
  • 29
  • 34
  • The OP asked for (the type) `Object`, not _object_ (as in _instance of a type_). And [if one rephrases the question accordingly...](https://stackoverflow.com/a/12886252/1744774) – Gerold Broser Aug 14 '17 at 23:35