The Null literal (written as null
) represents the one and only value of the null type. Here are some examples
MyClass object = null;
MyClass[] objects = new MyClass[]{new MyClass(), null, new MyClass()};
myMethod(null);
if (objects != null) {
// Do something
}
The null type is rather unusual. It has no name, so you cannot express it in Java source code. (And it has no runtime representation either.)
The sole purpose of the null type is to be the type of null
. It is assignment compatible with all reference types, and can be type cast to any reference type. (In the latter case, the cast does not entail a runtime type check.)
Finally, null
has the property that null instanceof <SomeReferenceType>
will evaluate to false
, no matter what the type is.