Cannot Instantiate Generic Types with Primitive Types
Generics in Java are an entirely compile-time construct - the compiler turns all generic uses into casts to the right type. This is to maintain backwards compatibility with previous JVM runtimes.
This:
List<ClassA> list = new ArrayList<ClassA>();
list.add(new ClassA());
ClassA a = list.get(0);
gets turned into (roughly):
List list = new ArrayList();
list.add(new ClassA());
ClassA a = (ClassA)list.get(0);
So, anything that is used as generics has to be convertable to Object (in this example
get(0)
returns an Object
), and the primitive types aren't. So they can't be used in generics.
C# is a separate matter - generics are implemented directly as part of the runtime, so primitive types can be used - the CLR generates new versions of generic classes for primitives and structs as they are used. The only disadvantage is (until .NET 4) no generic covariance or contravariance was allowed, unlike Java (see the
super
and extends
keywords in generic definitions)Cannot Create Instances of Type Parameters
E elem = new E(); // compile-time errorAs a workaround, you can create an object of a type parameter through reflection:
public static <E> void append(List<E> list, Class<E> cls) throws Exception { E elem = cls.newInstance(); // OK list.add(elem); }
Cannot Declare Static Fields Whose Types are Type Parameters
A class's static field is a class-level variable shared by all non-static objects of the class. Hence, static fields of type parameters are not allowed.Cannot Use Casts or instanceof with Parameterized Types
Because the Java compiler erases all type parameters in generic code, you cannot verify which parameterized type for a generic type is being used at runtime:
Cannot Create Arrays of Parameterized Types
As mark said, the types are not reifiable, which is a problem in the following case:
try {
doSomeStuff();
} catch (SomeException<Integer> e) {
// ignore that
} catch (SomeException<String> e) {
crashAndBurn()
}
Both
SomeException<Integer>
and SomeException<String>
are erased to the same type, there is no way for the JVM to distinguish the exception instances, and therefore no way to tell which catch
block should be executed.If arrays of parameterized lists were allowed, the previous code would fail to throw the desired ArrayStoreException.
Cannot Overload a Method Where the Formal Parameter Types of Each Overload Erase to the Same Raw Type
A class cannot have two overloaded methods that will have the same signature after type erasure.
The overloads would all share the same classfile representation and will generate a compile-time error.
The overloads would all share the same classfile representation and will generate a compile-time error.