File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes questions about overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "questions about overloading" Watch "questions about overloading" New topic

questions about overloading

Storm Zcm

Joined: Jul 08, 2007
Posts: 10
In a class, there are a lot of methods with the same name and their parameters's number is also the same.
what's the algorithm that java choose method?

when there are implicit change,explicit change,boxing.
what's the algorithm that java choose method?
Pankaj Mittal
Ranch Hand

Joined: Jul 09, 2008
Posts: 32
Hi buddy

try to be precise.
When there are several methods are by same name even no of parameters are same but atleast one parameter must be different in order to use method overloading facility. return type, throwing exception type and access specifier may changed.

void method(int i1,int i2,int i3){}

void method(int i1,int i2,float f){}

void method(int i1,int i2,double l){}

public static void main(String args[])
method(1,1,1); //will invoke first method
method(1,1,1.0f) //will invoke second method
method(1,1,1,0) //will invoke third method

go through kathy sierra once for more clarification on this topic.

2008 SCJP(5.0) 88%
Storm Zcm

Joined: Jul 08, 2007
Posts: 10
[Edit: non-English text elided. Please post only English.]

Refer to which methods to choose in overloading.
in my opinion.

First of all, the compiler consider exact the same.
the method's name must be the same,then the parameters's number must be the same,then the location and parameters type must be the same.
If all of these is the same, then choose this methods.

If there's implicit conversion.
It follow the
to judge the parameters.

If there's reference types.
First find the reference type,the find whether is the subclass of the reference type.

In J2SE 5.0, there's auto-boxing. Then to judge the parameters.

[ July 17, 2008: Message edited by: Bear Bibeault ]
[ July 17, 2008: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63870

"As Angel", please check your private messages for an important administrative matter.

And please post only English text. Thanks.

[Asking smart questions] [About Bear] [Books by Bear]
Storm Zcm

Joined: Jul 08, 2007
Posts: 10
i'm sorry ,
i have changed my display name.
And I'll use only english in the forum.
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

"Storm Zcm",
Please check your private messages regarding an important administrative matter.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Sandeep Bhandari
Ranch Hand

Joined: Apr 16, 2004
Posts: 201
And note that its the method calls which make overloaded methods ambiguous as overloaded method calls are resolved at compile time, the compiler flashes error for any ambiguous call.

SCJP 96% | SCWCD 90%| SCJP mock exams | My SCJP blog
I agree. Here's the link:
subject: questions about overloading
jQuery in Action, 3rd edition