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

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

    In the above example

    int a = 5,i;
    
    i=++a + ++a + a++;        //Ans: i = 6 + 7 + 7 = 20 then a = 8 
    
    i=a++ + ++a + ++a;        //Ans: i = 8 + 10 + 11 = 29 then a = 11
    
    a=++a + ++a + a++;        //Ans: a = 12 + 13 + 13 = 38
    
    System.out.println(a);    //Ans: a = 38
    
    System.out.println(i);    //Ans: i = 29
    

提交回复
热议问题