What will be the result of the following program compilation and execution?

public class Test {
    public static void main(String[] args) {
        int myInt = 0;
        float myFloat = 0;
        try {
            float result = myFloat / myFloat;
        } catch (RuntimeException re) {
            System.out.println("Exception 1");
        }

        try {
            float result = myFloat / myInt;
        } catch (RuntimeException re) {
            System.out.println("Exception 2");
        }

        try {
            float result = myInt / myFloat;
        } catch (RuntimeException re) {
            System.out.println("Exception 3");
        }

        try {
            float result = myInt / myInt;
        } catch (RuntimeException re) {
            System.out.println("Exception 4");
        }
    }
}

All calculations that involve float are evaluated to NaN, as specified in IEEE 754 standard. Only integer "divide by zero" will throw ArithmeticException.

2016 Apr 25, 7:12:34 PM

because "float myFloat = 0;" - type int

2016 Apr 14, 4:01:44 PM

Why?!

2016 Mar 25, 8:49:19 AM

Why? Really not obviously.

2015 Nov 8, 3:39:07 PM

Слідкуй за CodeGalaxy

Мобільний додаток Beta

Get it on Google Play
Зворотній Зв’язок
Продовжуйте вивчати
тести з Java
Cosmo
Зареєструйся Зараз
або Підпишись на майбутні тести