<< Chapter < Page | Chapter >> Page > |
The bad news
The bad news about doing arithmetic using operands of integer types is that when the result is not within the allowable value range for the wider of the integer types, the results are garbage, having no usable relationship to the correct result. (Floating arithmetic has a much higher probability of producing approximately correct results, even though the results may not be exact.)
For this specific case...
As you will see by the answer to this question given below, when a value of 2 (see Listing 2 ) was added to the largest positive value that can be stored in type int , the incorrect result was a very large negative value.
This result is simply not correct. (If you know how to do binary arithmetic, you can figure out how this happens, but that is a topic for another lesson on another day.)
No safety net in this case -- just garbage
Furthermore, there was no compiler error and no runtime error. The program simply produced an incorrect result with no warning.
You need to be especially careful when writing programs that perform arithmetic using operands of integer types. Otherwise, your programs may produce incorrect results without you even knowing that it is happening.
And the correct answer to the question is...
D. -2147483647
What output is produced by the program in Listing 3 ?
Listing 3 . Integer divide by zero. |
---|
public class Division03{
public static void main(String args[]){
new Worker().doWork();}//end main()
}//end class definitionclass Worker{
public void doWork(){int myVar01 = 11;
int myVar02 = 0;System.out.println(myVar01/myVar02);
}//end doWork()}//end class definition |
Dividing by zero
This program attempts to divide the int value of 11 by the int value of zero.
Integer division by zero is not allowed
This code produces the runtime error shown in Figure 2 , which terminates the program.
Two ways to deal with this kind of problem
One way is to test the right operand before each divide operation to ensure that it isn't equal to zero, and to take appropriate action if it is.
A second (probably preferred) way is to use exception handling and to surround the divide operation with a try block, followed by a catch block for the type java.lang.ArithmeticException . The code in the catch block can be designed to deal with the problem if it occurs. (Exception handling will be discussed in a future lesson.)
And the answer to the question is...
The code in Listing 3 produces the runtime error shown in Figure 2 .
Figure 2. Runtime error. |
---|
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Worker.doWork(Division03.java:13)at Division03.main(Division03.java:5) |
What output is produced by the program shown in Listing 4 ?
Listing 4 . Floating divide by zero. |
---|
public class Division02{
public static void main(String args[]){
new Worker().doWork();}//end main()
}//end class definitionclass Worker{
public void doWork(){double myVar01 = -11;
double myVar02 = 0;System.out.println(myVar01/myVar02);
}//end doWork()}//end class definition |
Notification Switch
Would you like to follow the 'Ap computer science a, clarification of the java subset' conversation and receive update notifications?