问题描述
限时送ChatGPT账号..嘿,我有以下两行代码:
Hey, I have the following two lines of code:
result[i] = temp[i] + temp[i + 1] + " " + temp[i + 2];
i += 2;
我想知道这行代码是否会做同样的事情:
I am wondering if this line of code would do the same:
result[i] = temp[i] + temp[i++] + " " + temp[i++];
我可以确定 每个 VM 都会从左到右处理该行吗?谢谢,托比
Can I be sure, that EVERY VM would process the line from the left to the right? Thanks, Tobi
推荐答案
来自 Java 语言规范:
Java 编程语言保证运算符的操作数以特定的求值顺序进行求值,即从左到右.
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.
这篇关于Java:在一行中增加多个 ++.哪个先?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论