vendredi 7 mars 2014

type erasure in implementation of ArrayList in Java


Vote count:

1




I was reading this article on Java Generics and there it is mentioned that the constructor for arrayList looks somewhat like this:



class ArrayList<V> {
private V[] backingArray;
public ArrayList() {
backingArray = (V[]) new Object[DEFAULT_SIZE];
}
}


I was not able to understand how type erasure and type checking by compiler happens as explained there. One point though I got was the type parameter is transformed to Object type.


so I would imagine it as (replacing all V with Object). but this definitely wrong.



class ArrayList<Object> {
private Object[] backingArray;
public ArrayList() {
backingArray = (Object[]) new Object[DEFAULT_SIZE];
}
}


How exactly is it transformed to Object type but still retaining the type safety for V? when I have ArrayList and ArrayList are there two different classes for each? if not where the type information of String and Integer is stored?



asked 1 min ago






Aucun commentaire:

Enregistrer un commentaire