java中关键字和字面量的区别
我是 java 新手,正在读一本书,遇到了这些行:
“文字 true
、false
和 null
是小写,而不是大写就像在 C++ 语言中一样。严格来说,这些不是关键字而是文字。”
为什么这些是文字,以及某些关键字需要什么要求才能被称为文字..?
I am new to java and was reading a book and came across these lines:
"The literals true
,false
and null
are lowercase,not uppercase as in C++ language.Strictly speaking,these are not keywords but literals."
why these are literals, and what requirements are needed for some keywords to be called literal..?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
关键字是用作代码结构一部分的单词,例如
for
或while
。它们改变了编译器处理代码块的方式,例如,for
告诉编译器重复执行指定范围内的代码,直到达到给定的退出条件。class
关键字告诉编译器将指定范围内的所有内容视为特定类的一部分。关键字名称受到限制,因此不能将它们用作变量名称。像
true
、false
和null
这样的文字是可以赋值的值,但它们的名称受到与关键字相同的限制,即您不能有名为true
或for
的变量。它们构成表达式的一部分,但不会改变编译器处理代码的方式。Keywords are words that are used as part of code structure, like
for
orwhile
. They change the way a compiler handles a block of code, e.g. afor
tells the compiler to execute the code within the specified scope repeatedly, until the given exit condition is reached. Theclass
keyword tells the compiler to treat everything within the specified scope to be part of a particular class. Keyword names are restricted, so you can't use them as variable names.Literals like
true
,false
andnull
are values that can be assigned, but their names are restricted in the same way that keywords are, i.e. you can't have a variable calledtrue
orfor
. They form parts of expressions, but don't change the way a compiler handles code.true
、false
和null
是表达式。它们表示特殊的内置值,因此它们被视为文字(以及更传统的文字,例如123
和“xyz”
)。for
、if
、class
等都是关键字。它们将您的声明和语句传达给编译器,但它们不代表值。这就是为什么它们不是文字。true
,false
andnull
are expressions. They denote special built-in values, so they are considered literals (along with more traditional literals, such as123
and"xyz"
).for
,if
,class
, etc. are keywords. They communicate your declarations and statements to the compiler, but they do not represent values. That is why they are not literals.这些关键字在 Java 语言规范 #3.9。 “真实”并不在其中。这些文字在 #3.10 中定义,其中包括“true”。这些部分的文字完全回答了您的问题。
The keywords are defined in the Java Language Specification #3.9. 'true' is not among them. The literals are defined in #3.10, and they include 'true'. The text of those sections answers your question completely.
文字是符号,而标识符是关键字。
嗯,不,但是一个例子可能会有所帮助:
12, 1e3, 0x4a, 'a', "Hello\n" -- 文字
_debug, n, stdio, main, argc, printf -- 标识符
Literals are the symbols whereas the identifiers are the keywords.
Well, no, but an example may help:
12, 1e3, 0x4a, 'a', "Hello\n" -- literals
_debug, n, stdio, main, argc, printf -- identifiers