2+3 是否被视为文字?

发布于 2024-09-01 14:37:07 字数 91 浏览 2 评论 0原文

假设我有类似的东西

int x = 2 + 3;

x 被认为是一个文字吗?

Suppose i have something like

int x = 2 + 3;

Is x considered to be a literal?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

秉烛思 2024-09-08 14:37:07

x 是一个符号。 2 + 3 是一个表达式。 23 是文字。

x is a symbol. 2 + 3 is an expression. 2 and 3 are literals.

喵星人汪星人 2024-09-08 14:37:07

不,它是编译时常量表达式中的两个文字。根据编译方式的不同,您可能无法区分生成的二进制文件的差异。

No, it's two literals in a compile-time constant expression. Depending on how it gets compiled, you may not be able to tell the difference in the resulting binary.

旧伤还要旧人安 2024-09-08 14:37:07
  • int - 变量类型,标识符
  • x - 变量名称,标识符
  • = - 赋值
  • 2 - 文字
  • + - 两个文字之间的运算
  • 3 - 文字
  • ; - 声明结束
  • int - type of the variable, identifier
  • x - name of the variable, identifier
  • = - assignment
  • 2 - literal
  • + - operation between two literals
  • 3 - literal
  • ; - end of the statement
素罗衫 2024-09-08 14:37:07
  • int 是类型
  • x 是标识符
  • =,+ 是运算符
  • 2,3 是文字 “StringLiteral” 将是要

了解所有这些语法元素的命名方式,您可以浏览 ig

http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html

http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc .html

  • int is a type
  • x is an identifier
  • =,+ are operators
  • 2,3 are literals as "StringLiteral" would be

To learn how all these syntacically elements are named you could browse i.g.

http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html

http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html

猫瑾少女 2024-09-08 14:37:07

是的,这是一个字面意思。表达式将被简化为文字,即甚至在程序运行之前,因此它是

您在现实世界中可能看到的其他类型的文字(程序员不会费心去自己计算它,让编译器来做确定文字的繁重工作):

const unsigned int NEGATIVE_TESTER_FOR_32_BIT = 1 << 31;

const char ALPHABET_PIVOT = 'A' + ( ('Z' - 'A') / 2);

[编辑:关于取决于编译器]
是的,这取决于编译器。但我想大多数编译器编写者都会做足功课,如果他们能够生成该语言所需的汇编或虚拟机指令,那么在编译时计算东西对他们来说只是在公园散步。事实上,即使是字符文字“H”,也不会逐字存储在文件中 'H',它会存储为数字文字(72,或十六进制视图 0x48 或 48h )在最终的机器代码中。我可以冒险猜测所有编译器都会将这两个文字减少为文字,而不是表达式。这对他们来说就像在公园散步(编译器编写者)

yeah it is a literal. the expression will be reduced to literal, i.e. even before the program is run, so it is literal

other types of literal you might see in real-world (the programmer can't be bothered too compute it by himself, let the compiler do the grunt work of determining the literal):

const unsigned int NEGATIVE_TESTER_FOR_32_BIT = 1 << 31;

const char ALPHABET_PIVOT = 'A' + ( ('Z' - 'A') / 2);

[EDIT: regarding depending on the compiler]
yeah it depends on the compiler. but i guess most of compiler writers do their homework, if they can generate the necessarily assembly or virtual machine instruction of the language, computing things in compile time is just a walk in the park for them. in fact, even the character literal 'H' for example, doesn't even get stored in file as verbatim 'H', it gets stored as number literal (72, or in hex view 0x48 or 48h) in final machine code. i can hazard a guess that all compilers will reduce the two literals to a literal, not expression. it's a walk in the park for them(compiler writers)

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