Follow by Email

Saturday, November 13, 2004

Generics in J2SE 5.0

It sounds like I'm not the first person to recognize that in J2SE 5.0, the following causes a compiler error:
public void arrayTest(List<T> list) {
T[] array = new T[list.size()];
}

because you're not allowed to create generic arrays. This has to do with the way Sun implemented Generics by preserving a significant amount of backward compatibility and by dropping the type safety once the source is compiled. In other words, these two statements generate the same bytecode:

List<String> list = new ArrayList<String>();
List list = new ArrayList();

I have a way to go before I can make any determinations, but I'm not particularly pleased with this.

No comments: