Consider the following code:
public class SimpleTest {
public static void print(Collection<Object> strings) {
System.out.println("Collection overload: " + strings);
}
public static void print(Object... strings) {
System.out.println("Vararg overload: " + Arrays.asList(strings));
}
@Test
public void test() {
List<String> strings = Arrays.asList("hello", "world");
print(strings);
}
}
What's your prediction of the output?
It turns out, the correct answer is this!
Vararg overload: [[hello, world]]
If you guessed right, could you please explain why it's not the collection overload?
Update
If I change the collection overload to
public static void print(Collection<String> strings) {
System.out.println("Collection overload: " + strings);
}
or
public static void print(Collection strings) {
System.out.println("Collection overload: " + strings);
}
Then it outputs
Collection overload: [hello, world]