First of all, null
is not a type but a value. Variables whose type is a reference type might have the value null, but you can’t use null
at places, where a reference type is expected, you can’t declare a variable of type null
and you can’t have a type parameter null
, e.g. you can’t declare a List<null>
.
The value null
has a special “null type” that is only relevant during compilation (and verification) to be able to formulate the formal rule that the “null type” is assignable to a reference type. But the
“null type” is not a reference type.
To add some quotes from the specification:
JLS §4.1:
There is also a special null type, the type of the expression null (§3.10.7, §15.8.1), which has no name.
…
The null reference can always be assigned or cast to any reference type (§5.2, §5.3, §5.5).
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.
JVMS §4.10.1.2:
Verification type hierarchy:
top
____________/\____________
/ \
/ \
oneWord twoWord
/ | \ / \
/ | \ / \
int float reference long double
/ \
/ \_____________
/ \
/ \
uninitialized +------------------+
/ \ | Java reference |
/ \ | type hierarchy |
uninitializedThis uninitialized(Offset) +------------------+
|
|
null