Post Increment with substitution

后端 未结 2 1933
北恋
北恋 2021-01-29 11:26

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++);


        
相关标签:
2条回答
  • 2021-01-29 12:04

    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.

    0 讨论(0)
  • 2021-01-29 12:08

    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

    0 讨论(0)
提交回复
热议问题