0

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?

6
  • Definitely won't be undefined behaviour. Java has very few of those. Commented Jan 3, 2020 at 0:27
  • The code you posted is defined behavior. The answer is in the JLS, possibly under 15.7. Commented Jan 3, 2020 at 0:33
  • Your though is correct. For if str = ... was evaluated first, the whole expression would always be true. Commented Jan 3, 2020 at 0:35
  • Evaluation order isn't the same thing as operator precedence. Commented Jan 3, 2020 at 3:05
  • Be aware of NPE in such case Commented Jan 3, 2020 at 3:08

1 Answer 1

2

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".

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.