相关疑难解决方法(0)

为什么会进入无限循环?

我有以下代码:

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

我们知道他应该只是写x++或者x=x+1,但是x = x++它首先要归于x自身,然后再增加它.为什么x继续0作为价值?

--update

这是字节码:

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 …
Run Code Online (Sandbox Code Playgroud)

java loops increment operators variable-assignment

490
推荐指数
12
解决办法
2万
查看次数

105
推荐指数
7
解决办法
20万
查看次数

标签 统计

increment ×2

java ×2

operators ×2

loops ×1

syntax ×1

variable-assignment ×1