I want to create two functions, say
long min(long...);
int min(int...);
But when I try to invoke the second i.e min(1, 5) one I get ambiguous method call
Is there workaround except renaming?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It is a known bug
The behaviour you describe is a bug which has been fixed with Java 7. See details in the release notes, section called “Changes in Most Specific Varargs Method Selection”.
The reason why it should compile
Variable arity comes last in determining the most specific method. The rules to determine which vararg method applies when there are several are defined in JLS 15.12.2.4 – here is an extract:
In your case, k = n, and
U1[] = int[]andT1[] = long[]so the determination can be made ifint <: longor the opposite.In other words, the type taken into account is not
int[]vs.long[]butintvslong. And it happens thatint <: longso theint...method should be chosen and it should compile.Conclusion:
The code should (and does) compile fine with Java 7 but would not compile with Java 5 or 6. The code below prints
intwith Java 7: