2011-03-24 99 views
25

从下面的程序或here,为什么上次拨打System.out.println(i)打印价值7Java:增量/减量运算符的前缀/后缀?

class PrePostDemo { 
    public static void main(String[] args){ 
      int i = 3; 
      i++; 
      System.out.println(i); // "4" 
      ++i;    
      System.out.println(i); // "5" 
      System.out.println(++i); // "6" 
      System.out.println(i++); // "6" 
      System.out.println(i); // "7" 
    } 
} 
+6

我相信我多少有些了解您的误解来自于。你认为只有当它是一个声明时,一个新值才会被分配给'i'?将参数传递给函数时,语句(在本例中为post和前缀)在传递它们之前执行。如下面的答案中所述,添加postfix和prefix之间的行为差​​异,并理解为什么您会得到该输出。 – 2011-03-24 01:22:39

+0

[x = x ++“之后的x是什么?](http://stackoverflow.com/questions/7911776/what-is-x-after-xx) – nawfal 2014-07-20 08:56:27

+0

http://stackoverflow.com/a/ 30480534/4533771 – dnuka 2015-06-16 02:00:39

回答

54
i = 5; 
System.out.println(++i); //6 

这种打印出“6”,因为它需要我增加了一个它和返回值。 5 + 1 = 6;这是前缀,在操作中使用它之前添加到数字中。

i = 6; 
System.out.println(i++); //6 (i = 7, prints 6) 

这打印出“6”,因为它需要我,存储副本,加1并返回副本。所以你得到了我的价值,同时也增加了它的价值。因此,你打印出旧值,但它会增加。后缀增量的美观。

然后当你打印出我时,它显示了我的实际价值,因为它已经增加了。 7

+1

在这里所有其他的答案说,“我”的价值将首先使用,然后它会增加,但正如你所说的是正确的,价值增加,然后返回旧值的复制。当我们以i = 5; i = i ++;为例时,我们理解更多;如果这个值先被赋值然后递增,那么我会是6,但在这种情况下是5 – Saumyaraj 2016-07-15 18:34:23

2
System.out.println(i++); // "6" 

这发出println我有这行代码之前的值(6),然后递增I(〜7)。

2

为什么变量没有被更新?

  • 后缀:将i的当前值传递给函数,然后递增它。
  • 前缀:递增当前值,然后将其传递给函数。

你不会做任何事的行我没有区别。

注意,这也是功课true:

i = 0; 
test = ++i; // 1 
test2 = i++; // 1 
0

考虑一下临时变量吧。

i =3 ; 
i ++ ; // is equivalent to: temp = i++; and so , temp = 3 and then "i" will increment and become  i = 4; 
System.out.println(i); // will print 4 

现在,

i=3; 
System.out.println(i++); 

相当于++ii++类似于i = i+1.虽然它不尽相同

temp = i++; // temp will assume value of current "i", after which "i" will increment and become i= 4 
System.out.println(temp); //we're printing temp and not "i" 
3

思考。不同的是,实际上i将被分配新的增量。

in ++i,增量立即发生。

但是如果i++在程序转到下一行时会出现增量。

看看这里的代码。

int i = 0; 
while(i < 10){ 
    System.out.println(i); 
    i = increment(i); 
} 

private int increment(i){ 
    return i++; 
} 

这将导致非无限循环。因为我将以原始值返回,在分号后我会增加,但返回的值没有。因此,我永远不会实际返回作为递增值。

9

我知道这已被回答,但认为另一种解释可能会有所帮助。

另一种方式来说明它是:

++i会给new i的结果,i++会给原来i的结果和存储new i下一个动作。

想一想的方法是,在表达式中做其他事情。在打印当前值i时,它将取决于i是在表达式内还是在表达式之后被更改。

int i = 1; 
result i = ++i * 2 // result = 4, i = 2 

i在计算结果之前进行评估(更改)。对此表达式打印i显示用于该表达式的改变的值i

result i = i++ * 2 // result = 2, i = 2 

i在计算结果后进行评估。因此,从该表达式中打印i给出了在该表达式中使用的原始值i,但i仍然为了任何进一步的用途而改变。因此,在表达式之后立即打印i的值,将显示新增值i。由于i的值已更改,无论是打印还是使用。

result i = i++ * 2 // result = 2, i = 2 
System.out.println(i); // 2 

如果你保持一致的模式,包括所有的值打印线:

int i = 3; 
System.out.println(i); // 3 
System.out.println(i++); // 3 
System.out.println(i); // "4" 
System.out.println(++i); // 5   
System.out.println(i); // "5" 
System.out.println(++i); // "6" 
System.out.println(i++); // "6" 
System.out.println(i); // "7" 
+2

我希望这个答案可以帮助有人用java挣扎,因为我一直在挣扎。欢呼声 – 2013-11-18 06:33:58

0

也许你能理解这个例子更好的前缀/后缀。

public class TestPrefixPostFix 
{ 
    public static void main (String[] args) 
    { 
     int x=10; 
     System.out.println((x++ % 2 == 0)?"yes "+ x: " no "+x); 
     x=10; 
     System.out.println((++x % 2 == 0)?"yes "+ x: " no "+x); 
    } 
}  
0

这是我的答案。有些人可能会觉得很容易理解。

package package02; 

public class C11PostfixAndPrefix { 

    public static void main(String[] args) { 
     // In this program, we will use the value of x for understanding prefix 
     // and the value of y for understaning postfix. 
     // Let's see how it works. 

     int x = 5; 
     int y = 5; 

     Line 13: System.out.println(++x); // 6 This is prefixing. 1 is added before x is used. 
     Line 14: System.out.println(y++); // 5 This is postfixing. y is used first and 1 is added. 

     System.out.println("---------- just for differentiating"); 

     System.out.println(x); // 6 In prefixing, the value is same as before {See line 13} 
     System.out.println(y); // 6 In postfixing, the value increases by 1 {See line 14} 

     // Conclusion: In prefixing (++x), the value of x gets increased first and the used 
     // in an operation. While, in postfixing (y++), the value is used first and changed by 
     // adding the number. 
    } 
} 
1

它打印最后声明,在上面的语句COS,它的价值是,它的递增至7时,最后一个语句会打印

+0

提问者有代码,请用代码解释你的答案 – Ibo 2018-02-12 20:38:04