Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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: 4599
45
  • 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
 
This. Exactly this. This is what my therapist has been talking about. And now with a tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic