The scenario:
public static void test1(Object... args) {
}
public static void test1(List<Object> args) {
}
there are two overloading method, but when you’re using
List<Long> list1 = new ArrayList<>();
test1(list1);
It’s always using the first method test1(Object... args), but when using List<Object> list2 the executor is different, It’s too wired
Some document identified
The example above shows why generics and arrays don’t mix well together. An array is what is called reifiable type — a type where full type information is available during run-time. It is because Java array is reifiable that the Java run-time can check what we store into the array matches the type of the array and throw an
ArrayStoreExceptionat us if there is a mismatch. Java generics, however, is not reifiable due to type erasure. Java designers have decided not to mix the two.1
the
List<?>means you can assign any type of List to it andList<Object>means you can store any type of object into it.2
Sure, it’s not the same question, but I think some conception can go to the same destination
I think the runtime compiler design to obey this:
Listsame asList<Object>List<Type> list1with generics can accept any typeList<Object>means just acceptList<Object>orListList<T> list1calling varargs method equalsnew Object[]{ list1 }- Using
test1(Collections.singletonList(x))equalstest1(new ArrayList<Object>())
So this is the check trick problem
generics safety term like PECS
public static void test1(List<Object> args) {
}
Diff with
public static void test1(List args) {
}
public static void test1(List<?> args) {
}