Java Reference
In-Depth Information
ing occurs in line 34 when the program tries to push a primitive double value onto the
doubleStack , which stores only references to Double objects.
Method testPopDouble (lines 39-59) invokes Stack method pop (line 50) in an
infinite loop (lines 48-52) to remove all the values from the stack. The output shows that
the values indeed pop off in last-in, first-out order (the defining characteristic of stacks).
When the loop attempts to pop a sixth value, the doubleStack is empty, so pop throws
an EmptyStackException , which causes the program to proceed to the catch block (lines
54-58). The stack trace indicates the exception that occurred and shows that method pop
generated the exception at line 32 of the file Stack.java (Fig. 20.7). The trace also shows
that pop was called by StackTest method testPopDouble at line 50 (Fig. 20.9) of Stack-
Test.java and that method testPopDouble was called from method main at line 17 of
StackTest.java . This information enables you to determine the methods that were on
the method-call stack at the time that the exception occurred. Because the program
catches the exception, the exception is considered to have been handled and the program
can continue executing.
Auto-unboxing occurs in line 50 when the program assigns the Double object popped
from the stack to a double primitive variable. Recall from Section 20.4 that the compiler
inserts casts to ensure that the proper types are returned from generic methods. After era-
sure, Stack method pop returns type Object , but the client code in testPopDouble expects
to receive a double when method pop returns. So the compiler inserts a Double cast, as in
popValue = (Double) stack.pop();
The value assigned to popValue will be unboxed from the Double object returned by pop .
Methods testPushInteger and testPopInteger
Method testPushInteger (lines 62-73) invokes Stack method push to place values onto
integerStack until it's full. Method testPopInteger (lines 76-96) invokes Stack meth-
od pop to remove values from integerStack . Once again, the values are popped in last-
in, first-out order. During erasure , the compiler recognizes that the client code in method
testPopInteger expects to receive an int when method pop returns. So the compiler in-
serts an Integer cast, as in
popValue = (Integer) stack.pop();
The value assigned to popValue will be unboxed from the Integer object returned by pop .
Creating Generic Methods to Test Class Stack<T>
The code in methods testPushDouble and testPushInteger is almost identical for push-
ing values onto a Stack<Double> or a Stack<Integer> , respectively, and the code in
methods testPopDouble and testPopInteger is almost identical for popping values from
a Stack<Double> or a Stack<Integer> , respectively. This presents another opportunity to
use generic methods. Figure 20.10 declares generic method testPush (lines 24-35) to per-
form the same tasks as testPushDouble and testPushInteger in Fig. 20.9—that is, push
values onto a Stack<T> . Similarly, generic method testPop (Fig. 20.10, lines 38-58) per-
forms the same tasks as testPopDouble and testPopInteger in Fig. 20.9—that is, pop
values off a Stack<T> . The output of Fig. 20.10 precisely matches that of Fig. 20.9.
 
Search WWH ::




Custom Search