Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Which overloaded method is called?

 
Michael Iaria
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ankit Garg
Sheriff
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Ankit G@rg", please check your private messages for an important administrative matter.
 
Michael Iaria
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is all making sense. What about this?



Which method will be invoked??
[ August 07, 2008: Message edited by: Michael Iaria ]
 
Ankit Garg
Sheriff
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic