how is that x=20;x= ++x + ++x + x++ ;final value o

2019-01-29 08:44发布

问题:

This question already has an answer here:

  • How do the post increment (i++) and pre increment (++i) operators work in Java? 15 answers

how is this possible as post increment operator should increase x to 66?

When i did the same for y= ++x + ++x + x++; it gave a value 65 for y and 23 for x.

So let me know how is java compilers solving these expression.

回答1:

Let Java show you. javap -c MyClass shows you bytecode:

  public static void main(java.lang.String[]);
    Code:
       0: bipush        20
       2: istore_1      
       3: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
       6: iinc          1, 1
       9: iload_1       
      10: iinc          1, 1
      13: iload_1       
      14: iadd          
      15: iload_1       
      16: iinc          1, 1
      19: iadd          
      20: dup           
      21: istore_1      
      22: invokevirtual #3                  // Method java/io/PrintStream.println:(I)V
      25: return        

And the result is completely logical if you think about it: you have two preincrements and one postincrement. So, your code is in effect:

y = 0

x++      // 21
y += x

x++      // 22
y += x

y += x   // (still 22!)
x++      // 23

x = y    // (21 + 22 + 22 at this point)


回答2:

++x is different from x++

++x increments x before any operations done in that same line.

x++ increments x after any operations done in the same line.

For it to calculate to 65, it must be doing a calculation like the following.

(1+20)+(1+21)+(22)= 65

Afterwards, x would be 23



回答3:

I think that y= (++20) + (++21) + 22 = 21 + 22 +22 = 65



回答4:

So let me know how is java compilers solving these expression.

Java compiler are simply implementing the Java Language Specification.

If you really need to understand how compiler evaluates horrible and bizarre statements like that, you need to understand the relevant parts of the spec:

  • 15.7 Evaluation Order
  • 15.14.2 Postfix Increment Operator ++
  • 15.15.1 Prefix Increment Operator ++

and so on.



回答5:

First, you should understand this:

++i increments i and returns i.

i++ returns i and then increments it.

Now that we have established this, let's break the program down.

At the start of your program, x = 20. So, ++x would return 21. Now when you increment x in this fashion again, you will be incrementing 21 and not 20. So, ++x + ++x will evaluate to 21 + 22 which equals 43. At this point in the program, x equals 22. So if you add x++ to 43, you will add the value of x to 43 and only then increment x. This ultimately results in y having a value of 65, and x having a value of 23.



回答6:

don't use ++ and = on the same variable in the same expression, the increment will not take into effect. from Java™ Puzzlers: Traps, Pitfalls, and Corner Cases By Joshua Bloch, Neal Gafter Puzzle #25:

As the puzzle's title suggests, the problem lies in the statement that does the increment:

j = j++;

Presumably, the author of the statement meant for it to add 1 to the value of j, which is what the expression j++ does. Unfortunately, the author inadvertently assigned the value of this expression back to j. When placed after a variable, the ++ operator functions as the postfix increment operator [JLS 15.14.2]: The value of the expression j++ is the original value of j before it was incremented. Therefore, the preceding assignment first saves the value of j, then sets j to its value plus 1, and, finally, resets j back to its original value. In other words, the assignment is equivalent to this sequence of statements:

int tmp = j;

j = j + 1;

j = tmp;

as a result your doe looks like this when it evaluates:

int x=20
int sum;
x=x+1;         //x=20=1
sum=x;         //sum and x equal 21
x=x+1;         //x=22
sum=sum+x;     //sum=43
sum= sum+x;    //sum=65
x= x+1;        //x=23
x=sum;        //x=65;

This is why x=65 and not 66



回答7:

Overall this is bad programming and should never ever be used in actual code because it is easy to get lost in all the pre and post increments.

But here is the basic explanation.

simple enough:
x = 20 

Here is where it gets messy:
y = ++(20) + ++(++(20)) + (++(++(20)))++

Pre increment --> ++x
Post increment --> x++

Pre increments happen inside the evaluation and post 
increments happen after the evaluation.
So that statement can be reduced in the following steps.

y = 21 + ++(21) + (++(21))++

y = 21 + 22 + (22)++

y = 21 + 22 + 22

y = 65

After all these increments x = 23. In the statement above though, x equals multiple
numbers because of all the pre and post increments.

Moral of the story, don't ever do this and pre increments take place before the expression is evaluated and post increments take place after the expression is evaluated.



回答8:

You should remember this C Operator Precedence

so post increment goes first ++X = 20 then x++=22 then x++ = 23 so total 65.