java中关键字和字面量的区别

发布于 2024-12-22 04:44:48 字数 185 浏览 0 评论 0原文

我是 java 新手,正在读一本书,遇到了这些行:

“文字 truefalsenull 是小写,而不是大写就像在 C++ 语言中一样。严格来说,这些不是关键字而是文字。”

为什么这些是文字,以及某些关键字需要什么要求才能被称为文字..?

I am new to java and was reading a book and came across these lines:

"The literals true,falseand 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

坚持沉默 2024-12-29 04:44:48

关键字是用作代码结构一部分的单词,例如 forwhile。它们改变了编译器处理代码块的方式,例如,for 告诉编译器重复执行指定范围内的代码,直到达到给定的退出条件。 class 关键字告诉编译器将指定范围内的所有内容视为特定类的一部分。关键字名称受到限制,因此不能将它们用作变量名称。

truefalsenull 这样的文字是可以赋值的值,但它们的名称受到与关键字相同的限制,即您不能有名为 truefor 的变量。它们构成表达式的一部分,但不会改变编译器处理代码的方式。

Keywords are words that are used as part of code structure, like for or while. They change the way a compiler handles a block of code, e.g. a for tells the compiler to execute the code within the specified scope repeatedly, until the given exit condition is reached. The class 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 and null 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 called true or for. They form parts of expressions, but don't change the way a compiler handles code.

野稚 2024-12-29 04:44:48

truefalsenull 是表达式。它们表示特殊的内置值,因此它们被视为文字(以及更传统的文字,例如123“xyz”)。

forifclass等都是关键字。它们将您的声明和语句传达给编译器,但它们不代表值。这就是为什么它们不是文字

true, false and null are expressions. They denote special built-in values, so they are considered literals (along with more traditional literals, such as 123 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.

伪装你 2024-12-29 04:44:48

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.

去了角落 2024-12-29 04:44:48

文字是符号,而标识符是关键字。

嗯,不,但是一个例子可能会有所帮助:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文