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

need help in overloading...

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


compilation error.

my doubt is how the compiler can select specific method internally?

is it compare first parameter of method1 with first parameter of method2 and second parameter to second parameter and third parameter to third parameter?
is it select specific method based on the only first matching parameter(widening>boxing>var-args)?

is it consider most no of matches in a method after comparing the parameters in different methods?

can anyone give me explanation?
[ November 02, 2008: Message edited by: Ganeshkumar cheekati ]
 
Sheriff
Posts: 9704
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the compiler matches all the parameters based on the precedence (widening>boxing>var-args)
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i will be thankful if you can give example

or else can you explain with this code



why the compiler specified method2 and given ouput as int,long,int.
[ November 02, 2008: Message edited by: Ganeshkumar cheekati ]
 
Ankit Garg
Sheriff
Posts: 9704
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in the code that you gave the compiler chooses method2 because it has to do just one widening and no boxing. I think the compiler first matches a method directly, then widens each argument one by one, then boxes each argument one by one, then boxes and widens each argument and so on. I cannot tell you the exact order as there are so many things like var-args etc...
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


i thought that the compiler can select method1 in which only one widening takes place.
but it is compilation error as ambiguity in methods.

why?
 
Ankit Garg
Sheriff
Posts: 9704
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you need the complete details, then I think you should read JLS (Java Language Specification). Here's a URL to how method invocations are resolved. I think if you read it completely, then you will get answers to all your questions.....
 
I think I'll just lie down here for a second. And ponder this 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