Help coderanch get a
new server
by contributing to the fundraiser
Plus win three weeks of access to Security in Spring Boot REST Web Service Applications this week in the Spring 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Casting

Ranch Hand
Posts: 104
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
Hi,
Thnx David & Ragu i got it all.

Ranch Hand
Posts: 3141
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
Thanks Jane,
You're a great help
-Shyam

Ranch Hand
Posts: 57
• Number of slices to send:
Optional 'thank-you' note:
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

 brevity is the soul of wit - shakepeare. Tiny ad: We need your help - Coderanch server fundraiser https://coderanch.com/t/782867/Coderanch-server-fundraiser