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

What is operator precedence ordering in java

DDD
Release: 2023-08-09 15:12:35
Original
5662 people have browsed it

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 (<<), right shift (>>) and unsigned right shift (>>>).

Relational Operators: including less than (<), greater than (>), less than or equal to (<=) and greater than or equal to (>=).

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
Copy after login

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template