In Java I can cast:
List<?> j = null;
List<Integer> j2 = (List<Integer>)j;
So why does the following fail?
List<List<?>> i = null;
List<List<Integer>> i2 = (List<List<Integer>>)i;
The Java compiler won't let you cast a generic type across its type parameters because the target type, in general, is neither a subtype nor a supertype.
As mentioned previously, generics can eliminate the requirement for casting.
We can call the generics method by placing the actual type <String> and <Integer> inside the bracket before the method name. demo. <String>genericMethod("Java Programming"); demo. <Integer>genericMethod(25);
In your 1st snippet:
List<?> j = null;
List<Integer> j2 = (List<Integer>)j;
The compiler won't give you error, because List<?>
is a super type of List<Integer>
, because the family of types denoted by the wildcard "?"
is a superset of Integer
. So you can perform the cast from List<?>
to List<Integer>
(A downcast, you can say), but the compiler will show you an Unchecked Warning, to save you against a cast from say - List<Date>
to List<Integer>
. The warning is shown, because the cast would otherwise succeed at runtime, due to type erasure.
In the 2nd case:
List<List<?>> i = null;
List<List<Integer>> i2 = (List<List<Integer>>)i;
Here you are casting from List<List<?>>
(referred to by FIRST from hereon) to List<List<Integer>>
(referred to by SECOND from hereon).
Since, FIRST is not a super type of SECOND, clearly because the family of types denoted by List<?>
(it can be List<Long>
, List<Date>
, List<String>
or anything) is not a super set of List<Integer>
. Hence a Compiler Error.
Suggested Reading:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With