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 ArrayStoreException at 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 and List<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:
List same as List<Object>
List<Type> list1 with generics can accept any type
List<Object> means just accept List<Object> or List
List<T> list1 calling varargs method equals new Object[]{ list1 }
Using test1(Collections.singletonList(x)) equals test1(new ArrayList<Object>())
The scenario:
there are two overloading method, but when you're using
It's always using the first method
test1(Object... args)
, but when usingList<Object> list2
the executor is different, It's too wiredSome document identified
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:
List
same asList<Object>
List<Type> list1
with generics can accept any typeList<Object>
means just acceptList<Object>
orList
List<T> list1
calling varargs method equalsnew Object[]{ list1 }
test1(Collections.singletonList(x))
equalstest1(new ArrayList<Object>())
So this is the check trick problem