Exception in division by zero in Java [duplicate]

3

Why does this generate 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 does not (print "Infinity")?

for (int i = 1; i <= 5; i++) {
        System.out.println(i);
        double a = i / 0.0;
        System.out.println(a);
    }
    
asked by anonymous 01.05.2018 / 09:37

1 answer

2

This is not just in Java. This has to do with the same mathematical representation. In the set of integers you can not represent the whole division by zero. Already in the set of real numbers you can use techniques like limit to calculate a certain value and in this case, when dividing by zero tends to infinity. The Java implementation only reflects math.

    
01.05.2018 / 12:36