Return to Video

vimeo.com/.../380274435

  • 0:04 - 0:09
    In this lecture, we’ll explore what references are
    and how they relate to objects in Java.
  • 0:10 - 0:13
    All variables hold a single and only a single value.
  • 0:14 - 0:18
    You should be familiar with the 8 primitive types such as byte and boolean.
  • 0:19 - 0:24
    Each type reserves a fixed amount of memory to hold each
    time that you create that type of variable
  • 0:25 - 0:33
    For example, a byte is 8 bits so each time you create
    a byte, Java reserves 8 bits of space in memory.
  • 0:33 - 0:39
    A reference, however, is a type that refers to any value that is not a primitive.
  • 0:39 - 0:49
    It's a convenient way to label data that refers to a single instance of
    a class, interface, array, enum or annotation.
  • 0:49 - 0:56
    For example, when you create a String, you create
    an object that is a String type in memory.
  • 0:57 - 1:01
    Then if you create a String variable and assign it the value of the string,
  • 1:01 - 1:05
    you’re actually creating a reference to that String object.
  • 1:05 - 1:09
    In Java, when you create a variable that refers to a class,
  • 1:09 - 1:14
    you create a bond between that variable and the class type.
  • 1:15 - 1:20
    This creates a strong reference bond between
    that object and the reference variable.
  • 1:22 - 1:28
    That reference doesn’t “refer” directly to the object, but to its location in memory.
  • 1:28 - 1:32
    This is an important aspect of reference variables that I’ll highlight more.
  • 1:34 - 1:38
    The variable points to the memory location and not the object itself.
  • 1:39 - 1:45
    The variable follows the lifecycle of the
    object from creation to garbage collection.
  • 1:45 - 1:49
    You should note that objects, which are typically class types,
  • 1:49 - 1:57
    can have properties that are other class types and those
    classes may have properties that refer to other classes.
  • 1:58 - 2:06
    For each of these references, the object created must allocate
    memory for all of the properties (primitive or reference type).
  • 2:07 - 2:13
    You can imagine how complex the task would be if
    you had to manage the memory allocation yourself.
  • 2:14 - 2:18
    Thankfully, Java handles memory management for you.
  • 2:20 - 2:27
    One of the effects of references that Java supports
    is the ability to pass variables by value.
  • 2:28 - 2:36
    This means that when you pass a reference type to a method,
    that method has access to the data and can change it.
  • 2:37 - 2:43
    It does not have access to the original reference
    itself, and cannot change it to point elsewhere.
  • 2:43 - 2:47
    This makes Java different than languages like C++
  • 2:47 - 2:54
    where pointers are supported and variables are pass-by-reference instead of pass-by-value.
  • 2:54 - 3:02
    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.
  • 3:03 - 3:09
    Furthermore, in Java, pass-by-value means that
    the value is copied to a new location.
  • 3:10 - 3:16
    This is why you can alter a value within a method and
    it won’t affect the variable outside of the method.
  • 3:16 - 3:18
    Let’s take a look at another example.
  • 3:19 - 3:21
    The following is a simple declaration statement.
  • 3:22 - 3:28
    Here we’re creating a reference variable that will point to a type specified by ClassName.
  • 3:28 - 3:32
    Moving on, when you create an object from a class,
  • 3:32 - 3:34
    typically by using the ‘new’ keyword,
  • 3:34 - 3:39
    then Java allocates the amount of memory that
    the object requires to store the object.
  • 3:41 - 3:45
    The code snippet here creates an object of type Sample.
  • 3:46 - 3:53
    It also creates a variable named ‘ob’ that refers to a Sample type
  • 3:53 - 3:57
    and is associated with the object that was created by the same line of code.
  • 3:58 - 4:03
    Remember, in Java, the variable is assigned a
    reference to the object and not the object itself.
  • 4:04 - 4:10
    This reference is the address of the memory location where the object is stored.
  • 4:10 - 4:14
    We can also create multiple references to the same object.
  • 4:14 - 4:23
    For example, the following added line creates a reference variable
    ‘r’ that is associated with the instance of Sample.
  • 4:24 - 4:30
    Here, both variables, ‘ob’ and ‘r’, refer to the same Sample instance.
  • 4:32 - 4:42
    Now let’s talk about objects briefly.
    An entity that has state and behavior and exists in memory is known as an object.
  • 4:43 - 4:48
    An object is considered an instance of a class, a living, breathing copy of it.
  • 4:50 - 4:53
    So how do objects and references relate?
  • 4:53 - 4:56
    Well, an object is an instance of a class.
  • 4:56 - 5:02
    A reference describes
    the memory location of where an object is stored.
  • 5:02 - 5:13
    Thus, a reference variable may be associated with a single memory
    address of where an object of the indicated type exists.
  • 5:17 - 5:27
    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.
  • 5:27 - 5:35
    If you have learned about Inheritance, then you know that a
    class can extend another which creates a parent-child relationship.
  • 5:36 - 5:40
    The child inherits properties and behavior of the parent.
  • 5:40 - 5:43
    This creates an “IS-A” relationship.
  • 5:43 - 5:48
    Child classes are not the only constructs that form IS-A relationships.
  • 5:49 - 5:54
    A class that implements an interface also forms an IS-A relationship with that interface.
  • 5:54 - 5:57
    So, what does this all mean?
  • 5:57 - 6:07
    It means that a reference variable can be the child or parent type
    of the memory location of the object that pairs with it.
  • 6:10 - 6:17
    Let’s review again, how an object, a class, and a reference relate to one another.
  • 6:17 - 6:21
    A class is a template used to instantiate objects.
  • 6:22 - 6:29
    It’s also called a ‘type’ in some circumstances,
    such as when used with a reference variable.
  • 6:29 - 6:33
    An ‘object’ is an instance of a class in memory.
  • 6:34 - 6:38
    It cannot be accessed directly; only through a ‘reference’.
  • 6:38 - 6:44
    A ‘reference variable’ is a variable that stores the reference to an object in memory.
  • 6:44 - 6:51
    In this lesson, we covered how a reference is a type of variable that stores the memory address of where an object is located.
  • 6:52 - 6:57
    It isn’t the object itself, but the doorway to use to access the object.
  • 6:58 - 7:03
    Reference variable types do not have to exactly
    match the type of the referenced object.
  • 7:04 - 7:06
    It can coincide with an IS-A relationship.
  • 7:08 - 7:09
    Thanks again for tuning in.
  • 7:10 - 7:16
    I hope that you found this lesson helpful in understanding
    reference variables and that you’ll watch more of our videos.
Title:
vimeo.com/.../380274435
Video Language:
English
Duration:
07:20

English subtitles

Revisions