Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling varargs method mixing elements and array of elements does not work

I have a method with the following signature:

public void foo(String... params);

So all of these calls are valid:

foo("Peter", "John");
foo(new String[] { "Peter", "John" });

But why is this one not valid?

foo("Peter", new String[] { "John" });
like image 764
EA. Avatar asked Nov 06 '10 13:11

EA.


2 Answers

From the docs:

The three periods after the final parameter's type indicate that the final argument may be passed as an array or as a sequence of arguments.

You can't pass an argument and an array.

like image 176
dogbane Avatar answered Nov 10 '22 19:11

dogbane


That's because in fact you try to pass Array containing String and another Array.

like image 35
Lavir the Whiolet Avatar answered Nov 10 '22 19:11

Lavir the Whiolet