0% found this document useful (0 votes)
62 views

1 Arithmetic Operators and Order of Precedence

The document discusses Java arithmetic operators and order of precedence. It details the five basic arithmetic operators in Java (+, -, *, /, %) and their use with both integral and floating-point data types. It also explains that operators with the same precedence are evaluated left to right, and parentheses can be used to specify order of operations. Sample code is provided to demonstrate arithmetic expressions and the output.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

1 Arithmetic Operators and Order of Precedence

The document discusses Java arithmetic operators and order of precedence. It details the five basic arithmetic operators in Java (+, -, *, /, %) and their use with both integral and floating-point data types. It also explains that operators with the same precedence are evaluated left to right, and parentheses can be used to specify order of operations. Sample code is provided to demonstrate arithmetic expressions and the output.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

JAVA PROGRAMMING FUNDAMENTALS

ARITHMETIC OPERATORS AND ORDER OF PRECEDENCE

ARITHMETIC OPERATORS IN JAVA

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.

The following Java program evaluates the preceding expressions:


Example 1
Sample Code
public class Arithmetic_Expression {
public static void main(String[] args) {
System.out.println("2 + 5 = " + (2+5));
System.out.println("13 + 89 = " + (13+89));
System.out.println("34 - 20 = " + (34-20));
System.out.println("45 - 90 = " + (45-90));
System.out.println("2 * 7 = " + (2*7));
System.out.println("5 / 2 = " + (5/2));
System.out.println("14 / 7 = " + (14/7));
System.out.println("34 % 5 = " + (34%5));
System.out.println("4 % 6 = " + (4%6));
}
}
Output:
2+5=7
13 + 89 = 102
34 - 20 = 14
45 - 90 = -45
2 * 7 = 14
5/2=2
14 / 7 = 2
34 % 5 = 4
4%6=4

The following Java program evaluates various floating-point expressions:


Example 2
Sample Code
public class Arithmetic_Expression2 {
public static void main(String[] args) {
System.out.println("5.0 + 3.5 = " + (5.0 + 3.5));
System.out.println("3.0 + 9.4 = " + (3.0 + 9.4));
System.out.println("16.4 - 5.2 = " + (16.4 - 5.2));
System.out.println("4.2 * 2.5 = " + (4.2 * 2.5));
System.out.println("5.0 / 2.0 = " + (5.0 / 2.0));
System.out.println("34.5 / 6.0 = " + (34.5 / 6.0));
System.out.println("34.5 % 6.0 = " + (34.5 % 6.0));
System.out.println("34.5 / 6.5 = " + (34.5 / 6.5));
System.out.println("34.5 % 6.5 = " + (34.5 % 6.5));
}
}

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:

* , /, %

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.
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?

 Two rules apply when evaluating a mixed expression :


1. When evaluating an operator in a mixed expression:
a) If the operator has the same types of operands (that is, both are integers or both
are floating-point numbers), the operator is evaluated according to the type of the
operand. Integer operands yield an integer result; floating-point numbers yield a
floating-point number result.
b) If the operator has both types of operands (that is, one is an integer and the
other is a floating-point number), during calculation the integer is treated
temporarily as a floating-point number with the decimal part of zero, and then the
operator is evaluated. The result is a floating-point number.
2. The entire expression is evaluated according to the precedence rules. The
multiplication, division, and modulus operators are evaluated before the addition and
subtraction operators. Operators having the same level of precedence are evaluated
from left to right. Grouping is allowed for clarity.
EXAMPLE
MIXED EXPRESSION EVALUATION
= 1 + 5.0
3 / 2 + 5.0
= 6.0
= 7.8 + 5
15.6 / 2 + 5
= 12.8
= 4 + 2.5
4 + 5 / 2.0
= 6.5
= 12 + 7 / 5 – 25.5
= 12 + 1 – 25.5
4 * 3 + 7 / 5 – 25.5
= 13 – 25.5
= -12.5

The following Java program evaluates the preceding expressions:


Sample Code
public class Mixed_Expression {
public static void main(String[] args) {
System.out.println("3 / 2 + 5.0 = " + (3 / 2 + 5.0));
System.out.println("15.6 / 2 + 5 = " + (15.6 / 2 + 5));
System.out.println("4 + 5 / 2.0 = " + (4 + 5 / 2.0));
System.out.println("4 * 3 + 7 / 5 - 25.5 = " + (4 * 3 + 7 / 5 - 25.5));
}
}

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

You might also like