1

So I just found a behaviour I didn´t expect and I´d like for someone to clarify it for me, because while there are a lot of questions on how to overload a varargs there are none that I found to address this issue specifically.

Having the next piece of code:

public foo(Object... bar) { ... }

public foo(List<Object> bar) { ... }

And then calling this foo method as

List<Sting> myParam = initializeWithSomething();
foo(myParam);

The varargs foo method is favoured, putting the list as the only element in the created array during the method execution. If Java usually chooses the most specefic of the possible overloads, how come this happens?

Aru Díaz
  • 51
  • 1
  • 5
  • 5
    A `List` is not a `List`. If you want your method to accept a list of anything, you could declare `foo(List> bar)`. – khelwood Jun 08 '18 at 07:59

0 Answers0