 Okay, welcome to introduction to a big computer science a unit to be using objects in this case We're looking at string objects. Okay, so again, let's look at our boilerplate to get our Java programs working. We always need class in this case. I'm gonna be using unit to be in my parentheses to start and end the class and I'm gonna make my Main method so public static void main and notice that is lowercase and string Again, this won't make sense yet But it is just something you need to memorize at this point. It will all be explained at some point So we are now looking at part 2.6, which is string objects And we're looking at concatenation some of this we looked at before Concatenation literals and more. Okay, so Might be a little bit noise in the background apologize for that. So we're gonna make a string So name one equals new string And I'm gonna say the zaffod bevel rocks affod bevel Brocks Okay, and that is a character from the hitchhiker's guide the galaxy So I mentioned before if you've never read this book stop the video now and go buy it and read it Then come back to us So let me save that into the correct folder Oops, that's not the correct folder This top intro videos and save Okay, so I have made a new string now This is one way of doing it. I do not have to do it that way Java is Kindly gives us a nice shortcut. I say string name to equals Ford prefect also a character from the hitchhiker's guide to the galaxy So what I can do at this point is I could say system dot out that print then and I'll say See let's say president. I'll just leave it there name one He is the president of the galaxy and name one plus two Two And this is this is already concatenation cat nation is just a fancy word for Combining so we took this string added it to this string Object add it to this string and added it to this string object now Just a quick aside strings are not the same as ints bullions or doubles A string is actually an object reference and that'll become important later But for now just it's enough to understand it is not actually a primitive The primitives are the built-in types This is something that's been kind of added on and a string is made up of characters and I'll leave it at that for now So let's compile that and run it and see what happens Okay, so we got Zephyde bubble rocks and Ford prefects So let us So I put that here under concatenation since we've already done that and So we've added all of these together So now I can make some more strings. I want to call this first name And this is Arthur again from the hitchhiker's guy of the galaxy string last name He is dent And I'm gonna make a new string Called full name. I'm gonna make that blank. Oops make that blank So what I can do Okay, is I could do something like this. I could say full name equals first name plus last name Okay, or I could do something like this full name It's full name equals first name plus now I want to put a space there plus last name I want to put a space and another thing I could do is full name plus equals last name Now this will be the equivalent of this. There'll be no space Okay, so let's go ahead and just take that out of there and Actually, no, that was not correct. I apologize. I forget that one Full name plus equals last name is not correct. That would just give us the last name So what I'd have to do is full name plus equals first name and then Since full name is nothing Full name plus equals last name That's assuming that We didn't have those in there Okay for me, let's just do it like this then Let's do full name Yeah, let's just do it. Let's do it with the space in the middle. So again concatenation is combining strings so let me Print this out system oops system out that print ln Full name plus full name. Let's try that and run it Okay, so we got our full name is Arthur space dent Which is what we wanted now We can also concatenate and we've seen this already. This is just a review concatenation Concatenation with the primitives. Okay, so remember our primitives were Boolean Boolean int and What was it doubles we're doing here? Okay, so let's say int the answer 42 which we've seen before so I can say system dot out that print ln the answer but the ultimate answer So that's our two life the universe Come up and everything is quote plus the answer Plus quote let's put a period again and save that and run it And let's see what we get Oops put a space there. Not good. Okay. It's a bad habit So notice again camel casing small t big a And there we go the ultimate answer to life the universe and everything is 42 if you read the novel you understand why that is Something with printing that you'll see a lot are is called an escape sequence See sequences So for example, let's say I wanted to print out the following okay Why are you so far away? She said And that's a quote so I want to put some quotation marks around. Why are you so far away? Can I watch see the colors are changing there a little bit weirdly? um That's a line from the cure and So if I try to compile this and run it We're gonna get an error So you see line 28 uh, right parentheses Expected so it's kind of You see a little career is pointing to here. So there's a problem Okay, so basically our string remember strings are contained by quotation marks Okay, but the problem here is we actually want to print a quotation mark So it kind of throws things off. So the escape character is the backward slash Let's I think that's the backward one So what that does is instead of treating this Like the final quotation mark Okay, it treats it as an actual character and it'll get printed out. So let me run that Okay, so you see now I was able to get some quotation marks Like that now the escape characters Um, there's more than just the uh the quotation mark Um, so let me uh take a look here. I think I have a list somewhere Ah, that's right. Okay, so If I wanted to say print system system dot out that print l n Something went wrong print l n So let's say I want to actually print a slash. I had to do an escape character slash you know That is a slash. I think it's a backslash Hope I didn't get those backwards. Um, that is a backslash Okay, and I can run that And there we go. I got my backslash. I can also do something along the lines of like this system dot out that print l n there is actually The new line character Use that so say I just I just printed just printed two extra blank lines Yes, let's run that and see what happens Okay, so we got so we had our print line So that went to the next line and then new line new line and then print two extra blank lines And the final one that we'll be using Out dot dot print l n is slash t for tab. Let me say A slash t b slash t c Let's just do abc's So this this lets us kind of align columns to the tab stops Okay, so let's take a look at that So you can see abc. So what that does is if I want to go ahead and do alpha beta There's no c so we'll say gamma You know run that It gives us a tab rather than the spaces. So let's just line up the text a little bit easier Um, you may find that usually you may not but uh, definitely the the new line character You'll be seeing a lot and this will be seeing if you want to print quotation marks So definitely make sure you're familiar with how to run that Um, so strings Just like with our previous example with the dog class. There are various string methods now We're not going to look at all of them There are there are quite a few but here are the ones that you definitely want to know for the ap exam And again the ap exam changes from year to year So, you know, please look at the the more recent materials, but I'm doing this in 2019 So hopefully it'll be pretty up to date for the for the time being So we can find the length of a string So I'm gonna say full name length and gth Plus full name dot length And because it's a method we have the parentheses. Don't forget the final parenthesis Which matches this parenthesis. You're always going to have an even number of parentheses Okay, unless unless in here you decide to to put A parenthesis but outside of the quotation marks, you'll always have an even number Okay, um So let me try that Okay, so full name's length is 11. So let's check that and full name was a while ago And what was the full name arthur dent? So a is one two three four five six seven Spaces is space is a character. So that's seven eight nine 10 T is 11. Okay, so we do have 11 characters, which is very very important Okay, if we did a length of a string So, uh next one up the next one is Something called substring Which is something you'll see and be using a lot of Especially on the ap exam And watch what I do here Full name substring And this is not the actual format. Let's let's do zero six for now And let's see what happens So watch what I do here. So it's full name dot substring zero comma six So and what did I do that? I told you not to do don't forget the closing parenthesis So let's see what happens here. Okay, so zero comma six is arthur so Strings are made up of characters. So characters each character has its own index So in the case of arthur dent a is zero So it always starts at zero. Don't forget that. Okay, that's a very big uh beginner gotcha always starts at zero So r is one t is two h is three u is four r is five Okay, so in this case Okay, this six is what we call non inclusive Okay, uh, I know it's hard to tell because the way we did it. There is no space there You can't say that there's a space at the end of that Okay, it actually stops with five Okay, so let me change arthur to arthur dent Uh, let me get rid of the space just to demonstrate that all right Or I'll put the up with a plus here so we can see what that looks like Okay, so so we can see that there's a character Okay, so notice the plus did not appear Because the plus is actually the sixth character So it starts on the zero th character and goes up But not to that next character So let's try that with a one and see what happens Because that'll tell us a little bit about how it actually functions So let me try one six Okay, and I run it Compile and run I should say And we still get arthur. Okay, so that plus signed it and come out So it's starting index ending index plus one and that's where it really confuses a lot of people Okay, and I honestly I find it confusing as well. Okay, so let's go moving on We also have The next one so full name and index of Okay, in this case, I'm going to do dent Okay, I wonder if you can guess full name dot index of quote dent Don't forget that second parenthesis at the end there Okay, so what this will tell us is whether or not dent exists in our string And what the index of dent is Okay, so index of dent is seven. So let's go back up and take a look at this I should tell you what when we do it this way Make it make our lives easier. I should have done this earlier Why just print out here system dot out dot print And Full name So that way we can just kind of be a little bit easier. So let's run that again So index of dent so again notice the index is seven. So let's count that out. Remember a zero R is two three four five six Seven Okay, so we got a plus zero one two three four five six. Sorry count it wrong D is seven. So zero a one two Three four five six is the plus sign and D is seven So dent is there and it is at the spot seven now watch what happens if I change this to a small d What if it doesn't exist? Let's run that and see what happens Very interesting It returns a minus one So if the string that you're searching for Does not exist or is not found inside of that other string the substring the substring is not inside the string Then you'll get minus one. Let's hold that for a second Okay, so uh, if it doesn't exist you get that minus ones Sorry for the interruption. All right, and Here's one. It's actually pretty uh, how can I put it not intuitive, but here's what people try to do Um, I'll make this its own section Uh comparisons string comparisons Okay, so What if we want to know if two strings are the same? Okay, and this is where it gets it gets a little bit complicated. Um, so I can say system dot out dot print l n full name oops full name equals last Yeah, okay, so full name equals last name Plus and here's what people want to do full name Equals you use two equal signs uh If you've done in the programming languages Last name you cannot do that with strings in java. Okay, you need to actually use the equals method and last name, okay now intuitively you should know that The last name and the full name are not the same. So let's see what happens when we do that See what we get out of that Okay, false. Okay, so that returns a boolean true or false because the full name does not equal the last name And we can also do instead of equals we can do compare to full name compare to last name use full name dot compare to last Last name And let's see what happens Okay, somebody forgot a parenthesis We're deleted one. Yeah, I was deleted a parenthesis or uh I deleted a brace Didn't notice it happens all the time So you notice the error message reached the end of file while parsing So it was looking for another one of these and it was in the wrong spot So this one should be lined up with That one this one is lined up with So that's how it actually kind of tells you here, okay This one matches the one on line 46 and this one matches the one on line 44 Which is kind of nice of Jay grasp to do that for you. Um, so let's see what comparison does. So full name compared to last name Minus three Okay, so full name is arthur dent last name is dent Okay, so the full name Comes before the last name in alphabetical order. So it's minus And by minus three A b c d So again, that's something to look into for the future, but just understand that compared to If it comes before in alphabetical order It is going to be a minus if it's the same It's going to be a zero and if it comes after it will be a positive number Okay, but those are the ones you're going to need to know Here's something that's very very important Uh, especially for the java ap exam, you'll see this type of question a lot Getting one letter from a string So we're going to use substring an index comma index plus That's why we can put a space every one So for example So system dot out dot print l n Uh full name substring six comma seven I'm going to put full name dot substring Six comma seven Don't forget that second parenthesis Let's see what we get out of that Okay, it gave me the plus Okay, now. Why did it give me the plus because the plus is the sixth character Using zero one two three four five six. Let's pull this up a little bit Okay, so zero one two three four five six is the plus Seven is the d, but it's not inclusive. Okay, so if we wanted the d We would do substring seven and eight So it's always the letter we want plus one because it's not inclusive It's not going to include the e. It's just going to stop there with the seven All right, oops Oh, I forgot to change it over here Hopefully some of you caught that before I did And that gives us the d And moving on we got a couple more things to cover here um Not necessarily related to strings, but Important nonetheless a 2.8 something called a wrapper class Okay classes integer Double Because java is object oriented there are times when we only want to use objects But there are other times I guess when we want to use the primitives So java has added something called the wrapper classes and they are classes that wrap around a primitive Okay, so what we can do is just like we've done before so There is an integer class Okay, notice it's essentially the same as the int But it's an integer class again. There are times when you'll want to use this And so for example age equals new integer And let's say I'm not 25, but I wish I were that was a good good year for me And then I could do something double height equals new double And I am 175.0 centimeters Now this is basically the same as age equals 25 Or int sorry, pardon me int age equals 25 and double height equals 175.0 But we wanted to make them objects not Primitives okay, so to get a value Oops from a wrapper class We we use int value or double value. So system dot system dot out dot print ln Okay, so age plus age dot int value and parentheses That's a method and system dot out that print ln height And height and this is a double so double value So if I run that let's see what happens Okay, so 25 and 175 very very useful Again, probably don't see the use of that now, but at some point it will come up There are a couple other special values. There's min value And there is max value So because of the way that numbers are represented in the computer's memory There are minimum values and there are maximum values if you try to go over that value you will have some trouble So for example, I'm just going to copy this for my my notes This is a lot of typing and it's just basically demonstrating something So the the integer class Integer wrapper class has min value now notice it is in capital letters with an underscore just like we saw in an earlier video With speed of light. So this is a final value. This value does not change. Okay, and that is set by Is determined by java could be could change based on your system, but Generally speaking, it'll be the same for 64 bit computers. I believe If i'm wrong, I apologize. So let's take a look at that So we've got the integer minimum value integer max value the double min value and the double max value And those cut these come in handy when you're trying to do some sorting exercises So you can see the minimum value of an integer is negative 2 billion 147 million 483,648 to 2 billion 147 483,647 notice it's off by one There's there's a reason for that, but I won't go into it here So if you want to use a higher integer value For the ap subset you'd have to use a double But there is actually something a long integer you could use but again ap doesn't cover it. So I'm going to ignore that here To keep things relatively simple. Okay, there's something called auto boxing Java very rarely does like convenient things, but this is one of them. So even though I am using an object and not a primitive It will actually Do it automatically forward. I don't have to use the methods Because java conveniently converts it for us So you can basically treat it as a primitive and you will get the the correct values out of there So age 25 right 75 and then we could also do something along the lines of when we're assigning Just like we did with strings. Oops. We saw this with strings So but we can also do this with our integers. So instead of doing integer equals new integer Integer let's say weight was new integer 80 Not anymore, but that's that's my goal So I could do it like this. I could just skip the new integer and make it 80 Okay, and that's part of the auto boxing. I could also do double bmi Equals 25.5 and instead of doing the double dot or the double in parentheses with us, that'll also work And then our final section here We want to take a look at the math class Okay, the math class is one of the java classes that comes with java and it is just a set of math Basically functions or methods that we can use to do some calculations And we're going to look at some of the ones here For the ap subset. There are a bunch of others But for now, we'll just look at a few of them. So int x equals negative 43 for example And then double y equals negative 2.0. I just pick some pick some numbers here for you and So what I can do I'm actually I'm just going to copy this because This is running a little bit long And I'll go over it line by line for you. Okay, so we have absolute value Okay Now the math class is automatically imported. We don't have to actually import that ourselves Talk about importing another time. So abs is the absolute value And then we have absolute value of y So it works on integers. It also works on doubles We can square something Using math dot power. Notice we're using the class name dot The method. This is the format for static methods, which we'll get to at some point So I want to say y to the second power So negative 2.0 to The second 0.0 power Is y squared We can also do square root Okay, now it kind of makes sense that this should be a double Notice this is a double. This is a double y is a double And then I'll talk a little bit about the random method when we see the result Okay, so we got absolute value of negative 43 is 43 absolute value of negative 2 is 2 So it works with integers. It works with doubles y squared is 4.0. So 2.0 to the power of 2.0 is 4.0 Square root square root of 25.0 is 5.0 And random this is a built-in function for making random numbers And what this does is it gives us a random number between 0 and 1 0 is inclusive 1 is not inclusive as far as I know So let me run that again every time you run it you will get a different number So we can use that information so we could do something like times 100 And let's see what happens that gives us a random number So 96.9 86.9 So this will give us a random number each time 48.1 now we could cast that using int If you remember from a previous lesson, let's try that and see what happens Okay, maybe oh I always mess it up when I do the casting I'm probably thinking python there. So don't forget the parentheses around casting So this gives us a nice little integer that gives us 57 That gives us five now you have to play around and figure out what the limits are on this Would we be able to get 100 out of there? As there's no Would we be able to get zero out of there? I believe the answer is yes on that one So depending on the ranges that you want, uh, you use a different formula, but we'll get to that some other time Okay, so that was a bit of a long one Um about a half hour But that was is some important stuff there. So we looked at string objects How to catnate them and how to create strings And then we looked at using primitives, which we had done before but it's good to you know Look at it explicitly talked about escape sequences The ones we looked at were escape with quotation marks escape with a slash New lines probably the one of the ones you'll use most commonly And the tab method the tab escape sequence. Excuse me Uh, there's various string methods The big one you're going to be using a lot of Length um substrings very very important You'll see a lot of questions on the ap exam using substring So the sooner you get used to that happier you'll be oops, and that should be a one probably messed that up earlier and And also index of to see if a string exists inside of an existing string Um comparisons we don't use two equal signs with this we use the equals method We use compare to We don't to compare if something is Where it is in alphabetical order And then we can use Substring to get one letter out Okay, so it's always index plus one Okay, we talked about wrapper classes which put an object around A primitive and that comes in handy and You know in an array list and things you'll you'll see later and How to get the value but we also talked about the minimum values and the maximum values And those would be useful when we do sorting later and we talked about auto boxing which is which is basically Java kindly converts it for you. So you don't have to worry too much about it Which is kind of weird, but uh, it's weirdly convenient Compared to a lot of things in java and then finally we talked a little bit about the math class There's there's there's plenty more going on in the math class But these are the ones that mainly you'll see In the ap computer science Uh program Okay, so stay tuned for more