OBJECT-ORIENTED PROGRAMMING

Java reference types

A reference variable does not hold all the object's data itself. It refers to an object whose attributes and methods we can access.

A primitive value and a reference

A primitive variable, such as int kiekis = 3;, stores the value itself, 3. A reference variable, such as Puodelis manoPuodelis, stores a reference to a particular object.

Comparison of primitive and reference types
PropertyPrimitive typeReference type
Exampleint, double, booleanClass, array, String, interface
The variable storesThe value itselfA reference to an object or null
MethodsNoneThe object can have methods
Default field valueDepends on the type, e.g. 0null
A reference is not the object itself.

It lets the Java virtual machine find the object. The programmer does not need to know or manage its actual memory address.

A class instance and a reference

class Puodelis {
    String spalva;
}

public class Main {
    public static void main(String[] args) {
        Puodelis manoPuodelis = new Puodelis();
        manoPuodelis.spalva = "mėlyna";

        System.out.println(manoPuodelis.spalva);
    }
}
Console output

mėlyna

  1. Puodelis defines a new reference type.
  2. new Puodelis() creates a new object.
  3. manoPuodelis receives a reference to the created object.
  4. The dot operator accesses that object's attribute spalva.
  5. println() displays the text stored in the object.

Two references to the same object

Assigning one reference variable to another does not copy the object. It copies the reference:

Puodelis pirmas = new Puodelis();
pirmas.spalva = "balta";

Puodelis antras = pirmas;
antras.spalva = "žalia";

System.out.println(pirmas.spalva);
System.out.println(antras.spalva);
Console output

žalia
žalia

  1. Only one instance is created: Puodelis object.
  2. antras = pirmas does not add another cup.
  3. Both variables refer to the same object.
  4. Changing the color through antraschanges the shared object.
  5. Both references therefore read the same new color.

What does null mean?

Puodelis puodelis = null;

System.out.println(puodelis);
// puodelis.spalva would cause a NullPointerException
Console output

null

null means that the variable currently refers to no object. Attempting to access a field or call a method through such a reference causes NullPointerException.

What counts as a reference type in Java?

  • Classes and their objects, for example, Puodelis or Scanner.
  • String – a class for text, although we often use it like a simple type.
  • Arrays, even if their elements are primitive, such as int[].
  • Interfaces – a reference can point to an instance of a class that implements the interface.
  • Enums and collections, for example, ArrayList or HashMap.
int[] pazymiai = {8, 9, 10};
String vardas = "Rūta";

System.out.println(pazymiai[0]);
System.out.println(vardas.length());
Console output

8
4

pazymiai refers to an array object, while vardas – String object. We can therefore access an array element and call the string method length().

Key points to remember

  • new usually creates a new object.
  • A reference variable lets you access that object.
  • Several references can refer to the same object.
  • null means the reference currently points to no object.
  • Assigning a reference to another variable does not automatically copy the object itself.