Home > Java > javaTutorial > body text

How to sort operator precedence in java

DDD
Release: 2023-06-01 14:55:26
Original
4909 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.

How to sort operator precedence in java

The operating environment of this tutorial: Windows 10 system, javascript version 1.8.5, dell g3 computer

Operator priority in Java As follows:

  1. The parentheses have the highest priority.

  2. Auto-increment and auto-decrement operators have higher priority than arithmetic operators.

  3. Logical operators have lower precedence than arithmetic operators.

  4. Assignment operators have lower precedence than arithmetic operators.

How to sort operator precedence in java

After reading the priority order, let’s go through the code to strengthen it:

int n = 3>4 ? 100 : 200;
Copy after login

The order of operation execution of this line of code is that we Let’s write it down:

1. Perform operations 3>4 and get the result of Boolean type false

2. Through the result false, use the result 200 of the corresponding expression 2 as the final result of the operation Result

3. Assign 200 to the variable n

Next, let’s look at a more complicated code:

int a = 5;
int b = 3;
int c = 1;
int n2 = (a>b && b>c) ? (c++) : (++c);
Copy after login

Let’s also write the order of execution of this code:

1. Parentheses have high priority, we operate the code in the first group of parentheses first

1.1. The comparison operator “>” has a higher priority than the logical operator “&&”

First execute a>b and get the result true;

Then execute b>c and get the result true;

Finally execute the result of a>b&& the result of b>c , that is, true && true, the result is true

2. The conditional judgment result in the ternary operator is true, and the result c of expression 1 is returned

First change the original value of variable c Assign a value to variable n2, that is, the value of n2 is 1;

Then increase the value of variable c by 1 and update it to 2.

The above is the detailed content of How to sort operator precedence 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