O'Reilly logo

Component Development for the Java™ Platform by Stuart Dabbs Halloway

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

7.7. Generating Strongly Typed Collections

As a simple example of the performance tradeoffs that drive a GP design, consider the collection classes in the java.util package. The various collections (ArrayList, HashMap, etc.) are all of type Object—in other words, the collections are entirely generic. If you want to use a collection in a type-safe fashion, then you must write additional code to enforce type safety at runtime, as shown here:

// Must pay runtime cost of casting to String
// also possible that cast might fail
String value = (String) stack.pop();

One workaround to this problem is to write your own strongly typed collections, such as hand-coded StringStack, IntStack, and so on. Such work is tedious, error-prone, and better suited ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required