• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Overloaded methods

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I have the overloaded methods 1 which takes argument as double and int and 2 which takes the argument as double and long and if I invoke the method by passing values 2.0 and 4 which overloaded method will be invoked. Why?Can anybody please explain.Thanks
[ May 18, 2004: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the oiverloaded method 1 because the lieral 4 or literal 2 is by default an int and not a long.
Right?
 
Swamy Nathan
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
4 or 2 or 3 is int as its in range (else wont compile)
4l or 2l or 3l is long
4.0 or 2.0 or 3.0 is double
4.0f or 3.0f or 2.0f is float
[ May 20, 2004: Message edited by: Swamy Nathan ]
 
Sridhar Srinivasan
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How abt the case that the overloaded methods take arguments double or float or long and you pass 2.Which one will get invoked?Thanks
 
Swamy Nathan
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int -> long -> float -> double
is the order of widening so it will be promoted to a long and the long argument method will be invoked
 
Sridhar Srinivasan
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot!
 
Sridhar Srinivasan
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry!One more qustion related to this.How about passing a null for object and the overloaded objects take a String or someother object.Can u please expplain?
 
Swamy Nathan
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We were discussing primitives earlier.
Objects cannot be overloaded. Methods can.

I guess this is what you wanted to ask:-
Lets say we have overloaded methods-method(...) one version of the method takes a String as argument another say takes a Integer as argument.

If your null is anonymous ie you did a method(null); then the compiler will complain about ambiguity. However if the null was a local object reference or a field then it wont complain.

The method will be as per the type of the reference

String s=null;method(s); will invoke the String argument method
 
Sridhar Srinivasan
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot!
 
Sometimes you feel like a nut. Sometimes you feel like a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic