0:00:03.876,0:00:09.113 In this lecture, we’ll explore what references are [br]and how they relate to objects in Java. 0:00:09.595,0:00:13.464 All variables hold a single and only a single value. 0:00:13.900,0:00:18.122 You should be familiar with the 8 primitive types such as byte and boolean. 0:00:18.749,0:00:24.404 Each type reserves a fixed amount of memory to hold each [br]time that you create that type of variable 0:00:24.708,0:00:32.750 For example, a byte is 8 bits so each time you create [br]a byte, Java reserves 8 bits of space in memory. 0:00:33.112,0:00:39.251 A reference, however, is a type that refers to any value that is not a primitive. 0:00:39.352,0:00:48.632 It's a convenient way to label data that refers to a single instance of [br]a class, interface, array, enum or annotation. 0:00:49.102,0:00:55.751 For example, when you create a String, you create [br]an object that is a String type in memory. 0:00:56.535,0:01:00.950 Then if you create a String variable and assign it the value of the string, 0:01:00.960,0:01:05.263 you’re actually creating a reference to that String object. 0:01:05.367,0:01:08.680 In Java, when you create a variable that refers to a class, 0:01:09.176,0:01:13.848 you create a bond between that variable and the class type. 0:01:14.717,0:01:20.483 This creates a strong reference bond between [br]that object and the reference variable. 0:01:21.583,0:01:27.583 That reference doesn’t “refer” directly to the object, but to its location in memory. 0:01:28.431,0:01:32.469 This is an important aspect of reference variables that I’ll highlight more. 0:01:33.561,0:01:38.046 The variable points to the memory location and not the object itself. 0:01:39.063,0:01:44.873 The variable follows the lifecycle of the [br]object from creation to garbage collection. 0:01:45.015,0:01:48.844 You should note that objects, which are typically class types, 0:01:49.224,0:01:57.407 can have properties that are other class types and those [br]classes may have properties that refer to other classes. 0:01:57.686,0:02:06.078 For each of these references, the object created must allocate [br]memory for all of the properties (primitive or reference type). 0:02:06.756,0:02:12.767 You can imagine how complex the task would be if [br]you had to manage the memory allocation yourself. 0:02:13.766,0:02:17.799 Thankfully, Java handles memory management for you. 0:02:20.091,0:02:27.361 One of the effects of references that Java supports [br]is the ability to pass variables by value. 0:02:28.230,0:02:36.245 This means that when you pass a reference type to a method, [br]that method has access to the data and can change it. 0:02:36.519,0:02:43.004 It does not have access to the original reference [br]itself, and cannot change it to point elsewhere. 0:02:43.222,0:02:47.278 This makes Java different than languages like C++ 0:02:47.481,0:02:53.571 where pointers are supported and variables are pass-by-reference instead of pass-by-value. 0:02:53.795,0:03:02.433 In pass-by-reference, the location in memory of where the value [br]is stored is passed as opposed to a copy of the value itself. 0:03:02.747,0:03:09.065 Furthermore, in Java, pass-by-value means that [br]the value is copied to a new location. 0:03:09.559,0:03:15.870 This is why you can alter a value within a method and [br]it won’t affect the variable outside of the method. 0:03:16.094,0:03:17.906 Let’s take a look at another example. 0:03:18.703,0:03:21.324 The following is a simple declaration statement. 0:03:21.741,0:03:28.296 Here we’re creating a reference variable that will point to a type specified by ClassName. 0:03:28.466,0:03:31.517 Moving on, when you create an object from a class, 0:03:31.958,0:03:34.257 typically by using the ‘new’ keyword, 0:03:34.466,0:03:39.446 then Java allocates the amount of memory that [br]the object requires to store the object. 0:03:40.573,0:03:45.020 The code snippet here creates an object of type Sample. 0:03:45.548,0:03:52.724 It also creates a variable named ‘ob’ that refers to a Sample type 0:03:52.960,0:03:57.487 and is associated with the object that was created by the same line of code. 0:03:57.556,0:04:03.476 Remember, in Java, the variable is assigned a [br]reference to the object and not the object itself. 0:04:04.146,0:04:09.917 This reference is the address of the memory location where the object is stored. 0:04:09.958,0:04:13.630 We can also create multiple references to the same object. 0:04:14.336,0:04:23.011 For example, the following added line creates a reference variable [br]‘r’ that is associated with the instance of Sample. 0:04:23.725,0:04:30.462 Here, both variables, ‘ob’ and ‘r’, refer to the same Sample instance. 0:04:32.272,0:04:41.816 Now let’s talk about objects briefly. [br]An entity that has state and behavior and exists in memory is known as an object. 0:04:42.759,0:04:48.276 An object is considered an instance of a class, a living, breathing copy of it. 0:04:49.932,0:04:52.847 So how do objects and references relate? 0:04:52.973,0:04:56.039 Well, an object is an instance of a class. 0:04:56.382,0:05:02.037 A reference describes[br]the memory location of where an object is stored. 0:05:02.439,0:05:13.461 Thus, a reference variable may be associated with a single memory [br]address of where an object of the indicated type exists. 0:05:16.551,0:05:27.129 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. 0:05:27.203,0:05:34.929 If you have learned about Inheritance, then you know that a [br]class can extend another which creates a parent-child relationship. 0:05:35.581,0:05:40.161 The child inherits properties and behavior of the parent. 0:05:40.260,0:05:42.542 This creates an “IS-A” relationship. 0:05:42.721,0:05:48.302 Child classes are not the only constructs that form IS-A relationships. 0:05:48.753,0:05:54.218 A class that implements an interface also forms an IS-A relationship with that interface. 0:05:54.235,0:05:56.579 So, what does this all mean? 0:05:57.233,0:06:07.207 It means that a reference variable can be the child or parent type [br]of the memory location of the object that pairs with it. 0:06:10.271,0:06:16.624 Let’s review again, how an object, a class, and a reference relate to one another. 0:06:16.775,0:06:21.276 A class is a template used to instantiate objects. 0:06:21.802,0:06:28.772 It’s also called a ‘type’ in some circumstances, [br]such as when used with a reference variable. 0:06:28.820,0:06:33.349 An ‘object’ is an instance of a class in memory. 0:06:33.726,0:06:37.775 It cannot be accessed directly; only through a ‘reference’. 0:06:37.931,0:06:43.790 A ‘reference variable’ is a variable that stores the reference to an object in memory. 0:06:44.152,0:06:51.335 In this lesson, we covered how a reference is a type of variable that stores the memory address of where an object is located. 0:06:51.758,0:06:56.726 It isn’t the object itself, but the doorway to use to access the object. 0:06:58.041,0:07:02.771 Reference variable types do not have to exactly [br]match the type of the referenced object. 0:07:03.666,0:07:06.366 It can coincide with an IS-A relationship. [br] 0:07:07.663,0:07:09.214 Thanks again for tuning in. 0:07:09.537,0:07:16.494 I hope that you found this lesson helpful in understanding [br]reference variables and that you’ll watch more of our videos.