我有以下代码:

public class Tests {
    public static void main(String[] args) throws Exception {
        int x = 0;
        while(x<3) {
            x = x++;
            System.out.println(x);
        }
    }
}

我们知道他应该只写x++或x=x+1,但在x=x+ +时,它应该先把x归为自己,然后再加1。为什么x的值是0 ?

- - -更新

下面是字节码:

public class Tests extends java.lang.Object{
public Tests();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

public static void main(java.lang.String[])   throws java.lang.Exception;
  Code:
   0:   iconst_0
   1:   istore_1
   2:   iload_1
   3:   iconst_3
   4:   if_icmpge   22
   7:   iload_1
   8:   iinc    1, 1
   11:  istore_1
   12:  getstatic   #2; //Field java/lang/System.out:Ljava/io/PrintStream;
   15:  iload_1
   16:  invokevirtual   #3; //Method java/io/PrintStream.println:(I)V
   19:  goto    2
   22:  return

}

我会仔细阅读说明,试着理解……


当前回答

这是因为x的值根本没有增加。

x = x++;

等于

int temp = x;
x++;
x = temp;

解释:

让我们看看这个操作的字节代码。考虑一个示例类:

class test {
    public static void main(String[] args) {
        int i=0;
        i=i++;
    }
}

现在运行类反汇编程序,我们得到:

$ javap -c test
Compiled from "test.java"
class test extends java.lang.Object{
test();
  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_0
   1:    istore_1
   2:    iload_1
   3:    iinc    1, 1
   6:    istore_1
   7:    return
}

现在Java虚拟机是基于堆栈的,这意味着对于每个操作,数据将被推入堆栈,从堆栈中,数据将弹出来执行操作。还有另一种数据结构,通常是存储局部变量的数组。局部变量的id是数组的索引。

让我们看看main()方法中的助记符:

iconst_0: The constant value 0 is pushed on to the stack. istore_1: The top element of the stack is popped out and stored in the local variable with index 1 which is x. iload_1 : The value at the location 1 that is the value of x which is 0, is pushed into the stack. iinc 1, 1 : The value at the memory location 1 is incremented by 1. So x now becomes 1. istore_1 : The value at the top of the stack is stored to the memory location1. That is 0 is assigned to x overwriting its incremented value.

因此,x的值不会改变,从而导致无限循环。

其他回答

这是因为它是后增量的。它意味着变量在表达式求值之后递增。

int x = 9;
int y = x++;

X现在是10,但y是9,这是X加之前的值。

更多信息请参见后增量的定义。

据我所知,错误发生了,由于赋值覆盖了增量值,在增量之前的值,即它取消了增量。

具体来说,"x++"表达式在递增前的值为'x',而"++x"表达式在递增后的值为'x'。

如果你对研究字节码感兴趣,我们将看一下有问题的三行:

 7:   iload_1
 8:   iinc    1, 1
11:  istore_1

7: iload_1 #将第二个本地变量的值放在堆栈上 8: iinc 1,1 #将使第二个局部变量加1,注意它不影响堆栈! 9: istore_1 #将弹出堆栈顶部,并将该元素的值保存到第二个局部变量中 (你可以在这里阅读每条JVM指令的效果)

这就是为什么上面的代码将无限循环,而带有++x的版本不会。 ++x的字节码应该看起来很不一样,据我所知,在我一年多以前写的1.3 Java编译器中,字节码应该是这样的:

iinc 1,1
iload_1
istore_1

所以只是交换了前两行,改变了语义,使得留在堆栈顶部的值,在增量之后(即表达式的“值”)是增量之后的值。

自增操作符应用于要赋值的变量。那是自找麻烦。我相信你可以看到你的x变量的值,同时运行这个程序....这应该清楚了为什么循环永远不会结束。

其他人已经解释得很好了。我只包含了指向相关Java规范部分的链接。

X = x++是一个表达式。Java将遵循求值顺序。 它将首先计算表达式x++,该表达式将增加x并将结果值设置为x的前一个值。 然后它将表达式结果赋值给变量x。最后,x返回到它之前的值。

这就像你期望的那样。这就是前缀和后缀的区别。

int x = 0; 
while (x < 3)    x = (++x);