Post and Pre increment operators

后端 未结 7 1094
面向向阳花
面向向阳花 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

    If we expand i = i++ + f1(i) statement, we get something like following

    save the value of i in a temp variable, say temp1 // temp1 is 1
    increment i by one (i++)                          // i gets the value 2
    execute f1(i), save return value in, say temp2    // temp2 is 0, print 2
    assign temp1 + temp2 to i                         // i becomes 1 again
    

    I guess main steps can be summarized like above.

提交回复
热议问题