Why does this go into an infinite loop?

2019-01-02 16:46发布

I have the following code:

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

We know he should have writen just x++ or x=x+1, but on x = x++ it should first attribute x to itself, and later increment it. Why does x continue with 0 as value?

--update

Here's the bytecode:

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

}

I'll read about the instructions to try to understand...

27条回答
一个人的天荒地老
2楼-- · 2019-01-02 16:47

I think because in Java ++ has a higher precedence than = (assignment)...Does it? Look at http://www.cs.uwf.edu/~eelsheik/cop2253/resources/op_precedence.html...

The same way if you write x=x+1...+ has a higher precedence than = (assignment)

查看更多
伤终究还是伤i
3楼-- · 2019-01-02 16:49

Before incrementing the value by one, the value is assigned to the variable.

查看更多
呛了眼睛熬了心
4楼-- · 2019-01-02 16:50

The x++ expression evaluates to x. The ++ part affect the value after the evaluation, not after the statement. so x = x++ is effectively translated into

int y = x; // evaluation
x = x + 1; // increment part
x = y; // assignment
查看更多
后来的你喜欢了谁
5楼-- · 2019-01-02 16:51

You're effectively getting the following behavior.

  1. grab the value of x (which is 0) as "the result" of the right side
  2. increment the value of x (so x is now 1)
  3. assign the result of the right side (which was saved as 0) to x (x is now 0)

The idea being that the post-increment operator (x++) increments that variable in question AFTER returning its value for use in the equation it's used in.

Edit: Adding a slight bit because of the comment. Consider it like the following.

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.
查看更多
只若初见
6楼-- · 2019-01-02 16:53

From http://download.oracle.com/javase/tutorial/java/nutsandbolts/op1.html

The increment/decrement operators can be applied before (prefix) or after (postfix) the operand. The code result++; and ++result; will both end in result being incremented by one. The only difference is that the prefix version (++result) evaluates to the incremented value, whereas the postfix version (result++) evaluates to the original value. If you are just performing a simple increment/decrement, it doesn't really matter which version you choose. But if you use this operator in part of a larger expression, the one that you choose may make a significant difference.

To illustrate, try the following:

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

Which will print 1 and 0.

查看更多
看风景的人
7楼-- · 2019-01-02 16:53
 x = x++; (increment is overriden by = )

because of above statement x never reaches 3;

查看更多
登录 后发表回答