Java 中的运算顺序能保证从左到右吗?

发布于 2024-12-29 19:05:42 字数 286 浏览 5 评论 0原文

考虑一下这个函数:

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 技术交流群。

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

发布评论

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

评论(10

小巷里的女流氓 2025-01-05 19:05:42

事实上,我不同意其余答案。人们所指的 JLS §15.7 讨论了操作数的求值。也就是说,在表达式 中

x = foo() - 1 + bar()

,将按什么顺序调用方法。

相关章节为§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

x = foo() - 1 + bar()

, in which order will the methods be invoked.

The relevant section is §15.7.3, which specifies

An implementation may not take advantage of algebraic identities such as the associative law to rewrite expressions into a more convenient computational order unless it can be proven that the replacement expression is equivalent in value and in its observable side effects [...]

Since the expression x = x - 1 + q is equivalent in all ways to x = x + q - 1, a conforming implementation is allowed to rewrite the expression (if it for some reason should decide that is more efficient).

白日梦 2025-01-05 19:05:42

是的,它采用 Java 语言规范 ,§15.7。

Java 编程语言保证运算符的操作数按特定的求值顺序(即从左到右)求值。

建议代码不要严重依赖此规范。当每个表达式最多包含一个副作用(作为其最外层操作),并且代码不完全依赖于表达式从左到右求值所导致的异常时,代码通常会更清晰。

Yes, it's in the Java language specification, §15.7.

The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right.

It is recommended that code not rely crucially on this specification. Code is usually clearer when each expression contains at most one side effect, as its outermost operation, and when code does not depend on exactly which exception arises as a consequence of the left-to-right evaluation of expressions.

音盲 2025-01-05 19:05:42

根据此处

同一行上的运算符具有相同的优先级。当运营商
相同的表达式中出现相同的优先级,必须遵循规则
首先评估的是哪个。除 之外的所有二元运算符
赋值运算符从左到右求值;任务
运算符从右到左计算。

所以是的。

According to here:

Operators on the same line have equal precedence. When operators of
equal precedence appear in the same expression, a rule must govern
which is evaluated first. All binary operators except for the
assignment operators are evaluated from left to right; assignment
operators are evaluated right to left.

So yes.

沫尐诺 2025-01-05 19:05:42

是:

http://java.sun.com/docs /books/jls/third_edition/html/expressions.html#15.7

JVM 将解释

x = input - 1 + q;

x = (input - 1) + q;

Yes:

http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.7

The JVM will intepret

x = input - 1 + q;

as

x = (input - 1) + q;
白云不回头 2025-01-05 19:05:42

您可以在此处查看 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))

云归处 2025-01-05 19:05:42

实际上,JVM 实现并不重要,因为它们只是执行类文件中的指令列表。

It would actually not be the JVM implementation that matters, since they just execute a list of instructions from the class file.

2025-01-05 19:05:42

是的,尽管更大的问题是这真的重要吗?减法和加法在任何科学数学运算中具有相同的优先级并且是可交换的。即:

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;

源来凯始玺欢你 2025-01-05 19:05:42

是的,它总是会的,即使它不会影响加/减的结果。

Yes it always will, even though it wouldn't affect the result of adding / subtracting anyway.

旧情勿念 2025-01-05 19:05:42

在我看来,执行总会有副作用
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.

乄_柒ぐ汐 2025-01-05 19:05:42

这里需要补充的重要一点是,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

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