|
Fill in the blanks so that the values in valA are copied into the corresponding slots of valB. A good answer might be:
| |
Copying Values in SlotsIn this example, the int in slot 0 of valA is copied to slot 0 of valB, and so on. This is just like an assignment statement where both variables are of primitive type int. After the four assignment statements of the answer have executed, each array contains the same values in the same order: +--------+ +--------+ | 12 | 0 | 12 | 0 +--------+ +--------+ | 23 | 1 | 23 | 1 +--------+ +--------+ | 45 | 2 | 45 | 2 +--------+ +--------+ | 56 | 3 | 56 | 3 +--------+ +--------+ valA valB The following statement does not do the same thing: Remember that arrays are objects. The statement above will merely copy the object reference invalB = valA ; valA into the object
reference variable valB,
resulting in two ways to access the single array object:
The object that+--------+ | 12 | 0 +--------+ | 23 | 1 +--------+ | 45 | 2 +--------+ | 56 | 3 +--------+ valA, also valB valB previously referenced is now lost
(it has become garbage.)
| |
QUESTION 12:Say that the statement valB = valA had been executed, resulting in the above picture. What would the following two statements do? Click Here after you have answered the questionvalA[2] = 999; System.out.println( valA[2] + " " + valB[2] ); |