Assigning the maximum or minimum value for a type to a variable of that type and then using the variable in calculations may cause overflows.

Before using the variable, ensure that it is reassigned a value that does not cause an overflow, or use a wider type to do the arithmetic.

In this example, assigning Long.MAX_VALUE to a variable and adding one causes an overflow. However, casting to a long beforehand ensures that the arithmetic is done in the wider type, and so does not overflow.

  • SEI CERT Oracle Coding Standard for Java: NUM00-J. Detect or prevent integer overflow.