• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Primitive conversions

 
lakshmi nair
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I'm new to this..
I had a doubt.
class LongTest{
void method(float fl) {
System.out.println("Method Float");
}
void method(double dl) {
System.out.println("Method Double");
}
public static void main(String[] args) {
long l = 45L;
LongTest f = new LongTest();
f.method(l);
}
}
The above code outputs "Method Float". Long is of size 64 bits,Float, 32bits and double 64bits. I dont get how float is selected when what i understood is that in method calls the technique followed is Widening Conversions. Am i mistaken in this,anywhere ? Please help...
 
Arpitha Reddy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It appears that the type is being promoted to the next higher type..in this case from long to float. If you had a similar method on long and called it with an int, it would probably result in the 'long'.
 
Anna s Iyer
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the order in which widening conversion takes place.
byte to short, int, long, float, or double
short to int, long, float, or double
char to int, long, float, or double
int to long, float, or double
long to float or double
float to double

In your case long is first looks for float and if not goes to double.
Hope this helps,
Anna S Iyer.
 
lakshmi nair
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Arpitha and Anna..
So that means it has practically nothing to do with the sizes,32 and 64...
Lakshmi
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic