简体   繁体   English

Java中的优先级

[英]Precedence in Java

根据优先级表,一元postfix递增和递减运算符比关系运算符具有更多的优先级,那么为什么在这样的表达式(x ++> = 10)中,关系运算符首先求值,然后变量递增?

The operator isn't evaluated first. 不首先评估操作员。 The ordering is: 订购是:

  • Evaluate LHS ( x++ ) - the result is the original value of x , then x is incremented 求值LHS( x++ ) - 结果是x的原始值,然后x递增
  • Evaluate RHS ( 10 ) - the result is 10 评估RHS( 10 ) - 结果是10
  • Compare the results of the LHS and RHS 比较LHS和RHS的结果

Here's code to demonstrate that: 这是用于演示以下内容的代码:

public class Test {

    static int x = 9;

    public static void main(String[] args) {
        boolean result = x++ >= showXAndReturn10();
        System.out.println(result); // False
    }

    private static int showXAndReturn10() {
        System.out.println(x); // 10
        return 10;
    }
}

That prints out 10 then false , because by the time the RHS is evaluated x has been incremented... but the >= operator is still evaluating 9 >= 10 as the result of the expression x++ is the original value of x , not the incremented one. 打印出10然后false ,因为由时间RHS被评估x 递增...但是>=操作者仍然在评估9 >= 10作为表达式的结果x++原始x ,而不是增加一个。

If you want the result after incrementing, use ++x instead. 如果想增量得到结果,请改用++x

The relational operator is not evaluated before the increment. 在增量之前不评估关系运算符。

First the operands of the relational operator ( x++ and 10 ) are evaluated. 首先评估关系运算符( x++10 )的操作数。

However, the evaluation of x++ increments x but returns the original value of x , so even though the increment already took place, the value passed to the relational operator is the original value of x . 然而,评价x++增量x ,但返回的原始值x ,所以尽管增量已经发生,传递给关系运算符的值是原始值x

Your conclusion is not correct. 你的结论是不正确的。 The x++ is evaluated fist, just its value is the value of x before the increment as defined for the postfix increment operation. x ++被评估为第一个,只是它的值是在为后缀增量操作定义的增量之前的x值。

Because this is how "++, --" are working. 因为这就是“++, - ”的工作原理。 If it comes after the variable, the old value is used then the value increases, and if it comes before the variable, the increment is occurred first, then the new value is used. 如果它在变量之后,则使用旧值,然后值增加,如果它在变量之前,则首先发生增量,然后使用新值。 So, if you want to use the variable after increasing its value and before you check it, then use (++x >= 10), or increase it without reference and then check it, like that: 因此,如果您想在增加其值之后使用该变量并在检查之前使用该变量,则使用(++ x> = 10),或者在没有引用的情况下增加它,然后检查它,如下所示:

int x = 0;
x++;
if(x >= 10) {...}

Unary Operators 一元运算符

However you put the unary operators in an expression, the following table summarize its usage. 但是,您将一元运算符放在表达式中,下表总结了它的用法。

+----------+-------------------+------------+-----------------------------------------------------------------------------------+
| Operator |       Name        | Expression |                                    Description                                    |
+----------+-------------------+------------+-----------------------------------------------------------------------------------+
| ++       | prefix increment  | ++a        | Increment "a" by 1, then use the new value of "a" in the residing expression.     |
| ++       | postfix increment | a++        | Use the current value of "a" in the residing expression, then increment "a" by 1. |
| --       | prefix decrement  | --b        | Decrement "b" by 1, then use the new value of "b" in the residing expression.     |
| --       | postfix decrement | b--        | Use the current value of "b" in the residing expression, then decrement "b" by 1. |
+----------+-------------------+------------+-----------------------------------------------------------------------------------+

Then, consider the following java program: 然后,考虑以下java程序:

public class UnaryOperators {
    public static void main(String args[]) {
        int n;

        // postfix unary operators
        n = 10;
        System.out.println(n); // prints 10
        System.out.println(n++); // prints 10, then increment by 1
        System.out.println(n); // prints 11

        n = 10;
        System.out.println(n); // prints 10
        System.out.println(n--); // prints 10, then decrement by 1
        System.out.println(n); // prints 9


        // prefix unary operators
        n = 10;
        System.out.println(n); // prints 10
        System.out.println(++n); // increment by 1, then prints 11
        System.out.println(n); // prints 11

        n = 10;
        System.out.println(n); // prints 10
        System.out.println(--n); // decrement by 1, then prints 9
        System.out.println(n); // prints 9
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM