null 是 Java 关键字吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/519631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is null a Java keyword?
提问by
Is null
is a keyword in Java?
是null
Java中的关键字吗?
采纳答案by Warrior
No.It is not a keyword.
不,它不是关键字。
回答by Jon Skeet
Not according to the Java Language Specification list of keywords. On the other hand, this doesn't compile:
不符合Java 语言规范的关键字列表。另一方面,这不会编译:
int null = 10;
The rules for identifiersspecify 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).
标识符是 Java 字母和 Java 数字的无限长度序列,其中第一个必须是 Java 字母。标识符不能与关键字 (§3.9)、布尔文字 (§3.10.3) 或空文字 (§3.10.7) 具有相同的拼写(Unicode 字符序列)。
I'm not sure what the benefit of making it not-a-keyword is, to be honest.
老实说,我不确定让它不是关键字的好处是什么。
回答by Marc Gravell
回答by Nrj
No. See this for complete list: Java Language Keywords
不。请参阅此以获取完整列表:Java 语言关键字
回答by Peter Lawrey
trueand falseare also literals. Java trivia: constand gotoare keywords.
true和false也是文字。Java 小知识:const和goto是关键字。
回答by paulmurray
nullis 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.
null是文字,与false、10和 '\n'是文字的意义相同。从技术上讲,它不是“关键字”,但它是一个字符串,如果编译器在 java 源文件中遇到它,则编译器会对其进行特殊处理。
So, no, you cannot name a variable "null". The lexical analyzer will decide that it is not an identifier.
因此,不,您不能将变量命名为“null”。词法分析器将决定它不是标识符。
回答by saurabh kimothi
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.
null
是类似于Java 中的true
和的字面量false
。这些不是关键字,因为它们是某物的值。与null
引用变量true
的值一样,是布尔变量的值。
回答by Pankaj Goyal
No. null is not a keyword in Java.
不,null 不是 Java 中的关键字。
回答by NAGHMAAN MOHASEEN
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.
它代表一个特殊的值,但它不是 Java 中的关键字,并不意味着我们可以将它用作标识符。它被限制用作标识符,这很明显,因为我们不能将特殊值作为标识符,因为编译器将该字 (null) 视为特殊值,而不是标识符。