🧱 Arrays · Beginner

Arrays are objects in Java

Array variables are references; assigning shares the same array.

🧩 The mysteryYou make a backup of your array with b = a, then edit b. You check the backup... wait, which one is the backup?

Variables hold addresses

An array is an object living on the heap. An array variable holds a reference to it, like a house address. int[] b = a; copies the address, not the elements: one array, two names.

int[] a = {1, 2, 3};
int[] b = a;   // same array!
b[0] = 99;     // a[0] is now 99 too
🔮 Predict it

Your turn

What prints?

int[] a = {1, 2, 3};
int[] b = a;
b[1] = 50;
System.out.println(a[1]);
  1. 2
  2. 50
  3. Compile error
Show the answer

a and b refer to the same array object, so writing through b changes what a sees: 50.

Re-pointing is different

Writing into the shared array (b[0] = 9) affects both names. But assigning a new array to b just points b somewhere else. From then on, a and b are independent.

int[] a = {1, 2};
int[] b = a;
b = new int[]{7, 8}; // b moves away
b[0] = 5;            // a untouched
🔮 Predict it

Trace it

What prints?

int[] a = {1, 2};
int[] b = a;
b[0] = 9;
b = new int[]{5, 5};
b[1] = 0;
System.out.println(a[0] + " " + a[1]);
  1. 9 2
  2. 5 0
  3. 9 0
  4. 1 2
Show the answer

b[0] = 9 writes into the shared array, so a[0] is 9. Then b is re-pointed to a new array, and b[1] = 0 changes only that new one. a is {9, 2}.

⚠️ The trap

Methods get the address too

Passing an array to a method copies the reference. So the method can change the caller's elements. But re-pointing its parameter to a new array only changes its own copy of the reference.

static void zero(int[] arr) {
    arr[0] = 0;          // caller sees this
    arr = new int[]{9};  // caller doesn't
}
int[] a = {5, 6};
zero(a); // a is now {0, 6}
🤔 Think first

Arrays and Object

Can an int[] be stored in a variable of type Object? Can an array variable be null?

Think about it, then reveal the answer

Yes and yes. Every array is an object, so Object o = new int[3]; compiles. And int[] a = null; is legal: the variable simply refers to no array (then a.length throws NullPointerException).

💼 In the real world

In real projects

A getter that returns its private array lets any caller change the object's insides. Experienced developers return a defensive copy (return scores.clone();) instead, a classic tip from Joshua Bloch's book *Effective Java*.

Key takeaways

  1. int[] b = a; means one array with two names
  2. Changes made through b are visible through a
  3. An array variable can be null
  4. Any array can be assigned to a variable of type Object

💡 An array variable is a remote control, not the TV. int[] b = a; gives you a second remote for the same TV.

🤯 Did you know?

Arrays are objects, so they even have methods! Besides everything inherited from Object, every array has a public clone() method and its length field.

Practice questions

What does this print?

int[] a = {1, 2, 3};
int[] b = a;
b[0] = 99;
System.out.println(a[0]);
  1. 1
  2. 99
  3. Compile error
Check your answer

99. a and b refer to the same array object, so writing through b changes what a sees.

What does this print?

int[] a = {1, 2};
int[] b = a;
b = new int[]{7, 8};
b[0] = 5;
System.out.println(a[0] + " " + b[0]);
  1. 5 5
  2. 1 5
  3. 7 5
  4. 1 7
Check your answer

1 5. b is re-pointed to a brand-new array before the write, so a's array is untouched.

Next: how to make a REAL copy, and why even a "real" copy can still share secrets.