Java has several arithmetic operators that can be used to perform mathematical operations on variables and values. These operators include:
- Addition (+): This operator is used to add two values together. For example, “5 + 6” would evaluate to 11.
- Subtraction (-): This operator is used to subtract one value from another. For example, “8 – 5” would evaluate to 3.
- Multiplication (*): This operator is used to multiply two values. For example, “3 * 4” would evaluate to 12.
- Division (/): This operator is used to divide one value by another. For example, “8 / 4” would evaluate to 2.
- Modulus (%): This operator is used to find the remainder of a division operation. For example, “8 % 3” would evaluate to 2.
In addition to these basic arithmetic operators, Java also has several compound assignment operators that can be used to perform arithmetic operations and assign the result to a variable in a single statement. These operators include +=, -=, *=, /=, and %=. For example, the statement “x += 3” would add 3 to the value of x and assign the result back to x.
It is important to note that the order of operations (also known as operator precedence) applies to arithmetic operations in Java. This means that certain operations are performed before others, based on their precedence. For example, multiplication and division have higher precedence than addition and subtraction, so they are performed before those operations. You can use parentheses to specify the order of operations, or to group operations together.