Is null a Java keyword?

2019-01-24 09:19发布

Is null is a keyword in Java?

9条回答
乱世女痞
2楼-- · 2019-01-24 09:28

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

查看更多
\"骚年 ilove
3楼-- · 2019-01-24 09:29

No. See this for complete list: Java Language Keywords

查看更多
萌系小妹纸
4楼-- · 2019-01-24 09:33

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.

查看更多
相关推荐>>
5楼-- · 2019-01-24 09:42

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.

查看更多
【Aperson】
6楼-- · 2019-01-24 09:43

Not a keyword - the null literal.

an identifier (oops, no it isn't)

查看更多
劫难
7楼-- · 2019-01-24 09:43

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.

查看更多
登录 后发表回答