Arrays are objects in Java
Array variables are references; assigning shares the same array.
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 tooYour turn
What prints?
int[] a = {1, 2, 3};
int[] b = a;
b[1] = 50;
System.out.println(a[1]);250Compile 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 untouchedTrace 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]);9 25 09 01 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}.
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}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 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
- int[] b = a; means one array with two names
- Changes made through b are visible through a
- An array variable can be null
- 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.
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
- 99
- 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]);- 5 5
- 1 5
- 7 5
- 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.