Nov 8, 2018

null in Java

null is a literal, not a keyword in Java.

null is merely a special literal that can be of any reference type. it is a special null type. The null reference can always be cast to any reference type. 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.

There is no type which null is an instanceof.

RelationalExpression instanceof ReferenceType

At run time, the result of the instanceof operator is true if the value of the RelationalExpression is not null and the reference could be cast to the ReferenceType without raising a ClassCastException. Otherwise the result is false.

In Java, null == null (this isn't always the case in other languages). Note also that by contract, it also has this special property (from java.lang.Object):

public boolean equals(Object obj)

For any non-null reference value x, x.equals(null) should return false.

It is also the default value (for variables that have them) for all reference types:

It is a very common use pattern: null is used to denote non-existence of an object.