...
The built-in integer operators do not indicate overflow or underflow in any way. Integer operators can throw a
NullPointerException
if unboxing conversion of anull
reference is required. Other than that, the only integer operators that can throw an exception are the integer divide operator/
and the integer remainder operator%
, which throw anArithmeticException
if the right-hand operand is zero, and the increment and decrement operators ++ and -- which can throw anOutOfMemoryError
if boxing conversion is required and there is not sufficient memory available to perform the conversion.
The integral types in Java are byte
, short
, int
, and long
, whose values are 8-bit, 16-bit, 32-bit and 64-bit signed twoâs-complement integers, respectively, and char
, whose values are 16-bit unsigned integers representing UTF-16 code units.
Wiki Markup |
---|
According to the Java Language Specification \[[JLS 05|AA. Java References#JLS 05]\], section 4.2.1 "Integral Types and Values", the values of the integral types are integers in the following ranges: |
- For byte, from â“128 to 127, inclusive
- For short, from â“32768 to 32767, inclusive
- For int, from â“2147483648 to 2147483647, inclusive
- For long, from â“9223372036854775808 to 9223372036854775807, inclusive
- For char, from '\u0000' to '\uffff' inclusive, that is, from 0 to 65535
The table shown below enlists the operators that can lead to overflows:
...
If the result of the addition is greater than the maximum value or less than the minimum value that the int
type can representcan be represented as an int
, then the variable temp
will contain an erroneous result.
...
Explicitly check the range of each arithmetic operation and throw an ArithmeticException
on overflow. When performing operations on values of type int
or smaller, the arithmetic can be done performed using variables of type long
. For performing arithmetic operations on numbers of type long
, the BigInteger
Class must be used.
...
The
int
data type is a 32-bit signed two's complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive).The
long
data type is a 64-bit signed two's complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive). Use this data type when you need a range of values wider than those provided byint
.
Since Because a variable of the long
type is guaranteed to hold the result of an addition, subtraction or multiplication of values of type int
, the result can be assigned to such a variable, and if the result is in the integer range, we can simply downcast it to a value of type int
.
...
This compliant solution uses a variable of type long
to store the result of the addition and proceeds to range check its value.
If the value cannot be represented in a variable of type int
, it throws an ArithmeticException
. Otherwise, it downcasts down casts the result to a value of type int
.
Code Block | ||
---|---|---|
| ||
public int do_operation(int a, int b) throws ArithmeticException { long temp = (long)a + (long)b; if (temp > Integer.MAX_VALUE || temp < Integer.MIN_VALUE) { throw new ArithmeticException("NotOut inof range"); } return (int)temp; // Value within range; can perform the addition } |
...