I just wrote some code with the following structure:
public void method(int x) {
//...
}
public void method(int x, String... things) {
//...
}
I was rather surprised that this compiled, and that if I invoked
method(3);
then it would pick the first one. Obviously this is in some sense the natural one to pick, but if the first method didn't exist, this would be a reasonable way of invoking the second (with an empty varargs array). So surely it should be considered ambiguous and produce a compile-time error?
Or is this treated as a special case?
It seems wrong to treat it as such, because it means that adding a new method could break existing code, and that is not a very happy state of affairs.
(Goodness only knows which one you'd end up invoking if the first one were added as a new method of a subclass containing the second one...)
String...
part can take any number of arguments, including zero. – Collinsia