tags:

views:

748

answers:

6

Is null is a keyword in Java?

+3  A: 

No.It is not a keyword.

Warrior
+12  A: 

Not according to the Java Language Specification list of keywords. On the other hand, this doesn't compile:

int null = 10;

The rules for identifiers specify that:

An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter. An identifier cannot have the same spelling (Unicode character sequence) as a keyword (§3.9), boolean literal (§3.10.3), or the null literal (§3.10.7).

I'm not sure what the benefit of making it not-a-keyword is, to be honest.

Jon Skeet
Lol, this answer implies that there are implementations of Java that accept null as a keyword.
Gamecat
Gamecat, the way i read it, it says clearly the spec does _not_ consider null a keyword.
tehvan
+6  A: 

Not a keyword - the null literal.

an identifier (oops, no it isn't)

Marc Gravell
It's not an identifier either, according to http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#40625
Jon Skeet
Yes, I realised by mistake already and fixed it...
Marc Gravell
Up vote just because it's a trick question...
Tom Hawtin - tackline
+1  A: 

No. See this for complete list KeywordList

Nrj
+1  A: 

true and false are also literals. Java trivia: const and goto are keywords.

Peter Lawrey
+2  A: 

null is a literal, in the same sense that false, 10, and '\n' are literals. It's not a "keyword", technically, but it is a character string that is treated specially by the compiler if the compiler encounters it in a java source file.

So, no, you cannot name a variable "null". The lexical analyzer will decide that it is not an identifier.

paulmurray