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

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

++ x被称为preincrement而x ++被称为postincrement。

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 

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

例:

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

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

这些被称为后缀和前缀运算符。 两者都会将1加到variables中,但是语句的结果有所不同。

 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 

是,

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

将打印6

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

将打印5

我从它最近的一个dup中find了这个问题,尽pipe这个问题已经不止回答了,但我忍不住要将代码反编译并添加“另一个答案”:-)

为了准确(也许有点迂腐),

 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虚拟机规范的帮助下评论主要方法):

 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 

当考虑电脑实际上是什么…

++ 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就简单多了。

是。

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

是的,使用++ X,将在expression式中使用X + 1。 使用X ++,将在expression式中使用X,并且仅在expression式被评估之后才会增加X.

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

如果它像许多其他语言,你可能想要一个简单的尝试:

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

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

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

 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 

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

下面的代码,检查推和popupfunc。

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

是的,在x ++(postincrement)中有区别,在expression式中将使用x的值,并且在expression式被评估之后x将被增加1,另一方面++ x(preincrement),x + 1将用在expression式中。 举个例子:

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

这个问题已经回答了,但是也允许我从我这边补充。

首先++表示递增1, 表示递减1。

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

在Java中, x ++和++ x 是有区别的

++ x是前缀forms:它增加variablesexpression式,然后在expression式中使用新的值。

例如,如果在代码中使用:

 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 ++是一个后缀forms:variables值首先在expression式中使用,然后在操作之后递增。

例如,如果在代码中使用:

 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' 

希望这是明确的。 运行和使用上面的代码应该有助于你的理解。

使用i ++,它被称为后增量(postincrement),并且在任何情况下使用该值然后递增; ++我preincrement递增值,然后在上下文中使用它。

如果你没有在任何情况下使用它,那么你使用什么并不重要,但后增量是按惯例使用的。

有一个巨大的差异。

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

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