【问题标题】:Is there a difference between x++ and ++x in java?java中的x++和++x有区别吗?
【发布时间】:2010-11-08 20:30:55
【问题描述】:

java中的++x和x++有区别吗?

【问题讨论】:

    标签: java syntax operators increment


    【解决方案1】:

    在Java中x++和++x有区别

    ++x 是前缀形式: 它递增变量表达式,然后在表达式中使用新值。

    例如如果在代码中使用:

    int x = 3;
    
    int y = ++x;
    //Using ++x in the above is a two step operation.
    //The first operation is to increment x, so x = 1 + 3 = 4
    //The second operation is y = x so y = 4
    
    System.out.println(y); //It will print out '4'
    System.out.println(x); //It will print out '4'
    

    x++ 是后缀形式: 变量值首先在表达式中使用,然后在操作后递增。

    例如如果在代码中使用:

    int x = 3;
    
    int y = x++;
    //Using x++ in the above is a two step operation.
    //The first operation is y = x so y = 3
    //The second operation is to increment x, so x = 1 + 3 = 4
    
    System.out.println(y); //It will print out '3'
    System.out.println(x); //It will print out '4' 
    

    希望这很清楚。运行和玩上面的代码应该可以帮助你理解。

    【讨论】:

      【解决方案2】:

      有很大的不同。

      由于大多数答案已经指出了理论,我想指出一个简单的例子:

      int x = 1;
      //would print 1 as first statement will x = x and then x will increase
      int x = x++;
      System.out.println(x);
      

      现在让我们看看++x

      int x = 1;
      //would print 2 as first statement will increment x and then x will be stored
      int x = ++x;
      System.out.println(x);
      

      【讨论】:

        【解决方案3】:

        当考虑计算机实际做什么时......

        ++x:从内存中加载x,递增,使用,存储回内存。

        x++:从内存中加载x,使用,递增,存储回内存。

        考虑: a = 0 x = f(a++) y = f(++a)

        函数 f(p) 返回 p + 1

        x 将为 1(或 2)

        y 将是 2(或 1)

        这就是问题所在。编译器的作者是在检索后、使用后还是存储后传递参数。

        通常,只需使用 x = x + 1。这样更简单。

        【讨论】:

          【解决方案4】:

          这些被称为后缀和前缀运算符。两者都会给变量加1,但是语句的结果有区别。

          int x = 0;
          int y = 0;
          y = ++x;            // result: y=1, x=1
          
          int x = 0;
          int y = 0;
          y = x++;            // result: y=0, x=1
          

          【讨论】:

          • 不应该是suffix吗?
          【解决方案5】:

          在 i++ 中,它被称为后增量,该值在任何上下文中使用,然后递增; ++i 是 preincrement 先递增值,然后在上下文中使用它。

          如果您不在任何上下文中使用它,那么使用什么都没有关系,但按照惯例使用后增量。

          【讨论】:

            【解决方案6】:

            问题已经回答了,但也请允许我从我这边添加。

            首先++表示加一,--表示减一。

            现在 x++ 表示在此行之后增加 x++x 表示在此行之前增加 x

            检查这个例子

            class Example {
            public static void main (String args[]) {
                  int x=17,a,b;
                  a=x++;
                  b=++x;
                  System.out.println(“x=” + x +“a=” +a);
                  System.out.println(“x=” + x + “b=” +b);
                  a = x--;
                  b = --x;
                  System.out.println(“x=” + x + “a=” +a);
                  System.out.println(“x=” + x + “b=” +b);
                  }
            }
            

            它将给出以下输出:

            x=19 a=17
            x=19 b=19
            x=18 a=19
            x=17 b=17
            

            【讨论】:

            • 这个回答如果再配上几句解释就更好了。
            【解决方案7】:

            是的,有一个区别,在 x++(postincrement) 的情况下,x 的值将在表达式中使用,并且 x 将在表达式被计算后增加 1,另一方面 ++x(preincrement) , x+1 将在表达式中使用。 举个例子:

            public static void main(String args[])
            {
                int i , j , k = 0;
                j = k++; // Value of j is 0
                i = ++j; // Value of i becomes 1
                k = i++; // Value of k is 1
                System.out.println(k);  
            }
            

            【讨论】:

              【解决方案8】:

              好的,我来到这里是因为我最近在检查经典堆栈实现时遇到了同样的问题。提醒一下,这是在基于数组的 Stack 实现中使用的,它比链表快一点。

              下面的代码,检查推送和弹出功能。

              public class FixedCapacityStackOfStrings
              {
                private String[] s;
                private int N=0;
              
                public FixedCapacityStackOfStrings(int capacity)
                { s = new String[capacity];}
              
                public boolean isEmpty()
                { return N == 0;}
              
                public void push(String item)
                { s[N++] = item; }
              
                public String pop()
                { 
                  String item = s[--N];
                  s[N] = null;
                  return item;
                }
              }
              

              【讨论】:

                【解决方案9】:

                我是从它最近的一个dup 来到这里的,虽然这个问题已经得到解答,但我还是忍不住反编译了代码并添加了“又一个答案”:-)

                准确地说(可能有点迂腐),

                int y = 2;
                y = y++;
                

                编译成:

                int y = 2;
                int tmp = y;
                y = y+1;
                y = tmp;
                

                如果你javac这个Y.java类:

                public class Y {
                    public static void main(String []args) {
                        int y = 2;
                        y = y++;
                    }
                }
                

                javap -c Y,你会得到以下jvm代码(我允许我在Java Virtual Machine Specification的帮助下评论main方法):

                public class Y extends java.lang.Object{
                public Y();
                  Code:
                   0:   aload_0
                   1:   invokespecial  #1; //Method java/lang/Object."<init>":()V
                   4:   return
                
                public static void main(java.lang.String[]);
                  Code:
                   0:   iconst_2 // Push int constant `2` onto the operand stack. 
                
                   1:   istore_1 // Pop the value on top of the operand stack (`2`) and set the
                                 // value of the local variable at index `1` (`y`) to this value.
                
                   2:   iload_1  // Push the value (`2`) of the local variable at index `1` (`y`)
                                 // onto the operand stack
                
                   3:   iinc  1, 1 // Sign-extend the constant value `1` to an int, and increment
                                   // by this amount the local variable at index `1` (`y`)
                
                   6:   istore_1 // Pop the value on top of the operand stack (`2`) and set the
                                 // value of the local variable at index `1` (`y`) to this value.
                   7:   return
                
                }
                

                因此,我们终于有了:

                0,1: y=2
                2: tmp=y
                3: y=y+1
                6: y=tmp
                

                【讨论】:

                  【解决方案10】:

                  是的,返回的值分别是递增前后的值。

                  class Foo {
                      public static void main(String args[]) {
                          int x = 1;
                          int a = x++;
                          System.out.println("a is now " + a);
                          x = 1;
                          a = ++x;
                          System.out.println("a is now " + a);
                      }
                  }
                  
                  $ java Foo
                  a is now 1
                  a is now 2
                  

                  【讨论】:

                    【解决方案11】:

                    是的。

                    public class IncrementTest extends TestCase {
                    
                        public void testPreIncrement() throws Exception {
                            int i = 0;
                            int j = i++;
                            assertEquals(0, j);
                            assertEquals(1, i);
                        }
                    
                        public void testPostIncrement() throws Exception {
                            int i = 0;
                            int j = ++i;
                            assertEquals(1, j);
                            assertEquals(1, i);
                        }
                    }
                    

                    【讨论】:

                      【解决方案12】:

                      如果它像许多其他语言一样,您可能想尝试一下:

                      i = 0;
                      if (0 == i++) // if true, increment happened after equality check
                      if (2 == ++i) // if true, increment happened before equality check
                      

                      如果上述情况不是这样发生的,它们可能是等价的

                      【讨论】:

                        【解决方案13】:

                        是的,使用 ++X,表达式中将使用 X+1。使用 X++,X 将在表达式中使用,并且 X 只会在表达式被计算后增加。

                        所以如果 X = 9,使用 ++X,将使用值 10,否则将使用值 9。

                        【讨论】:

                          【解决方案14】:

                          是的

                          ++x 增加 x 的值,然后返回 x
                          x++ 返回 x 的值,然后递增

                          示例:

                          x=0;
                          a=++x;
                          b=x++;
                          

                          代码运行后,a 和 b 都将为 1,但 x 将为 2。

                          【讨论】:

                            【解决方案15】:

                            是的,

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

                            将打印6

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

                            将打印5

                            【讨论】:

                            • 这个“答案”只是告诉你一个测试用例的输出,我认为输出不是答案。相反,通常某些代码执行的(意外)结果会导致问题。因此我投了反对票。
                            【解决方案16】:

                            ++x 称为前增量,而 x++ 称为后增量。

                            int x = 5, y = 5;
                            
                            System.out.println(++x); // outputs 6
                            System.out.println(x); // outputs 6
                            
                            System.out.println(y++); // outputs 5
                            System.out.println(y); // outputs 6
                            

                            【讨论】:

                              猜你喜欢
                              • 1970-01-01
                              • 2019-03-25
                              • 2015-10-15
                              • 1970-01-01
                              • 2013-02-12
                              • 2013-09-18
                              • 1970-01-01
                              • 1970-01-01
                              • 2019-12-12
                              相关资源
                              最近更新 更多