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)

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes