Command Palette

Search for a command to run...

Lesson 0.3 · Java for DSA

Objects, References and Nodes

Classes and objects, what a reference really is, null, and the ListNode and TreeNode classes used in linked list and tree problems.

15 min

Think of it like this

A reference is like a house address written on a card. Copying the card gives you a second card with the same address, not a second house. If someone repaints the house using either card, both cards lead to the repainted house.

1.Classes, objects and constructors

A class is a blueprint; an object is one thing built from it with new. The constructor (a method with the class's name) sets up the fields.

Variables of object types (including arrays and strings) hold a reference: the address of the object, not the object itself.

Point.java
class Point {
    int x, y;
    Point(int x, int y) { this.x = x; this.y = y; }
}

public class Main {
    public static void main(String[] args) {
        Point a = new Point(1, 2);
        Point b = a;            // copies the reference, not the object
        b.x = 99;
        System.out.println(a.x);   // 99: a and b point to the same object
    }
}

Output

99

2.Passing references to methods

Java always passes a copy of the value. For objects, the value is the reference, so a method can change the object's contents (and the caller sees it), but reassigning the parameter inside the method doesn't affect the caller's variable.

That's why in-place array problems work: your method receives the same array the caller has, and swapping elements changes it for everyone.

PassRef.java
import java.util.Arrays;

public class Main {
    static void change(int[] arr) {
        arr[0] = 42;            // visible to the caller
        arr = new int[]{7};     // only changes the local copy of the reference
    }
    public static void main(String[] args) {
        int[] nums = {1, 2, 3};
        change(nums);
        System.out.println(Arrays.toString(nums));
    }
}

Output

[42, 2, 3]

3.null and NullPointerException

null means "this reference points to nothing". Calling a method or reading a field through null throws NullPointerException. In linked list and tree code, the end of the list or a missing child is null, so always check before stepping: while (node != null).

4.ListNode and TreeNode

Linked list problems give you a ListNode with a value and a reference to the next node. Tree problems give you a TreeNode with a value and references to left and right children. These are the standard definitions used across coding judges and in this course.

Nodes.java
class ListNode {
    int val;
    ListNode next;
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}

class TreeNode {
    int val;
    TreeNode left, right;
    TreeNode(int val) { this.val = val; }
}

public class Main {
    public static void main(String[] args) {
        ListNode head = new ListNode(1, new ListNode(2, new ListNode(3)));
        for (ListNode n = head; n != null; n = n.next) System.out.print(n.val + " ");
    }
}

Output

1 2 3 

Remember

  • Object variables hold references (addresses), not the objects.
  • Methods can change an object passed to them; reassigning the parameter has no effect outside.
  • Check for null before following a reference.
  • ListNode has val and next; TreeNode has val, left and right.

Common mistakes

  • Thinking b = a copies an object.
  • Following node.next without checking node != null.
  • Expecting a method that reassigns its parameter to change the caller's variable.

Words used in this lesson

Class / object
A blueprint / one thing built from it.
Reference
The address of an object, held by a variable.
null
A reference that points to nothing.
Node
One element of a linked list or tree, holding a value and links.