Java 中的运算顺序能保证从左到右吗?
考虑一下这个函数:
public static final int F(int a, int b) {
a = a - 1 + b;
// and some stuff
return a;
}
JVM 的实现是否需要在 + b
之前执行 - 1
?
如果我们有一个附加到 JVM 的系统分析器,我们会看到 + b
操作在 + 1
操作之前执行吗?
Consider this function:
public static final int F(int a, int b) {
a = a - 1 + b;
// and some stuff
return a;
}
Is it required for implementations of JVMs to execute - 1
before + b
?
If we have a system profiler attached to the JVM, will we see the + b
operation being carried out before the + 1
operation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
事实上,我不同意其余答案。人们所指的 JLS §15.7 讨论了操作数的求值。也就是说,在表达式 中
,将按什么顺序调用方法。
相关章节为§15.7.3,其中规定
因为表达式 x = x - 1 + q 在所有方面都等价于 x = x + q - 1,一个允许一致的实现重写表达式(如果出于某种原因应该决定这样做更有效)。
Actually, I will disagree with the rest of the answers. The JLS §15.7 that people are referring to discusses the evaluation of operands. That is, in the expression
, in which order will the methods be invoked.
The relevant section is §15.7.3, which specifies
Since the expression
x = x - 1 + q
is equivalent in all ways tox = x + q - 1
, a conforming implementation is allowed to rewrite the expression (if it for some reason should decide that is more efficient).是的,它采用 Java 语言规范 ,§15.7。
Yes, it's in the Java language specification, §15.7.
根据此处:
所以是的。
According to here:
So yes.
是:
http://java.sun.com/docs /books/jls/third_edition/html/expressions.html#15.7
JVM 将解释
为
Yes:
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.7
The JVM will intepret
as
您可以在此处查看 Java 中的运算符优先级:http://bmanolov.free.fr/javaoperators.php。由于
+
和-
具有相同的优先级,因此它们将按照它们出现的顺序执行。如果您想更清楚哪个操作首先发生(或者如果您想打破内置优先级),您可以(并且应该)使用括号(
(
和)
),像这样:x = (a - b) + (c * (d - e))
You can see here the operator precedence in Java: http://bmanolov.free.fr/javaoperators.php . Because
+
and-
have the same precedence, they will be executed in the order in which they appear.If you want to be more clear about what which operation takes place first (or if you want to break the built-in precedence), you can (and should) use parantheses (
(
and)
), like this:x = (a - b) + (c * (d - e))
实际上,JVM 实现并不重要,因为它们只是执行类文件中的指令列表。
It would actually not be the JVM implementation that matters, since they just execute a list of instructions from the class file.
是的,尽管更大的问题是这真的重要吗?减法和加法在任何科学数学运算中具有相同的优先级并且是可交换的。即:
x = 输入 - 1 + q;
相同;
与x = 输入 + q - 1
Yes, although the bigger question is does it really matter? Subtraction and addition have the same precedence in any scientific mathematical operation and are commutable. That is:
x = input - 1 + q;
is the same as
x = input + q - 1;
是的,它总是会的,即使它不会影响加/减的结果。
Yes it always will, even though it wouldn't affect the result of adding / subtracting anyway.
在我看来,执行总会有副作用
X+q-1
当你应该执行的时候
X - 1 + q。
当 x + q 之和超过 Bignum 1 时,从左到右执行就会成功……乱序执行会产生溢出。
所以乱序执行是有副作用的。
除非乱序执行捕获自身溢出,否则在必要时以正确的顺序重新进行计算以避免副作用。
It seems to me there will always be a side effect from executing
X + q - 1
When you should be executing
X - 1 + q.
When the sum x + q exceeds Bignum by 1, the left to right execution will succeed... The out of order execution will generate an overflow.
So the out of order execution has a side-effect.
Unless the out-of-order execution traps that overflow itself, then re-do es the calculation in the correct order when necessary to avoid the side effect.
这里需要补充的重要一点是,java 确实具有运算符的行优先级,这似乎是基于操作的基本算术顺序。有关完整优先级的列表,请查看此来源
Important to add here is that java does have in line precedence for operators which appears to be based on the basic arithmetic order of operations. For a list of full precedence look at this source