Array Name
1. What Array Name Really Is (beginner)
Array name = reference (address) of first box, NOT the boxes themselves. int[] a = new int[3] → a holds Heap address. Copying name copies address (both point to SAME boxes!).
2. Code — Reference Proof (exam favourite!)
int[] a = {10, 20, 30};
int[] b = a; // copies ADDRESS (no new boxes!)
b[0] = 99;
System.out.println(a[0]); // 99! (same boxes — surprise!)
System.out.println(a); // [I@1a2b3c (address, not elements!)
System.out.println(java.util.Arrays.toString(a)); // [99, 20, 30] (correct print!)
Passing array to method passes address → method changes original!
static void zero(int[] x) { x[0] = 0; }
zero(a); // a[0] becomes 0 (shared!)
3. Name vs Content
| Code | Meaning |
|---|---|
a |
address of array |
a[0] |
first box value |
a.length |
box count |
a == b |
same address? (not same content!) |
Arrays.equals(a,b) |
same content? (correct compare!) |
4. Traps
System.out.println(a)prints gibberish address — useArrays.toString(a).int a[](C-style) works butint[] a(Java-style) preferred in exams.
5. Exam Points
- Reference diagram +
b=aoutput — 5 marks, very frequent. - Print with
Arrays.toString— 2-3 marks, frequent.