Today we begin a series of lessons that will deepen our understanding of Kotlin. They will also introduce an incredibly important and prevalent idea in computer science—references. So let's get started!
As we frequently do, we'll begin with a puzzle. Examine the following code (duplicated from the lesson header) and try to predict what should happen:
The critical bit is the line
val you = me.
There are two things that could happen here:
meand save it to
you, meaning that at that point there would be two
Given the result, the answer is clearly the latter. On some level we should find this reassuring, since previously we had said that (1) objects are only created when you see the constructor called and (2) Kotlin does not have a built in way of making object copies. However, it does leave us with the question: what is happening, and why?
Until today we've been somewhat vague about exactly what is being stored in our variables. No longer. Variables in Kotlin that store objects do not store the object instance itself. Rather, they store a reference to the instance.
What is a reference? I'm glad you asked! Wikipedia says.
In computer science, a reference is a value that enables a program to indirectly access a particular datum, such as a variable's value or a record, in the computer's memory or in some other storage device. The reference is said to refer to the datum, and accessing the datum is called dereferencing the reference.
Let's parse this together:
In Kotlin, when we assign an instance of an object to a variable, what we are really doing is creating a reference variable. The variable stores a reference to the object, not the object itself:
If and when you get lost here, here's something that will help.
Objects are only created when you see the constructor called.
In the example above, even though we have two reference variables (
t), we have only one
Person created when we see the constructor called.
A reference is not the thing it refers to. This becomes clear when we examine some real-world examples of references:
In each of these cases above, a reference is something that we can use to access the object it refers to. If I give you my phone number, you can call me. If I give you my address, you can visit! If I make a bunch of copies of my address, you could all come over and visit! But I would still only have one house.
Let's continue the analogy above. Imagine I give two of you the address to my house. The next day, one of you comes by and "decorates" my house with sanitary paper. If the other comes by later, they also see the change!
So the next rule of references: changes to object instances are visible to all reference holders.
This is why, in the example above,
me.name returns "Fantastic Student" even though the change was made using the reference variable
Both refer to the same object, the only one that was created by calling the
Note that all that we said above is not true for Kotlin basic types. They store their values directly in the variable:
But this only works for the eight basic types. Any variable that stores an object in Kotlin is a reference variable, and actually stores an object reference.
nullis a Reference
Finally, this also gives us a better understanding of
null is the empty reference:
null indicates that a variable that could store a reference does not, and is empty.
This also explains what happens if we try and follow or deference
(Imagine if I told you to call a phone number, but then handed you a blank sheet of paper!)
Let's get some practice manipulating references.
Copying a reference does not copy the object it refers to! Let's return to our example and discuss what is actually happening:
Now, let's go through this step by step using a diagram:
Imagine I have the following code, and I want to swap the objects that
you refer to.
(Searching for the fountain of youth, I guess...)
Let's walk through how we would do that:
Next, let's examine a similar example using a diagram:
.equals and so always tests instance equality as defined
by the class.
However, what if we need to test reference equality?
There's also a way to do that!
Let's see how.
One important caveat is that
=== only works the way you expect if the values are not Kotlin basic types.