- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 < Integer.MIN_VALUE) { throw 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
To Continue Learning Please Login
Login with Google