• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

need help in overloading...

 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • 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 ]
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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.....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic