There is a question How to Convert List<String> to List<Object> and many similar questions about List<P>
and List<C>
with C extends P.
Obviously, we cannot using cast like this:
List<String> list0 = Arrays.asList("abc", "xyz");
List<Object> list1 = (List<Object>) list0; //ERROR here
the accepted answer in above question is:
List<Object> objectList = new ArrayList<Object>(stringList);
but when I try this code:
List<String> list0 = Arrays.asList("abc", "xyz");
List<Object> list1 = (List<Object>) (Object) list0;
System.out.println(list1);
Even like this:
List<String> list0 = Arrays.asList("abc", "xyz");
List<Integer> list1 = (List<Integer>) (Object) list0;
System.out.println(list1);
It runs successfully, so we can cast indirectly! I really doubt this! any acceptable reason for this?