 Hey everybody, this is Brian and this is the sixth tutorial in our Java series. Today we're going to be covering classes a little more in-depth. Also variables. So let's just start and notice how I make a comment says this is a string. Now what we're doing here is we are creating a variable that is a string type and we're giving it a name of ironically and we're setting the value to Brian. So what's happening here? Well there is a class out there called string. See how we have a class called start? There's a class out there called string and you can set its value to Brian. Now a string is kind of an array of characters, you notice how we have some letters here. Strings are immutable. What does that mean? Hmm. Think about that for a minute. Immutable. Immutable means it cannot change. Now why can't it change? That makes no sense. I mean surely we can say name equal. My cat's name is fellow. And you see there's no problem. It's not complaining. And even if we go system.out.oops. Now a little shortcut here. If you're using Eclipse and the italic sense isn't popping up, press control space. It'll pop it up. Print line. And we're just going to say name. So when we run this, what do you think it's going to print? I said that strings are immutable. It cannot change. We set it to Brian and we set it to Othello. But I said it couldn't change. Are you going to guess? So is Othello? Hmm. Am I wrong? No. Actually what's going on here is you're creating two strings in memory. You're creating this first string. Whoops. Sorry, my new gaming mouse. You're creating this first string named Brian. Now actually what you're doing is you're creating a reference to a string. This is the actual string that's sitting out in memory. This is the reference. I know all you C++ programmers out there are dying for me to say the word pointer. I'm not going to do it. I'm going to resist the urge. It's a reference. And then we're taking this reference and pointing it to another string called Othello. That's why when we get to here that reference is pointing to Othello, not to Brian. So what happens to Brian? Well, Brian just kind of hangs out in memory all sad and lonely by himself. And then eventually this big monster called garbage collection comes and eats him. What is garbage collection? We should touch on this real quick. When you create a variable, it sits out in memory and it sits there until you delete it. That's how programming languages like C++ work. With Java, you don't have to delete it. It deletes it for you in the background. So the runtime will actually see that the string of Brian is not being referenced anywhere and it'll delete it out of memory so it's not taking up room. That's called a memory leak. When you create something and leave it out in memory, C++ programs are notorious for memory leaks. They're also a security vulnerability. Excuse me. So what you need to know is that strings are immutable. They cannot be changed. You're actually creating two strings when you say this. What if we say my daughter's name Heather? How many strings are we creating here? Creating three. Brian will tell on Heather. We're not creating three variables. We have one variable, but it's pointing to three different locations depending on where you are in this code. When we run this, it's going to print out Heather. See? There we go. Now, we've said that string is a class. Sure enough, when you type the reference name dot, you see it has all these little items in the list. These help you do specific things. For example, you can say care at and get the character at a certain position, compare it to another string, combine it with another string or concat. You can see if the content is equal. For example, you can say name equal and let's just do, that makes no sense. Let's copy that, paste that in here, get ready your semicolon and we're going to see if it equals Brian. False because it's Heather. Let's delete these other two strings. True. True. Now, why wouldn't you say name equal Brian? For example, why wouldn't you say equals Brian? Well, that's because that is a string. This is a reference to a string. This string can be in multiple places in memory, meaning you can have a hundred strings in memory. Some of them say Brian, some of them don't. The Java runtime is sometimes smart enough to point different references to the same place in memory because they're the same string, but it doesn't always work that way. I know that was a bit confusing. You might want to re-watch the video and let it soak in. Once again, this is Brian. Thank you for watching. I hope you found this video educational and entertaining.