我在 Oracle Java Tutorial 中找到了以下关于文字概念的陈述:
A literal is the source code representation of a fixed value; literals are represented directly in your code without requiring computation
我知道 int * int
或 String + String
... 不是文字。所以我尝试这段代码:
int num1 = 3*5;
int num2 = 15
System.out.println(num1==num2);// it print true
我的问题:我的代码中的 3 * 5 是否是文字?为什么?谢谢!
最佳答案
对您问题的评论是混淆概念的。定义很清楚,3
和 5
是字面意思,但 3*5
不是,因为它需要计算。
确实,编译器可能会在编译时执行该计算,因为结果是编译时已知的常量,但它不会用文字替换它。请参阅您引用的定义。文字是源代码表示。如果编译器在编译时执行该操作,它将影响字节码,而不是源代码。
您对 num1==num2
所做的评论与问题完全无关。再次查看定义。文字被定义为值的表示,其中相等是关于值本身的声明。
关于java - "Literals are represented directly in your code without requiring computation"。这是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42800740/