Why is it not allowed in Java to overload Foo(Object...) with Foo(Object[])?
Asked Answered
J

2

13

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?

Jailbreak answered 12/1, 2012 at 16:14 Comment(2)
I may be missing something, but why do you need an overload? If you declare your method as void foo(Object... args) then you can call it with either foo(new Object(), new Object()) or foo(new Object[] { new Object(), new Object() }) and get the same result. See this related question for an example.Faction
the vararg is just a flag to the method signature transmuting the last declared array (like int[] or Object[]) to vararg type. Other than that - no difference between the method signatures. The same question would be like can't overload private and public method.Praenomen
P
25

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.
Phira answered 12/1, 2012 at 16:18 Comment(2)
+1 nice explanation. Is there any language specifications written for this?Jailbreak
Have a look on this question please.Jailbreak
P
3

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.

Proconsulate answered 12/1, 2012 at 17:30 Comment(1)
they are exactly the same, not ambiguity, they just can't exist in the same classPraenomen

© 2022 - 2024 — McMap. All rights reserved.