-5
int i = 1;
System.out.println(i++ + ++i);

Why is this 4 given the following:

  1. Expressions in Java are evaluated from left to right (after precedence)
  2. Post increment (i++) has higher precedence. So it should be 1 + 2.

I recommend deleting the question by a moderator, since the answer is simply (as per Joachim's comment) that the evaluation of expression in context of post increment operator includes any expression within the expression and not just the top one of the primary statement (sysout in this case). Given i++ is a statement in itself which increments a variable by 1 it gets already executed in sub-expression (i++) but returns i to parent expression. that was simply what I was not aware of.

user1132655
  • 243
  • 4
  • 14
  • https://stackoverflow.com/a/3879280/829571 – assylias Aug 19 '21 at 09:10
  • 2
    You seem to understand that the evaluation order is from left to right, so `i++` is evaluated first to `1` and `i` is now 2. So `i` is 2 when `++i` is evaluated. Since `i` is 2, `++i` evaluates to 3. Nothing to do with precedence. – Sweeper Aug 19 '21 at 09:11
  • 3
    @user1132655: users are not required to explain downvotes. I've not downvoted, but I can imagine two reasons: 1. there's already **tons** of very thorough explanations of what happens in these situations on SO, it's not our problem if you didn't find them and 2. this kind of problem is a purely theoretical one: if you were to write code like that in production, any sane code reviewer would tell you rewrite it in a more readable way. There's literally no reason to ever really write code like that. – Joachim Sauer Aug 19 '21 at 09:14

1 Answers1

4

It is evaluated left-to-right:

  1. i++ is evaluated.
    • i is incremented by 1, so it's now 2
    • the value of this expression is 1, as the initial value of i is returned
  2. ++i is evaluated
    • i is incremented by 1, so it's now 3
    • the value of this expression is 3, as the value after incrementing is returned
  3. the addition in i++ + ++i is evaluated
    • i++ was evaluated to 1
    • ++i was evaluated to 3
    • 1+3 is 4
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • You say: i is incremented by 1, so it's now 2. But it is not, given that it will be 2 only after the whole expression (i++ + ++i) is evaluated. Or am I wrong here? – user1132655 Aug 19 '21 at 09:19
  • You are wrong, and I don't understand why you'd think that. After `i++` is evaluted (before anything else), `i` is already `2`. Yes, this is different from the *value of the expression `i++`, but that's the whole difference between `i++` and `++i`. After the whole expression is evaluated `i` will be 3. – Joachim Sauer Aug 19 '21 at 09:28
  • As I thought: (i++ + ++i) is the expression itself. And only after it is evaluated (the full expression) will i increase for the postfix. I also added more details to the original post. It seems that the issue here is that I misunderstand the notion of expression. – user1132655 Aug 19 '21 at 09:32
  • And that's where you're wrong. Each part of that expression is also an expression. `i` on its own would be an expression. `i++` is an expression that follows the rule that I've noted above. `i++` doesn't "wait" for "the whole expression" to finish calculating before updating `i`. It does its own job and then is done. – Joachim Sauer Aug 19 '21 at 09:35
  • I don't understand your logic: I'm explaining a rule set that is consistent with the results taht you observe. And yet you insist on applying a *different* rule set that is *not* consistent with the rules that you observe. if the results agree with my definition, then why do you insist that my definition is wrong? – Joachim Sauer Aug 19 '21 at 09:36
  • Thanks, Joachim, No I am not insisting on anything, all I thought is that the expression is always the full expression of a statement. Now that you say "i on its own would be an expression" and "Each part of that expression is also an expression" that answers the question for me. I just did not know this, I thought that is called sub-expression and is not subject to "post increment after evaluation". – user1132655 Aug 19 '21 at 09:42