In section 15.12.2.5 of the Java Language Specification, it talks about how to choose the most specific method in both cases of methods with fixed arity and methods of variable arity (i.e. varargs
).
What I can't find in the JLS is anything about deciding between two methods where one is of fixed arity and one of variable arity however. For example:
public interface SomeApi {
public String getSomething(String arg); // method 1
public String getSomething(String ... args); // method 2
}
Compiles just fine as one would expect (for reasons outlined by Yoni below). This calling code compiles also:
SomeApi api = ...
Object o = api.getSomething("Hello");
and if you run it, method #1
(i.e. the non-varargs method) is called. Why is this calling-code not ambiguous? Why is the fixed arity method more specific than the variable-arity one? Can someone point me to the relevant bit of the spec?