wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt on Method Overloading using Var-args Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt on Method Overloading using Var-args" Watch "Doubt on Method Overloading using Var-args" New topic

Doubt on Method Overloading using Var-args

Devidas Menon Aniyath

Joined: Apr 28, 2006
Posts: 12
Hi Ranchers,
I got a doubt while following chapter 3 of SCJP 1.5 study guide(K&B)
When I compiled the following code I got a compilation error

reference to wide_vararg is ambiguous, both method wide_vararg(l
ong...) in Vararg and method wide_vararg(java.lang.Integer...) in Vararg match wide_vararg(i,i)

Can any one please explain me which rule was applied by the compiler so that it got failed

I know that Var-arg has the lowest priority.My assumption is as follows

Since the overloaded methods are Var-args as its parameter it leaves both method with the same priority for the compiler. What is the next step followed

Widening ? Boxing ? or Box and then Widen ?

Also the below code will not compile though it's obvious that it's 'non var-arg' partner will compile

Can any one please address my doubts


Anything acquired without effort, and without cost is generally unappreciated, often discredited<br />-:Napoleon Hill
Sid Murrey
Ranch Hand

Joined: Jul 07, 2008
Posts: 58
If you call

it works. Maybe this helps finding the problem?
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Originally posted by Devidas Menon Aniyath:
static void wide_vararg(long ...x)

static void wide_vararg(int ...x)

static void wide_vararg(long ...x)

Isn't the Syntax wrong?

Thought it's supposed to be

static void wide_vararg(long... x)
static void wide_vararg(int... x)
static void wide_vararg(long... x)

The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Seema Gaurav
Ranch Hand

Joined: Apr 29, 2008
Posts: 47
Hi Devidas,
Here's a possible explanation.
In this case, there are 2 possibilities - while invoking wide_arg(i,i):
1.The compiler can either widen these numbers to long and call the method wide_vararg(long... x), or
2. The compiler can box them to an Integer and call the method wide_vararg(Integer... x)

Since both these ways are legal, the compiler is unable to pick one over the other and hence the result is ambiguous.

Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
Hi Devidas,

I think especially your second example shows that the vararg design with respect to overloading is rather poor. Best you post a feature request on www.java.sun.com to improve it.

SCJP 5 (98%) - SCBCD 5 (98%)
Zaheer Ahmed

Joined: Feb 06, 2007
Posts: 22
Hi Dev!

This has actually to do with the new features implemented as part of Java 1.5.

Var-args and auto-boxing are both new features introduced in Java 5.

As you are passing ints to the methods, they get confused to which method to call because both of these new features have the same priority.

If you had removed the method with long var-args and just had two longs in the method, and then called the method passing two ints, then int would be up-casted to long instead of being auto-boxed to Integer objects. This keeps Java backward compatible because if you have 1.4 and it ran fine and then you introduced auto-boxing or var-args methods, it will still keep running fine.

This is really creepy explanation, but try hard to understand. If you have KnB book that explains the concept in detail.

Zaheer Ahmed<br />SCJP 5
Anand Shrivastava
Ranch Hand

Joined: Jul 22, 2007
Posts: 125
Dear friend,


Widening > Boxing > Varargs.

Therefore while choosing over Boxing (Integer), widening (long) would be given preference and the output will be long. I hope this clears it.

Anand Shrivastava
Anand Shrivastava
Ranch Hand

Joined: Jul 22, 2007
Posts: 125
Sorry, i failed to notice that long is being used as vararg. So in this case Integer (boxing) would be chosen and the output will be integer.
Devidas Menon Aniyath

Joined: Apr 28, 2006
Posts: 12
Thanks Ranchers for all your responses.

Anand, the code I have listed won't compile
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

java will choose most matching method...
Anand Shrivastava
Ranch Hand

Joined: Jul 22, 2007
Posts: 125
Sorry, now i followed your problem correctly.

In the k&B book itself it is written that

"Used individually, boxing and var-args are compatible with overloading"

In this example you have coupled overloading with boxing as well as vararg in the Integer... method and widening with overloading in the long... method.

Thus according to the book, you can only box or use var-arg (not both simultaneously) while overloading.

I hope that fixes it.
wood burning stoves
subject: Doubt on Method Overloading using Var-args