 Hi, everyone. In this video, I'm going to be going over question three from the compound data to workshop So in this question here, we are looking at the difference between a shallow copy and a deep copy of an array So if that's still something that you're not quite sure on Definitely have a look over the notes on reference semantics and there are some videos here That you can watch there as well So for this question, we're just going to go line by line and have a look at what our code will look like using memory diagrams And then when we get to a print line statement, we can work out what for example source zero is at this point in time So if we're going line by line First we have an integer array called source So we're declaring creating and assigning So in memory it's going to look something like this We've got the values 10, 70, 20 and 90 So source is a reference to an integer array and it's referencing a place of memory that has an array of these four values So that's our first line and then our second line says integer array Destination is equal to source. So that means that we have a reference called destination Which refers to an integer array that's somewhere in memory and Destination is equal to source and source Which is up here source Contains a reference to a place in memory that has this array. So when I'm saying destination is equal to source I'm saying that destination is equal to this same reference in memory Where this array is stored. So this is creating a shallow copy We're not creating a new place in memory. We don't have that keyword new Which says I want a new place in memory This is a shallow copy and it's referring to the same place So when I get here and I say destination index zero is equal to 40 Well, if I have a look at Destination and I have a look at where it's pointing Index zero is over here. So it's this value here that's being updated okay, so That's being updated there and when I print The value of source zero will source zero is pointing to that same place in memory that same array at index zero So when we run this code, what's printed is the number 40 So this question is just getting you used to the idea of the difference between Shallow copy and deep copy and this is an example of a shallow copy cool So our next line says we have another integer array called yet another So that's another reference to an integer array. So I'll create that reference Down the bottom here and that is referring to an array that has these three values 80 60 50 80 60 50 So that's a reference to another place in memory and now I have destinations equal to yet another so destinations equal to Yet another which is a reference to a place in memory. So that breaks the reference between Destination and the array it was previously referencing it doesn't reference that anymore It is now equal to whatever yet another is referencing Which is this array here So now when I say destination one is equal to 100 destination Which is this reference here is now referring to this array at index one So it's this value here. That's being updated to 100 and now when we print source one well source one is up here and The item at index one is 70. So what I'll be printing is 70 I won't be printing a hundred because destination no longer refers to the same place in memory a source So that's what you should get there the first one for source zero is 40 and the source one is 70 So this is a good example of a shallow copy in question 3.2 is an example of a deep copy So in Question 3.2. We've got an integer array a which has the values one two three four five So I've got a reference called a which is existing its own place in memory and its score. It stores the memory address For this array here which has these values Our second line says integer array B is a new integer array with five spaces in memory so I've got my reference B and It's Referring to an array that has five spaces in memory. So I can create that and I'll just have them like this for now and Then I have this loop that we've talked about before this loop that we know will go through every item in an array Where i is representing the index of each item in an array So for each item we're saying bi is equal to a a dot lengths minus one minus i So let's write that out for the first few so we can get an idea of what's happening And then when we recognize the pattern We can just skip ahead and fill in the rest of our values because this doesn't seem like a regular deep copy where we're just copying The exact values across from a to b So if you have a look at when i is equal to zero we've got b zero is equal to a a dot lengths minus one Minus i which is zero Great. So b zero which is this here It's equal to a a dot lengths minus one minus zero. So let's fill in these values. We've got a a dot lengths we know is five because there are five items inside a Or five spaces in memory we said so We can replace that with five minus one minus zero So we've got five minus zero minus Sorry five minus one minus zero Which is four? So in the end we've got b zero is equal to a four So b zero is equal to a at index four, which is the value five So I can fill that in there great So if we move to our Next index which is one so we've got b one is equal to a dot lengths minus one minus one We said a dot length was five. So it's a five minus one minus one So five minus one minus one is three so we've got b one is equal to the value inside a three Which is this one here So it's value is four and we'll just do one more here just So we can make sure that we've got the correct pattern So our next index is index two So a or b two is equal to a at a dot lengths minus one minus two So it's a five minus one minus two And that will give us two So we've got b two is equal to A two So that's three So we can start to see the pattern now as our i value our index increases the place or the value inside a that we're referencing Is starting at the last index and moving backwards. So we're getting the reverse So once this is completed We'll have something like this. So b is a copy of a but a reversed So a bit different to your regular deep copy Of an array, it's not an exact copy, but it's just an adjusted copy And that's a little look into the difference between doing a shallow copy and a deep copy with compound data