If I can do int n = 0
, why can't I null n = null
?
Isn't null
a data type just like int
?
Asked
Active
Viewed 68 times
1 Answers
4
No. null
is not a data type. null
is a value (specifically to represent no valid value). Also, you can't assign null
to a primitive.

Elliott Frisch
- 198,278
- 20
- 158
- 249
-
1theoretically speaking, `null` is a type which has no name. see http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.1 – Jerry Chin Apr 13 '17 at 01:18
-
@JerryChin Also from your link, *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.* – Elliott Frisch Apr 13 '17 at 01:28
-
I can' agree with you more, let linguists sort it out. – Jerry Chin Apr 13 '17 at 02:43
-
@JerryChin Small distinction: `null` **has** a type (the "null type") which has no name. – Erwin Bolwidt Apr 13 '17 at 03:16
-
@ErwinBolwidt [Semantics](https://s-media-cache-ak0.pinimg.com/736x/9a/fd/6a/9afd6a24081ca81cbdbdcf676e4bd75c.jpg). – Elliott Frisch Apr 13 '17 at 03:20
-
"Semantics" means "meaning", and meaning matters, @ElliottFrisch. The distinction between "value" and "type" in Java is pretty important. And the JLS makes it quite clear that the `null` literal is a value, not a type. "There is also a special _null type_, the type of the expression `null`". The typographical distinction is in the original. Don't treat semantics like it's trivial. – Lew Bloch Apr 13 '17 at 03:33
-
@LewBloch "The null reference is the only possible value of an expression of null type." but more directly to OP's question "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." - if OP was implementing a JDK or compiler, then I wouldn't have posted a link to a punny cartoon ("No man" kills the witch-king) – Elliott Frisch Apr 13 '17 at 03:43