0
    double doubleResult = 1d/0d;
    System.out.println(doubleResult);

    int intResult = 1/0;
    System.out.println(intResult);

The output is:

Infinity
Exception in thread "main" java.lang.ArithmeticException: / by zero

Why does double zero division returns Infinity and int zero division throws exception?

volatile
  • 179
  • 8
  • See also [this question](http://stackoverflow.com/questions/2381544/why-doesnt-java-throw-an-exception-when-dividing-by-0-0). – Tom Scallon Jul 04 '16 at 14:04

1 Answers1

1

This is principally due to the fact that the double type (which in Java is an IEEE754 64 bit double precision type) has a representation for infinity, whereas an int type does not.

Note that double doubleResult = 1 / 0; would still cause an exception to be thrown: the type of the variable to which the result is assigned is not relevant.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483