 Welcome to Intro to Java with an emphasis on AP Computer Science A. Today's topic is Strings, and I am Tokyo EdTech. Let's take a look at what we're going to look at today. As mentioned, Strings, we're going to learn what is a string, and string is textual information. We're going to look at string concatenation, which is combining strings, and we've seen a little bit of this before in this series. We're going to look at five string methods. These are the five that the AP emphasizes, but there are plenty more. I'll show you a little bit about those as well. And the five are the length method, substring, index of equals, and compare to. And just as an aside, I want to talk a little bit about objects and collections. This is the first time we're seeing these, for the most part. But they are important concepts that you will see throughout this course. Let's go ahead and get started. So as I mentioned, a string is a, basically it's a data type, and it is textual information. Previously, we looked at data types such as int. So int age equals one. We did a bunch of different things. If you haven't seen that video, please go back, watch it, come back. So this time we're looking at strings. So let's go ahead and declare a string or we might hear it called instantiating. So name equals quote, Tokyo EdTech. So what I've done here is I have created a string. And notice I called the class strings. Don't call it string because that'll cause problems. But we can call it strings because it's different to this. We have our public static void main method, and of course our closing. So I have just created a string. I've declared it or instantiate it, whatever you want to call it. And I can go ahead and print this. I can go system.out.println, say name, file that, execute it. And you'll see here, go edtech. Okay, pretty cool. So I can go ahead and make a few more. So I can do string, say favorite movie. Casablanca. I haven't seen it. Stop what we're doing right now and go and watch it. And we can do something like, oh, what in the day in Japanese, for example. That is sui gobi. That or if I'm able to type in Japanese on this computer, but I did copy and paste it earlier, I could do that. So I can go ahead and combine these strings. I can go ahead and say string. Let's go ahead and cat nation. And cat nation is just a fancy term for combining. So I can say string, I'll say introduction. I'll say, hello, plus me, let's quote. Today, yes. Actually, it's Thursday, but don't tell anybody. And we'll go ahead and put web net. Spell it. System.out.println. Let's see if I did that right. So far so good. Let's run it. Hello, I'm Tokyo edtech. My favorite movie is Casablanca. And today is sui gobi. You know, as I mentioned, it is Thursday. I think it might be Wednesday somewhere in the world. So I think it's still early. So basically, we can create strings and we can combine them using the plus operator. And we can run with our other apps. So one thing to take note of here, notice how string is capitalized. So if you remember with int, it was lowercase. You know, it's how it kind of changed colors here in genie. Now, depending on your editor, it might change colors. So one, for example, so it is capitalized because it's capitalized. This tells us that strings are objects. They are not the primitive types that we learned in the previous units. They are objects. I'll talk a little bit more about that as we go through. So with our string objects, we have something called methods. And these are things that I think of them as abilities, things that those objects can do. So there are five here listed that we're going to go through. So let's go ahead and do length. Okay, so I'm going to go ahead and make a new string and I'm going to call it favorite song. And my favorite song is pictures of you is by the band, the cures. If you haven't heard that yet, I would probably stop what you're doing on YouTube and listen to that. So the length method, length method, unsurprisingly, returns, that's an important concept, is it returns the length of that string. And by length, I mean the number of letters. You know, spaces are considered letters, so the number of characters that makes a little more sense to you. So if I could do this, now it's not going to do anything, but you'll say favorite song.length. So I'm going to go ahead and compile that. I'm going to run it. You can see how nothing happened here. We didn't get an error, which is a good thing. But we didn't actually see the value, the length of pictures of you. And that is why I would say it returns the value. So we return the value, but it didn't go anywhere. So I could do something like this. I could do int length equals favorite song.length. I could do system.out.println. I could say favorite song plus quote as quote plus length plus quote space letters. Oh, I'll say characters, it's more accurate, characters. So again, I'm doing a little string concatenation. I'm using an int. I'm using a string. I'm going to go ahead and compile that. Let's see it says pictures of you has 15 characters. So let's see if that's accurate. So one, two, three, four, five, seven, nine, the space is nine, 12, 13, 15. So it's accurate. Now, just as a side note, I didn't have to do this. I could have taken this and put it directly here. Do it however you like. It's probably a good habit to get into and know how you can do both of them. So as I mentioned, it returns the length. So what happens in your head? You think of, okay, when it gets to here calculates the number of characters and this is transformed. That then becomes 15. And so then it prints out. So length is pretty straightforward. There's not a lot. I can tell you about that. But this has actually come in very, very handy in future units to make sure you understand that length is the number of characters. Now substrate is kind of an interesting one. It kind of tells you what it is sub means like part or under and string is is a string. So we know what that is. So what we can do is some string. Let's us take a piece of that string. Let's cut out pieces of the string. So I'm going to go ahead and try this. I'm going to say string, say word forward, favorite song not substring 0 comma. I'm going to say system dot out dot print L N word. And let's see what we get here to compile that. And it gives us pictures. Let me explain how we got pictures out of zero and eight. So earlier we said there are 15 characters, but we need to be able to refer to each of these characters individually. Now this is the first case of where we're seeing what I would refer to as a collection. You can think of this as a collection of characters. We've got a P and I, we've got a C, we've got a T, we've got a U, we've got an R, et cetera, et cetera, et cetera. So a string is really just a combination. It's a set, an ordered set of characters. And remember if you remember from the previous unit, we do have that car or char type, whatever they call it. And that's what this is actually made of. So what we can do here is we can pull out, that's the expression I usually use, is we can pull out parts of the string because we kind of slice it up. So this zero refers to this character. It's a little confusing. Human beings, we count from one. So you want to think this is one. And the way the computer thinks this is zero. We've got zero, one, two, three, four, five, six, seven, and then eight is the space. So it's a little unclear here. So let me go ahead and get rid of the spaces and show you something. Compiled, run. Notice I still got pictures. I didn't get the O. Again, let me count through that for you. So zero, one, two, three, four, five, six, seven, eight. So substrate, it includes the zero. It does not include the eight character eight. So it includes this, does not include this. This is called inclusive, and this is called exclusive. So it's going to put the spaces back in. So we can pull out different parts of the string. So I can also do something like this. I can do string, song title, push, other cure song. So if I wanted to print P by itself, U by itself, S by itself, H by itself, I would do something like this. System.out.ln, say song title.substring. And remember this is zero. I want zero by itself. I want P by itself. So it's zero comma one because we don't include that. And go ahead and just copy and paste. So zero, one, one, two, three, sorry, two, three, four. Pilots. I always recommend like when you do copying and pasting that you always test it because it makes a mistake. So you can see here, we print it out from push, zero, one, so that was zero and one, but the one is not included. And we print it out one and two, two is not included. Three and four, four is not included. And zero, two, sorry, zero, two and three, I've had zero and one is not included, one and two is not included, two and three is not included, three and there is no four. So that's okay, we can go over. Just gotta be careful. If your numbers are wrong, if you try four and five, there is no four, zero, one, two, three, four. There is none. If you compile it, get an error. It compiles, you get all these weird things. You'll see string index out of bounds exception. So four and five is four is too big. There is no, or again, you'll be doing a lot of this if you take my, it's really important to be able to do that. Okay, so that is the substring method. Oh, just a quick thing. Actually, no, we did that, so zero and eight, so I know we did. So the other thing is, notice how I have a starting index and an ending index like plus one. I can also do the following. I could do system.out.println, say favorite song.substring. Now, in this case, I'm only going to put one number in. So let's say I wanted to print u. I can do zero, one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve. So why is that index twelve? Now, I could put twelve and fifteen because they're the length is fifteen. But if I do this, it will start here and it will go all the way to the end. So if there's a hundred characters, a thousand characters, it doesn't matter. If there's only one number, it goes all the way to the end. Go ahead and compile that and run it. You'll see pictures and it started at x12, it went all the way. I changed that to eleven. File that, run it. You'll see that'd be a little extra space because the space is at next up, index of. Index of is a very useful method and what this does is it tells us the index of the matching. So for example, now we still have our favorite song up here, just pictures. So what I can do is I can say int index equals favorite song dot index of. I can also go ahead and do of. So what it does is it looks in the string that looks for of. In this case, does of exist? Yes, it does. So zero, one, two, three, four, five, six, seven, eight, nine. The index of o is nine. So it should return nine. System.out.println index is us index, let's quote period. Let's compile it and we'll see the, pretty cool. Now let's go ahead and try and look for something that doesn't exist. Your fan, you know why I choose that. Compile it. Now what do you think it's going to return? Now we can see a lease is not in this song title. So what does it return in that case? And you can see here. So just as a reminder, let's do pictures. And I can do single letters doesn't matter, but I could do, I could do p. And the index of p, of course, is going to be zero. So if the index exists, if the substring exists, it's going to be from zero and higher. If it doesn't exist, it returns negative one. So that's how we can know, does this character exist in there? So let's say, for example, I could do something like this, I can say string vowels. Equals, A, E, I, O, U, and E, I, O, U, because capitals in our case are different to the computer. And I could say system.out.println, vowels.index of, say A. And I can say system.system.out.println, vowels.index of. We'll learn how to do this more of this later, X. So what is the index of A, and what is the index of X? So I missed A. I do that all the time. Okay, so you can see the index of A is five, then the index of X is in a one, zero to five. So using this method, we can tell whether or not something is a member or is a part of another group of something, another collection. Again, we'll be using that quite a lot in the future. So just something to note for future reference. Alrighty, now. Equals, A.Equals. I don't think I mentioned this previously, but let's say int X equals five, let's say int Y equals five. New system.out.println, X equals Y. I'm going to compile that. I'm going to run it, and you'll see where it comes up with true. And this is, we'll talk a lot more about this next unit, which is conditionals. So what this tells us, what this checks, is X equal to Y? And in this case, of course, it is because five is the same. Five. Let's try 10 and see what happens here. Now it returns false. So again, remember int is a primitive type. So with these primitive types, we're going to use equals equal. However, as I mentioned previously, strings are objects. So we're not going to use two equal signs. We're going to use the dot equals method. And no, there are cases where we do use two equal signs, but their results are not what you'd expect. We'll learn more about that later. So let's go ahead and try this. So string, let's say correct password. 1138, that's not my real password. I don't try it. Copy that. And we'll say input password. And then to compare them. So I can go ahead and make a Boolean from the last unit. So is correct. Say is correct password. Not equals input password. So does THX 1138 equal THX 1138? Of course, I think the answer is correct. So it is the same. So system.out.println. We'll say it's correct. And I should go ahead. All right, so let's go ahead and compile it. Run it. And say is the password correct? True. So this is how we find out our two strings. Do two strings have the same values? Let's say I entered the wrong password. And I'm going to run it to D2. Yeah, if you're a sci-fi fan, I don't know why I chose those names. If you're not, Google it. I'm going to compile it. I'm going to run it. And you can say is the password correct? False. Again, in the next unit, we'll learn how to change the output based on that. But if you're going to be comparing strings, we don't want to use, let's put this back to 1138. See if it works. I don't know if it's going to work. The rules in this were a little bit weird. In this case, it actually did work. There are a lot of cases where it doesn't. So please do not. So that is how we check if two strings are equal to other. So is the equals? And the last one, the last method I want to look at here is compare two. And this is really important when we're doing sorting and things like that. Let's compare a string to another. What do we mean by compare? So say int, I'm going to call this distance. I don't know what else to call it. Equals a.compare to. So think about this. We know the alphabet looks like this. So on and so forth. So compare to c. What is a? So from C. This is our starting point from C to a. How far away are they from each other? So minus one, minus two. I do system.system.out.printl. And distance. Run it. You get negative two. So common beginner mistake would be to get those backwards like positive two instead of negative two. So we reverse it. So compare to a. Where is C? So from a, one, two, it's positive two to the right is positive. Left is negative, just like a number line. And we get two. So that's pretty cool. Now we can also do, let's say we wanted to compare C to C. So what do you think the results going to be? So far is C away from C. And if you guess zero, it's the same distance, same place. You guessed right. So it is a zero. And this is something we would use with sorting. So we can also do not just single letters, but we can do actual words. This is where it actually comes in handy. So I'd say string. Singer one equals, we'll say Bob mold. It's the lead singer of Fiskardu. Sugar. Singer, singer, singer two. Bob Dylan who we need. So looking at these two. Let's go ahead and. Calculate the distance. So we're going to put singer, singer one dot. Here. Singer two. It's on a side note. Notice I did not put in here. So if I compile this. This variable distance is already defined. You only declare the type one time. After that. So let's look at this. So you can see here that B and B are the same. So you might think, okay, well it's going to return zero. Which would be wrong. But what happens is when the letters are the same, it looks at the next letter. Same, next letter, same, next letter, same. Ah, M and D. IJK. So it's going to compare the M and the D. So singer one is Bob mold compared to Bob Dylan. So starting at D. What's the distance to M? So it's one, two, three, four, five, six, seven, eight, nine. It should give us, compile that, run it. That's the distance of nine, which is pretty cool. Yeah, pretty happy with that. And then just one last final thing here. Let's take a look at this. Let's say distance equals, if we do A, compare to, what do you think is going to, now a human being, you might think, well A and A are the same thing. But as was probably mentioned previously, every letter or every symbol has a number associated with it. So if I compile this, if I write to that and run it, you'll see that the answer is 32. So compared to A, this A is 32 in distance. So if you look up at what's called an ASCII table, you'll see that like A is like 65 and blah, blah, blah. And then small A is 97. So 97 minus 65 is 30. So you have to remember that everything in the computer's memory is... So anyway, that is the basics of strings. That is the five methods that are emphasized in the AP computer science. For those of you doing AP computer science. If you're not doing AP computer science, there are tons and tons and tons of Java string methods. And some of them are really helpful. A lot of them are really helpful. You can do a lot of really cool stuff. The simple ones are two lowercase where you can convert to lowercase and convert to uppercase. And there's like quite a lot of things that you do. Again, if you're an AP computer science student, you basically just need to know the five that I listed here. But anyway, it's definitely worth playing around with those. Before we finish, I do want to come back to this last part here about concepts, objects and collections. These are two really key concepts that you'll see in Java and especially in my course and in computer science. So what I mean by an object. An object is something that has attributes and methods. Attributes and... So in this case, a string. So a string has a value. So let's see here. Well, we'll just use what we have. So we already have... I'll say we'll do favor band. So string... string... band... here. So because... You notice this is capitalized. That tells us that this is an object. We'll learn more about this later. And because it's an object, this object has methods. So this is called dot notation, where you'll see object.met. On a side note, we also saw in the last unit, math.sqrt. That is a class.met. We'll learn more about it. But we have objects and these objects have methods. We looked at five for the string object. And we can create our object this way. Now, strings are a special type of object. There is another way to create objects. So it's string. Favorit band. View string. In this case, I would put the... I can't do it twice. I have to... Comment that out so I can pile it. This is the standard method of creating an object. So it is the class. Variable... new class. What are called arguments. So in the end, this is something that we'll come back to later when we learn more about objects. But anyway. This tells us that strings are objects. They follow the same rules as the naming rules as our primitives. So remember this phrase.notation. So object.met. One, two. And then the other thing that I want to look at is collections. This is a super important concept that you'll see over and over and over. So you can think of a string as a collection of characters. So... So you can say favoritband. It equals T plus H plus T plus space, etc, etc. And so on and so forth. It is a collection of these singular items. And what we'll be doing with this is later you'll learn different collections called arrays or an array list and 2D arrays. So what we can do is we can do quite a lot of functions. We can go through and determine, for example, is this a capital letter or not? We can count the number of capital letters. So what we do with a collection is we take each thing individually and one at a time we look at each individually. So it's something that you'll see later. I'm not going to go over here, but just understand this can be thought of as a collection of characters. And we will be doing tons and tons and tons of work with collections. They're just one of those key. So objects and collections. Yeah, this stuff we will go over quite a lot later. And that's something to be aware of. But going back to coding concepts we learned what a string is and is a text type. Oh, just a quick side note. If you're coming from other computer languages, you're probably used to doing something like this using single quotation marks. You do this in Java, it will not work. Java requires double quotation marks for strings. If it's a single quotation type, it is a char or char type. That's a Java thing. So don't make that mistake. And we looked at... Sorry, I was on the wrong screen there. So if you put single quotes here, single quotes there, you'll get a compilation error. So if you're coming from another language, you might be able to do that like Python, you can do that, but in Java you cannot. And then there are five main string methods that are used in the E, length, substring, index of, equals and compare to. And then also just a quick mention that strings are objects. And you can think of them as a collection of characters. Quite a lot. Anyway, thanks for watching and keep on coding.