I was reading this article on Java Generics and there it is mentioned that the constructor for an 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 the compiler happens as explained there. One point I got was that the type parameter is transformed to Object type.
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<String> and ArrayList<Integer> are there two different classes for each? If not, where is the type information of String and Integer is stored?