Home > Common Problem > body text

Operator precedence ordering in java

zbt
Release: 2023-06-16 13:55:34
Original
2804 people have browsed it

Operator priority ordering in Java: 1. Parentheses have the highest priority; 2. Auto-increment and auto-decrement operators have higher priority than arithmetic operators; 3. Logical operators have low priority. than arithmetic operators; 4. The priority of assignment operators is lower than that of arithmetic operators.

Operator precedence ordering in java

The operating environment of this tutorial: Windows 10 system, Java 20 version, DELL G3 computer.

In the Java programming language, operator precedence is a basic knowledge that programmers must master. Because different operators have different priorities, if you don't understand these priorities, it will cause logic errors in the program. This article will introduce the priority ordering of operators in Java and provide rich code examples to help readers better understand.

Ordering of operator precedence in Java

The operator precedence in Java from high to low is as follows:

Operator precedence ordering in java

As can be seen from the above figure, parentheses have the highest priority and logical operators have the lowest priority.

Code Case

Next, we will demonstrate the priority ordering of different operators in Java through a series of code cases.

1. Parentheses have the highest precedence

In Java, parentheses have the highest operator precedence. The following code demonstrates the difference between the results without parentheses and the results with parentheses:

Operator precedence ordering in java

It can be seen that since the multiplication operator has a higher precedence, so without using parentheses, b* is evaluated first The result of c is then added to a. In the case of using parentheses, the result of a b is calculated first and then multiplied by c.

2. The priority of auto-increment and auto-decrement operators is higher than that of arithmetic operators

In Java, the priority of auto-increment and auto-decrement operators is higher for arithmetic operators. The following code demonstrates this:

Operator precedence ordering in java

It can be seen that the prefixed increment operator will first increase the value of a by 1, and then add it to b, so The result of result1 is 31. The post-increment operator will first add the value of a to b, and then add 1 to the value of a, so the result of result2 is 30.

3. Logical operators have lower priority than arithmetic operators

In Java, logical operators have lower priority than arithmetic operators. The following code demonstrates this:

Operator precedence ordering in java

It can be seen that since the greater than operator has higher precedence than the logical AND operator, without parentheses , first calculate the result of a b > c, and then compare it with a > b performs logical AND operations. In the case of using parentheses, the results of (a b > c) and (a > b) are calculated first, and then the logical AND operation is performed.

4. Assignment operators have lower priority than arithmetic operators

In Java, assignment operators have lower priority than arithmetic operators. The following code demonstrates this:

Operator precedence ordering in java

It can be seen that since the multiplication operator has a higher priority than the assignment operator, the result of b * c is calculated first, Then add the value of a to this result.

The above is the detailed content of 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template