A mindless query arising from Java's less known data type 'null'.
Would somebody be able to clarify whether or not 'null' is a literal reserved by the Java language and thus the disparity between it satisfying as primitive data type and a digital value?
NULL default_object_value; // a Syntax error
NULL acct_balance_at_westbank = null; // Another Syntax error
String aStrokeOfLuck = null; // a feasible variable declaration & assignment
According to JLS 4.1, there actually is a type for null
.
"There is also a special null type, the type of the expression null (§3.10.8, §15.8.1), which has no name." - JLS 4.1
So.
null
is a literal reserved by the Java language.
null
denotes a value not a type.
The type of null
cannot be named. Hence your failed attempts to do that.
(I don't even think there is a reflective java.lang.Class
representation for the null type ...)
The JLS says that the type of null
is neither a primitive type or a reference type.
Various contexts allow null
to be converted to the null value of any reference type; e.g.
"Finally, a value of the null type (the null reference is the only such value) may be assigned to any reference type, resulting in a null reference of that type." - JLS 5.2
I imagine that there is some technical reason that they have chosen to specify the null type this way. Maybe this makes other parts of the spec simpler. I don't know. And unless you are a black-belt type theoretician, the why question is most likely irrelevant.
... the disparity between it satisfying as primitive data type and a digital value?
I cannot make sense of that part of your question.
As mentioned in the comments, NULL
is just an ordinary identifier. The Java class libraries don't declare a class with that name, and as such you will most likely get an unresolved symbol error. You could declare one for yourself, but that NULL
class will not (and cannot) denote the null type as far as the Java compiler or runtime is concerned.