unkletayo
3

Assignment and what gets copied

Why two variables can silently share the same object, and when that matters.

Adetayo Akinsanya2026-06-166 min read

  • Java
  • Core Java
  • References
  • Memory

Part 3 of the series.Read How Java stores values (Part 2) first if you have not.

In this article (6 sections)
  1. Assignment copies the value
  2. Modifying through a shared reference
  3. null: a reference that points nowhere
  4. What “immutable” means in this model
  5. The practical pattern to watch for
  6. Prediction exercise
int[] arr1 = {1, 2, 3};
int[] arr2 = arr1;
arr2[0] = 99;
System.out.println(arr1[0]);

What does this print?

A lot of Java confusion traces back to this question. The answer is 99. We only wrote to arr2, but arr1[0] changed. This isn’t a bug in Java. It’s the direct consequence of what = does to a reference variable, and once you understand that, the behaviour is obvious.

Assignment copies the value

The = operator always copies the value on the right into the storage location on the left. For primitives, the value is the number itself.

int a = 5;
int b = a;
a = 10;
System.out.println(b);  // 5

b gets a copy of the bits in a at the moment of assignment: the value 5. Changing a afterward has no effect on b. They’re independent.

For a reference variable, the value is not the object. It’s the address of the object. That address is what gets copied.

String x = "hello";
String y = x;

After this, x and y both hold the same address. They point to the same String object in the heap. This is called aliasing: two names for the same thing.

Now:

y = "world";

This replaces the address stored in y with the address of a different String object. It does not touch the "hello" object. x still holds the original address, so x still points to "hello". Reassigning y only changes what y holds; it cannot change what x holds.

Modifying through a shared reference

Reassigning is different from mutating. When you have two references to the same object, and you use one of them to modify the object’s state, both references see the change, because there’s only one object.

StringBuilder sb = new StringBuilder("hello");
StringBuilder copy = sb;
copy.append(" world");
System.out.println(sb);    // hello world
System.out.println(copy);  // hello world

sb and copy hold the same address. They both refer to the same StringBuilder object. Calling copy.append(...) modifies that object in place. Because sb points to the same object, sb sees the modification. This isn’t surprising once you know that copy = sb copied the address, not the object.

Now go back to the array example:

int[] arr1 = {1, 2, 3};
int[] arr2 = arr1;
arr2[0] = 99;
System.out.println(arr1[0]);  // 99

arr2 = arr1 copies the address. Both variables point to the same array in the heap. arr2[0] = 99 writes to slot 0 of that array. Since arr1 points to the same array, arr1[0] reflects the change.

Contrast this with:

arr2 = new int[]{7, 8, 9};

This creates a new array and stores its address in arr2. Now arr2 points to a different array. arr1 is unchanged, still pointing to {1, 2, 3} (or {99, 2, 3} if the previous mutation already happened). Reassigning arr2 changes only what arr2 holds.

The rule is consistent: = copies the value. For a reference, the value is an address, not an object. Aliasing is assignment doing exactly what it says.

null: a reference that points nowhere

A reference variable doesn’t have to hold the address of an object. It can hold null, a special value meaning “no object here.”

String s = null;
System.out.println(s);        // null (println handles null specially)
System.out.println(s.length()); // NullPointerException

s holds null, not an address. When you call s.length(), the JVM tries to dispatch the method call to the object at the address in s. There’s no address. There’s no object. The JVM throws a NullPointerException.

null is not an empty string, not a zero-length array, not a default object. It’s the absence of a reference. A String variable is null by default if declared as a field without initialisation; a local variable is not given a default and must be initialised before use. The compiler enforces this.

String a = "";      // a reference to an empty String object
String b = null;    // no object at all
int len = a.length();  // 0 — fine
int len2 = b.length(); // NullPointerException

The distinction matters often enough that Java 14 added helpful NullPointerException messages that tell you which variable was null and which method was being called.

What “immutable” means in this model

String is immutable. No method on String changes the string’s characters. Every method that seems to modify a string (like replace, toUpperCase, substring) returns a new String object and leaves the original untouched.

String s = "hello";
String t = s.toUpperCase();
System.out.println(s);  // hello
System.out.println(t);  // HELLO

s and t now hold different addresses. s still points to "hello". t points to the new "HELLO" object. The original string was not modified. String objects have no method that could modify them.

This matters for aliasing. With a mutable object like StringBuilder, two references sharing the same object means mutations are visible from both. With an immutable object like String, this concern disappears. You can share a String reference freely because nothing can change the object it points to.

The practical pattern to watch for

The mistake that bites new Java developers most often is assuming that assigning a reference variable copies the object.

// Wrong mental model: does this protect arr1?
int[] arr1 = {1, 2, 3};
int[] backup = arr1;
// 'backup' doesn't protect arr1 from mutation through arr1
// it *is* arr1 — same array, two names

If you want an independent copy of an array, you need to copy the array itself:

int[] backup = Arrays.copyOf(arr1, arr1.length);
// or:
int[] backup = arr1.clone();

Now backup points to a new array with the same values. Mutations through arr1 don’t affect backup, and vice versa.

Prediction exercise

StringBuilder a = new StringBuilder("cat");
StringBuilder b = a;
StringBuilder c = new StringBuilder(a.toString());

b.append("s");
c.append("s");

System.out.println(a);
System.out.println(b);
System.out.println(c);

Predict what each line prints before you run it. Trace which variables share the same object in the heap, and which are independent. The answer follows directly from what you now know about assignment: b = a copied the address; new StringBuilder(a.toString()) created a new object.

Questions or corrections

Found a mistake or have a question about this breakdown? Reach out directly.

Reply on X (@unkletayor)Email feedback