Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How can we check an underflow occurs in Java?
When a value is assigned to a variable that is less than the minimum allowed value for that variable, then an underflow occurs. There is no exception thrown by the JVM if an underflow occurs in Java and it is the responsibility of a programmer to handle the underflow conditions.
Example
public class UnderlowTest {
public static void main(String[] args) {
int num1 = -2147483648;
int num2 = -1;
System.out.println("Number 1: " + num1);
System.out.println("Number 2: " + num2);
long sum = (long)num1 + (long)num2;
if(sum new ArithmeticException("Underflow occurred!");
}
System.out.println("The sum of two numbers : " + (int)sum);
}
}
Output
Number 1: -2147483648
Number 2: -1
Exception in thread "main" java.lang.ArithmeticException: Underflow occurred!
at UnderlowTest.main(UnderlowTest.java:9) Advertisements
