6

I just came across a piece of code I find interesting (because I have never seen it as a question before in 2 years of programming)

int x = 5;
int y = 3;
int z = y + (y+=1) % 4 + (x-=2) / 3;
System.out.println(z);

The output is 4.

I am wondering why is the left most 'y' evaluated first instead of the '(y+=1)' which would then resulted in an output of 5. (in other words, why is the bracket not forcing the order of precedence?)

I am not sure what to search since searching 'java order of precedence' returns results that at best shows tricky examples of y++, ++y kind of questions or just the order of precedence table.

I tagged Java but I have tested this with C# and javascript so it is probably a general thing in programming.

Update

I was mistaken about the order of precedence and order of evaluation.

This article helped me to further understand the answers provided.

10
  • 3
    What is strange about evaluating the left-most term first? Commented Aug 25, 2016 at 11:35
  • 4
    You wouldn't see that as a question because any programmer worth his salt wouldn't write something like that. These kinds of questions are quite worthless. Commented Aug 25, 2016 at 11:36
  • 1
    @JFMeier I would like to know why the bracket '(y+=1)' was not done first. Commented Aug 25, 2016 at 11:37
  • 2
    @Kayaman I know that no 1 would write something like this, it is just 1 of those academic questions used to test students on their understanding of order of precedence. Commented Aug 25, 2016 at 11:37
  • 5
    Don't confuse precedence with order of evaluation. The brackets change how the expression is parsed but not the order of evaluation. Commented Aug 25, 2016 at 11:42

2 Answers 2

11

In short, the parentheses only apply to a particular term.

The expression y + (y+=1) % 4 + (x-=2) / 3; can be rewritten as t1 + t2 + t3, with t1 standing for y, t2 for (y+=1) % 4, and t3 for (x-=2) / 3.

Java always evaluates this from left to right, since the associativity of the binary operator + is from left to right. Hence t1 is the first term to be evaluated and so is done so with the unincremented value of y.

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

10 Comments

This is the key - evaluation is always LTR. I believe in C++ the evaluation order is undefined...
Indeed, the evaluation of + is from LTR: I've added that. Note that ternary conditionals, for example, are evaluated from RTL.
This is the order most western language are read left to right. Arabic, Hebrew are read right to left by comparison.
@BoristheSpider: In C++ and C, the value of this this expression is indeed undefined. But it's more to do with the fact that + is not a sequencing point in C and C++, as opposed to the order of evaluation.
A source of confusion is school mathematics teachers who say you should evaluate brackets first (with mnemonics such as BODMAS). Unfortunately, people tend to confuse precedence order with evaluation order, They are totally different things.
|
8

As per Java language Specs Evaluate Left-Hand Operand First and Evaluate Operands before Operation

1 Comment

This is the correct answer - the fact that evaluation order is prescribed by the Java Language Specification.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.