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

Varargs overloading problem

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you resolve this problem

//Program for Overloading varargs
class A{
void metodA(int... a){
// -------Body of the method ---------
}
void methodA(double... b){
//----------- mody of the method
}
}


class B{
{
public static void main(String[] args){
A a=new A();
a.methodA(1,2,3,4,5); // i am getting the problem over here(ambiguity error) why this occurs....
}
}

//#################################################################################################

//If i did the same program with a single argument it works
class A{
void metodA(int a){
// -------Body of the method ---------
}
void methodA(double b){
//----------- mody of the method
}
}


class B{
{
public static void main(String[] args){
A a=new A();
a.methodA(1); //here it works perfectly
}
}
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you really getting a ambiguity error or are you getting the illegal start of expression error. Well the illegal start of expression error is because of the extra { near class B. As far ambiguity is concerned, how these methods can be ambiguous as the method signatures are totally different. One is int[] and the other is double[].
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even I am getting the Ambiguous error

" The method method(int[]) is ambiguous for the type <ClassName>"



Also I tried all posible combinations among int, float, double and long; I was getting the same error.

When I tried a Class, Subclass combination, I was not getting the error.

Can anyone help why this ambiguity in Number Types only?
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Harinath subu,
Please use codetags when posting code here. And Welcome to Javaranch
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It worked fine for me.
Java(TM) SE Runtime Environment (build 1.6.0_01-b06)
 
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually you get an ambiguity error when you use the combination of var-args and widening + var-args together
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raju Champaklal wrote:actually you get an ambiguity error when you use the combination of var-args and widening + var-args together



can you explain it in detail?
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for example m(int... i) and m(Integer... i)/m(long... l) would give a compile error....if you invoke it with m(4).....

widening + varargs or boxing + varargs with var args.....i hope am right

because cant beleive you are asking me to explain
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are also not very sure how and why compiler behaves this way. As here method is overloaded with int and double arguments which are different.

I think this should explain why is it happening.
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you mean this

This seems to be the problem when finding the most specific method, because it is neither int[] <: float[] nor float[] <: int[], so ambiguous.

It is (nearly) impossible to convert between arrays of primitives when passing them by reference. Imagine: changes to the converted referenced array should have to be written back to original array of other primitive type!

But then there >should< have be done any other trick when implementing varargs, because as mentioned before it is just a silly feature if it is not possible to overload in such a simple manner as max(int... a) with max(float... a)

awesome Neha...tussi great ho
 
Sheriff
Posts: 9708
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

Raju Champaklal wrote:awesome Neha...tussi great ho


Please only use English language on javaranch. Javaranch is visited by users from all around the globe not from only India...
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"tussi great ho" means "you are awesome" .......just joking...wont use hindi again
 
Vinay Dinakar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



My question is:

-- Tell me if my assumptions are correct or wrong , which i mentioned in comments ?
-- how different it is when overloading var-agrs (int... i) and (float... f) from (int i) and (float f). ?
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what i understood from that link is that jvm choses the most specific method...now since 4 is an int and int is more specific than float so int will be chosen...and i hope you know that float[] and int[] cannot be casted to each other so they cant be compared....like int[] cant be casted or compared with Integer[] so compiler wont be able to find out the most specific out of them......they both look the same to the compiler....

P.s Arrays are objects....

 
I knew I would regret that burrito. But this tiny ad has never caused regrets:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic