1 Arithmetic Operators and Order of Precedence
1 Arithmetic Operators and Order of Precedence
One of the most important features of a computer is its ability to calculate. You can use the standard
arithmetic operators to manipulate integral and floating-point data types.
Java has five arithmetic operators:
OPERATOR SYMBOL
Addition +
Subtraction or negation -
Multiplication *
Division /
Modulus/ mod or Remainder %
You can use these operators with both integral and floating-point data types. When you use /
with the integral data type, it gives the quotient in integer form. That is, integral division
truncates any fractional part; there is no rounding. Similarly, when you use % with the integral
data type, it gives the remainder in integer form.
Example:
(i) -5
(ii) 8 – 7
(iii) 3 + 4
(iv) 2 + 3 * 5
(v) 5.6 + 6.2 * 3
(vi) x + 2 * 5 + 6 / y
In expression (vi), x and y are some unknown numbers. Formally, an arithmetic expression is
constructed by using arithmetic operators and numbers. The numbers and alphabetical
symbols in the expression are called operands. Moreover, the numbers and alphabetical
symbols used to evaluate an operator are called the operands for that operator.
In expression (i), the operator – (subtraction) is used to specify that the number 5 is negative.
Moreover, in the expression -5, - has only one operand, which is 5. Operators that have one
operand are called unary operators.
In expression (ii), the symbol – is used to subtract 7 from 8. In this expression, - has two
operands, 8 and 7. Operators that have two operands are called binary operators.
In expression (iii), 3 and 4 are the operands for the operator +. Because the operator + has two
operands, in this expression, + is a binary operator.
Now, consider the following expression:
+27
In this expression, the operator + is used to indicate that number 27 is positive. Here,
because + has only one operand, it acts as a unary operator.
From the preceding discussion, it follows that – and + can be unary or binary arithmetic
operators, However, the arithmetic operators +, / , and % are binary and must have two
operands.
The following examples show how arithmetic operators especially / and % work with integral
data types. From these examples, the operator / represents the quotient in ordinary division
when used with integral data types.
Example:
ARITHMETIC RESULT DESCRIPTION
EXPRESSION
2+5 7
13 + 89 102
34 - 20 14
45 - 90 -45
2*7 14
In the division 5 / 2, the quotient is 2 and the
5/2 2 remainder is 1. Therefore, 5 / 2 with integral
operands evaluates to the quotient, which is 2.
14 / 7 2
In the division 34 / 5, the quotient is 6 and the
34 % 5 4 remainder is 4. Therefore, 34 % 5 evaluates to
the remainder, which is 4.
In the division 4 / 6, the quotient is 0 and the
4%6 4 remainder is 4. Therefore, 4 % 6 evaluates to
the remainder, which is 4.
Output:
5.0 + 3.5 = 8.5
3.0 + 9.4 = 12.4
16.4 - 5.2 = 11.2
4.2 * 2.5 = 10.5
5.0 / 2.0 = 2.5
34.5 / 6.0 = 5.75
34.5 % 6.0 = 4.5
34.5 / 6.5 = 5.3076923076923075
34.5 % 6.5 = 2.0
You should be careful when evaluating the mod operator with negative integer operands. You
might not get the answer you would expect. For example, -34 % 5 = -4, because in the division
-34 / 5, the quotient is -6 and the remainder is -4. Similarly, 34 % -5 = 4, because in division
-34 / 5, the quotient is -6 and the remainder is 4. Also -34 % -5 = -4, because in the division
-34 / -5, the quotient is 6 and the remainder is -4.
ORDER OF PRECEDENCE
When more than one arithmetic operator is used in an expression, Java uses the operator
precedence rules to determine the order in which the operations are performed to evaluate the
expression. According to the order of precedence rules of arithmetic operators:
* , /, %
+, -
Note that the operators *, / , and % have the same level of precedence. Similarly, the
operators + and – have the same level of precedence.
When arithmetic operators have the same level of precedence, operations are performed from
left to right. To avoid confusion, you can use parentheses to group arithmetic expressions.
EXAMPLE
Using the order of precedence rules,
3*7–6+2*5/4+6
means the following:
( ( ( 3 * 7 ) – 6) + ( ( 2 * 5 ) / 4 ) ) + 6
= ( ( 21 – 6) + (10 / 4 ) ) + 6 Evaluate *
= ( ( 21 – 6) + 2 ) + 6 Evaluate /. Note that this is an integer division.
= ( 15 + 2 ) + 6 Evaluate –
= 17 + 6 Evaluate first +
= 23 Evaluate +
Note that using parentheses in the preceding expression clarifies the order of precedence.
Because arithmetic operators are evaluated from left to right, unless parentheses are present,
the associativity of arithmetic operators are said to be from left to right.
MIXED EXPRESSIONS
An expression that has operands of different types data types is called a mixed expressions. A
mixed expression contains both integers and floating-point numbers. The following
expressions are examples of mixed expressions:
2 + 3.5
6 / 4 + 3.9
5.4 * 2 – 13.6 + 18 /2
In the first expression, the operand + has one integer operand and one floating-point operand.
In the second expression, both operands for the operator / are integers; the first operand of +
is the result of 6 / 4, and the second operand of + is a floating-point number. The third example
is a more complicated mix of integers and floating-point numbers. How does Java evaluate
such mixed expressions?
Output:
3 / 2 + 5.0 = 6.0
15.6 / 2 + 5 = 12.8
4 + 5 / 2.0 = 6.5
4 * 3 + 7 / 5 - 25.5 = -12.5