Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting

 
Bindesh Vijayan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
Thanks in Advance
Shyam
[This message has been edited by Shyamsundar Gururaj (edited August 25, 2001).]
 
Bindesh Vijayan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thnx David & Ragu i got it all.
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jane,
You're a great help
-Shyam
 
Jimmy Blakely
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic