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

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

    I believe you are executing all these statements differently
    executing together will result => 38 ,29

    int a=5,i;
    i=++a + ++a + a++;
    //this means i= 6+7+7=20 and when this result is stored in i,
    //then last *a* will be incremented 
    i=a++ + ++a + ++a; //this means i= 5+7+8=20 (this could be complicated, //but its working like this),
    a=++a + ++a + a++; //as a is 6+7+7=20 (this is incremented like this)

提交回复
热议问题