x=20;x= ++x + ++x + x++ ;java 中 x 的最终值是 65

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19216118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:08:55  来源:igfitidea点击:

How is that x=20;x= ++x + ++x + x++ ;final value of x in java is 65

javaincrementoperator-precedencepost-increment

提问by user1517393

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

这怎么可能因为后增量运算符应该将 x 增加到 66?

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

当我为y= ++x + ++x + x++;它做同样的事情时,它给出了 65 fory和 23 for的值x

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

所以让我知道 java 编译器是如何解决这些表达式的。

回答by Amadan

Let Java show you. javap -c MyClassshows you bytecode:

让 Java 向您展示。javap -c MyClass向您显示字节码:

  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)

回答by Quaz and Wally

++x is different from x++

++x 与 x++ 不同

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

++x 在同一行中完成任何操作之前增加 x。

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

x++ 在同一行中完成任何操作后递增 x。

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

要计算到 65,它必须进行如下计算。

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

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

Afterwards, x would be 23

之后,x 将是 23

回答by Shane

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

我认为 y= (++20) + (++21) + 22 = 21 + 22 +22 = 65

回答by Rohan

First, you should understand this:

首先,你应该明白这一点:

++iincrements iand returns i.

++i递增i并返回i

i++returns iand then increments it.

i++返回i然后增加它。

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

现在我们已经建立了这一点,让我们分解程序。

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

在您的程序开始时,x = 20. 所以,++x会返回 21。现在当你x再次以这种方式递增时,你将递增21而不是20。因此,++x+++x将计算出21 + 22which 等于 43。在程序的这一点上,xequals 22。因此,如果添加x++到 43,则将 的值添加到 43,x然后才增加x。这最终导致y值为 65,x值为 23。

回答by vandale

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:

不要在同一个表达式中的同一个变量上使用++和=,增量不会生效。来自爪哇?Puzzlers: Traps, Pitfalls, and Corner Cases 作者 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;

正如谜题的标题所暗示的,问题在于执行增量的语句:

j = j++;

据推测,该语句的作者的意思是将 j 的值加 1,这就是表达式 j++ 所做的。不幸的是,作者无意中将这个表达式的值赋回给了 j。当放在变量之后时,++ 运算符用作后缀递增运算符 [JLS 15.14.2]:表达式 j++ 的值是 j 递增之前的原始值。因此,前面的赋值首先保存 j 的值,然后将 j 设置为其值加 1,最后将 j 重置为其原始值。换句话说,赋值等效于以下语句序列:

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

这就是为什么 x=65 而不是 66

回答by Stephen C

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

所以让我知道 java 编译器是如何解决这些表达式的。

Java compiler are simply implementing the Java Language Specification.

Java 编译器只是实现 Java 语言规范。

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:

如果你真的需要了解编译器如何评估这样可怕和奇怪的语句,你需要了解规范的相关部分:

and so on.

等等。

回答by Dom

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.

这个故事的寓意是,永远不要这样做,并且在计算表达式之前会发生前增量,并且在计算表达式之后会发生后增量。

回答by santhosh

You should remember this C Operator Precedence

你应该记住这个C 运算符优先级

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

所以后增量先行++X = 20,然后x++=22x++ = 23使65个。