How do the post increment (i++) and pre increment (++i) operators work in Java?

后端 未结 14 2545
暖寄归人
暖寄归人 2020-11-21 04:45

Can you explain to me the output of this Java code?

int a=5,i;

i=++a + ++a + a++;
i=a++ + ++a + ++a;
a=++a + ++a + a++;

System.out.println(a);
System.out.p         


        
14条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-21 05:04

    Pre-increment means that the variable is incremented BEFORE it's evaluated in the expression. Post-increment means that the variable is incremented AFTER it has been evaluated for use in the expression.

    Therefore, look carefully and you'll see that all three assignments are arithmetically equivalent.

提交回复
热议问题