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.
The operating environment of this tutorial: Windows 10 system, javascript version 1.8.5, dell g3 computer
Operator priority in Java As follows:
The parentheses have the highest priority.
Auto-increment and auto-decrement operators have higher priority than arithmetic operators.
Logical operators have lower precedence than arithmetic operators.
Assignment operators have lower precedence than arithmetic operators.
After reading the priority order, let’s go through the code to strengthen it:
int n = 3>4 ? 100 : 200;
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);
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!