What Happens on Overflow?

image

When a result is too big for the type intended to hold it because of a cast, an implicit type conversion, or the evaluation of an expression, something has to give! What happens depends on whether the result type is integer or floating point.

Integer overflow

When an integer-valued expression is too big for its type, only the low end (least significant) bits get stored. Because of the way two's-complement numbers are stored, adding one to the highest positive integer value gives a result of the highest negative integer value. Watch out for this (it's true for all languages that use standard arithmetic, not just Java).

There is only one case in which integer calculation ceases and overflow is reported to the programmer: division by zero (using / or %) will throw an exception. To “throw an exception” is covered in Chapter 10. It means the flow of control is changed to go to an error-handler that you provide.

There is a class called Integer that is part of the standard Java libraries. It contains some useful constants relating to the primitive type int.

public static final int   MIN_VALUE = 0x80000000; // class Integer
public static final int   MAX_VALUE = 0x7fffffff; // class Integer

There are similar values in the related class Long. Notice how these constants (final) are also static. If something is constant, you surely don't need a copy of it in every object. You can use just one copy for the whole class, so make it static.

One possible use for these constants would be to evaluate an expression at long precision, and then compare the result to these int endpoints. If it is between them, then you know the result can be cast into an int without losing bits, unless it overflowed long, of course.

Floating-point overflow

When a floating-point expression (double or float) overflows, the result becomes infinity. When it underflows (reaches a value that is too small to represent), the result goes to zero. When you do something undefined like divide zero by zero, you get a NaN. Under no circumstances is an exception ever raised from a floating-point expression.

The class Float, which is part of the standard Java libraries, contains some useful constants relating to the primitive type float.

public static final float POSITIVE_INFINITY;
public static final float NEGATIVE_INFINITY;
public static final float NaN;
public static final float MAX_VALUE = 3.40282346638528860e+38f;
public static final float MIN_VALUE = 1.40129846432481707e-45f;

One pitfall is that it doesn't help to compare a value to NaN, for NaN compares false to everything (including itself)! Instead, test the NaNiness of a value like this:

if (Float.isNaN( myfloat ) )  // It's a NaN

There are similar values in the class Double.

Arithmetic that cannot overflow

There is a class called java.math.BigInteger that supports arithmetic on unbounded integers, and a class called java.math.BigDecimal that does the same thing for real numbers. They simulate arbitrary precision arithmetic in software. Because the operations are implemented by software, not hardware, they are not as fast as arithmetic on the primitive types, but they offer arbitrary precision operands and results.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.159.223