To understand why the program in Segment 545 works, you need to know that, when an array is assigned to a variable, the value is represented as an address of a chunk of memory representing an array instance:
mainArray, in main *-------------------* | Address | *-------------------* | v *-------------------* | Chunk of memory | | representing the | | array | *-------------------*
When you hand mainArray
to readData
, the value of the
parameter, movies
, becomes a copy of the address of
mainArray
, because Java's parameters are call-by-value
parameters, as you learned in Segment 131:
mainArray, in main movies, in readData *-------------------* *-------------------* | Address | | Address copy | *-------------------* *-------------------* | | | *-----------------------------* v v *-------------------* | Chunk of memory | | representing the | | array | *-------------------*
Note, however, that only the address is copied; the contents of the chunk
of memory representing the array are not copied. Accordingly, any changes to the
elements of the array inside readData
are retained after
readData
returns. In this respect, the argumentparameter
relationship of an array is like the argumentparameter relationship of an
instance, as described in Segment 185.