This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi All, int j = (int)Float.POSITIVE_INFINITY; j==Integer.MAX_VALUE; It gives true then, short i = (short)Float.POSITIVE_INFINITY; i == Short.MAX_VALUE;It gives false I really not understanding these two cases, will anyone please explian me.? ----------- Prashant
Beware of casts to integer types other than long or int. They do not work the way you might think. When you convert a float (or double) to a byte, short, or char, the number is first converted to an int and thentruncated to a short. So what happens in this case is that Float.POSITIVE_INFINITY is converted to Integer.MAX_VALUE (01111111111111111111111111111111), then this value is simply truncated to a 16 bit short value (1111111111111111). The result is therefore -1 rather than Short.MAX_VALUE (0111111111111111). The details are in the JLS section 5.1.3 Narrowing Primitive Conversions. - Peter