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
.
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
and there is not sufficient memory available to perform the conversion.
Example 4.2.2-1. Integer Operations
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
.