aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes need help in overloading... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "need help in overloading..." Watch "need help in overloading..." New topic
Author

need help in overloading...

Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362


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 ]

SCJP5 and SCWCD1.5
Think Twice Act Wise...
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

the compiler matches all the parameters based on the precedence (widening>boxing>var-args)


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
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

Joined: Aug 03, 2008
Posts: 9280
    
  17

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

Joined: Oct 13, 2008
Posts: 362


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

Joined: Aug 03, 2008
Posts: 9280
    
  17

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 agree. Here's the link: http://aspose.com/file-tools
 
subject: need help in overloading...
 
Similar Threads
Returning to methods
need help debugging this code:
need help debugging this code:
Covariant doubt
private vs. public