问题
I was wondering why it is not allowed in Java to overload Foo(Object[] args)
with Foo(Object... args)
, though they are used in a different way?
Foo(Object[] args){}
is used like:
Foo(new Object[]{new Object(), new Object()});
while the other form:
Foo(Object... args){}
is used like:
Foo(new Object(), new Object());
Is there any reason behind this?
回答1:
This 15.12.2.5 Choosing the Most Specific Method talk about this, but its quite complex. e.g. Choosing between Foo(Number... ints) and Foo(Integer... ints)
In the interests of backward compatibility, these are effectively the same thing.
public Foo(Object... args){} // syntactic sugar for Foo(Object[] args){}
// calls the varargs method.
Foo(new Object[]{new Object(), new Object()});
e.g. you can define main() as
public static void main(String... args) {
A way to make them different is to take one argument before the varargs
public Foo(Object o, Object... os){}
public Foo(Object[] os) {}
Foo(new Object(), new Object()); // calls the first.
Foo(new Object[]{new Object(), new Object()}); // calls the second.
They are not exactly the same. The subtle difference is that while you can pass an array to a varargs, you can't treat an array parameter as a varargs.
public Foo(Object... os){}
public Bar(Object[] os) {}
Foo(new Object[]{new Object(), new Object()}); // compiles fine.
Bar(new Object(), new Object()); // Fails to compile.
Additionally, a varags must be the last parameter.
public Foo(Object... os, int i){} // fails to compile.
public Bar(Object[] os, int i) {} // compiles ok.
回答2:
The most direct answer to the question is that having both declarations would create an ambiguity in the method lookup logic. If you declared both in the same class, there would be no way to figure out which method you wanted when calling thusly:
Object[] a = new Object[10];
Foo(a); // array or vararg?
And Java requires that there always be a most specific method for every method invocation. For the why, see Peter's answer.
来源:https://stackoverflow.com/questions/8838339/why-is-it-not-allowed-in-java-to-overload-fooobject-with-fooobject