Home > Java > javaTutorial > body text

Is multiplication and division first followed by addition and subtraction in java?

下次还敢
Release: 2024-05-01 18:00:26
Original
1049 people have browsed it

In Java, the operator priority order is: parentheses, unary operators, multiplication and division, addition and subtraction. Follow the principle of multiplication and division before addition and subtraction. When there are no parentheses, multiplication and division operations take precedence over addition and subtraction operations.

Is multiplication and division first followed by addition and subtraction in java?

Operator precedence in Java: multiplication and division first, then addition and subtraction

In Java, the precedence of operators Priority determines the order in which arithmetic operations are performed. The higher the operator precedence, the stronger the precedence.

Priority rules:

Brackets (())> Unary operators (,-)> Multiplication and division (*,/)> Addition and subtraction (, -)

Multiply and divide before addition and subtraction:

This means that, without parentheses, multiplication and division operations will be performed before addition and subtraction operations. For example:

<code class="java">3 + 4 * 5</code>
Copy after login

According to the priority rules, multiplication is performed first, then addition:

<code>3 + (4 * 5) = 3 + 20 = 23</code>
Copy after login

If you need to change the order of operations, you can use brackets:

<code class="java">(3 + 4) * 5</code>
Copy after login

In this case Next, perform the addition within the brackets first, then perform the multiplication:

<code>(3 + 4) * 5 = (7) * 5 = 35</code>
Copy after login

The above is the detailed content of Is multiplication and division first followed by addition and subtraction in java?. For more information, please follow other related articles on the PHP Chinese website!

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