Evaluation order between a method call and arguments in Java

房东的猫 提交于 2020-01-06 05:31:03

问题


Dealing with another SO question, I was wondering if the code below has an undefined behavior:

if (str.equals(str = getAnotherString())) {
  // [...]
}

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?


回答1:


This is clearly specified in the JLS Section 15.12.4:

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. Compute Target Reference (If Necessary)

...

  • If form is ExpressionName . [TypeArguments] Identifier, then:
    • If the invocation mode is static, then there is no target reference. The ExpressionName is evaluated, but the result is then discarded.
    • Otherwise, the target reference is the value denoted by ExpressionName.

So the "target reference" is the str bit in str.equals - the expression on which you are calling the method.

As the first quote says, the target reference is evaluated first, then the arguments. Therefore, str.equals(str = getAnotherString()) only evaluates to true if getAnotherString returns a string that has the same characters as str before the assignment expression.

So yeah, the thing that you tend to think is correct. But this is not "undefined behaviour".



来源:https://stackoverflow.com/questions/59571742/evaluation-order-between-a-method-call-and-arguments-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!