9/17/2008
9/17/2008
9/17/2008
9/17/2008
Overflow
•An infinite loop often results in an overflow error.
•An overflow error occurs when you attempt to assign a value larger than the maximum value the variable can hold.
•In Java, an overflow does not cause program termination. With types float and double, a value that represents infinity is assigned to the variable. With type int, the value “wraps around” and becomes a negative value.
When an overflow error occurs, the execution of the program is terminated in almost all programming languages. When an overflow occurs in Java, a value that represents infinity (IEEE 754 infinity, to be precise) is assigned to a variable and no abnormal termination of a program will happen. Also, in Java an overflow occurs only with float and double variables; no overflow will happen with int variables.  When you try to assign a value larger than the maximum possible integer an int variable can hold, the value “wraps around” and becomes a negative value.

Whether the loop terminates or not because of an overflow error, the logic of the loop is still an infinite loop, and we must watch out for it. When you write a loop, you must make sure that the boolean expression of the loop will eventually become false.