java - java中的序列点

标签 java sequence-points

是否有保证执行以下 java 代码的顺序:

int i = getA() + getB();

getA() 是否总是在 getB() 之前执行,正如任何普通人所期望的那样?

最佳答案

是的,是的。来自 JLS,第 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.

...

The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated.

还有:

The Java programming language also guarantees that every operand of an operator (except the conditional operators &&, ||, and ? :) appears to be fully evaluated before any part of the operation itself is performed.

关于java - java中的序列点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4352954/

相关文章:

java - Intellij Java : java. lang.NoClassDefFoundError: org/json/JSONException

java - Jackson Parser - 如何让对象映射器知道类的名称与响应中字段的名称的大小写不同

c - c中(a=a+1)+(a=a+1)+(a=a+1)的结果?

c - 为什么这些构造使用增量前和增量后未定义的行为?

c - 了解 C 中的后增量概念

java - 使用不同的构建器创建 java 子类型

java - 在 Java 中为 Array 中的元素分配引用时到底发生了什么?

java - 我应该如何在java中实现子字符串函数?

c - 为什么这些构造使用增量前和增量后未定义的行为?

c - 为什么这些构造使用增量前和增量后未定义的行为?