Java Basics: Operator Precedence

In the previous article, we ended the knowledge points of all operators in Java. Let's first recall what operators in Java include through a picture:

So many operators, if they are used in combination, what is their priority?

This article will take you into the precedence of operators.

overview

The execution order of Java's operators is very important, because the difference in the order will directly lead to the difference in the results.

Let's first illustrate the priority through a table:

priority operator
Highest ++x --x +x ~ !
new(type)x
* / %
+ -
<< >> >>>
< > <= >= instanceof
== !=
&
^
|
&&
||
?:
lowest = += -= *= /= %= &= ^= |= <<= >>= >>>=

Known from the table:

  • Postfix operators (such as a++, a–) have the highest precedence in java.
  • The assignment operator and its different forms have the lowest precedence in java.

In addition, the following rules need to be kept in mind when calculating:

  • Operands of operators are evaluated from left to right, for example in expression ++a + b–, operand ++a is evaluated first and then b– is evaluated.
  • Each operand of the operator (except the conditional operator, and &&) || before ? : is fully evaluated before any part of the operation itself is performed. For example, in the expression ++a + b–, the addition operation will only occur after ++aandb– has been evaluated.
  • The left operand of a binary operator is fully evaluated before any part of the right operand is evaluated.
  • The order of evaluation given by parentheses ( ) takes precedence over operator precedence.
  • The prefix version of ++or – evaluates/uses the value incremented in the expression, while the postfix version of ++or – evaluates the current value, then increments/decrements the operand value.
  • With the exception of assignment operators, all binary operators are evaluated from left to right.

How to change operator precedence?

Brackets "()" are used to change the precedence of operators. For example, in an expression a+b*c, if you want the addition operation to be calculated first, then rewrite the expression as (a+b)*c.

code demo

Based on the operator precedence introduced above, we briefly introduce typical examples in the form of code:

package com.test.javaroads.operator;

/**
 * @author: javaroads
 * @date: 2022/12/8 22:36
 * @description:
 */
public class Fifteen {

    public static void main(String[] args) {

        int x = 6, y = 7, z = 8;

        int r1 = x + y * z / 10;
        System.out.println(r1);

        int r2 = x + --y - ++z;
        System.out.println(r2);

        int r3 = ++x + ++y + 5 << 1 | 2;
        System.out.println(r3);

        boolean x1 = false, y1 = true, z1 = false;
        boolean r4 = x1 == y1 == z1;
        System.out.println(r4);

        int r5 = x - ++y - ++z;
        System.out.println(r5);
    }
}

Results of the:

11
3
38
true
-11

Let's analyze them one by one:

Case number one

int x = 6, y = 7, z = 8;
int r1 = x + y * z / 10;
  • The priority of and / is greater than that of the + operator, so it y * z / 10will be executed first, * and / have the same priority, so they will be associated in order from left to right, so they are equivalent x + (y * z) / 10.

case two

int x = 6, y = 7, z = 8;
int r2 = x + --y - ++z;

The precedence of ++ and -- is greater than that of - and + operators, so they r2 = x + --y - ++zare equivalent to r2 = x + (--y) - (++z).

case three

int x = 6, y = 7, z = 8;
int r3 = ++x + ++y + 5 << 1 | 2;

The priority of ++ and -- is greater than that of + and << operators, so the sum ++xwill ++ybe executed first. In addition, the priority of << is greater than that of the + operator. In summary, it ++x + ++y + 5 << 1 | 2is equivalent to (((++x) + (++y) + 5) << 1)| 2.

case four

boolean x1 = false, y1 = true, z1 = false;
boolean r4 = x1 == y1 == z1;

The assignment operator = has the lowest priority, and the equal operator (==) evaluates from left to right, so r4 = x1 == y1 == z1it is equivalent to r4 = ((x1 == y1) == z1).

case five

int x = 6, y = 7, z = 8;
int r5 = x - ++y - ++z;

In the same way, ++the priority ratio -is higher, so x - ++y - ++zit is equivalent to x - (++y) - (++z).

Guess you like

Origin blog.csdn.net/weixin_43025343/article/details/132269475