The Full Story
References, Objects and Values
In this section we will discuss references, objects, values and primitive types in more detail and allow for a greater understanding of these topics. Even though this is not required for the lecture itself it may help a lot in understanding how your code works.
Motivation in Form of a Short Story
When I was teaching this exact topic to students some time ago I used a analogy from life to explain how references, objects and values work.
The Actors
The story is about three people and is based on real occurrences. The street names and names of the people involved were altered however. The story is about Alice, Bob and Aaron.

Alice

Bob

Aaron
The Meeting
Aaron met up with Alice and Bob and gave to each a piece of paper with the address of the new apartment. This piece of paper contains only the address of the house but it is enough for both Alice and Bob to find the house and the apartement.

Alice
Rämistrasse 101
8092 Zurich
Switzerland

Bob
Rämistrasse 101
8092 Zurich
Switzerland
Find the apartment
Given the address both Alice and Bob can now locate the House in which Aaron lives in. The address thus gives them a method of accessing the house (assuming locks are not a thing) and even change the looks of the house by going to the house and then painting it for example.
Alice
Rämistrasse 101
8092 Zurich
Switzerland


Address change
Aaron mistakenly gave Alice and Bob the wrong address. Alice noticed and called Aaron which then supplied Alice with the new address. Alice erases the old address from the sheet and writes the new one on it.
Leonhardstrasse 36 8092 Zürich
Switzerland

Alice

Local Change
Just because Alice now has the correct address does not mean that Bob has it as well. Hence if Bob were to attempt to visit Aaron's apartment it would not find the correct one.


Rämistrasse 101
8092 Zurich
Switzerland
Bob
Making Changes
Alice can use the address on the sheet of paper to go to Aaron's home and then make changes to it or take a look at it. Alice hence travels to Aaron's home and draws a huge smiley on Aaron's home.
Leonhardstrasse 36 8092 Zürich
Switzerland

Alice

Local Change
Bob does not know that Aaron's address has changed. Bob hears that Alice has drawn a huge smiley on Aaron's home and thus uses the address on the sheet to visit Aaron's home. Bob finds no smiley on Aaron's home and thus assumes that Alice did not tell the truth.


Rämistrasse 101
8092 Zurich
Switzerland
Bob
The Phone Number
One might ask where Alice found Aaron's phone number. Alice owns many sheets with personal information of Aaron. These are just numbers so they are written directly onto the sheet rather than storing an address where the information can be found. We can carry a phone number in our pocket but we cannot carry a building in our pocket.
22

186
Alice
Translating the Story
The story contains all aspects of why Java works the way it works and why this is a reasonable choice. We will now dissect the story and connect each aspect with a Java concept.
The Variable
The sheet of paper on which we write the address, phone number, age, height, etc. is the variable.
Rämistrasse 101
8092 Zurich
Switzerland
22
186
The Value
The information on the sheet of paper is the value. We differentiate between values which are addresses and thus only tell us where to find what we are looking for and values which are already what we are looking for.
Rämistrasse 101
8092 Zurich
Switzerland

The Object
The reference allows us to locate the underlying object, the object is the home in the example. Once we have found the object we can find it and make changes to it.
Rämistrasse 101
8092 Zurich
Switzerland
Leonhardstrasse 36 8092 Zürich
Switzerland
= Assignment
Whenever we use the equality operator = we make changes to the variable. This means that we can only affect this local copy of the value stored inside the reference. This change only affects the variable the changes were made to.
Rämistrasse 101
8092 Zurich
Switzerland
186
Passing Variables to Methods
When a variable is passed to a method the value inside a variable is copied and this copy is what the method receives. A copy of a non-reference value means that we are restricted to local changes (value semantics) and a copy of a reference value means that we have access to the underlying object as the reference tells us where the object is located (reference semantics).