Because of the implementation of containers, the contained type cannot be instantiated within them. An inconvenient consequence is toArray. There are two ways to call it
Object arr = list.toArray();
But it's not easy to change Object to Type.
Type arr = new Type[m];
But it would be much better not to have to allocate arr separately.
The next version of Java should really reconsider the implementation of templates.
Wednesday, September 17, 2008
In Java 1.5 (and 1.6), generics is used by containers to avoid runtime type cast. Generics are not implemented in the same way as C++ templates, and are much less flexible. A generic class is compiled into only one class file, instead of one for each template instance. A big disadvantage is that a generic container cannot make new instances of its elements.