tags:

views:

970

answers:

14
+30  Q: 

Is null an Object?

Is null an Object in Java?

+8  A: 

No, it is not an object.

ammoQ
Absolutely correct, if a bit terse. +1.
Carl Smotricz
+10  A: 

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
+9  A: 

Null is the lack of an object.

Tommy Carlier
But the null type is assignable to every reference type.
Tom Hawtin - tackline
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
Tommy: JLS 4.1: "There is also a special null type, the type of the expression null, which has no name."
Ken
+1 for philosophy
Ahmad Dwaik
+20  A: 

Absolutely not: null instanceof Object returns false.

Darin Dimitrov
And yet it is assignment compatible with all reference types.
Christian Vest Hansen
null is kind of ugly. Haskell has its problems, but at least it's impossible to get a null pointer exception!
Claudiu
+24  A: 

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 special variable null provided by the compiler.

Dana the Sane
null is a type and a value.
Joachim Sauer
The `null` name is an "instance" of the `null` type, apparently.
strager
+4  A: 

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

Jacek Groszewski
In fact, `null instanceof <anytype>` will return `false`, too.
Bombe
+41  A: 

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
The most accurate and informative answer I see here. +1.
Carl Smotricz
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_D
+4  A: 

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
+4  A: 

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
+3  A: 

No. Even if it was it was, it is useless as it does not have any methods or fields.

Thorbjørn Ravn Andersen
+4  A: 

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
+2  A: 
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.

mmsmatt
+3  A: 

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

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

irreputable
I think there's a stain on your dress. Want me to analyze its DNA for you?
Steven Sudit
A: 

if you need such object there is a Void http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Void.html

"The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the Java keyword void."

It usefull if you need set generic type package ext;

import ext.Design.*;

public class Design {
    public static void main(String[] args) {
        new Design().test();
    }

    private void test() {
        new Cmd<String,Void>(){
            @Override
            protected Void exec(String in) {
                System.out.println(in); 
                return null;
            }
        };
    }

    public static abstract class Cmd<T1,T2>{
        protected abstract T2 exec(T1 in);
    }

}
Maciek Kreft