Home  >  Article  >  Java  >  What is operator precedence ordering in java

What is operator precedence ordering in java

DDD
DDDOriginal
2023-08-09 15:12:195628browse

The priority order of operators in Java is: postfix operator, prefix operator, unary operator, multiplication and division operators, addition and subtraction operators, shift operator, relational operator, equality operator operators, bitwise operators, logical operators, conditional operators, and assignment operators. It should be noted that the precedence of operators is not absolute, and the order of operations can be changed by using parentheses (). Expressions enclosed in parentheses are evaluated first and then based on operator precedence.

What is operator precedence ordering in java

The operating environment of this article: Windows 10 system, Java 19.0.1 version, Dell G3 computer.

In Java, operator precedence determines the order in which operators in an expression are evaluated. When an expression contains multiple operators, operator precedence determines which operators will be evaluated first and which operators will be evaluated later.

Operator precedence in Java can be sorted in the following order:

Postfix Operators (Postfix Operators): perform operations after the operands, such as suffix increment (i) and suffix decrement (i--).

Prefix Operators (Prefix Operators): perform operations before the operand, such as prefix increment (i) and prefix decrement (--i).

Unary operators (Unary Operators): operate on a single operand, such as positive sign ( ), negative sign (-), logical NOT (!) and bitwise NOT (~).

Multiplicative and Division Operators: including multiplication (*), division (/) and modulo (%).

Addition and subtraction operators (Additive Operators): including addition ( ) and subtraction (-).

Shift Operators: including left shift (752f06d4fdf2559b05e6383f4a1890e7>) and unsigned right shift (>>>).

Relational Operators: including less than (d82e2731711b03b94af0d2ca856ebd07), less than or equal to (35aafbe26102e008dccae4e6d4d56ecc=).

Equality Operators: including equality (==) and inequality (!=).

Bitwise Operators: including bitwise AND (&), bitwise OR (|), bitwise XOR (^) and bitwise negation (~).

Logical Operators: including logical AND (&&), logical OR (||) and logical NOT (!).

Conditional Operators: including conditional expressions (?:).

Assignment Operators: including simple assignment (=) and compound assignment (=, -=, etc.).

It should be noted that the priority of operators is not absolute, and the order of operations can be changed by using parentheses (). Expressions enclosed in parentheses are evaluated first and then based on operator precedence.

The following is a simple example to demonstrate the role of operator precedence:

int result = 10 5 * 2; // Multiplication has higher priority than addition, so 5 * 2 is calculated first , plus 10, the result is 20

System.out.println(result); // 输出20

result = (10 5) * 2; // Use parentheses to change the order of operations, first calculate the expression in the parentheses, then multiply by 2, the result is 30

System.out.println(result); // 输出30

By understanding operator precedence, you can better understand and write complex expressions and avoid errors caused by improper operator ordering.

The above is the detailed content of What is operator precedence ordering in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn