Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 435
Eng.Fouad Avatar asked Jan 12 '12 16:01

Eng.Fouad


1 Answers

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.
like image 56
Peter Lawrey Avatar answered Sep 28 '22 02:09

Peter Lawrey