I read Java Criticism about why Type erasure makes java less powerful then some languages in generic. In wiki page, here is one example:
public class MyClass<E> {
public static void myMethod(Object item) {
if (item instanceof E) { //Compiler error
...
}
E item2 = new E(); //Compiler error
E[] iArray = new E[10]; //Compiler error
}
}
I understand why E item2 = new E();
should be compiled error because we really don't know class E before calling them. But I don't know why this line should be error:
if (item instanceof E) { //Compiler error
...
}
Because as I know, when compiling java application, all generic type will replaced by its concreted type. for example:
// all E will be substituted by Integer
MyClass<Integer> myObject = new MyClass<Integer>();
So I don't think why should Java throws compile error at above line.