130

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

4

17 回答 17

324

++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
于 2009-07-07T21:09:57.477 回答
71

是的

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

例子:

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

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

于 2009-07-07T21:10:27.147 回答
19

这些被称为后缀和前缀运算符。两者都会给变量加 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
于 2009-07-07T21:13:01.433 回答
12

是的,

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

将打印6

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

将打印5

于 2009-07-07T21:10:13.450 回答
9

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

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

int y = 2;
y = y++;

被编译成:

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

如果你javacY.java门课:

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

并且javap -c Y,您将获得以下 jvm 代码(我允许我在Java 虚拟机规范的帮助下注释 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
于 2012-08-24T14:25:46.290 回答
9

在考虑计算机的实际功能时...

++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。它更简单。

于 2016-06-04T02:23:07.403 回答
9

在 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' 

希望这很清楚。运行和使用上面的代码应该有助于您的理解。

于 2017-10-02T23:43:29.783 回答
4

是的。

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);
    }
}
于 2009-07-07T21:12:41.997 回答
3

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

因此,如果 X = 9,使用 ++X,将使用值 10,否则将使用值 9。

于 2009-07-07T21:11:03.700 回答
3

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

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

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

于 2009-07-07T21:11:18.877 回答
3

是的,返回的值分别是增量之后和之前的值。

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
于 2009-07-07T21:12:44.053 回答
2

好的,我来到这里是因为我最近在检查经典堆栈实现时遇到了同样的问题。只是提醒一下,这是在基于数组的 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;
  }
}
于 2014-02-17T13:23:10.233 回答
2

是的,有一个区别,在 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);  
}
于 2014-06-18T13:04:36.733 回答
2

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

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

现在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
于 2015-04-21T12:07:24.700 回答
0

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

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

于 2015-05-18T17:21:53.190 回答
0

这是个很大的差异。

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

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);
于 2017-07-03T01:46:08.020 回答
0
public static void main(String[] args) {    

int a = 1;    
int b = a++; // this means b = whatever value a has but, I want to    
increment a by 1    
    
System.out.println("a is --> " + a);    //2    
System.out.println("b is --> " + b);    //1     
a = 1;    
b = ++a;  // this means b = a+1    
    
System.out.println("now a is still  --> " + a);    //2    
System.out.println("but b is --> " + b);           //2    

}    
于 2022-02-07T00:21:29.733 回答