Java中方法调用和参数之间的求值顺序 [英] Evaluation order between a method call and arguments in Java

查看:175
本文介绍了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.计算目标参考(如有必要)

...

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆