Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的优先级_Java_Increment_Operator Precedence - Fatal编程技术网

Java中的优先级

Java中的优先级,java,increment,operator-precedence,Java,Increment,Operator Precedence,根据优先级表,一元后缀递增和递减运算符比关系运算符具有更多的优先级,那么为什么在这样的表达式(x++>=10)中,关系运算符首先求值,然后变量递增?在递增之前不求值关系运算符 首先计算关系运算符(x++和10)的操作数 但是,对x++的求值会增加x,但会返回x的原始值,因此,即使已经发生了增量,传递给关系运算符的值也是x的原始值。运算符不会首先求值。订单如下: 计算LHS(x++)-结果是x的原始值,然后x递增 评估RHS(10)-结果为10 比较LHS和RHS的结果 下面是演示以下内容的代

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

在递增之前不求值关系运算符

首先计算关系运算符(
x++
10
)的操作数


但是,对
x++
的求值会增加
x
,但会返回
x
的原始值,因此,即使已经发生了增量,传递给关系运算符的值也是
x
的原始值。运算符不会首先求值。订单如下:

  • 计算LHS(
    x++
    )-结果是
    x
    的原始值,然后
    x
    递增
  • 评估RHS(
    10
    )-结果为10
  • 比较LHS和RHS的结果
下面是演示以下内容的代码:

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;
    }
}
打印出
10
然后
false
,因为在计算RHS时,
x
已增加。。。但是
=
运算符仍在计算
9>=10
,因为表达式
x++
的结果是
x
的原始值,而不是递增值


如果您想要递增后的结果,请使用
++x

您的结论不正确。首先对x++求值,只是它的值是为后缀增量操作定义的增量之前的x值。

因为这就是“+,-”的工作方式。如果它位于变量之后,则使用旧值,然后值增加;如果它位于变量之前,则先增加,然后使用新值。 因此,如果您希望在增加变量值之后和检查变量之前使用该变量,请使用(++x>=10),或者在不引用的情况下增加变量,然后检查它,如下所示:

int x = 0;
x++;
if(x >= 10) {...}
单目运算符 无论您如何在表达式中放置一元运算符,下表总结了其用法

+----------+-------------------+------------+-----------------------------------------------------------------------------------+
| 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. |
+----------+-------------------+------------+-----------------------------------------------------------------------------------+
然后,考虑下面的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
    }
}