Java preincrement and postincrement operators

社会主义新天地 提交于 2019-12-13 10:03:55

问题


public class Sample 
{
    public static void main(String[] args) throws Exception 
    {
        //part 1
        int i=1;
        i=i++;
        i=++i;
        i=i++;
        System.out.println(i);

        //part 2
        i=1;
        int a=i++;
        a=++i;
        a=i++;
        System.out.println(a+"\n"+i);
    }
}

Output
2
3
4

Yesterday my friend asked this question. I little bit confused about this. part 1 prints the i value as 2. Post increment is not working here. But in the part 2, it works. I can understand the part 2 but i have confused in part 1. How actually it works? Can anybody make me understand?


回答1:


Part one should print i = 2. This is because:

public class Sample 
{
    public static void main(String[] args) throws Exception 
    {
        //part 1
        int i=1;
        // i++ means i is returned (which is i = 1), then incremented, 
        //  therefore i = 1 because i is incremented to 2, but then reset 
        //  to 1 (i's initial value)
        i=i++;
        // i is incremented, then returned, therefore i = 2
        i=++i;
        // again, first i is returned, then incremented, therefore i = 2 
        //  (see first statement)
        i=i++;
        System.out.println(i);

        //part 2
        i=1;
        // first i is returned then incremented, so i = 2, a = 1
        int a=i++;
        // i is incremented then returned, so i = 3 and a = 3
        a=++i;
        // i is first returned, then incremented, so a = 3 and i = 4
        a=i++;
        System.out.println(a+"\n"+i);
    }
}

Perhaps the simplest way to understand this is to introduce some extra variables. Here is what you have:

// i = i++
temp = i;
i = i + 1;
i = temp; // so i equals the initial value of i (not the incremented value)

// i = ++i;
i = i + 1;
temp = i;
i = temp; // which is i + 1

// i = i++
temp = i;
i = i + 1;
i = temp; // so i equals the previous value of i (not i + 1)

Notice the difference in order of when the temp variable is set--either before or after the increment depending on whether or not it's a post-increment (i++) or a pre-increment (++i).




回答2:


The reason that i = i++ does not work in the way you expect it to work, is because post increment (the way you used it in) works like this:

  1. Get the value of i
  2. Increment original (copied in step one) i by one.
  3. Set new i value to the original value.

So then i would still be i




回答3:


In Java

   //part 1
    int i = 1;
    i = i++; // i is still 1 since increment will rest by the assignment.
    i = ++i; // i+1=2 (since prefix increment)
    i = i++; // i is still 2
    System.out.println(i); // out put 2

    //part 2
    i = 1;
    int a = i++; // a is 1
    a = ++i; //a=1+2(since i++ now i is 2)=3
    a = i++;// a still 3
    System.out.println(a + "\n" + i); // now a is 3 is 4(since i++)

Read prefix and postfix operator in Java.




回答4:


Find the byte code of i=i++

2: iload_1  ; load to stack (local = 1, stack = 1)
3: iinc          1, 1; increment local variable (local =2, stack = 1)
6: istore_1 ; store value from stack to local variable (local = 1 )

Here we are storing old value from stack to local variable. Also find the comments in below code to see all the value changes.

public static void main(String[] args) throws Exception 
{
    //part 1
    int i=1;
    i=i++;// i=1 here i will be assigned with 1. increment will not affect
    i=++i;// i=2 
    i=i++;// i=2
    System.out.println(i);

    //part 2
    i=1;
    int a=i++;// a=1, i=2
    a=++i;// a=3, i= 3
    a=i++;// a=3, i=4
    System.out.println(a+"\n"+i);
}



回答5:


ok i think already you understood the second part and your confusion in first part

++i will increment the value of i, and then return the incremented value.

 i = 1;
 j = ++i;
 here -->i is 2, j is 2
i++ will increment the value of i, but return the original value that i held before being incremented.

 i = 1;
 j = i++;
 here -->i is 2, j is 1)

check the example above from that you can understand easly what happends here ....

as per your code

int i=1;
        i=i++;
        i=++i;
        i=i++;
        System.out.println(i);

first i=i++;

then the value of i will be 1

then next setp i=++i;

then it will be incremented by 1 and now i will be 2

next step again i=i++;

then also i value remain same that is 2 ! // if the last step is i=++i; then the result will be 3



来源:https://stackoverflow.com/questions/26728216/java-preincrement-and-postincrement-operators

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!