20.7 Raw Types

The test programs for generic class Stack in Section 20.6 instantiate Stacks with type arguments Double and Integer. It’s also possible to instantiate generic class Stack without specifying a type argument, as follows:

Stack objectStack = new Stack(5); // no type argument specified

In this case, the objectStack has a raw type—the compiler implicitly uses type Object throughout the generic class for each type argument. Thus the preceding statement creates a Stack that can store objects of any type. This is important for backward compatibility with prior Java versions. For example, the data structures of the Java Collections Framework (Chapter 16) all stored references to Objects, but are now implemented ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.