Java does not handle integer overflows and underflows. The values will be wrap around by adding 1 to the maximum values of a primitive data type, which returns the minimum value.
How does Java handle integer overflow?
4. Handling Underflow and Overflow of Integer Data Types
- 4.1. Use a Different Data Type. If we want to allow values larger than 2147483647 (or smaller than -2147483648), we can simply use the long data type or a BigInteger instead. ...
- 4.2. Throw an Exception. ...
- 4.3. Before Java 8.
What happens when an integer type number overflows?
When you go above the maximum value of the signed integer, the result usually becomes a negative number. For example, 2,147,483,647 +1 is usually −2,147,483,648. When you go below the minimum value (underflow), the result usually becomes a positive number. For example, −2,147,483,648 − 1 is usually 2,147,483,647.