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

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

    ++a increments and then uses the variable.
    a++ uses and then increments the variable.

    If you have

    a = 1;
    

    and you do

    System.out.println(a++); //You will see 1
    
    //Now a is 2
    
    System.out.println(++a); //You will see 3
    

    codaddict explains your particular snippet.

提交回复
热议问题