
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Infinity or Exception in Java When Divide by 0
Consider the following code snippet where we divide a number by 0.
Example
public class Tester{ public static void main(String[] args) { double d = 100; System.out.println(d/0); } }
Output
Infinity
Now consider the following code snippet.
Example
public class Tester{ public static void main(String[] args) { int d = 100; System.out.println(d/0); } }
Output
Exception in thread "main" java.lang.ArithmeticException: / by zero at Tester.main(Tester.java:5)
As you've noted, the Infinity vs ArithmeticException, a different result for similar divide by zero program. The difference lies in floating point arithmetic used in first program and integer arithmetic used in second program.
Advertisements