Suppose that we say:

int [] myarray = new int[1000];
About how many bytes are used by (a) the variable myarray, and (b) what it points to?

  • A: 1000 and 1000
  • B: 4000 and 4000
  • C: 8 and 1000
  • D: 8 and 4000
  • E: 1000 and 4000

    Answer: D

    myarray is a reference variable, so it contains only a pointer to the actual value. Pointers are 8 bytes on a more modern machine.

    Each int is 4 bytes; since there are 1000 of them, the total is 4000 bytes. There is some additional storage in the array for storing the Array class designation, .length, and hash code; this is small compared to the bulk of the array.

    Prev    Next    Page+10