Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly cast nested generic types in Java

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;
like image 484
JRR Avatar asked Aug 06 '13 19:08

JRR


People also ask

Can you cast generic type java?

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.

Does generics eliminate use of casting?

As mentioned previously, generics can eliminate the requirement for casting.

How to use generics in method in Java?

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);


1 Answers

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:

  • Angelika Langer - Generics FAQs
  • Is List<Dog> a subclass of List<Animal>? Why aren't Java's generics implicitly polymorphic?
like image 131
Rohit Jain Avatar answered Sep 17 '22 05:09

Rohit Jain