aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Which overloaded method is called? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Which overloaded method is called?" Watch "Which overloaded method is called?" New topic
Author

Which overloaded method is called?

Michael Iaria
Greenhorn

Joined: Aug 07, 2008
Posts: 11
I have an intuitive grasp on why the output of the following code is what it is. The output is: -434. The study guide mentions that overloaded var-args methods are chosen last. Can someone please provide me with or point me to the hard and fast rules regarding which overloaded methods are invoked?

Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

The compiler chose the one which is nearest to a method call !

Like here, the confusion is between


and


for call


But any array extends Object class so , here compiler chose 2nd method with Object as parameter , the first with var-args will be chosen If only when no other defined method matches OR the arguments are of same type but more than once passed to method !

Second call is straight forward,

In second call , the the 7 is auto boxed into Integer and passed as Object !

Help me ranchers, If I`m wrong in explaining.


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Well after compilation a var-args method is converted into an array
Eg

method(String... args)
after compilation will become
method(String[] args)

So if your parameter is an array it will become a 2-d array
method(String[]... args)
after compilation will become
method(String[][] args)

So in you case methods


will become


And as you know all arrays are sub-classes of Object[] and Object[] is itself a sub-class of Object.
So the call
sifter(aa); //aa is A[]

will match the method
static void sifter(Object o)
{
s += "4";
}

call to
sifter(ba); //ba is B[]
will match
static void sifter(B[] b1)
{
s += "3";
}

and call to
sifer(7); //it will become sifer(new Integer(7));

will match
static void sifter(Object o) //Integer will be upcasted to Object
{
s += "4";
}

I hope you will get it....


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60995
    
  65

"Ankit G@rg", please check your private messages for an important administrative matter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Michael Iaria
Greenhorn

Joined: Aug 07, 2008
Posts: 11
This is all making sense. What about this?



Which method will be invoked??
[ August 07, 2008: Message edited by: Michael Iaria ]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

First of all I changed my display name to G@rg because the name Ankit Garg is not available. Sheriff you mail box is also full. If you still want me to change my name I will do it...

Now to the question the method sifter(int x) will be called.... as it is the closest of short...
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

You can find out it easily , If ypu have a JVM !

The primitive find prmitive first , if not then for respective wrapper class and finall for Object parameter..

Its all depends upon hiereachy, like

short
|
int
|
double
|
Wrapper class
|
Object

Hope this help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Which overloaded method is called?