我有以下代码:

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

}

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


当前回答

检查下面的代码,

    int x=0;
    int temp=x++;
    System.out.println("temp = "+temp);
    x = temp;
    System.out.println("x = "+x);

输出将是,

temp = 0
x = 0

后增量是指将值递增,并返回该增量之前的值。这就是temp值为0的原因。那么如果temp = i并且这是在一个循环中(除了第一行代码)。就像问题!!!!一样

其他回答

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

具体来说,"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

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

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

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

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

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

你有效地得到了以下行为。

获取x的值(它是0)作为右边的“结果” 增加x的值(x现在是1) 将右边的结果(保存为0)分配给x (x现在是0)

其思想是,后增量操作符(x++)在返回变量的值后对该变量进行增量,以便在使用该变量的方程中使用。

编辑:由于评论的原因,稍微增加了一点。像下面这样考虑。

x = 1;        // x == 1
x = x++ * 5;
              // First, the right hand side of the equation is evaluated.
  ==>  x = 1 * 5;    
              // x == 2 at this point, as it "gave" the equation its value of 1
              // and then gets incremented by 1 to 2.
  ==>  x = 5;
              // And then that RightHandSide value is assigned to 
              // the LeftHandSide variable, leaving x with the value of 5.

这是因为在这种情况下它永远不会增加。x++会先使用它的值,然后再进行递增,在这种情况下,它会像这样:

x = 0;

但如果你用++x;这将会增加。

x++表达式求值为x。++部分影响求值之后的值,而不是语句之后的值。所以x = x++可以有效地转换成

int y = x; // evaluation
x = x + 1; // increment part
x = y; // assignment