Home  >  Article  >  Java  >  Arithmetic operator precedence order in java

Arithmetic operator precedence order in java

下次还敢
下次还敢Original
2024-04-26 01:00:23309browse

Priority order of arithmetic operators in Java: unary operators ( , --) multiplication and division operators (*, /, %) addition and subtraction operators ( , -)

Arithmetic operator precedence order in java

Priority order of arithmetic operators in Java

Answer:
In Java, Arithmetic The priority order of operators is as follows:

1. Unary operators

  • , --:Highest priority
  • , -: positive, negative

2. Multiplication and division operators

  • *,/,%:Higher priority

3. Addition and subtraction operators

  • , -:Lower priority

Expand answer:

Unary operators have the highest precedence, which means they are executed first. For example:

<code class="java">int num = 10;
int result1 = ++num; // result1 = 11, num = 11
int result2 = -num; // result2 = -11</code>

Next, the multiplication, division, and modulo operators have higher precedence. For example:

<code class="java">int num1 = 10;
int num2 = 3;
int result3 = num1 * num2; // result3 = 30
int result4 = num1 / num2; // result4 = 3
int result5 = num1 % num2; // result5 = 1</code>

Finally, addition and subtraction operators have lower precedence. They are executed after all other operators. For example:

<code class="java">int num3 = 10;
int num4 = 5;
int result6 = num3 + num4; // result6 = 15
int result7 = num3 - num4; // result7 = 5</code>

Note:

  • If two operators have the same precedence, they are executed from left to right.
  • You can use parentheses to change the precedence of operators.

The above is the detailed content of Arithmetic operator precedence order 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