Testing the Generic Stack Class of Fig. 20.7
Now, let’s consider the application (Fig. 20.9) that uses the Stack
generic class (Fig. 20.7). Lines 12–13 in Fig. 20.9 create and initialize variables of type Stack<Double>
(pronounced “Stack
of Double
”) and Stack<Integer>
(pronounced “Stack
of Integer
”). The types Double
and Integer
are known as the Stack
’s type arguments. The compiler uses them to replace the type parameters so that it can perform type checking and insert cast operations as necessary. We’ll discuss the cast operations in more detail shortly. Lines 12–13 instantiate doubleStack
with a capacity of 5
and integerStack
with a capacity of 10 (the default). Lines 16–17 and 20–21 call methods testPushDouble
(lines 25–36), testPopDouble ...
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.