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?
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.
e.g. you can define main() as
A way to make them different is to take one argument before the varargs
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.
Additionally, a varags must be the last parameter.