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

后端 未结 14 2576
暖寄归人
暖寄归人 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:08

    In both cases it first calculates value, but in post-increment it holds old value and after calculating returns it

    ++a

    1. a = a + 1;
    2. return a;

    a++

    1. temp = a;
    2. a = a + 1;
    3. return temp;

提交回复
热议问题