Exception divided by zero in Java

Asked

Viewed 301 times

3

Why this generates an Arithmeticexception / by zero:

for (int i = 1; i <= 5; i++) {
        System.out.println(i);
        int a = i / 0;
        System.out.println(a);
    }

And that’s not (print "Infinity")?

for (int i = 1; i <= 5; i++) {
        System.out.println(i);
        double a = i / 0.0;
        System.out.println(a);
    }
  • I found this: https://stackoverflow.com/questions/12954193/why-does-division-by-zero-with-floating-point-or-double-precision-numbers-not

  • After I understood that dividing by 0.0 gives Infinity. That’s why I deleted my answer.

1 answer

2

This is not so only in Java. This has to do with the mathematical representation itself. In the set of integers you cannot represent the entire division by zero. In the set of real numbers you can use techniques as a limit to calculate a certain value and, in this case, when it divides by zero it tends to infinity. Java implementation only reflects mathematics.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.