Java preincrement和postincrement运算符

时间:2014-11-04 05:18:49

标签: java increment

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);
    }
}

输出
2
3
4

昨天我的朋友问了这个问题。我对此有点困惑。 第1部分 i 值打印为 2 。后增量在这里不起作用。但是在第2部分中,它有效。我可以理解第2部分,但我对第1部分感到困惑。它究竟如何运作?任何人都可以让我明白吗?

5 个答案:

答案 0 :(得分:2)

第一部分应打印i = 2。这是因为:

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);
    }
}

理解这一点的最简单方法可能是引入一些额外的变量。这是你拥有的:

// 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)

注意设置temp变量的时间顺序的差异 - 之前的之后的取决于它是否' sa后增量(i++)或预增量(++i)。

答案 1 :(得分:0)

i = i++无法按照您期望的方式工作的原因是因为后期增量(您使用它的方式)的工作方式如下:

  1. 获取i
  2. 的值
  3. 将原始文件(在步骤1中复制)i增加一个。
  4. 将新的i值设置为原始值。
  5. 那么i仍然是i

答案 2 :(得分:0)

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++)

Java中阅读prefixpostfix运算符。

答案 3 :(得分:0)

好吧,我想你已经理解了第二部分和你在第一部分的困惑

++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)

检查上面的示例,你可以很容易地理解这里发生的事情......

根据您的代码

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

first i=i++;

那么i的值将为1

then next setp i=++i;

然后它将增加1,现在我将是2

next step again i=i++;

那么我的价值仍然是2! //如果最后一步是i = ++ i;然后结果将是3

答案 4 :(得分:0)

找到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 )

这里我们将旧值从堆栈存储到局部变量。还可以在下面的代码中找到注释,以查看所有值的更改。

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);
}