Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt on Method Overloading using Var-args

 
Devidas Menon Aniyath
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

Thanks
Devidas
 
Sid Murrey
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you call



it works. Maybe this helps finding the problem?
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Seema Gaurav
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

HTH
Seema
 
Ralph Jaus
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Zaheer Ahmed
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anand Shrivastava
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear friend,

Remember,

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
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ranchers for all your responses.

Anand, the code I have listed won't compile
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java will choose most matching method...
 
Anand Shrivastava
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic