GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Varargs overloading problem 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 "Varargs overloading problem " Watch "Varargs overloading problem " New topic
Author

Varargs overloading problem

Harinath subu
Greenhorn

Joined: Aug 03, 2009
Posts: 5
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
}
}
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

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[].


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Akanksha Mittal
Greenhorn

Joined: Jul 29, 2009
Posts: 26
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?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3833

Hi Harinath subu,
Please use codetags when posting code here. And Welcome to Javaranch


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Vinay Dinakar
Greenhorn

Joined: Dec 30, 2008
Posts: 17
It worked fine for me.
Java(TM) SE Runtime Environment (build 1.6.0_01-b06)


Thanks,

~ Vinay Ds
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
actually you get an ambiguity error when you use the combination of var-args and widening + var-args together


scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
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?


SCJP 1.6 96%
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
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

Joined: Oct 30, 2009
Posts: 504
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

Joined: Dec 10, 2009
Posts: 521
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
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

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


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
"tussi great ho" means "you are awesome" .......just joking...wont use hindi again
Vinay Dinakar
Greenhorn

Joined: Dec 30, 2008
Posts: 17



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

Joined: Dec 10, 2009
Posts: 521
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....

 
GeeCON Prague 2014
 
subject: Varargs overloading problem