Home  >  Article  >  Java  >  What does java operator precedence look like?

What does java operator precedence look like?

coldplay.xixi
coldplay.xixiOriginal
2020-08-18 15:38:489866browse

java operator priority: 1. Operators with higher levels perform operations first, and operators with lower levels perform operations later; 2. Parentheses [()] have the highest priority, and expressions containing parentheses , the contents of the parentheses are executed first. If multiple parentheses are included, the execution order is from left to right.

What does java operator precedence look like?

java operator precedence:

Java expressions may have multiple operators, between operators There is a priority relationship. Operators with higher levels perform operations first, and operators with lower levels perform operations later. The following table lists the priorities of operators. In the priority column in the table, the smaller the number, the higher the priority. Each operator is separated by a Chinese comma.

What does java operator precedence look like?

#Associativity in the table refers to the order in which operators are combined, usually from left to right. The most typical right-to-left operator is the plus or minus sign in front of the value. For example, 3 -4 means 3 plus -4. The sign is first combined with the content on the right side of the operator.

The operator ‘[]’ in the table has not been mentioned before. This operator means declaring an array. Arrays will be discussed in detail later.

The parentheses ‘()’ in the table have the highest priority. If the expression contains parentheses, the content of the parentheses will be executed first. If it contains multiple parentheses, the execution order is from left to right.

For example, assuming the value of variable a is 12, the execution of the following statements will have different results:

(1) Execute a 18 % 4, because the operation The priority of the symbol % is higher than that of the operator. This statement first performs the remainder operation, and then performs the addition operation. The result is 14;

(2) executes (a 18) % 4, because the parentheses have priority The highest level, this statement first executes the expression a 18 in parentheses, and then performs the remainder operation, and the result is 2;

(3) executes a * ( ( a 18 ) % 4 ), this statement There are brackets nested within brackets. The execution order is to perform the operation of the inner bracket first, and then the operation of the outer bracket. The operation result is 24.

The program function is mainly to demonstrate the priority of operators.

The statement "a = b;" involves the operator = and two operators. From the operator priority table, it can be seen that the priority of the operator is higher than the = operator, and b should First perform the auto-increment operation, and then add it to a. The result is 15. A special case is the statement "a = b;". Although its priority is higher than =, this statement is a suffix expression. The suffix expression is used first and then increments (see the section on arithmetic operators). Therefore, b is first compared with After a is added, the auto-increment operation is performed again, and the result is 14.

Compile the "PrioritySample.java" file, enter "javac PrioritySample.java" in the command line window and execute the command. After the compilation is passed, enter "java PrioritySample" in the command line window to run the Java program, and the command line window displays The following information:

It can be seen from the above results that the priority of the operator is higher than that of the = operator. b should be incremented first and then added to a. The result is 15

Related learning recommendations: java basic tutorial

The above is the detailed content of What does java operator precedence look like?. 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
Previous article:How to save data in javaNext article:How to save data in java