Generic arrays in Java

Generics and arrays don’t mix, basically. The short answer is that you can work around this problem. The longer answer is that you probably shouldn’t and I’ll explain why.

You could use Array.newInstance() like this:

private Comparable[] hashtable;


hashtable = (Comparable[])Array.newInstance(Comparable.class, tableSize);

but you can’t create an array of your parameterized type.

Arrays are covariant. That means they retain the type of their elements at runtime. Java’s generics are not. They use type erasure to basically mask the implicit casting that is going on. It’s important to understand that.

So when you create an Object array you can’t cast it to, say, a Comparable array (or any other type) because that is not correct.

To give you an example. With generics this is perfectly legal:

List<String> list = new ArrayList<String>();
List<Integer> list2 = (List<Integer>)list;

It’s also why you can’t do this:

public <T> T newInstance(T t) {
  return new T(); // error!

ie at runtime there is no knowledge of T’s class. This is why the above code is more often written as:

public <T> T newInstance(T t, Class<T> clazz) {
  return clazz.newInstance();

because their is no runtime type for the generic argument. But with arrays:

String arr[] = new String[10];
Integer arr2[] = (Integer[])arr; // error!

What you should be doing in this case (imho) is not using arrays but using an ArrayList. In all honesty, there is very little reason to use arrays over an ArrayList and generics is just one example of that.

For a better and more complete explanation see the (excellent) Java Generics FAQ:

Can I create an array whose component type is a concrete parameterized type?

No, because it is not type-safe.

Arrays are covariant, which means that
an array of supertype references is a
supertype of an array of subtype
references. That is, Object[] is a
supertype of String[] and a string
array can be accessed through a
reference variable of type Object[].

Leave a Comment