Arithmetic Operators
Lesson:
Main Points:
- The arithmetic operators have lower precedence than the unary
operators.
- There are five arithmetic operators: *, /, %, +, -.
- *, /, and % have higher precedence than + and -.
- Integer division can generate an ArithmeticException from a division by
zero.
- The + operator is overloaded in Java.
- NaN is a special value defined in the java.lang package.
Multiplication and Division Operators:
The * and / symbols are used for multiplication and division. The use of
these symbols are fairly straight forward and for the most part the only
tricky situations come when dividing. First of all, an ArithmeticException
is thrown when a division by zero is performed. This can freeze an applet if
the exception is not properly caught. Secondly, it should be pointed out
that integer division can only result in whole numbers and so there is
always a loss of precision when dividing integers. For instance, if x = 29
and y = 8, then the result of x/y will be 3. In order to get a result with
decimal values in it you would have to cast x and y to float or double.
Modulo Operator:
The % symbol is used for the modulo operator. This is a real handy operator
that you probably never heard about in school. It basically gives you the
remainder of a division problem. For instance, if x = 29 and y = 8, then the
result of x%y will be 5.
Addition and Subtraction Operators:
The + and - symbols are used for addition and subtraction. The interesting
point to made here has to do with the overloading of the + symbol. It can be
used for numeric addition or string concatenation. String concatenation
simply means the joining together of string values. However, since string
concatenation takes precedence over numeric addition, there are situations
where the programmer must use the + symbol carefully. Here's an example:
int x = 7;
int y = 5;
String output = "ANSWER: " + x + y;
What do you think will be stored in output here?
At this point output will contain the string: "ANSWER: 75". In order for it
to contain "ANSWER: 12" the following alteration is necessary.
int x = 7;
int y = 5;
String output = "ANSWER: " + ( x + y );
Now the numeric additon is performed and the result of this operation is
concatenated to the end of the string.
Arithmetic Error Conditions:
You should be aware of the following error conditions that can occur:
- Integer division by zero, including a modulo (%) operation, results in
an ArithmeticException being thrown.
- No other arithmetic causes any exception. Instead, the operation
proceeds to a result, even though that result might not be arithmetically
correct.
- Floating-point calculations represent out-of-range values using
infinity, minus infinity and Not a Number (NaN) values. The Float and Double
classes contain constants which represent these values.
- Integer calculations, other than division by zero, that cause overflow
or similar error, simply leave the final, often truncated, bit pattern as a
result. See the sample program for an example.
Example Applet:
import java.awt.*;
import java.applet.*;
public class ch202 extends Applet{
byte x;
int five;
public void init(){
setBackground(new Color(0,120,0));
}
public void paint(Graphics g){
g.setColor(Color.white);
g.drawString("BYTE VALUES:", 10, 20);
//byte value x will overflow during this loop
x = 123;
for(int c = 0; c<8; c++){
g.drawString("Value of byte value x: " + x, 10, 40 + 15*c);
x++;
}
g.drawString("MULTIPLES OF FIVE:", 300, 20);
//modulus operator used to format multiples of five
five = 5;
for(int c = 0; c<40; c++){
g.drawString(""+five, 300 + (c%5)*40, 40 + (c/5)*15);
five += 5;
}
}
}
Assignment:
Write an applet which uses the modulus operator in two ways. First of all,
the numbers from one to 200 should be displayed in rows containing ten
numbers each. Secondly, the multiples of five will be green, the multiples of
three will be red, the multiples of seven will be blue, and the multiples of
eleven will be magenta. All other numbers will be black and your background
color will be white.