• Post Reply Bookmark Topic Watch Topic
  • New Topic
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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Overloading and argument type conversion

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
When an overloaded method gets called, the compiler chooses the one with the same argument type as the argument in the call.
But what if the argument of the method call doesn'nt exactly match the arg type of any of the methods, but can be converted to match the argument type in more than one method. Which one does the compiler choose? Does it choose the closest one in the conversion chain?
Here is an example:
<pre>
public class OverLoad{
public void aMethod(int i){
System.out.println("int version : " +i);
}
public void aMethod(long l){
System.out.println("long version : " +l);
}
public static void main(String [] args){
byte b = 2;
OverLoad ol = new OverLoad();
ol.aMethod(b);
}
}
</pre>
The output is "int version : 2"
Why exactly is it choosing aMethod(int i) ?
Thank you.

[This message has been edited by Mafalda Alabort (edited March 24, 2001).]
 
Enthuware Software Support
Posts: 4906
60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In short "most specific method is chosen." As byte can be converted to int as well as to a long, both the methods are applicable, but as int can be converted to log ( not vice-versa), the method taking an int is more specific than the long one. So int version is selected.

You might want to read Section 15.12.2.2 Choose the Most Specific Method" for complete details.
------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your byte datatype comes with in the integer type datatype
int--->32 bit
byte-->8 bit
so, it will automatically cast small value to big value(8 to 32)
this is implicit casting in a method.
thats y u got int result.
ithink its enough,

------------------
R.Balu
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic