-
In this lecture, we’ll explore what references are
and how they relate to objects in Java.
-
All variables hold a single and only a single value.
-
You should be familiar with the 8 primitive types such as byte and boolean.
-
Each type reserves a fixed amount of memory to hold each
time that you create that type of variable
-
For example, a byte is 8 bits so each time you create
a byte, Java reserves 8 bits of space in memory.
-
A reference, however, is a type that refers to any value that is not a primitive.
-
It's a convenient way to label data that refers to a single instance of
a class, interface, array, enum or annotation.
-
For example, when you create a String, you create
an object that is a String type in memory.
-
Then if you create a String variable and assign it the value of the string,
-
you’re actually creating a reference to that String object.
-
In Java, when you create a variable that refers to a class,
-
you create a bond between that variable and the class type.
-
This creates a strong reference bond between
that object and the reference variable.
-
That reference doesn’t “refer” directly to the object, but to its location in memory.
-
This is an important aspect of reference variables that I’ll highlight more.
-
The variable points to the memory location and not the object itself.
-
The variable follows the lifecycle of the
object from creation to garbage collection.
-
You should note that objects, which are typically class types,
-
can have properties that are other class types and those
classes may have properties that refer to other classes.
-
For each of these references, the object created must allocate
memory for all of the properties (primitive or reference type).
-
You can imagine how complex the task would be if
you had to manage the memory allocation yourself.
-
Thankfully, Java handles memory management for you.
-
One of the effects of references that Java supports
is the ability to pass variables by value.
-
This means that when you pass a reference type to a method,
that method has access to the data and can change it.
-
It does not have access to the original reference
itself, and cannot change it to point elsewhere.
-
This makes Java different than languages like C++
-
where pointers are supported and variables are pass-by-reference instead of pass-by-value.
-
In pass-by-reference, the location in memory of where the value
is stored is passed as opposed to a copy of the value itself.
-
Furthermore, in Java, pass-by-value means that
the value is copied to a new location.
-
This is why you can alter a value within a method and
it won’t affect the variable outside of the method.
-
Let’s take a look at another example.
-
The following is a simple declaration statement.
-
Here we’re creating a reference variable that will point to a type specified by ClassName.
-
Moving on, when you create an object from a class,
-
typically by using the ‘new’ keyword,
-
then Java allocates the amount of memory that
the object requires to store the object.
-
The code snippet here creates an object of type Sample.
-
It also creates a variable named ‘ob’ that refers to a Sample type
-
and is associated with the object that was created by the same line of code.
-
Remember, in Java, the variable is assigned a
reference to the object and not the object itself.
-
This reference is the address of the memory location where the object is stored.
-
We can also create multiple references to the same object.
-
For example, the following added line creates a reference variable
‘r’ that is associated with the instance of Sample.
-
Here, both variables, ‘ob’ and ‘r’, refer to the same Sample instance.
-
Now let’s talk about objects briefly.
An entity that has state and behavior and exists in memory is known as an object.
-
An object is considered an instance of a class, a living, breathing copy of it.
-
So how do objects and references relate?
-
Well, an object is an instance of a class.
-
A reference describes
the memory location of where an object is stored.
-
Thus, a reference variable may be associated with a single memory
address of where an object of the indicated type exists.
-
One final note before we end this lesson is that a reference variable does not have to match exactly the type of object that it is associated with.
-
If you have learned about Inheritance, then you know that a
class can extend another which creates a parent-child relationship.
-
The child inherits properties and behavior of the parent.
-
This creates an “IS-A” relationship.
-
Child classes are not the only constructs that form IS-A relationships.
-
A class that implements an interface also forms an IS-A relationship with that interface.
-
So, what does this all mean?
-
It means that a reference variable can be the child or parent type
of the memory location of the object that pairs with it.
-
Let’s review again, how an object, a class, and a reference relate to one another.
-
A class is a template used to instantiate objects.
-
It’s also called a ‘type’ in some circumstances,
such as when used with a reference variable.
-
An ‘object’ is an instance of a class in memory.
-
It cannot be accessed directly; only through a ‘reference’.
-
A ‘reference variable’ is a variable that stores the reference to an object in memory.
-
In this lesson, we covered how a reference is a type of variable that stores the memory address of where an object is located.
-
It isn’t the object itself, but the doorway to use to access the object.
-
Reference variable types do not have to exactly
match the type of the referenced object.
-
It can coincide with an IS-A relationship.
-
Thanks again for tuning in.
-
I hope that you found this lesson helpful in understanding
reference variables and that you’ll watch more of our videos.