11

Is null is a keyword in Java?

Jonik
  • 80,077
  • 70
  • 264
  • 372
  • Why is this question -1? – Mystic Feb 06 '09 at 10:31
  • 4
    probably because it would've taken less time to google the answer than to ask it. The second google result has the answer in the summary, you wouldn't even have to click the link. – DevinB Feb 06 '09 at 13:16
  • No it is literal in Java.You can what null is [here][1] [1]: http://stackoverflow.com/questions/2707322/what-is-null-in-java/7258515#7258515 – Sanjay Jain Aug 31 '11 at 14:35

9 Answers9

47

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.

Vy Do
  • 46,709
  • 59
  • 215
  • 313
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
11

Not a keyword - the null literal.

an identifier (oops, no it isn't)

Pang
  • 9,564
  • 146
  • 81
  • 122
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 1
    It's not an identifier either, according to http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#40625 – Jon Skeet Feb 06 '09 at 09:15
9

null is a literal similar to true and false in Java. These are not keywords because these are the values of something. As null is the value of a reference variable, true is the value of a boolean variable.

Pang
  • 9,564
  • 146
  • 81
  • 122
7

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
  • 3,355
  • 1
  • 22
  • 17
6

No.It is not a keyword.

Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
Warrior
  • 39,156
  • 44
  • 139
  • 214
3

No. See this for complete list: Java Language Keywords

Pang
  • 9,564
  • 146
  • 81
  • 122
Nrj
  • 6,723
  • 7
  • 46
  • 58
1

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

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

No. null is not a keyword in Java.

Pang
  • 9,564
  • 146
  • 81
  • 122
Pankaj Goyal
  • 950
  • 9
  • 15
0

It represents a special value but it's not a keyword in Java, and it doesn't mean that we can use it as an identifier. It's restricted to be used as an identifier and that is obvious because we cannot have a special value as an identifier because the compiler will treat that word (null) as special value, not as identifier.

Pang
  • 9,564
  • 146
  • 81
  • 122