Objects and references

A variable of a class type or an array type holds a reference: a value that points at an object somewhere in the heap. Two references can point at one object, a reference can be reassigned to another object, and a reference can point at nothing at all. Every operation on an object goes through one.

A reference is not the object

The program below copies the reference to an array, writes through the copy and reads the original. The write is visible through both names because there is one array, and the comparison of the two references is true.

The file Refs.java:

public class Refs { public static void main(String[] args) { int[] a = {1, 2, 3}; int[] b = a; b[0] = 9; System.out.println("a[0]: " + a[0]); System.out.println("a == b: " + (a == b)); a = new int[] {4, 5}; System.out.println("a[0]: " + a[0]); System.out.println("b[0]: " + b[0]); int[] c = null; try { System.out.println(c[0]); } catch (NullPointerException error) { System.out.println("caught: " + error.getMessage()); } } }

javac -Xlint:all Refs.java
java Refs

a[0]: 9
a == b: true
a[0]: 4
b[0]: 9
caught: Cannot load from int array because "<local3>" is null

The first two lines show one array with two names. The next two show the reassignment: a now points at a new two-element array, while b still points at the old one, which is why the two reads differ.

Reassigning a reference

Reassignment changes which object a variable points at; it does not copy or modify the object the variable used to point at. An object with no reference left is garbage, and the collector of lesson 124 reclaims it when it needs the memory.

Passing a reference to a method copies the reference, which is the pass by value of lesson 018: the method can change the object, and it cannot change the caller variable to point somewhere else.

null

null is a reference that points at no object. It is the default of every field and array element of a reference type, and it is not a value of the class: null instanceof String is false, a method call on it throws NullPointerException, and only == against null is safe.

The caught message names the local variable, because Java 14 and later attach a helpful report to the exception. A program that may receive a null checks for it at the boundary, where the bad value entered, rather than deep inside where it is used.

== against equals

== asks whether two references point at the same object. equals asks the class whether two objects are equal by its own rule, which for String is the same characters.

public class StringRefs { public static void main(String[] args) { String a = "java"; String b = a; String c = new String("java"); System.out.println("a == b: " + (a == b)); System.out.println("a == c: " + (a == c)); System.out.println("a.equals(c): " + a.equals(c)); } }

javac -Xlint:all StringRefs.java
java StringRefs

a == b: true
a == c: false
a.equals(c): true

The rule is the same one lesson 012 gave for conditions and lesson 040 makes precise: compare contents with equals, compare identity with ==, and compare null with ==.

Exercises

  1. Add a third name for the first array in Refs.java before the reassignment and check that all three see the write.
  2. Set b to null after the first two lines and call a method on it. What does the message say?
  3. Write a method static void clear(int[] data) that sets every element to 0, and show that the caller sees it.
  4. Compare two strings read from a split with == and with equals, and explain the results.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does a variable of a class type hold?

    Answer

    A reference to an object

    The object lives in the heap and the variable holds the reference that points at it.

  2. What does assigning one reference variable to another copy?

    Answer

    The reference, so both names point at one object

    The value copied is the reference; the object is untouched and shared.

  3. What is null?

    Answer

    A reference that points at no object

    null is the absence of an object, and any use of it as an object fails.

  4. What does this program print? public class Q1 { public static void main(String[] args) { int[] a = {1, 2}; int[] b = a; b[1] = 9; System.out.println(a[1]); } }

    Answer

    9

    Both names point at one array, so the write through b is visible through a.

  5. What does this program print? public class Q2 { public static void main(String[] args) { int[] a = {1, 2}; int[] b = a; a = new int[] {3, 4}; System.out.println(a[0] + " " + b[0]); } }

    Answer

    3 1

    The reassignment points a at a new array, and b still points at the old one.

  6. What does this program print? public class Q3 { public static void main(String[] args) { String a = "x"; String b = new String("x"); System.out.println(a.equals(b) + " " + (a == b)); } }

    Answer

    true false

    The characters are equal and the two references are not.

  7. What does this program print? public class Q4 { public static void main(String[] args) { int[] a = null; try { System.out.println(a.length); } catch (NullPointerException error) { System.out.println("null"); } } }

    Answer

    null

    Reading length goes through the reference, and a null reference throws.

  8. What does this program print? public class Q5 { public static void main(String[] args) { int[] a = {1, 2, 3}; clear(a); System.out.println(a[2]); } static void clear(int[] data) { data[2] = 0; } }

    Answer

    0

    The method receives a copy of the reference and writes into the one array.

  9. What does this program print? public class Q6 { public static void main(String[] args) { String a = "ab"; String b = a; a = a + "c"; System.out.println(a + " " + b); } }

    Answer

    abc ab

    The concatenation creates a new string for a, and b still points at the old one.

  10. Why is == the safe comparison for a possible null?

    Answer

    It compares references without calling a method on the value

    equals is a method call and needs an object; == only compares the two reference values.

The code on this page was built with OpenJDK 21.0.13 (javac and java) and run on Alpine Linux in a Docker container.

Article author: Arthur Isaev

Banner image