 Hey everybody, it's Brian. Today we're going to be talking about strings. Here is our string, and here's our reference to the string. Now, if you remember from our previous tutorials, strings are not a primitive type. They are indeed an object. For example, you can go string. and get a list of properties. Those are static properties. We want the actual object. We will take our reference m and type m. Here's a list of methods, a list of things we can do to this string. There's a lot of them. Just kind of run through them real quick here. You can get the character at a certain position. Let's actually just do a couple of examples real quick here. We'll say m.careAt, and we'll say position3. We want to know what character is at position3. This is actually got to be in those brackets here. The character at position3 is an L. Remember this is zero base, so there's zero, one, two, three. If we increment that by one and say four, we should get the O. There's the O. You can do many other things like compare it to another string, concatenate it, which is simply joining it with another string. So you're taking two strings and making a new one with that addition at the end. Remember, strings are immutable, meaning you cannot change this. Once it's in memory, it stays that way forever until it's destroyed. Now, you might be saying, well, that's not true, because I can sit here and type m equals, oh my god. And when we run this, let's just get rid of this stuff, avoid confusion. You'll see that it prints out, oh my god. Well, we're not changing the string. We're actually creating another one. This is the first string. This is the second string. We're just pointing the reference, this m, to the second string. So you might be asking, what happened to the first string? Well, it sits out in memory until the garbage collector, something we'll cover in another topic, actually goes out and deletes it. For that reason, you should not use strings if you're going to be modifying a lot of strings. For example, the replace command. Let's just do replace. And we'll say we want to replace every l with a 9. Let's get rid of this. And you can see it says, you know, h-e-n-n-n. So what we've done is we replaced a lot of it. Now, what we've actually done is we've created another string in memory. m actually references a different string now. It's not this string modified. It's a whole new string. And that being said, if you're going to use a lot of modification, you should use what's called the string builder. You notice how there's a string buffer and a string builder. What's the difference between these two? Well, the string buffer is thread safe, and the string builder is not. Water threads. Every application you run runs in its own thread. Something we'll cover in another chapter, and I'm saying that a lot today, but there's a lot of ground we're covering here. So let's do the string builder. All you need to know is they are exactly the same, just one's thread safe and one's not. So if one's thread safe, why wouldn't we just use the thread safe one? Well, it's speed. The string buffer is a little bit slower than the string builder. And you would do sp.toString. Because remember, sp itself is not a string. And you would say sp.append. Carat. You got all these different methods you can do. Remember, when you do these, you're not creating another string. You're modifying the existing one. What the string builder does is it has a character array behind the scenes. And it increases and decreases the array dynamically. In other words, it creates a new one and destroys the old one. And it just works with the array rather than actually making a new string. So if you're going to do a lot of string modifications, you really should use the string builder or string buffer, depending on what you want to do. Now, when you run this, it says, you know, string builder.append. We're saying hello, string builder.append world. So this is just going to print out hello world. Now, like I said, the reason why this is not creating a new string in the background is because it has an array. And it's filling that array with characters. Now, you might be asking, well, didn't you just create two arrays? No, not really. Because what it does in the background is it creates a big array. We'll say, you know, a hundred characters long. Well, we haven't used a hundred characters. So when you say, you know, string builder to string, it just truncates it and only shows you what you put in there. Now, if you use, let's say, a hundred characters, it'll actually create a new array, dump the contents into the new array. But the new array will be two or three times that size. I don't know the exact numbers. And I don't believe you need to know that for the Sun Certified Java Programmer exam. So that in a nutshell is strings. I hope you found this educational and entertaining and thank you for watching.