The Java Type Library



next up previous
Next: Conclusions Up: Interaction of Parameterization Previous: Java Arrays

The Java Type Library

 

Java is being augmented with a library of interfaces and classes. Already there are several abstractions in the library that would be more useful if parameterization were available. Types such as Vector and Enumeration are most naturally written as parameterized types, but currently manipulate objects of type Object instead.

This has two unfortunate consequences:

  1. When new elements are added to a vector, or are stored in the vector, if they are of a primitive type such as int then it is necessary to objectify them. For example, int must be explicitly wrapped in an Integer object to make it usable as an Object. This wrapping step is awkward for the programmer and has runtime overhead.

  2. Whenever an element is fetched from a Vector or produced by an Enumeration, it must be explicitly cast from Object to the expected type. If the element has a primitive type, it also must be unwrapped after the cast, adding even more cost and coding complexity.

Both problems are ameliorated by parameterized types. When elements are added, there is no need to objectify them, and when they are retrieved from the collection, there is no need for the expensive runtime cast or unwrapping.



Andrew C. Myers, Joseph A. Bank, Barbara Liskov
Copyright © 1996 Association for Computing Machinery