Using Raw Types with Generic Class Stack

The test program of Fig. 20.11 uses the notion of raw type. Line 11 instantiates generic class Stack with raw type, which indicates that rawTypeStack1 can hold objects of any type. Line 14 assigns a Stack<Double> to variable rawTypeStack2, which is declared as a Stack of raw type. Line 17 assigns a Stack of raw type to Stack<Integer> variable, which is legal but causes the compiler to issue a warning message (Fig. 20.12) indicating a potentially unsafe assignment—again, this occurs because a Stack of raw type might store types other than Integer. Also, the calls to generic methods testPush and testPop in lines 19–22 result in compiler warning messages (Fig. 20.12). These occur because rawTypeStack1 and ...

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.