What is the value of:
12 - 4/2 + 2
12, since the expression means:
12 - 2 + 2
An arithmetic operator is a symbol that asks for two numbers to be combined by arithmetic. As the previous question illustrates, if several operators are used in an expression, the operations are done in a specific order.
Operators of higher precedence are done first. The table shows the precedence of some Java operators.
Some operators have
For example, addition
+ and subtraction
- have the same precedence.
The unary minus and unary plus operators are used as
part of a negative or a positive number.
-23 means negative twenty-three and
+23 means positive twenty-three.
More on this later.
When both operands (the numbers) are integers, these operators do integer arithmetic. If one operand or both operands are floating point, then these operators do floating point arithmetic. This is especially important to remember with division, because the result of integer division is an integer.
What is the value of the following expressions? In each expression, do the highest precedence operator first.