• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Confused about overloading

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why it is printing "Object Var-Args" but not "String Array Var-Args"? both are var-args.

If I change it to

Now it is printing "String Array Var-Args". What is the reasoning for this?
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pradeep, I have no idea. But, I'd like to guess because interacting with this forum is actually helping me learn.

Anyway, I think it's because the original argument is a 1-dimensional array "String[ ] aa". When passing to the overloaded methods, you have two choices: "String[ ]...a2" or "Object... o". Well, when you append the brackets to the var-args type, it may account for 2-dimensional arrays. I would guess if you passed "String[ ][ ] aa", it would indeed be passed to the overloaded String method instead of the Object method.

I'm having a hard time explaining what I understand in my head, so let me see if I can clear it up.

String... a :this will accept a String[ ] with any number of elements
String[ ]... a :this will accept a String[ ][ ] with any number of elements.
Object... o :this will accept any number of Objects

If you pass "String[ ] aa", it's closest match would be "String... a", followed by "Object... o"

Just a guess!
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In java
the static method of the type var-args are chosen last
The following principles follow here
Widening beats var-args
Var-args are given least preference for method calling
i.e. the methods with var-arg type are not chosen if there are parameters those are wider version of any
argument we are passing


here in case when you are calling the method overloading
when you call the method by passing the String array to it as


now that we are passing the array type
there are two methods available
first is var-arg and another is wider version
here
and Array is an object in java
hence the method with wider parameter gets selected
hence the method overloading(Object... o); gets called

you may wonder here as the method called is also an var-arg method
but please notice once again
there scope for the argument aa in overloading(aa) to widen to Object type
even if you change the code to

then the output does not change

Now when you change it to


and call the method using

then again there are two options
now here
both are array var-args
first is the String[] and the second is the Object[]
and we are passing the String[] to the method

Now the question is why the method with Object[] is not selected
notice here that
when you call method using overloading(aa)
there is parameter available i.e. String[]...
A var-arg parameter can take any number of arguments , even 0
so there is exact match
(Object[]... o) is not the exact match because there was widening from String to Object


Hope this is clear to now
 
Pradeep Kr
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prasad Kharkar wrote:(Object[]... o) is not the exact match because there was widening from String to Object


Why this reasoning is not applicable between String[]... and Object... methods. String[] is exact match. While Object... is using widening.

This question is not specific to SCJP exam. But It came up during reading one of the tread in this forum. Should I ask this in other forum?
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pradeep- Kumar wrote:both are var-args.


Both methods are var-args but one of them can be used as non-varargs.
Object... method has argument type of Object[] and so it can be called with String[] without using variable arity invocation. Non varargs call is always preffered against varargs call so the Object... method will be used.

Netbeans issues a warning about "non-varargs call of varagrs method" for this code.


When you change Object... to Object[]... then no method can be used for non-varargs call and from the 2 possible varargs invocations the String[] version is most specific so it will be used.
 
Beware the other head of science - it bites! Nibble on this message:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic