gpt4 book ai didi

java - java中的序列点

转载 作者:搜寻专家 更新时间:2023-10-31 08:21:18 25 4
gpt4 key购买 nike

是否有保证执行以下 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com