Home  >  Article  >  Java  >  Is multiplication and division first followed by addition and subtraction in java?

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

下次还敢
下次还敢Original
2024-05-01 18:00:26989browse

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>

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

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

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

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

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

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

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!

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