Header Ads

Breaking News
recent

Arithmetic Operators and Operator 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:
Arithmetic Operators: + (addition), - (subtraction or negation), * (multiplication), / (division), % (mod, (modulus 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. (Examples 2-2 and 2-3 clarify how the operators / and % work with integral and floating-point data types.)
You have probably worked with arithmetic expressions such as
the following:
(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 only 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. Unary operator: An operator that has only one operand. Binary operator: An operator that has two operands.

In the 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 the 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. As you can see from these examples, the operator / represents the quotient in ordinary division when used with integral data types.

Arithmetic Expression
Result Description
2 + 5 7
13 + 89 102
34 - 20 14
45 - 90 -45
2 * 7 14
5 / 2 2 In the division 5 / 2, the quotient is 2 and the remainder is 1.
Therefore, 5 / 2 with the integral operands evaluates to the quotient, which is 2.
14 / 7 2
34 % 5 4 In the division 34 / 5, the quotient is 6 and the remainder is 4.
Therefore, 34 % 5 evaluates to the remainder, which is 4.
4 % 6 4 In the division 4 / 6, the quotient is 0 and the remainder is 4.
Therefore, 4 % 6 evaluates to the remainder, which 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 for arithmetic operators: *, /, %
have a higher level of precedence than: +, -
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.

No comments:

Powered by Blogger.