Given the program fragment:

double p = Double.POSITIVE_INFINITY;
double n = Double.NEGATIVE_INFINITY;

System.out.println(
    ((long) p > (long) n) + " " +
    ((int) p > (int) n) + " " +
    ((short) p > (short) n) + " " +
    ((char) p > (char) n) + " " +
    ((byte) p > (byte) n));
What will be printed when this code is executed?
Explanation
This example demonstrates the principle of converting real data types to integer. At first, the cast to int (or long) is performed, and then value is just truncated if we need a byte, char or short. That's why byte and short behave strangely (for example -inf -> 0x80000000 -> 0x00=0, and +inf -> 0x7FFFFFFF -> 0xFF=-1)

Следи за CodeGalaxy

Мобильное приложение Beta

Get it on Google Play
Обратная Связь
Продолжайте изучать
тесты по Java
Cosmo
Зарегистрируйся сейчас
или Подпишись на будущие тесты