Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • For byte, from –128 to 127, inclusive
  • For short, from –32,768 to 32,767, inclusive
  • For int, from –2,147,483,648 to 2,147,483,647, inclusive
  • For long, from –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807, inclusive
  • For char, from \u0000 to \uffff inclusive, that is, from 0 to 65,535

...

When the result of the addition is outside the range that can be represented as an int, the variable temp will contain an erroneous result. Shorter This problem does not occur when using short types, such as byte and short, avoid this problem because the operands are promoted to type int before the operation is carried out. The language disallows storing the result of such operations in variables of types shorter than type int.

...

For all integral types other than long, the next larger integral type can represent the result of any single integral operation. For example, operations on values of type int, can be safely performed using type long. Therefore, we can perform an operation using the larger type and range-check before downcasting down casting to the original type. Note, however, that this guarantee holds only for a one arithmetic operation; larger expressions without per-operation bounds checks may overflow the larger type.

...

This compliant solution uses the BigInteger class as a wrapper to test for the overflow.

Code Block
bgColor#ccccff
public long do_operation(long a, long b) throws ArithmeticException {
  return longRangeCheck(BigInteger.valueOf(a).add(BigInteger.valueOf(b));
}

public long longRangeCheck(BigInteger val) throws ArithmeticException {
  if (val.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) == 1 ||
          val.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) == -1) {
    throw new ArithmeticException("Out of range for long");
  }
  return val.longValue();
}

With use of the The BigInteger class , eliminates integer overflows are eliminated. However, due to increased performance costs, it should be used only when other methods are not appropriatebut at the cost of increased overhead.

Subtraction

Subtraction overflows when the first operand is a negative integer and the second operand is a large positive integer such that their difference is not representable as a value of type int. Subtraction also overflows when the first operand is positive and the second operand is negative and their difference is not representable as a value of type int.

...

A related pitfall is the use of the Math.abs() method that takes a parameter of type int and returns its absolute value. Because of the asymmetry between the two's complement representation of negative and positive integer values (Integer.MAX_VALUE is 2147483647 and Integer.MIN_VALUE is -21474836482,147,483,648, which means there is one more negative integer than positive integers), there is no equivalent positive value (+21474836482,147,483,648) for Integer.MIN_VALUE. ThusConsequenly, the Math.abs() returns Integer.MIN_VALUE when the value of its argument is Integer.MIN_VALUE; this may surprise many programmers.

...