Post and Pre increment operators

2020-04-03 10:22发布

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

class ZiggyTest2{

        static int f1(int i) {  
            System.out.print(i + ",");  
            return 0;  
        } 

        public static void main(String[] args) {  
            int i = 0;  
            int j = 0;  
            j = i++;   //After this statement j=0 i=1
            j = j + f1(j);  //After this statement j=0 i=1
            i = i++ + f1(i);  //i++ means i is now 2. The call f1(2) prints 2 but returns 0 so i=2 and j=0
            System.out.println(i);  //prints 2?
        } 
    } 

I don't understand why the output is 0,2,1 and not 0,2,2

标签: java scjp ocpjp
7条回答
欢心
2楼-- · 2020-04-03 10:33

Hope this explaination might be of some help :

j = i++; // Here since i is post incremented, so first i being 0 is assigned to j
         // and after that assignment , i is incremented by 1 so i = 1 and j = 0.

i = i++ + f1(i); // here again since i is post incremented, it means, the initial value 
                 // of i i.e. 1 as in step shown above is used first to solve the 
                 // expression i = i(which is 1) + f1(1)**Since i is 1**
                 // after this step the value of i is incremented. so i now becomes 2
                 // which gets displayed in your last System.out.println(i) statement.   

Do try this

i = ++i + f1(i); // here i will be first inremented and then that value will be used 
                 // to solve the expression i = i + f1(i)'

So in short during post increment, expression is first solved and then value is incremented. But in pre increment, value is first incremented and then expression is solved.

But if you write only

i++;
++i;

Then both means the same thing.

Regards

查看更多
Anthone
3楼-- · 2020-04-03 10:40

To get In-depth you need to see Expression and its Evaluation Order

Here's little explanation about equation i++ + f1(i) evaluation

In Equation Compiler get "i" which is Equals to 1 and put it on stack as first operand then increments "i", so its value would be 2, and calculates second operand by calling function which would be 0 and at the time of operation (+) execution operands would be 1 and 0.

查看更多
乱世女痞
4楼-- · 2020-04-03 10:42

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.

查看更多
beautiful°
5楼-- · 2020-04-03 10:42

In Post increment operator value will of operand will increase after use. Example

int k =1;
        int l = k++;
        System.out.println("...k..."+k+"...l.."+l);

First k (value = 1) is assigned with l after that the k value will increase

Similarly thing happens in the following line

i = i++ + f1(i);
查看更多
来,给爷笑一个
6楼-- · 2020-04-03 10:47

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;
查看更多
Root(大扎)
7楼-- · 2020-04-03 10:49
 i = i++ + f1(i);  

i++ means i is now 2. The call f1(i) prints 2 but returns 0 so i=2 and j=0

before this i = 1 , now imagine f1() called and replaced with 0

so

i = i++ + 0;

now it would be

i = 1 + 0 // then it will increment i to 2 and then (1 +0) would be assigned back to `i`

In Simpler words (from here @ Piotr )

"i = i++" roughly translates to

int oldValue = i; 
i = i + 1;
i = oldValue; 

Another such Example :

查看更多
登录 后发表回答