2+3 是否被视为文字?
假设我有类似的东西
int x = 2 + 3;
x
被认为是一个文字吗?
Suppose i have something like
int x = 2 + 3;
Is x
considered to be a literal?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
假设我有类似的东西
int x = 2 + 3;
x
被认为是一个文字吗?
Suppose i have something like
int x = 2 + 3;
Is x
considered to be a literal?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
x
是一个符号。2 + 3
是一个表达式。2
和3
是文字。x
is a symbol.2 + 3
is an expression.2
and3
are literals.不,它是编译时常量表达式中的两个文字。根据编译方式的不同,您可能无法区分生成的二进制文件的差异。
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.
了解所有这些语法元素的命名方式,您可以浏览 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
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
是的,这是一个字面意思。表达式将被简化为文字,即甚至在程序运行之前,因此它是
您在现实世界中可能看到的其他类型的文字(程序员不会费心去自己计算它,让编译器来做确定文字的繁重工作):
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)