What is the value of: 12 - 4/2 + 2
12, since the expression means: 12 - 2 + 2.
Operator | Meaning | precedence |
---|---|---|
- | unary minus | highest |
+ | unary plus | highest |
* | multiplication | middle |
/ | division | middle |
% | remainder | middle |
+ | addition | low |
- | subtraction | low |
An arithmetic operator is a symbol that asks for doing some arithmetic. As the previous question illustrates, if several operators are used in an expression, there is a specific order in which the operations are done. Operators of higher precedence will operate first. There are many operators in Java, so the following shows just a few:
In the chart, operators in the same group have equal precedence. So, for example, plus ("+") and minus ("-") have the same precedence.
The unary minus and unary plus operators are used as part of a negative or a positive number. For example, -23 means negative twenty-three. More on this later.