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, While Iam trying to assign an int to byte, I get an error int i=90; byte b=i;
And rightly so, since byte is smaller than integer, and there is loss of bits(narrowing conversion). But while Iam trying to convert long to float, I get no errors. long l=1234555; float f = l; The explanation given is "The internal representation of integer types (byte, short, char, long) and floating point number (float, double) are totally different. converting from long to float is not a narrowing conversion.Even it may lose some significant digits. See the following table for details: Even it may lose some significant digits." I saw the internal representation of all types, but still could'nt make out the reason for such behavior. Please help.
Not the real representations, but this should give you the general idea. Think of a long representation as '10000000000' Think of a float/double representation as '10^10' 1234567890 in integerese becomes 12.3*10^8 in floatese When you convert it back to long, you have the significant digits, but not necessarily the less significant ones. 12.3*10^8 in floatese 1230000000 in longese See the Java Language Specification 2nd Ed., 5.1.2 'Widening Primitive Conversion'
Narrowing Primitive conversion are relatively picky Let me explain why? Basically when you narrow int to a byte there are 2 ways you can do it 1. byte b = 90; //declaration and assignment in a single line 2. final int i =90; byte b = i; //Observe the integer being declared as final But in both the cases the int value MUST be in the range of Destination type (ie byte/short/char) Above all this implicit narrowing will be applicable to (Byte/Short/Char/Ints) only
It'd be great if you could tell me why the code works fine if "i" is declared final and not otherwise. Or I'd rather put my question as why does declaring "i" as final free the variable of its obligation to be cast explicitly as a byte? Thanks in Advance Shyam [This message has been edited by Shyamsundar Gururaj (edited August 25, 2001).]
Hi Shyamsundar, When you declare a variable as 'final' the compiler knows the value will never change. It can then substitute the actual value in the bytecode instead of using producing bytecode that will look up the value at runtime. In the example, the compiler knows 'i' will always be '90' so it uses '90' wherever it finds 'i'. Hope that helps. ------------------ Jane Griscti Sun Certified Programmer for the Java� 2 Platform
Some rules to remember: int and char literals are the only literals that the compiler will perform implicit narrowing conversions with at compile time. For example: short s = 12; // an int literal being assigned to a short byte b = 19; // an int literal being assigned to a byte short s2 = 'c' // a char literal being assigned to a short int i = 12.0 // not okay: double literals are not implicitly casted float f = 14.0 // not okay: double literals are not implicitly casted