Java Reference
In-Depth Information
Any value of any integral type may be cast to or from any numeric type. There are no casts
between integral types and the type boolean .
See § 4.2.5 for an idiom to convert integer expressions to boolean .
The integer operators do not indicate overflow or underflow in any way.
An integer operator can throw an exception (§11) for the following reasons:
• Any integer operator can throw a NullPointerException if unboxing conversion
5.1.8 ) of a null reference is required.
• The integer divide operator / 15.17.2 ) and the integer remainder operator %
15.17.3 ) can throw an ArithmeticException if the right-hand operand is zero.
• The increment and decrement operators ++ 15.14.2 , § 15.15.1 ) and -- 15.14.3 ,
§ 15.15.2 ) can throw an OutOfMemoryError if boxing conversion (§ 5.1.7 ) is required
and there is not sufficient memory available to perform the conversion.
Example 4.2.2-1. Integer Operations
Click here to view code image
class Test {
public static void main(String[] args) {
int i = 1000000;
System.out.println(i * i);
long l = i;
System.out.println(l * l);
System.out.println(20296 / (l - i));
}
}
This program produces the output:
-727379968
1000000000000
and then encounters an ArithmeticException in the division by l - i , because l - i is zero.
The first multiplication is performed in 32-bit precision, whereas the second multi-
plication is a long multiplication. The value -727379968 is the decimal value of the low
32 bits of the mathematical result, 1000000000000 , which is a value too large for type
int .
Search WWH ::




Custom Search