0
package com;

public class Test {

    public static void main(String[] args)
    {
        int a= 11, b = 10;
        a = a++ + ++b;  //why? output is "22 11" and not "23 11"
        System.out.println(a+" "+b);

    }

}
Amit Bera
  • 7,075
  • 1
  • 19
  • 42
  • 2
    Possible duplicate of [How do the post increment (i++) and pre increment (++i) operators work in Java?](https://stackoverflow.com/questions/2371118/how-do-the-post-increment-i-and-pre-increment-i-operators-work-in-java) – Marvin Sep 14 '19 at 14:29
  • post-increment: a++ -> a will evaluate in the expression and then increment Pre-Increment: ++a -> a will be incremented first then it will be evaluated in the expression in your example: a = 11 will be used in the expression, then after a will be assigned with 12, b will be incremented first and it will be 11, and then it will be used in the expression. so a+ b will be : 22 and it's assigned to a, then a = 22 and b will be 11 before final print to console. – Arpan Saini Sep 14 '19 at 17:41
  • No professional writes code like `a = a++ + ++b;` – Raedwald Sep 17 '19 at 16:14

2 Answers2

0

Here's how the expression gets evaluated (roughly, I didn't check the JLS for evaluation order but I think it's from left to right):

a = a++ + ++b;  // a is 11, b is 10
a = 11 + ++b;  // a is 12 but its previous value 11 was returned by a++, b is 10
a = 11 + 11;  // a is 12, b is 11 and its updated value was returned by ++b
a = 22;  // a is 12, b is 11 and its updated value was returned by ++b

Therefore, it's the expected result, you just have to apply the definition of those operators.

Dici
  • 25,226
  • 7
  • 41
  • 82
  • you have written that variable a is 12...then why the compiler ignored the value of a++ . I know the difference between the pre and post increment operators. But this equation seems ambiguous to me. – Kunal Kumar Sep 14 '19 at 20:04
  • What do you mean it ignored the value? `a++` returns 11 if `a` is 11 before executing this expression, and after it `a` will be `12` but it will still have returned 11. I don't get what you find ambiguous – Dici Sep 14 '19 at 21:26
  • "after it a will be 12 but it will still have returned 11." okay i understood that a = 12 and returned 11 but what happened to the value 12 after all of this..... :| – Kunal Kumar Sep 16 '19 at 16:15
  • Well if you read `a`'s value again after this, it will be 12. The 12 has been stored in `a`. – Dici Sep 17 '19 at 07:54
  • No! That's the point of my question a's value is 22 and not 12 after it. Maybe 22 has overriden 12 right???? – Kunal Kumar Oct 03 '19 at 12:03
  • Ah yeah sorry it's reassigned. Yeah well, the 12 is just gone that's, just like when you do `a = 1; a = 2` – Dici Oct 03 '19 at 14:57
0

a++ means that "a" has old value in place where you write it and change it after. ++a means that "a" at fist change value and than will be calculate.

So:
a = 5;
b = 5;
So:
a++ + ++b = (5 (and a + 1 later) + (at first b + 1) 6.

I hope you understand me) My English is in the same level with your Java=)

Oleg Pavlyukov
  • 151
  • 1
  • 11