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

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

    pre-increment and post increment are equivalent if not in an expression

    int j =0;
    int r=0         
    for(int v = 0; v<10; ++v) { 
              ++r;
              j++;
              System.out.println(j+" "+r);
      }  
     1 1  
     2 2  
     3 3       
     4 4
     5 5
     6 6
     7 7
     8 8
     9 9
    10 10
    

提交回复
热议问题