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.
void foo(Object... args)
then you can call it with eitherfoo(new Object(), new Object())
orfoo(new Object[] { new Object(), new Object() })
and get the same result. See this related question for an example. – Factionint[]
orObject[]
) to vararg type. Other than that - no difference between the method signatures. The same question would be like can't overloadprivate
andpublic
method. – Praenomen