2

The oracle doc says Generics are implemented in java using a technique call type erasure and this is how it works.

  1. Replace all type parameters in generic types with their bounds or Object if the type parameters are unbounded. The produced bytecode, therefore, contains only ordinary classes, interfaces, and methods.
  2. Insert type casts if necessary to preserve type safety.
  3. Generate bridge methods to preserve polymorphism in extended generic types.

So if I have a Generic class say Container as below:

class Container<T>{
T initialValue;
List<T> valueList=new ArrayList<T>();
public List<T> getValueList(){
  return valueList;
}

}

it's equivalent class would look like after being processed by type erasure:

class Container{
    Object initialValue;
    List valueList=new ArrayList();
    public List getValueList(){
      return valueList;
    }

    }

Correct me if a wrong here

Similarly, if a modify the above class as below

class Container<T>{
    T initialValue;
    List<T> valueList=new ArrayList<T>();
    T[] arrayValue;
    public Container(T[] array){
       arrayValue=array;
     }
    public List<T> getValueList(){
      return valueList;
    }

    }

won't be this equivalent to???

class Container{
    Object initialValue;
    List valueList=new ArrayList();
    Object[] arrayValue;
    public Container(Object[] array){
       arrayValue=array;
     }
    public List getValueList(){
      return valueList;
    }

    }

if this is true then I should also have like this: T[] arrayValue=new T[10];//Compile time error; as the above statement would get converted into

Object[] arrayValue=new Object[10];

Need clarity on how type erasure works for Arrays in Java??

2
  • What is the error you are seeing, with what code, specifically? It's not really clear what you're asking. Commented May 22, 2016 at 5:00
  • why T[] arrayValue=new T[10]; is not allowed. Why can't we create an array Object of parameterized type. Commented May 22, 2016 at 5:02

1 Answer 1

6

You cannot create generic arrays. The reason is that arrays predate Java's generics, and arrays do not use type erasure. So, for example, an Integer[] and a String[] really have different type at runtime. If you write new T[], the compiler doesn't know what kind of array it needs to create.

You can create a fake generic array by doing:

T[] array = (T[]) new Object[10];

But you have to remember that you really created an Object array, not a T array. At run-time it is possible to put non-T instances in it, so only do this if the array is a private field of your class which is never passed to other objects, so that you can control exactly what objects are put in the array.

If you have a Class<T> instance (a so-called type token) you can use Array.newInstance to create a new array with the correct run-time type:

T[] array = (T[]) Array.newInstance(typeToken, 10); 
Sign up to request clarification or add additional context in comments.

1 Comment

No, the type erasure does not work with arrays. At run-time, a List<Integer> and a List<String> have the same type, namely List, but an Integer[] and a String[] have different types.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.