I have a question here already of my own but I want to extend it Post increment with example
char a = \'D\';
int b = 5;
System.out.println(a++/b+--a*b++);
Don't confused evaluation order with precedence.
Evaluation order states that operands are always executed left to right.
Precedence states that *
and /
operators are applied before +
and -
, unless overridden by parenthesis.
So, you're right that a++/b+--a*b++
means (a++ / b) + (--a * b++)
. That's precedence.
Since numeric operators promote values to int (in this case), you're also right that char a = 'D'
is equivalent to int a = 68
.
So:
(a++ / b) + (--a * b++) a = 68 b = 5
(68 / b) + (--a * b++) a = 69 b = 5
(68 / 5) + (--a * b++) a = 69 b = 5
(68 / 5) + (68 * b++) a = 68 b = 5
(68 / 5) + (68 * 5 ) a = 68 b = 6
13 + 340 a = 68 b = 6
353 a = 68 b = 6
As you can see, b++
does have an effect: On the value of b
after executing the expression.
Step 4: (68/6+68*5) its your wrong! b is 5 in all positions in the calculation instead of it's value 6.
so we have:
Step 4: (68/5+68*5) = (13+340)=353