Java Reference
In-Depth Information
copies the contents of the original array to the new array (Figure 2-9d) and then discards the original
array (Figure 2-9e). The following pseudocode summarizes these steps:
oldArray = myArray
myArray =
a new array whose length is
2 * oldArray.length
Copy entries from the original array—
oldArray—
to the new array—
myArray
oldArray =
null
// discard old array
FIGURE 2-9
(a) An array; (b) two references to the same array; (c) the origi-
nal array variable now references a new, larger array; (d) the
entries in the original array are copied to the new array; (e) the
original array is discarded
(a)
myArray
(b)
myArray
oldArray
(c)
myArray
oldArray
(d)
myArray
oldArray
(e)
myArray
oldArray
Note:
When an array is no longer referenced, its memory is recycled during garbage
collection, just as occurs with any other object.