Is null an Object
in Java?
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.
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.
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 thatnull
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.
No. Even if it was it was, it is useless as it does not have any methods or fields.
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
.
Is null an instance of java.lang.Object
? No.
Is null an object? depends on the definition of "is".
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);
}
}