Index

chapter1

chapter2page1

chapter2page2

chapter2page3

chapter2page4

chapter2page5

chapter2page6

chapter2page7

chapter2page8

chapter2page9

chapter2page10

chapter2page11

chapter2page12

chapter2page13

chapter2page14

chapter2page15

chapter2page16

chapter2page17

chapter2page18

chapter2page20

chapter2page21

chapter2page22

chapter2page23

chapter2page24

chapter2page25

chapter2page26

chapter2page27

chapter2page28

chapter2page29

chapter2page30

chapter2page31

chapter2page32

chapter2page33

chapter2page34

chapter3

link one

link one

link one

link one

link one

link one

2.6.2.2.3 - Multiplication Operator *

The multiplication operator acts exactly as the mathematical multiplication. But of course here, one must keep track of the data types to be multiplied. Automatic widening conversions may occur during the multiplication. For example an int multiplied by a double, result a double. An example is presented below:

package preliminary;
public class Multiplication {
public static void main(String[] args){
double width = 22.77;
int length = 0.03;
System.out.print(" Result = "  + width * length);
 } // end main
} //end class Multiplication

The result of this program :

       Result = 68.31

Which println() method has displayed the result ? First, the highest priority is at multiplication operator and the a value of data type int is multiplied by a value of data type double. The result is promoted to double by a widening automatic type conversion. Then a string is concatenated with a value of data type double, resulting the conversion of the whole chunk to String data type. Finally the println(String x) method is called and the result is displayed as a string.

In multiplication rapid increase/decrease of values may lead to overflow/underflow. Programers have to take this danger into account and take appropriate measures for the program will not crash.

2.6.2.2.4 - Division Operator /

The division operator acts exactly as the mathematical division but here, we must differenciate between integer division and real division. When both of the operands are one the integer data types, the result is an integer and the fractional part is lost. When one of the operands is real the result is real with fractional part. If we stick to int for integer types and with double for real types, real division will perform with minimum disturbance.

Two examples are presented below, one for integer division, the other for real division.:

package preliminary;
public class IntegerDivision {
public static void main(String[] args){
int step = 187;
int minutes = 2;
int ratio = step / minutes;
System.out.print(" Step per Minute = ");
System.out.println(ratio);
 } // end main
} //end class IntegerDivision

The result of this program :

      Step per Minute = 93
package preliminary;
public class RealDivision {
public static void main(String[] args){
double kilometers = 245.77;
int hours = 3;
double speed = kilometers / hours;

System.out.print(speed);
System.out.println(" (Kilometers per Hour)");

 } // end main
} //end class RealDivision

The result of this program :

      81.92333333333333 (Kilometers per Hour)

When The compiler is forced to execute the program below,

package preliminary;
public class DivisionIdeosynchrasy {
public static void main(String[] args){
int expression =-2_147_483_648 /(- 1);

System.out.println(" Result =");
System.out.print(expression);
System.out.println("  (Do not Believe Every Output Of Computers !");

 } // end main
} //end class DivisionIdeosynchrasy

The result of this program :

       Result =-2147483648  (Do not Believe Every Output Of Computers !)

The result is really a ideosynchrasy, because the division

-2_147_483_648 /(- 1);

should result 2_147_483_648 not -2_147_483_648. But the range of the primitive integer data type int is bounded by -2_147_483_648<Valid Data<2_147_483_647 and there no any int type data 2_147_483_647 +1 = 2_147_483_648. This result is an overflow and displays bogus numbers.

Also, do not try to divide anything with 0 (zero) , it will throw an ArithmeticException.

2.6.2.2.5 - Modulus (Remainder) Operator %

The modulus operatoraccepts two integer operands and returns the remainder of integer division. This operator is sometimes useful. One application is shown below:

package preliminary;
public class Modulus {
public static void main(String[] args){
int dividend = 300;

int divisor = 30;

Iint remainder = dividend % divisor;

System.out.print(" Remainder =");
System.out.println(remainder);

 } // end main
} //end class Modulus

The result of this program :

        Remainder = 0

Since the remainder is zero, then the dividend is exactly divisible by the divisor. This is one of the useful areas of application of the modulus operator.

2.6.2.3 - Arithmetic Expressions

Arithmetic expressions are data or variables separated with the arithmetic operators. An excpression may be as simple as an addition expression comprising only one addition operator and two operands, or it may be a crowded agglomeration of operators and operands.

In an expression, the sequence of operations is stricly controlled by the priorities of the operators involved. But, it will be wise that nothing will be left to the decision of the compiler. Using paranthesis is the programmer him or herself will take the lead and guide the compiler about the right way of assembling the actors in the play. As an example, the expression below, will return 16 if it is left as it is,

int result = 24 / 2 + 4;

But, if our intention is to divide 24 with 2 + 6 the sequence of operations should be explicitly defined by using the parenthesis :

int result = 24 / (2 + 4);

In this case of course, the expression will result to 6.

It is recommended to use as much as parenthesis needed to remove any ambiguity in the expressions. Anything you may read better, is executed with more ease by the compiler. The compiler understands better, what the humans understand better.

<< Index

Polyglott HTML5(XHTML5 compliant HTML5 code)