Wednesday 7 October 2015

Divide by zero in Double and Integer

Divide by zero in Double and Integer:
There is no exception in case of Double.


public class TestJavaHack {
       public static void main(String[] args) {
              double num1 = 1.0/0.0;
             
              System.out.println(num1); // print Infinity.
              if(Double.isInfinite(num1)) {
                     System.out.println("its Double.INFINITY");
              }
             
              int num = 1/0;
              System.out.println(num); // ArithmeticException
       }
}
Output:
Infinity
its Double.INFINITY
Exception in thread "main" java.lang.ArithmeticException: / by zero
       at TestJavaHack.main(TestJavaHack.java:10)


No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...