It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Confused about overloading 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 "Confused about overloading" Watch "Confused about overloading" New topic

Confused about overloading

Pradeep Kr

Joined: Feb 17, 2010
Posts: 22

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?
Chad Michaels
Ranch Hand

Joined: Jun 25, 2010
Posts: 42
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!
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446

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
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

SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
Pradeep Kr

Joined: Feb 17, 2010
Posts: 22
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?
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
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.
I agree. Here's the link:
subject: Confused about overloading
It's not a secret anymore!