 Hi, guys. So in this video, we're going to be looking at how we can go about copying an array into another array in Java. And specifically, we're going to be looking at the difference between a reference copy and an instance copy. Now, we talked about reference copies in our video on creating arrays, which is the first video in this series on arrays. So I'll put a link to that video in the description box below. But just to recap, if we create an array such as this one, and we then create a new array that we set as equal to our first array, then both of these arrays refer to the same instance, and they will both have the same ID. And 21 here is just an example. Furthermore, if we change the value of one of the items in one of the arrays, for example, B2 equals negative 50, then that same item in data will also be changed because both B and data refer to the same instance. So this becomes negative 50. So that's reference copies. But what if we want to avoid this situation? So what if we want to create a clone of data that we can then manipulate and change without worrying about our new array B affecting our original array? So in other words, how can we create a clone of an array without having both arrays refer to the same instance? Well, what we're going to do is create an instance copy. So let's say we have here an array x, and we want to create a clone of x. Well, we know that the clone will have the same number of items as x. So we can start by creating a new array with this with length equal to x dot length, like so. And then for each item in y, we're going to simply copy the current item of x into the current item of y. This will result in x referring to an instance that has four items. And y referring to another instance that also has four items. So now if we change y to to negative 50, y two will be changed. But x two will remain the same. So that's the difference between a reference copy and an instance copy. One more thing to note, I mentioned that when we create a reference copy, both arrays have the same ID. But when we create an instance copy, both arrays will have different IDs indicating that they both refer to different instances. So now let's just pop over to Eclipse so that we can confirm this using the debugger. All right. So first of all, you can see here I've just created our array data. I've then created an array B, which is just a reference copy of data. And I've then changed the value of B to to negative 50. So let's run this through the debugger so we can see what happens step by step here. All right. So first of all, we have our array data, you can see it has an ID of 22, and those are its four values. If we move on, we have B. Now you can see that B also has an ID of 22 indicating that it is indeed a reference copy of data. And you can see it has the exact same items it refers to the exact same instance as data. Then we step through we change B to to negative 50. And you can see that because these arrays are reference copies of each other, data to is also changed to negative 50. And then at the end, we print out data to and you can see in data was negative 50. All right, now let's just look at the next example below. I'll just hide this. And we'll just pop some break points in here. So you can see here I've got our array x, just like we saw in the example earlier. We then have the array y, which we are creating as an instance copy of x. Here, we're just filling it with all of the values from x. And then in the end, we change y to to negative 50. And we'll see what happens. All right. Okay, so first of all, we have our array x ID of 22. And those are the four values. Now, when we create y straight away, you can see that y has a different ID. Because in this case, we have created an where well, we're creating an instance copy. And you can see at first, since we haven't filled y yet, it just is each item in y is set to the default value of zero. All right, then in the for loop, we just move through. You can see here step by step, we're filling y. We exit the loop. Now we're going to change y2 to negative 50. And when we do this, we can see that y2 has changed, but x2 remains the same. We print out x2 and indeed it is 20. So that's just to give you a clearer look at what we were looking at earlier. So that's it for this video. I hope that you now have a good understanding of the difference between reference copies and instance copies of arrays in Java. Thank you for watching and have a great day. Bye.