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

后端 未结 14 2500
暖寄归人
暖寄归人 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 04:57

    ++a is prefix increment operator:

    • the result is calculated and stored first,
    • then the variable is used.

    a++ is postfix increment operator:

    • the variable is used first,
    • then the result is calculated and stored.

    Once you remember the rules, EZ for ya to calculate everything!

提交回复
热议问题