Post and Pre increment operators

后端 未结 7 1033
面向向阳花
面向向阳花 2021-01-17 21:43

When i run the following example i get the output 0,2,1

class ZiggyTest2{

        static int f1(int i) {  
            System.out.print(i + \",\");  
               


        
相关标签:
7条回答
  • 2021-01-17 22:37

    Pre increment means: add one to variable and return incremented value; Post increment - first return i, then increment it;

    int i, j, k;
    i = 0; // 0
    j = i++; // return i , then increment i
    // j = 0; i = 1;
    k = ++i; // first increment and return i
    //k = 2; i = 2;
    
    // now
    ++j == --k == --i // would be true => 1==1==1;
    // but , using post increment would 
    // j++ == k-- == i-- // false because => 0 == 2 == 2;
    // but after that statement j will be 1, k = 1, i = 1;
    
    0 讨论(0)
提交回复
热议问题