Java中方法调用和参数之间的求值顺序 [英] Evaluation order between a method call and arguments in Java
问题描述
处理另一个SO问题,我想知道下面的代码是否具有未定义的行为:
Dealing with another SO question, I was wondering if the code below has an undefined behavior:
if (str.equals(str = getAnotherString())) {
// [...]
}
我倾向于认为equals()
调用所依据的str
引用是在之前作为参数传递的进一步的str
赋值进行评估的.是否有消息来源?
I tend to think the str
reference from which the equals()
call is made is evaluated before the further str
assignment passed as argument. Is there a source about it?
推荐答案
在
在运行时,方法调用需要五个步骤.首先,可以计算目标参考.其次,对参数表达式进行求值. [...]
At run time, method invocation requires five steps. First, a target reference may be computed. Second, the argument expressions are evaluated. [...] 什么是目标参考"?你问?这将在下一部分中指定: What's a "target reference" you ask? This is specified in the next subsection: 15.12.4.1.计算目标参考(如有必要) ... 因此,目标参考"是 So the "target reference" is the 如第一个引号所述,首先对目标引用进行求值,然后对参数进行然后.因此, As the first quote says, the target reference is evaluated first, then the arguments. Therefore, 是的,您倾向于认为的事情是正确的.但这不是"未定义的行为". So yeah, the thing that you tend to think is correct. But this is not "undefined behaviour". 这篇关于Java中方法调用和参数之间的求值顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
str.equals
中的str
位-您要在其上调用该方法的表达式.str
bit in str.equals
- the expression on which you are calling the method.str.equals(str = getAnotherString())
仅在getAnotherString
返回与赋值表达式之前的字符与str
相同的字符串的情况下计算为true.str.equals(str = getAnotherString())
only evaluates to true if getAnotherString
returns a string that has the same characters as str
before the assignment expression.