This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma 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