• No results found

OPERATIONS ON NUMERIC DATA TYPES

In document Java Programming Fundamentals (Page 64-73)

Th ere are fi ve operations on numeric data types. Th ey are 1. Addition (symbol used is +)

2. Subtraction (symbol used is −) 3. Multiplication (symbol used is *) 4. Division (symbol used is /) 5. Modulus (symbol used is %)

TABLE 2.4 Real Numbers Printed in Java Floating Point Notation

Real Number Scientifi c Notation Floating Point Notation of Java

123.456 1.23456×(10)2 1.234560E2

.456 4.56×(10)−1 4.560000E–1

0.00000456 4.56×(10)−6 4.560000E–6

-1.234 –1.23×(10)0 –1.234000E0

1230000. 1.23×(10)6 1.230000E6

Apago PDF Enhancer

Th e following example illustrates addition, subtraction, multiplication, division, and modulus operations on integer data types.

Example 2.13

Th e following program illustrates all fi ve integer operators:

/**

Demonstration of numeric operations in integer mode

*/

public class IntegerOperator {

public static void main(String[] args) {

Apago PDF Enhancer

Output 12 + 51 = 63

12 + 51 = 39 51 12 = 39 51 12 = 63 3 * 8 = 24 19 / 5 = 3 20 / 5 = 4 19 % 5 = 4 19 % 5 = 4 19 % 5 = 4 19 % 5 = 4 5 % 19 = 5

All fi ve operations can be used on two integers or two fl oating point numbers or one integer and one fl oating point number.

Example 2.14 illustrates four operations on fl oating point numbers. Modulus operator is rarely used in connection with fl oating point operands and hence omitted.

Example 2.14

Th e following program computes all the expressions given in Table 2.5:

/**

Demonstration of numeric operations in floating point mode

*/

public class FloatingPointOperator {

public static void main(String[] args) {

System.out.println(" (12.0 + 49.2) = "+ (12.0 + 49.2));

System.out.println(" (12.3 + 49.2) = "+ (12.3 + 49.2));

System.out.println(" (12.3 - 49.2) = "+ (12.3 - 49.2));

System.out.println(" (12.3 * 49.2) = "+ (12.3 * 49.2));

System.out.println(" (12.3 / 49.2) = "+ (12.3 / 49.2));

} }

Output (12.0 + 49.2) = 61.2

(12.3 + 49.2) = 61.5

(12.3 - 49.2) = -36.900000000000006 (12.3 * 49.2) = 605.1600000000001 (12.3 / 49.2) = 0.25

Apago PDF Enhancer

Note that (12.3 – 49.2) is not –36.9, as you might have expected; rather, the value computed by the above program is –36.900000000000006. Again note that multi-plication also produced a result diff erent from what you might have expected. In other words, fl oating point arithmetic is not exact.

So far, we have used all operators with two operands. If an operator has two operands, it is called a binary operator. Th us addition, subtraction, multiplication, division, and modulo.

are binary operations. In other words, +, –, *, and % are binary operators. However, we also use symbols + and – as unary operators or operators with one operand. For example, in the expressions +12.5 and –51.5, the operators + and – are unary operators. Note further that in the expression +12.5, the symbol + does not stand for addition. Similarly, in the expression –51.5, the symbol – does not stand for subtraction.

Self-Check 23. What is the value of 17/18?

24. What is the value of 17 % 18?

Operator Precedence Rules

Consider the following arithmetic expression:

7 + 3 * 2

If you add 7 and 3 fi rst and then multiply by 2 the expression is equal to 20 and if you fi rst multiply 3 and 2 and then add 7, it is equal to 13. Which is the correct answer? From your high school mathematics, you know that multiplication needs to be done before addition.

Th erefore, in this case, the correct answer is 13 and not 20. In other words, the operator * is of higher precedence than the operator +.

Whenever more than one operator appears in an expression, there needs to be a rule to determine the order in which these operators are evaluated. Every programming lan-guage provides such rules and Java is no exception. Th ese rules are called operator prece-dence rules. Th e operator precedence rules for arithmetic operators can be summarized as follows:

Unary operators + and – have the same precedence, and they are evaluated from right to left .

TABLE 2.5 Operations on Floating Point Data Expression Mathematical Result

12.0 + 49.2 61.2

12.3 + 49.2 61.5

12.3 - 49.2 -36.9

12.3 * 49.2 605.16

12.3 / 49.2 0.25

Apago PDF Enhancer

Binary operators *, /, and % have the same precedence, and they are evaluated from left to right. Further, Unary operators + and – have higher precedence than the operators *, /, and %.

Binary operators + and – have the same precedence, and they are evaluated from left to right. Th e operators *, /, and % have higher precedence than the binary operators + and –.

Th e property that an operator is evaluated from left to right or from right to left is called the associativity of that operator. Th us, note that the associativity of unary arith-metic operators is from right to left and that of binary aritharith-metic operators is from left to right.

Introspection

What is 13 – 7 – 2? Is it (13 – 7) – 2 = 4 or 13 – (7 – 2) = 8?

It is customary to state these rules in the form of a table similar to Table 2.6. Observe that operators in the same row have the same precedence and an operator in a “higher row”

has a higher precedence over an operator in a “lower row.”

In an arithmetic expression, parentheses can be used to modify the order of execution.

Parentheses can also be used for better readability.

Example 2.15 Consider the following expression:

-4 * 2 + 8 - 9 / 3 * 5 + 7

Th e order of evaluation based on precedence rules is as follows:

= (-4) * 2 + 8 - 9 / 3 * 5 + 7 (unary minus is evaluated)

= ((-4) * 2) + 8 - 9 / 3 * 5 + 7 (leftmost mul./div. is evaluated)

= (-8) + 8 – (9 / 3) * 5 + 7 ( leftmost mul./div. is evaluated)

= (-8) + 8 – (3 * 5) + 7 (leftmost mul./div. is evaluated)

= ((-8) + 8) – 15 + 7 ( leftmost add /sub. is evaluated)

TABLE 2.6 Operator Precedence

Operator Operand Types Operation Associativity

+, − Number unary plus, unary minus Right to left

*, /, % Number Multiplication, division, modulus Left to right

+, − Number addition, subtraction Left to right

Apago PDF Enhancer

= (0 – 15) + 7 ( leftmost add /sub. is evaluated)

= (– 15 + 7) ( leftmost add /sub. is evaluated)

= -8

Example 2.16

In the expression 6 + 3 / 3

the division operator / is evaluated fi rst. Th us, the above expression is equivalent to 6 + (3 / 3) = 7. However, parentheses can be used to modify the order of execution as follows:

(6 + 3) / 3 = 3.

Good Programming Practice 2.9

Th e intended use of the char data type is to manipulate characters. However, since char is an integral data type, it is legal to perform arithmetic operations on char data type. Use integral types other than char for computational purposes.

Note 2.12 From Appendix B, it can be seen that character '6' has collating sequence value 54. Th us, '6'+'6' evaluates to 108.

Self-Check 25. What is 27 − 8 / 11 + 4?

26. What is (27 − 8) / 11 + 4?

Rules for Evaluating Mixed Expressions

An expression that has operands of more than one numerical data types is called a mixed expression. Th us, a mixed expression may contain int and long values or int and double values, and so on.

Example 2.17 Consider Table 2.7.

TABLE 2.7 Mixed Expressions

Mixed Expression Explanation

31L / 2 long and int values

32 + 12.5 int and double values

18.723F * 234L float and long values 353.35 – 0.001F double and float values

Apago PDF Enhancer

Th e rules for evaluating mixed expressions can be stated as follows:

Step 1. Determine the next operation based on operator precedence rules.

Step 2. Consider the operands involved. If both of them are of the same data type there is nothing to be done at this step. If they are of diff erent type, Java con-verts the data type of lesser range to the other data type for the purpose of this expression evaluation. Th at is, data values are converted as follows:

byte → short → int → long → float → double or any combination of these in the same direction.

Step 3. Evaluate the expression.

Th us, evaluation of a mixed expression is done on one operator at a time using the rules of precedence. If the operator to be evaluated has mixed operands, Java cannot evaluate as such. Th erefore, the operand with lesser range of values is promoted (or implicitly converted or coerced) to the data type of the other operand before the evaluation. For example, if one operand is int and the other is double, the int data value is promoted to double value. In this case, the fl oating point operation takes place and the result is of type double.

Similarly, if one operand is int and the other is long, the int data value is promoted to long value and the result is of type long. Th us an operator with integer operands will yield an integer result and if one of the operands is a fl oating point, the result is a fl oating point. Th e following example illustrates the evaluation of mixed expressions.

Example 2.18

Evaluation of Mixed Expressions Mixed

Expression

Step 1

(Operator) Step 2 (Operands) Evaluation

17 / 5 +

Apago PDF Enhancer

Advanced Topic 2.6: Mixed Expressions Involving String

As noted before, concatenation operator + can also be used to join a String and a numeric value or a character. In that case, Java fi rst converts the numerical value or character to a String and then uses the concatenation operator +. Th e following example illustrates the use of concatenation operator.

Strings play a very important role in Java. So we will address issues involving Strings in a later chapter. However, at this point you need to know the behavior of the concatenation operator when other data types are involved.

Example 2.19

Th e following Java application illustrates the concatenation operator on Strings and numbers, and Strings and characters:

/**

Demonstration of concatenation operation in mixed mode

*/

public class ConcatOperatorNumber {

public static void main(String[] args) {

System.out.print

("(1)\t 1600 + \" Pensylvania Avenue\" is ");

System.out.println(1600 + " Pensylvania Avenue");

System.out.print

("(2)\t \"Pensylvania Avenue \" + 1600 is ");

System.out.println("Pensylvania Avenue " + 1600);

System.out.print("(3)\t 563 + 34 is ");

Apago PDF Enhancer

System.out.println( 563 * 34);

System.out.print

("(9)\t \"Victoria, NE \" + 563 * 34 is ");

System.out.println("Victoria, NE " + 563 * 34 );

System.out.print

("(10)\t 563 * 34 + \" Victoria, NE \" is ");

System.out.println(563 * 34 + " Victoria, NE ");

System.out.print

("(11)\t \"Victoria, NE \" + (563 * 34) is ");

System.out.println("Victoria, NE " + (563 * 34));

} }

Output

(1) 1600 + " Pensylvania Avenue" is 1600 Pensylvania Avenue (2) "Pensylvania Avenue " + 1600 is Pensylvania Avenue 1600 (3) 563 + 34 is 597

(4) "Victoria, NE " + 563 + 34 is Victoria, NE 56334 (5) 563 + 34 + " Victoria, NE " is 597 Victoria, NE (6) "Victoria, NE " + (563 + 34) is Victoria, NE 597

(7) "Victoria, " + 'N' + 'E' + ' ' + 56334 is Victoria, NE 56334 (8) 563 * 34 is 19142

(9) "Victoria, NE " + 563 * 34 is Victoria, NE 19142 (10) 563 * 34 + " Victoria, NE " is 19142 Victoria, NE (11) "Victoria, NE " + (563 * 34) is Victoria, NE 19142

Explanation

(1) and (2) (A String and a number case): Th e number 1600 is converted to the String "1600". Th en the concatenation takes place and thus produces the String "1600 Pennsylvania Avenue" and "Pennsylvania Avenue 1600", respectively. (3) (Numbers only case): In this case, all numerical computa-tions will be done fi rst. Th us, 563 and 34 are added together to produce the num-ber 597. (4) (Numnum-bers aft er String case) : Java converts the int value 563 to the String “563” and appends at the end of the String "Victoria, NE". Th is produces a String "Victoria, NE 563". Now int value 34 is converted to the String "34" and concatenated to "Victoria, NE 563", thus producing

"Victoria, NE 56334". (5) (Numbers before String case): In this case, the fi rst + symbol is an addition symbol. Both 563 and 34 are numbers. Th erefore, + is not considered as a concatenation operator. In other words, for + to work as a concatenation symbol, at least one of the two operands must be a String.

Th erefore, 597 is computed fi rst. Now, 597 is a number and "Victoria, NE"

Apago PDF Enhancer

is a String, and therefore the String "Victoria, NE" gets appended to the String "597". (6) (Use of parenthesis): Due to parenthesis, Java adds 563 and 34 fi rst. Consequently, the number 597 is converted to the String "597" and appended to "Victoria, NE" as in cases (1) and (2). Th e output line (7) shows how characters can also be appended to a String using the concatenation opera-tor. Note that each character gets appended to the existing String. (9), (10), and (11) (Arithmetic operator with a higher precedence): Since multiplication has higher precedence, it is done before the concatenation operation.

In document Java Programming Fundamentals (Page 64-73)