• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

conversion from long to float

 
tony kanvas
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
see this code

i need some explanation about the conversion from long to float and why convert to double as we know double is A defult and long 64 and float32
 
Igor Zeta
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by tony kanvas:


In this code i see there's a call to an overloaded method,i think it will be called the triple int parameters method because best fits the call,
so why the thought of converting?
I apologize for my poor english...!

 
Rehana Shaik
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Conversion rule's in java are as follows
byte-->short--> int -->long--> float -->double
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rehana Shaik:
Conversion rule's in java are as follows
byte-->short--> int -->long--> float -->double

Anything moving in the diredction of the arrows does not require a cast. Anything moving against the arrows requires a cast.
 
Sridhar Srikanthan
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Thomas Paul:

Anything moving in the diredction of the arrows does not require a cast. Anything moving against the arrows requires a cast.

If the order given above is to be taken into account, how could one explain the result of
the above program as
Hello World!
One

instead of
Hello World!
Two

because the middle varibale of the method which prints Two is a double.That would be even more precise...or am I missing something here?
Thanks in advance
Sri
 
Rehana Shaik
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Method call conversions always look for the exact data type or a wider one in the method signatures. They will not do narrowing conversions to resolve methods, instead we will get a compile error. so it will go for float other than double.
 
John Paverd
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the Java Language Specification, Section 15.12:
The Java programming language uses the rule that the most specific method is chosen.
The informal intuition is that one method declaration is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error.

The method "Two" could not "pass on" the call to the method "One" because it would require an explicit cast of the 2nd parameter from double to float.
However, method "One" could "pass on" the call to method "Two", because the compiler can implicitly widen the 2nd parameter from float to a double.
Therefore method "One" is more specific than method "Two" and is chosen by the compiler.
And it's difficult to see in my browser, but the arguments in the method call are 7L, not 71.
 
Sridhar Srikanthan
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops,
John,
Even i thought them to be 71 instead of 7L. my mistake
Thanks for the answer
Sri
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic