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.
| Property | Primitive type | Reference type |
|---|---|---|
| Example | int, double, boolean | Class, array, String, interface |
| The variable stores | The value itself | A reference to an object or null |
| Methods | None | The object can have methods |
| Default field value | Depends on the type, e.g. 0 | null |
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);
}
}mėlyna
Puodelisdefines a new reference type.new Puodelis()creates a new object.manoPuodelisreceives a reference to the created object.- The dot operator accesses that object's attribute
spalva. 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);žalia
žalia
- Only one instance is created:
Puodelisobject. antras = pirmasdoes not add another cup.- Both variables refer to the same object.
- Changing the color through
antraschanges the shared object. - Both references therefore read the same new color.
What does null mean?
Puodelis puodelis = null;
System.out.println(puodelis);
// puodelis.spalva would cause a NullPointerExceptionnull
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,
PuodelisorScanner. - 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,
ArrayListorHashMap.
int[] pazymiai = {8, 9, 10};
String vardas = "Rūta";
System.out.println(pazymiai[0]);
System.out.println(vardas.length());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
newusually creates a new object.- A reference variable lets you access that object.
- Several references can refer to the same object.
nullmeans the reference currently points to no object.- Assigning a reference to another variable does not automatically copy the object itself.