I am having a bit of a confusion with the prefix and the postfix increment.
I understand the expression is evaluated from the right hand side and prefix have more precedence i.e:
Example 0
int i = 0;
int a = 0;
i = ++a + ++a + a++;
System.out.println("i: "+i+" a:"+a);
in above jvm will evaluate (++a + ++a) and the resultant a will be passed to (a)++ however JVM wont increment a in this cycle.
so the output will be a:3 i: 5 reason being ((1+2) + 2) at the end value of i is 5 and the value of a is 3.
My confusion is when I am doing
Example 1
int a=0,i = 0;
i=a + a++ + a;
System.out.println("a: "+a+" i: "+i);
Example 2
int a=0,i = 0;
i=a + a++ + a++;
System.out.println("a: "+a+" i: "+i);`
Example 3
int a=0,i = 0;
i=a++ + a++ + a++;
System.out.println("a: "+a+" i: "+i);`
the output is coming up as:
a: 1 i: 1
a: 2 i: 1
a: 3 i: 3
If we look at Example 0 we can deduce a++ wont be executed in the same expression which contradicts sout from Example 1, In Example 1 I was expecting i to be 0 as all a’s are 0 but instead its coming up as 1.
if we assume a++ is being executed in the same expression then why in the "Example 2" its not showing at least 2, why is it shoing 1.
In Example 3 I am assuming its some how calculating as (1+1+1) if this is the case then should’nt Example 2 be like (0+1+1)
Can someone please shed some light on this?
Have checked How do the post increment (i++) and pre increment (++i) operators work in Java? example with did clear some confusion but not all of them
>Solution :
Example 0
i = ++a + ++a + a++;
That turns into:
i = ((++a) + (++a)) + (a++); (because: Precedence rules; unary increments are resolved before binary add, and binary add is resolved left to right, so given X+Y+Z, first X+Y is calculated, then that+Z is calculated.
Thus (a starts at 0):
- First, resolve
++a; that expression’s value is 1, and as a side-effect,ais now 1. - Then eval
++a(the second++a); that expression’s value is 2, and as a side-effect,ais now 2. - Resolve the binary add; 1+2 is 3.
- Now resolve
a++– that expression’s value is 2, and as a side-effect,ais now 3. - Now do the add: 3+2 is 5. So,
iis 5.
Makes perfect sense, no?
Example 1
i=a + a++ + a;
That turns into:
i = (a + (a++)) + a; for the same reasons (unaries first, then left-to-right).
- First resolve
a, that’s 0 and has no side-effects. - Next resolve
a++, which is 0, and as a side-effect,ais now 1. - do the math – 0+0 is.. 0.
- resolve
awhich is 1, and has no side-effects. - Do the math – 0+1 is 1.
so, a is 1, and i is 1.
Example 2
i=a + a++ + a++;
That turns into:
i = (a + (a++)) + (a++);
- First resolve
a, that’s 0 and has no side-effects. - Next resolve
a++, which is 0, and as a side-effect,ais now 1. - do the math – 0+0 is.. 0.
- resolve
a++which is 1, and as a side-effect,ais now 2. - Do the math – 0+1 is 1.
so, a is 2, and i is 1.
Example 3
i=a++ + a++ + a++;
That turns into:
i = ((a++) + (a++)) + (a++);
- First resolve
a++(first one), that’s 0 and as a side-effect,ais now 1. - Next resolve
a++(second one), which is 1, and as a side-effect,ais now 2. - do the math – 0+1 is 1.
- resolve
a++(third one) which is 2, and as a side-effect,ais now 3. - Do the math – 1+2 is 3.
so, a is 3, and i is 3.
we look at Example 0 we can deduce
a++wont be executed
I have no idea how you deduced that, but that’s incorrect. Of course it’s executed. In general, ‘is not executed in the same expression’ doesn’t make any sense. You might be entirely misunderstanding how any of this works / you’re overcomplicating matters. Java is very simple:
Following the operator precedence list – start at the top of the list and find all operations using those operators. "reduce" them (calculate the result, i.e. turn 5+2 into 7), going left to right. Once you’ve gotten to the end, go to the next batch of operators on the list, and do the exact same thing. Keep going until you get to the end of the list. Voila. You’re done.