 Let's finish off some of the topics in chapter 10 and let's start with scope. And here are some of the rules for scope and I'm going to use this program as an example. It's the one that we did yesterday in fact. In general, the scope of a variable starts at the line where it's declared and goes till the end of its enclosing block. So mid X has a lifetime of scope where it's accessible from line 14 and its scope ends at line 17. Now P1 and P2 are parameters. Parameters are special. Their scope actually is as though it were inside this curly brace. So the scope of P1 begins at line 13 and ends at line 17. This variable X has a lifetime as long as it's a containing object has. So the lifetime of X here, as the attribute of P1 is the same as the lifetime of P1, which again is line 13 through line 17. What about the name of the method midpoint was its scope. The answer is, it's from line 13 until the end of its enclosing block and its enclosing block starts at line 11 and goes through line 40. That means that the scope of midpoint is lines 13 through 40. The scope of Maine is lines 19 through 40. And the scope of first point here is line 23. And then to the end of its enclosing block, which would be a line 39. So those are the general rules for how you do the scope of an object. Next thing we want to talk about quickly is garbage collection. You said that attributes exist as long as an object exists, but when does an object cease to exist. And the answer is we have a new point here. We have a blank refer to it and the second line changes blank to refer to nothing at all. So here's what it looks like blank no longer refers to anything and now we have the point object which we created earlier, but it's still hanging out in memory with nobody referring to it. So as far as the program is concerned, it ceases to exist. It's still in the memory taking up space on what's called the heap. When your program runs, the system automatically looks for objects like this and deletes them so they can reuse the space for new objects and that's called garbage collection. And you do not have to do anything to make this happen. It just happens periodically as part of the Java virtual machine. And if you have a high performance application says here, you may notice a slight delay every now and then while Java reclaim space from discarded objects. There is a way to manually invoke the garbage collector saying hey, do the garbage collection now. If anybody's interested in knowing how to do it, send me an email and I'll point you to a reference on it. But it's not something that comes up very often. We've talked about mutable and immutable strings and capital I integers can't be modified. You can't get their attributes directly, and you can't provide methods that change them. All you can do is reassign them with strings that's something of a problem. And it's not a gigantic problem like a let's not blow this proportion please. So here is a little program you're going to give me a delimiter and account, and I'll return a string that has that delimiter repeated that many times. So here's my method repeat. And it gives you delimiter an account and I'm going to set my result to the empty string. And then as many times as you want I'm going to add the delimiter on to the result and reassign it. And in fact let's run this and see that it works. So the limiter is going to be let's say, oh, how about vertical bar dash vertical bar. And I want 20 of those. And there's my repeated string. By the way, there's nothing particularly new in here. The only thing that might bother people is the fact that every time I take the old value of result and concatenate the delimiter to it, I now have to create a brand new string object, and then have result refer to that string object. The next time through the loop, my old object has the delimiter attached to it which means I now have to create yet another new object, and then reassign the reference. And since I had a count of 20, that means I was doing essentially 20 reallocations, and there were at the end of this 19 strings sitting around in the heap, totally unused with nobody referring to them. And that gives a garbage collector more to collect. And there's also the overhead of doing this. Now, again, if we're working from a scarcity model back in 1995. Then yeah, we don't have a lot of memory and processors are slow and it's a big problem for small numbers of strings. This is not a gigantic problem. Maybe if you have let's say method that was being called a million times a second by a million users, then yeah okay now we've got to optimize it, but let's not overthink this. However, if there, there are occasions when the fact that strings are immutable is just sort of a pain. And what we'd rather do is we would rather have something called a string builder and that's all the goodness of a string, but it's also mutable. So let's rewrite this. And let's call this string repeater. Call it string repeater. That'll work. What we're going to do now is what we're going to do is instead of, we're still going to return a string. In the immediate we're going to say string builder result is going to be a new string builder that has nothing inside of it. Then what I'm going to say is here result dot append delimiter, because this is immutable object. I don't have to reallocate it. Okay. Yes, as it grows we might have to allocate more memory for it, but we don't have to do it every single time we add on to the result. And then because I want to string back out of this I can say result dot to string. And let's compile this. I think I've got this right we'll find out in a moment. Yep. And if I do this again, vertical bar dash vertical bar, and I want 20 of them. I still get exactly the same thing that I did before. Except now things are a little bit easier on the Java virtual machine because it doesn't have to continually allocate and deallocate a bunch of strings. Okay, well, that's all well and good but why else would you want to use a string builder turns out because string builders can do a lot of cool stuff. Remember how I said you can't change a character inside of a string. Well you can change a character inside of a string builder. So I'm going to set a new string builder with the string gold. And then I'll print it out. And then I can say set the character at location zero to s which will change gold to sold. And then we'll print that out. Let me go through all of these and I'll show it to you when it runs. Well, let me just run this and you're going to see a lot of output and I'll go into the other output later. And sure enough there was the gold and now it's become sold. Another thing that we can do is we can do a pen we've already seen that when we did our repeater. So there's nothing particularly new here. And here's a nice thing that we can do. We can insert a given string at the index specified. Um, let me go here and split my window. And let's go here and oh dear me, excuse me, I'm going to have to pause recording for a second here. So here I have a B C D E F G H I J K. And let's put the numbers in for the indexes so that we can keep track of this. Five, six, seven, eight, nine, and here's 10. And naturally I'd have a 10 for one but oh well. And what I'm going to do here is I am going to insert X Y Z at position two. If I insert X Y Z at position two. I should get X Y Z C D E F G H I J K. Let's go back here and excuse it. And sure enough. There it is. At location to next thing I can do is I can replace parts of a string. So I can replace seven through 10, which will be the D E F seven through 10 seven up through but not including. So the E F G is going to disappear. Because that's seven up through but not including 10. And I'm going to put in 12345. By the way, this had L M N O P on it. And let's see if that's what came up here. And sure enough, we have a B C D a B X Y Z C D 12345. And here's another string that I have and I can delete from position three up through but not including position six. And that will allow me to see what is deleted. So let's go and show you that. So I have a B C. So from three up through but not including six would be items three four and five. So I'll get a B C D E F. And that's what happens. Oh, I still have, by the way, some of the things that I had in string index of and last index of still work great with string builders. Okay, so I now have a B C D E F in here. Let's say SB dot. That's the system index of BC. SB dot index of last index of D E is, and if something doesn't exist, let's say index of, we should get a negative one. Let's say a string builder. I don't know if I brought that up here or not. No, I don't have it. Let me find that again here real quick. And again, if I go to the API, I can see all the wonderful things I can do. I can find out how many characters will fit in the string builder at its current size. I can get a character at. I can do comparisons exactly the same way that I could do them with strings and equals will work the same way as with strings. I can get characters from the beginning point to the ending point and put them into a destination array. That's sort of interesting. Here we've got our index of a whole bunch of different ways of doing inserts, replace length, same way that you would get it for a string. And then when I'm done with my string builder and I need to convert it back to a string, I say to string. And then I get an ordinary string, which I can use elsewhere in my program. Oh, yeah, I also have substring. So that's the string builder class. And that finishes off chapter 10. So this is almost a micro lecture, I guess. And the next mini lecture is going to start with designing classes. And I'm going to pretty much follow the book I think for part of this, although I may move things around in order of it. Stay tuned. See you later.