 We've looked previously at compound data in form of p-vectors and arrays and there's a separate video about strings Which also fall into the category of compound data In this video, we're going to look at some of the behaviors of compound data Let's look at something that doesn't have any compound data in it. It's a simple thing here. A gets a value and B is initialized from A We change A's value to 5 and we print out B Of course the question here is What do we get? Do we get a 4 or a 5? So let's look closely. This by the way, you notice the heading here is value semantics as opposed to reference semantics and we'll see how this plays out So we can see that A It's got a memory box there that holds its value 4 and we're going to do this assignment Be getting assigned from A's value and let's see what happens So when an assignment happens Contents of A copied over into B And so we see B has now got a 4 in it and it's a separate 4 independent of the 4 that's in A So now when we change A's value to 5 we can see that it affects A but has absolutely no effect on B that initial assignment of saying int B equals A They just got done at the time. There was no lasting connection between B and A Now let's do this again, and this time A and B will be p vectors so A starts off P vector components of 3 and 4 Then A is assigned to B Now we change the Y component of A and Then we print out the corresponding Y component of B and again our question is just the same Do we get a 4 or a 5 printed out? So again, let's have a look at what the memory diagram looks like So here it's a got a bit more to it when we just had A just being an integer because A is a p-factor it's Pointing to a memory block that contains the p-vector information So we can see it's got the 3 and the 4 that are the two parts of the p-vector That's A's value and we can see that A holds a reference. We can think of it as a memory address It holds a reference To that memory block, you know how to get from A to that memory block now we're going to do the assignment and We're going to do the same thing we did before with integers. We're going to copy what's in A to B And what is in A? It's just a reference to that memory block. And so that's what gets copied over to B So now we end up with this A and B are Both pointing to the same memory block because all that got copied across was the reference to the memory block We didn't copy the memory block itself. We didn't end up with another copy of that block Instilled it's the one p-vector memory block and A and B both reference it So now when we change the y-component of A to 5 We can see that we're also changing the p-vector memory block that B is referencing So not surprisingly B comes out to be 5 as well Now we move on to arrays And we get the same story here Here we've got an array A That is 2, 3, 4, 7 we do our assignment of A getting assigned to B We change the A2 element to be 5 instead of the 4 that it is and we print out The B2 element and again the question is do we get a 4 or a 5? We can see from the memory diagram just like with the p-vectors That there is only one block that represents the array and we just have two references to it A and B are both pointing to the same thing So B of 2 is going to be 5 as well. We're going to get 5 printed out And we're talking about assignment here But assignment is also what happens when we pass values into functions So you can see we've got three separate columns here The first column is looking where we've got the same function Well essentially some function foo in each of the columns And foo's got a parameter B And we can see down in setup We're calling foo with variable A So what's happening here with all these cases is when the function foo gets invoked We're doing an assignment of A getting assigned to B The red thing in the center of the page here This is happening for all of them So we're just doing the same assignment that we've just seen in the previous examples And so it's the same thing happening here In the first case when B is an integer We do the B++ It has no effect on A But then when B is the P vector or the array Then just like we saw before A and B point to the same thing in memory Either the same P vector or the same array And so we can see in the second and third cases We do in fact see a change in A So we get a 5 printed out instead of a 4 Just like we saw before And all the memory diagrams we saw before Apply exactly here to show exactly the same thing And one else, another thing we should cover with references Is that we've seen, we can write P vector P equals new P vector 3 4 on in one line But often we don't want to do it in one line There's some reason to do it separately But we ask, well After we've done the first line What value does P have? It does have a default value Of null We can draw a little memory diagram for it there And show P It's got a value of null inside it Null just indicates that This reference Isn't a reference to anything It's like an empty reference It's a null reference And we use the word null to refer to it So it doesn't point to anything It just doesn't, it's not a pointer at all It's just, it's an absence of pointers It's an absence of reference Just a null So as I say there in the second point A null P vector is not really a P vector Because you can't do anything to it If P is null Then P dot X doesn't mean anything Because P doesn't reference Any memory block that is a P vector So null You just can't do anything with it If you've got a null array Then you can't do anything with that either If you want to say well how long is that null array Well it's not an empty array It's just not an array It's an absence of an array And so when we say If A is null, A is an array Of type array And it's null And A dot length We can't do that We can't say oh it has a good zero length And so A dot length will give us an error