Win a copy of Hello World this week in the Python forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Paul Clapham
• Bear Bibeault
• Liutauras Vilda
• Devaka Cooray
Sheriffs:
• Knute Snortum
• Junilu Lacar
• Henry Wong
Saloon Keepers:
• Ron McLeod
• Stephan van Hulst
• Tim Moores
• Carey Brown
• Tim Holloway
Bartenders:
• salvin francis
• Frits Walraven
• Piet Souris

# Casting

Ranch Hand
Posts: 104
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.

Ranch Hand
Posts: 1365
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'

Ranch Hand
Posts: 464
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

Ranch Hand
Posts: 18944
Ragu,
The compiler liked what you said.

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?
Shyam
[This message has been edited by Shyamsundar Gururaj (edited August 25, 2001).]

Bindesh Vijayan
Ranch Hand
Posts: 104
Hi,
Thnx David & Ragu i got it all.

Ranch Hand
Posts: 3141
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

Anonymous
Ranch Hand
Posts: 18944
Thanks Jane,
You're a great help
-Shyam

Ranch Hand
Posts: 57
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

 You ridiculous clown, did you think you could get away with it? This is my favorite tiny ad! Java file APIs (DOC, XLS, PDF, and many more) https://products.aspose.com/total/java