相关疑难解决方法(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万
查看次数

array[++variable] 代表什么?

示例代码在这里:

static class stack 
{
    int top=-1;
    char items[] = new char[100];

    void push(char x) 
    {
        if (top == 99)
            System.out.println("Stack full");
        else
            items[++top] = x;
    }
}
Run Code Online (Sandbox Code Playgroud)

当 items[++top] 出现时到底发生了什么?

java arrays iteration

0
推荐指数
1
解决办法
1089
查看次数