0

If there are two methods

public void foo(Something... things){}
public void foo(Something thing){}

And the following call with a single argument is made

foo(something);

Which of the above methods will be called? Does the order the above methods are declared in matter?

The reason I would have such method is to implement a for each loop in the varargs method which calls the single arguments method in the loop.

killjoy
  • 3,665
  • 1
  • 19
  • 16
the_prole
  • 8,275
  • 16
  • 78
  • 163
  • The second method is called. Specific methods are always preferred over varargs methods. – Andreas Apr 13 '18 at 21:23
  • If you're just iterating though the varargs and calling the single method, there's not much use for the single method. The performance drawback from creating a singleton loop is negligible if not non-existent. – killjoy Apr 13 '18 at 21:24
  • It may not be obvious first time looking at the code but if doing `do(something);` did indeed call the varargs version, how would we ever call the non-varargs version? – Ousmane D. Apr 13 '18 at 21:27

0 Answers0