在Java中使用增量和减量运算符造成混淆 [英] Confusion using Increment and decrement operators in Java

查看:93
本文介绍了在Java中使用增量和减量运算符造成混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有增量和减量运算符以及一些二进制运算符的表达式.

I had an expression with increment and decrement operators along with some binary operators.

public class precedence {
   public static void main(String ar[]){
     int a=3;
     int b=4;
     System.out.println(a++ * b-- / a-- + ++b);
     System.out.println(a+","+b);
   }   
}

第一个++ b替换为5,b变为5.

first ++b is replaced by 5 and b will be 5.

然后,由于所有其余术语均为修订后版本,因此评估顺序为从右至左

Then as all remaining terms are post fix versions the order of evaluation will be from right to left

a--将替换为3,并将a更改为2.

a-- will be replaced with 3 and a is changed as 2.

b--将替换为5,b变为4.

b-- will be replaced with 5 and b becomes 4.

a ++将替换为2,而a变为3.

a++ will be replaced with 2 and a becomes 3.

所以最终表达式应该是2 * 5/3 + 5,等于8,但是输出中显示的答案是7.有人可以告诉我我错了吗.

So the final expression should be 2 * 5 / 3 + 5 which is equal to 8 but the answer shown in the output is 7. Can some one tell me where I am wrong.

推荐答案

首先,如果您有类似的事情:

For a start, if you have something like:

A * B / C + D

该表达式是从左到右求值的,因为按照Java Language Spec, section 15.7, Evaluation order:

the expression is evaluated left to right, because there's no precedence, parentheses or associativity overriding that order, as per the Java Language Spec, section 15.7, Evaluation order:

Java编程语言保证运算符的操作数似乎按特定的评估顺序(即从左到右)进行评估.

The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right.

JLS section 15.7.3可以覆盖此正常顺序:

Java编程语言遵循括号中显式表示的评估顺序,并且运算符优先级隐式表示评估的顺序.

The Java programming language respects the order of evaluation indicated explicitly by parentheses and implicitly by operator precedence.

但是这里不是这种情况,因为除法和乘法的优先级高于加法,并且不包含括号.

But that's not the case here since division and multiplication have higher precedence than addition, and there are no parentheses involved.

此外,15.7.1明确指出(我强调):

In addition, 15.7.1 states clearly (my emphasis):

二进制运算符的左操作数似乎在 任何 部分之前被 完全 求值.右手操作数将被评估.

The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated.

因此,副作用属于子表达式评估,而不是整个表达式.这意味着,在a * --a之类的值中,a的减量影响乘法的左手端是不可能的.

Hence the side effects belong with the sub-expression evaluation, not the entire expression. That means, in something like a * --a, it is impossible for the decrement of a to affect the left hand side of the multiplication, which has already been fully evaluated.

因此,在上面显示的表达式中,A * B / C + DD实际上将被评估为 last ,并且在评估所有其他内容之前不会发生任何附带的副作用.此外,DBC的评估产生的任何副作用都将在D起作用之前发生.

Hence, in the expression shown above, A * B / C + D, D will actually be evaluated last and any side-effect attached to it won't happen before everything else is already evaluated. In addition, any side effects arising from the evaluation of A, B or C will have occurred before D is actioned.

将该推理应用于您的副作用表达,这实际上是:

Applying that reasoning to your side effect expression, which is effectively:

(a++) * (b--) / (a--) + (++b)

  • 最初,a3b4.
  • a++使用3,将a设置为4.
  • b--使用4,将b设置为3.
  • a--使用4,将a设置为3.
  • ++bb设置为4,使用4.
    • initially, a is 3, b is 4.
    • a++ uses 3, sets a to 4.
    • b-- uses 4, sets b to 3.
    • a-- uses 4, sets a to 3.
    • ++b sets b to 4, uses 4.
    • 所以:

        3 * 4 / 4 + 4
      =    12 / 4 + 4
      =         3 + 4
      =             7
      

      这篇关于在Java中使用增量和减量运算符造成混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆