The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Casting" Watch "Casting" New topic


Bindesh Vijayan
Ranch Hand

Joined: Aug 21, 2001
Posts: 104
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.

David Weitzman
Ranch Hand

Joined: Jul 27, 2001
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'
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
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

Joined: Nov 22, 2008
Posts: 18944
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?
Thanks in Advance
[This message has been edited by Shyamsundar Gururaj (edited August 25, 2001).]
Bindesh Vijayan
Ranch Hand

Joined: Aug 21, 2001
Posts: 104
Thnx David & Ragu i got it all.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
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

Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks Jane,
You're a great help
Jimmy Blakely
Ranch Hand

Joined: Jul 10, 2001
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
jQuery in Action, 2nd edition
subject: Casting