• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Which overloaded method is called?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic