【问题标题】:Java: Prefix/postfix of increment/decrement operators?Java:递增/递减运算符的前缀/后缀?
【发布时间】:2011-07-21 18:38:23
【问题描述】:

从下面的程序或here,为什么最后一次调用System.out.println(i) 打印值7

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"
     }
}

【问题讨论】:

  • 我相信我有点理解你的误解来自哪里。您认为只有在单独声明时才会将新值分配给i?将参数传递给函数时,语句(在本例中为 post 和 prefix)在传递它们之前被执行。添加后缀和前缀之间的行为差​​异,如下面的答案中所述,您就会明白为什么会得到该输出。

标签: java post-increment pre-increment


【解决方案1】:
i = 5;
System.out.println(++i); //6

这会打印出“6”,因为它需要 i,将 i 加一,然后返回值:5+1=6。这是前缀,在操作中使用之前添加到数字。

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

这会打印出“6”,因为它需要 i,存储一个副本,将 1 加到变量中,然后返回该副本。所以你得到了我的价值,但同时也增加了它。因此,您打印出旧值,但它会增加。后缀增量的美妙之处。

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

【讨论】:

  • 这里的所有其他答案都表示将首先使用“i”的值,然后将其递增,但正如您所说的正确,值会递增,然后返回旧值的 COPY .当我们以 i = 5;i = i++; 为例时,我们会理解更多。如果首先分配值并递增,则 i 将为 6,但在本例中为 5
【解决方案2】:

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

另一种说明方式是:

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

【讨论】:

    【解决方案3】:

    ++ii++ 视为与i = i+1. 相似,但它不一样。区别在于i 何时获得新的增量。

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

    但如果i++ 有增量,程序进入下一行时会发生增量。

    看这里的代码。

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

    这将导致非结束循环。因为i 将返回原始值,在分号之后我会增加但返回值没有。因此i 永远不会实际返回为递增值。

    【讨论】:

      【解决方案4】:

      为什么变量没有更新?

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

      你不做任何事情的台词 i 没有区别。

      请注意,赋值也是如此:

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

      【讨论】:

        【解决方案5】:
        System.out.println(i++);  // "6"
        

        这会发送println 我在这行代码之前的值 (6),然后将 I 递增(到 7)。

        【讨论】:

          【解决方案6】:

          我知道这是一个非常古老的问题,但我没有看到列出的此类答案。看一个实际操作符如何实现的例子对我有帮助,也许对其他人有帮助

          class Integer {
            private int __i;
          
            function Integer ++() { // prefix operator i.e. ++x
              __i+=1; //increment
              return this; //return object with incremented value
            }
          
            function Integer ++(Integer x) { //postfix operator i.e. x++
              __i+=1; //increment
              return x; //return original object
            }
          }
          

          【讨论】:

            【解决方案7】:

            考虑一下临时变量。

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

            等价于

            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"
            

            【讨论】:

              【解决方案8】:

              也许你可以通过这个例子更好地理解前缀/后缀。

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

              【讨论】:

                【解决方案9】:

                这是我的答案。你们中的一些人可能会发现它很容易理解。

                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. 
                    }
                }
                

                【讨论】:

                  【解决方案10】:

                  它为最后一条语句打印7,因为在上面的语句中,它的值是6,当最后一条语句被打印时它递增到7

                  【讨论】:

                  • 提问者有代码,请用代码说明你的答案
                  【解决方案11】:

                  这可能会有所帮助..我也理解了这个谜题..

                  公共类主要{ public static void main(String[] args) {

                  int x = 5;
                  int y=5;
                  
                  System.out.println(++x);
                  System.out.println(y++);
                  
                  System.out.println("------");
                  
                  System.out.println(x);
                  System.out.println(y);
                  

                  } }

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 2011-03-12
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2015-07-29
                    相关资源
                    最近更新 更多