1 00:00:12,958 --> 00:00:16,230 This presentation is delivered by the Stanford Center for Professional 2 00:00:16,230 --> 00:00:23,230 Development. 3 00:00:24,730 --> 00:00:28,210 It's time to launch into an entirely new topic, and the entirely new topic is 4 00:00:28,210 --> 00:00:31,939 something that we refer to as enumeration. 5 00:00:31,939 --> 00:00:35,408 Enumeration is a pretty basic idea, and it comes from the word enumerate, 6 00:00:35,408 --> 00:00:37,998 as you can kind of imagine. When you enumerate something, you basically 7 00:00:37,999 --> 00:00:42,588 just have some way of referring to something through numbers. So if we wanted to 8 00:00:42,588 --> 00:00:46,159 enumerate, for example, the year that someone is in college, we might have 9 00:00:46,159 --> 00:00:50,289 freshman and sophomores and juniors and seniors - that's the enumeration of the year 10 00:00:50,289 --> 00:00:51,280 you might be. 11 00:00:51,280 --> 00:00:56,149 And so the basic idea is we just want to have some set of things that we 12 00:00:56,149 --> 00:00:59,250 enumerate or give a set of numbers to, essentially, 13 00:00:59,250 --> 00:01:02,829 and you don't necessarily want to think of it as having 14 00:01:02,829 --> 00:01:08,099 to be a set of numbers, but it's basically just some set of items that 15 00:01:08,099 --> 00:01:11,299 go together. 16 00:01:11,299 --> 00:01:12,679 17 00:01:12,680 --> 00:01:15,110 18 00:01:15,109 --> 00:01:18,969 We generally give them some numbers or some listing as a way of 19 00:01:18,969 --> 00:01:22,228 keeping them all - a way we might keep track of them. 20 00:01:22,228 --> 00:01:26,019 One way we might do this in java, for example, is just have a series of 21 00:01:26,019 --> 00:01:29,798 constants that are integers and so just to save myself a little bit of time 22 00:01:29,799 --> 00:01:31,280 in writing, 23 00:01:31,280 --> 00:01:35,049 constants. Yeah, a beautiful thing. 24 00:01:35,049 --> 00:01:39,359 So we might have some constant public static final [inaudible], and so if we're going to do 25 00:01:39,359 --> 00:01:41,340 enumeration, oftentimes we just use integers 26 00:01:41,340 --> 00:01:45,639 to refer to each of the individual items and we just count them up. So frosh 27 00:01:45,638 --> 00:01:49,598 would be one, sophomores two, juniors three, seniors four, and grad is five. That's 28 00:01:49,599 --> 00:01:51,618 just what year you might be in school. 29 00:01:51,618 --> 00:01:55,019 Oftentimes, computer scientists actually start counting from zero, but 30 00:01:55,019 --> 00:01:57,560 sometimes it actually makes sense to have these things be numbers that start 31 00:01:57,560 --> 00:02:00,978 at one. For example, if you want to know which year someone is in school or 32 00:02:00,978 --> 00:02:04,750 if you're doing months of the year, January is generally number one as opposed to 33 00:02:04,750 --> 00:02:08,659 number zero, so just to keep with common [inaudible], we might actually number 34 00:02:08,659 --> 00:02:10,228 it this way. 35 00:02:10,229 --> 00:02:13,159 Now, there's something that was introduced in one of the later versions of java, 36 00:02:13,158 --> 00:02:16,388 java 5.0, which is kind of the second to latest version, 37 00:02:16,389 --> 00:02:18,939 which is something called enumerated types, and the book talks about them 38 00:02:18,938 --> 00:02:22,019 briefly. I'm not going to talk about them here sort of for the same reasons the 39 00:02:22,019 --> 00:02:24,849 book doesn't talk about them. The book actually talks about them and then says 40 00:02:24,849 --> 00:02:28,239 the advantages versus disadvantages of doing enumerations using this thing 41 00:02:28,240 --> 00:02:29,750 called enumerated type versus 42 00:02:29,750 --> 00:02:31,590 just listing them out as integers. 43 00:02:31,590 --> 00:02:35,188 This way, at least for the time being in sort of the development of java's world, 44 00:02:35,188 --> 00:02:36,239 seems to win out. 45 00:02:36,239 --> 00:02:39,269 We're just going to teach you this way. As a matter of fact, in the old school, anything before 46 00:02:39,269 --> 00:02:42,920 java 5.0 had to do it this way, so it's just best that you see it 47 00:02:42,919 --> 00:02:46,048 this way, because most code these days is written this way and it 48 00:02:46,049 --> 00:02:49,319 probably will continue to be until at some point this new enumerated type thing 49 00:02:49,318 --> 00:02:52,818 takes off. As you see in the book, it talks about enum type. 50 00:02:52,818 --> 00:02:55,498 Don't worry about it. We're just gonna do it this way. 51 00:02:55,498 --> 00:02:59,868 The only problem that comes up with doing something like this, though, 52 00:02:59,868 --> 00:03:01,098 is that 53 00:03:01,098 --> 00:03:04,298 you want to figure out, well, how do I read in these things and display them? 54 00:03:04,299 --> 00:03:07,929 Well, these are all just integers, so if I actually want to ask someone their year in school, 55 00:03:07,929 --> 00:03:11,949 I would have to keep track of that with some ints that I might call year, and so 56 00:03:11,949 --> 00:03:18,618 I would read in an int, and I might ask the person for their year, for example. And when I 57 00:03:18,618 --> 00:03:21,859 read that in, that's all good and well. The only problem is this thing is just 58 00:03:21,859 --> 00:03:22,730 an 59 00:03:22,729 --> 00:03:25,539 integer. The user gives me some number, hopefully between one and five. I might 60 00:03:25,539 --> 00:03:28,250 want to actually have some logic in here that checks to make sure they gave me a 61 00:03:28,250 --> 00:03:29,498 number between one and five, 62 00:03:29,498 --> 00:03:31,109 ecause if they gave me a six, 63 00:03:31,109 --> 00:03:33,969 I don't know what that corresponds to. Maybe that's the dreaded other student 64 00:03:33,968 --> 00:03:35,759 category. 65 00:03:35,759 --> 00:03:38,959 I need to do something to guarantee that it's in this list one through five. 66 00:03:38,960 --> 00:03:42,120 The other problem is if I actually want to print out someone's year, 67 00:03:42,120 --> 00:03:46,509 there's no way for the computer to know that year one should print out the word 68 00:03:46,509 --> 00:03:48,968 frosh, so if I do something like 69 00:03:48,968 --> 00:03:51,060 print lin here 70 00:03:51,060 --> 00:03:55,658 and I just write out year, that's gonna write out an integer. It's gonna write out a value, whatever the 71 00:03:55,658 --> 00:03:58,128 value the user gave me, presumably between one and five. 72 00:03:58,128 --> 00:04:02,248 So if I actually want to have some nicety in there and say, oh, if the year is one, I 73 00:04:02,248 --> 00:04:05,459 actually want to write out frosh as opposed to writing out a one, 74 00:04:05,459 --> 00:04:09,890 I need to do that manually. So somewhere in my program, I need to have some 75 00:04:09,889 --> 00:04:10,808 function 76 00:04:10,808 --> 00:04:14,459 that I do a switch statement or I can do cascaded ifs and I switch, for 77 00:04:14,459 --> 00:04:19,408 example, on year. I might say, well, in the case where year happens to be 78 00:04:19,408 --> 00:04:23,259 frosh, then what I'm 79 00:04:23,259 --> 00:04:26,150 going to do is actually print out 80 00:04:26,149 --> 00:04:28,028 print lin 81 00:04:28,028 --> 00:04:31,778 frosh in quotes, because that's the only way the computer knows about it, 82 00:04:31,778 --> 00:04:33,928 and then I would have a break, 83 00:04:33,928 --> 00:04:37,158 the funky syntax from our fun, the switch statement, and then I might have some 84 00:04:37,158 --> 00:04:39,408 case over here for sophomore. 85 00:04:39,408 --> 00:04:40,269 86 00:04:40,269 --> 00:04:41,459 87 00:04:41,459 --> 00:04:45,968 I need to do this manually, and that's just the way life is in the city. 88 00:04:45,968 --> 00:04:49,509 These things are just integers. We have some enumeration. So in the program, the 89 00:04:49,509 --> 00:04:52,959 program can read a little bit more text because we can refer to frosh, sophomore, 90 00:04:52,959 --> 00:04:55,829 junior, senior and grads as constants in the program, 91 00:04:55,829 --> 00:04:58,668 but when it comes to displaying them on the screen, we need to actually write it 92 00:04:58,668 --> 00:05:02,399 out manually because the compute has no other way of knowing that a one means 93 00:05:02,399 --> 00:05:07,068 frosh. 94 00:05:07,069 --> 00:05:10,460 Well, case one is the same thing as case frosh, ecause if these are in the same 95 00:05:10,459 --> 00:05:13,060 program, frosh is just the constant one, 96 00:05:13,060 --> 00:05:16,579 and so in fact that's why we want to refer to it that way because it's more 97 00:05:16,579 --> 00:05:17,778 clear for someone reading it. 98 00:05:17,778 --> 00:05:21,139 They see oh, what do you do in the case where you're a frosh? Well, I'm 99 00:05:21,139 --> 00:05:23,720 gonna write out frosh. It's fairly straightforward enough. 100 00:05:23,720 --> 00:05:26,390 But I'm just using these constants. If someone else wants to come along and say 101 00:05:26,389 --> 00:05:27,878 you know 102 00:05:27,879 --> 00:05:31,449 what? I love frosh and they're all great, but I'm a computer scientist. I start 103 00:05:31,449 --> 00:05:34,360 counting from zero. 104 00:05:34,360 --> 00:05:37,939 It'll just change everywhere in your program as long as you've used the constants if you're 105 00:05:37,939 --> 00:05:39,538 referring to 106 00:05:39,538 --> 00:05:43,478 zero. They might just say, well, actually frosh are the coolest. They're a six. 107 00:05:43,478 --> 00:05:44,430 108 00:05:44,430 --> 00:05:46,490 That's fine. You can do whatever you want. 109 00:05:46,490 --> 00:05:48,288 The computer really doesn't care. 110 00:05:48,288 --> 00:05:50,579 Most people probably won't care, either, 111 00:05:50,579 --> 00:05:53,738 so we just start counting from zero or one most of the time. 112 00:05:53,738 --> 00:06:00,738 Any questions about the general idea of enumeration? 113 00:06:04,319 --> 00:06:07,300 Well, that's the thing. In your program, you want to set up the expectation that they're entering a 114 00:06:07,300 --> 00:06:10,400 number. If they were to enter the string 115 00:06:10,399 --> 00:06:11,269 frosh, 116 00:06:11,269 --> 00:06:14,098 because read in does error checking, it's going to say that's not the right format. 117 00:06:14,098 --> 00:06:17,050 So one thing you could actually do is rather than reading an int, you can read in 118 00:06:17,050 --> 00:06:18,968 a line, which would read in a string, 119 00:06:18,968 --> 00:06:21,718 and then you'd need to have some logic to convert that over to one. So you'd 120 00:06:21,718 --> 00:06:24,718 sort of do this process but backwards. 121 00:06:24,718 --> 00:06:27,418 That's why enumerations are something that are useful to have when you're 122 00:06:27,418 --> 00:06:30,558 writing real programs, but they can get a little bit bulky to use because you have 123 00:06:30,559 --> 00:06:35,599 to do the conversions. 124 00:06:35,598 --> 00:06:39,019 Right now, I'm just making the constants public because I might 125 00:06:39,019 --> 00:06:42,658 want to refer to them in some other class. If I have some other class, they can 126 00:06:42,658 --> 00:06:44,000 also refer to these constants. 127 00:06:44,000 --> 00:06:46,990 If I was only going to refer to these constants within this class, I'd make them 128 00:06:46,990 --> 00:06:50,088 private. 129 00:06:50,088 --> 00:06:51,618 With that said, 130 00:06:51,619 --> 00:06:53,689 it's time for something completely different. 131 00:06:53,689 --> 00:06:56,620 I know the sun isn't out much right now, but 132 00:06:56,620 --> 00:07:01,149 I sort of had this thrill where I wanted to barbecue one last time, and I figure if I 133 00:07:01,149 --> 00:07:02,449 can't barbecue outside, I'm going 134 00:07:02,449 --> 00:07:04,699 to barbecue inside. 135 00:07:04,699 --> 00:07:07,639 Now I wanted to light a fire in here, 136 00:07:07,639 --> 00:07:10,668 but as you can imagine for various reasons, that was frowned upon by the 137 00:07:10,668 --> 00:07:11,939 university. 138 00:07:11,939 --> 00:07:15,319 So I can't actually light the fire, but I can basically do everything else that 139 00:07:15,319 --> 00:07:17,589 would be involved with grilling, which doesn't really turn 140 00:07:17,589 --> 00:07:21,098 out to be that exciting when you don't have a fire. 141 00:07:21,098 --> 00:07:24,779 But the basic idea is if I leave some things on the grill for a while, 142 00:07:24,779 --> 00:07:27,058 they'll get a little hot. 143 00:07:27,059 --> 00:07:30,520 Just pretend they were on the fire for a while, okay? If you 144 00:07:30,519 --> 00:07:34,128 leave something on the grill too long, what happens to it? It gets kind of burned. 145 00:07:34,129 --> 00:07:39,408 It accumulates something. It accumulates something we refer to as char. It turns out 146 00:07:39,408 --> 00:07:42,298 as a 147 00:07:42,298 --> 00:07:43,359 148 00:07:43,360 --> 00:07:46,319 computer scientist, you get a different kind of char than other 149 00:07:46,319 --> 00:07:49,249 people get on their burgers when they're grilling them, or, as happens to be the case, 150 00:07:49,249 --> 00:07:50,859 on their ding dongs. 151 00:07:50,858 --> 00:07:54,478 Turns out they don't make ding dongs. How many people know what a ding dong 152 00:07:54,478 --> 00:07:57,158 is? Good times. If you don't, come and look at the box. It turns out they don't 153 00:07:57,158 --> 00:08:01,639 actually make ding dongs. I'm sure this is copyright Hostess 154 00:08:01,639 --> 00:08:06,019 2007. I went 155 00:08:06,019 --> 00:08:09,139 so many places last night trying to find ding dongs, but you can eventually find 156 00:08:09,139 --> 00:08:09,929 them. 157 00:08:09,928 --> 00:08:12,899 The basic idea, though, is we want to think about something that we want to refer 158 00:08:12,899 --> 00:08:15,718 to in a program that isn't always just a number. So far, we've been 159 00:08:15,718 --> 00:08:18,748 dealing with a whole bunch of numbers. We had doubles. We had integers. Life was good. 160 00:08:18,749 --> 00:08:21,719 I told you about this thing called string, but we didn't really explore it, 161 00:08:21,718 --> 00:08:24,579 and now it's time to explore it a little bit further to figure out what these 162 00:08:24,579 --> 00:08:30,528 little puppies, this little char or little characters are all about. So 163 00:08:30,528 --> 00:08:32,320 what we're gonna do 164 00:08:32,320 --> 00:08:34,490 is we're gonna explore the world 165 00:08:34,490 --> 00:08:37,500 of char. 166 00:08:37,500 --> 00:08:38,860 167 00:08:38,860 --> 00:08:41,070 There's a type 168 00:08:41,070 --> 00:08:43,060 called CHAR, 169 00:08:43,059 --> 00:08:46,389 which actually is a character, 170 00:08:46,389 --> 00:08:49,960 and so the way we refer to this type, even though I just called it char, is 171 00:08:49,960 --> 00:08:51,330 we don't call it a char. 172 00:08:51,330 --> 00:08:55,580 We call it either a char like the first syllable in character. Some people 173 00:08:55,580 --> 00:08:58,310 call it a car, even though it's not called a character because they just look at it and they're 174 00:08:58,309 --> 00:09:01,309 like oh, if it was one syllable, it would just be car. 175 00:09:01,309 --> 00:09:03,750 And some people look at it and say no, Mehran, if it was just one syllable, it would 176 00:09:03,750 --> 00:09:04,879 be char. 177 00:09:04,879 --> 00:09:08,059 Don't call it char. I will beat 178 00:09:08,059 --> 00:09:10,359 you. That was a joke, by 179 00:09:10,360 --> 00:09:13,730 the way. I'll be trying 180 00:09:13,730 --> 00:09:20,480 to explain 181 00:09:20,480 --> 00:09:23,200 that to 182 00:09:23,200 --> 00:09:26,110 the judge when I'm in jail and he's like, well, the video I saw, it didn't 183 00:09:26,110 --> 00:09:27,120 appear to be a joke. 184 00:09:27,120 --> 00:09:29,570 CHAR 185 00:09:29,570 --> 00:09:31,610 is just a character. 186 00:09:31,610 --> 00:09:34,289 We can declare variables of this type 187 00:09:34,289 --> 00:09:36,250 by just saying 188 00:09:36,250 --> 00:09:37,019 CHAR 189 00:09:37,019 --> 00:09:40,639 and I'll call it CH. I have these little CHAR CH, and 190 00:09:40,639 --> 00:09:44,159 what do I assign to these puppies? What I assign to them is something that's 191 00:09:44,159 --> 00:09:46,578 inside single quotes. That's the key. 192 00:09:46,578 --> 00:09:51,448 I can say something like CH equals little a, and I put the a inside 193 00:09:51,448 --> 00:09:52,620 single quotes. 194 00:09:52,620 --> 00:09:56,139 Not to be confused with double quotes. If I put it in double quotes, that's a string. 195 00:09:56,139 --> 00:09:58,000 That's not the same thing as a CHAR. 196 00:09:58,000 --> 00:10:01,418 So I put it inside single quotes. That indicates that it's a character. 197 00:10:01,418 --> 00:10:04,698 The other thing that differentiates a character from a string is that a 198 00:10:04,698 --> 00:10:08,548 character always is just a single character. You can't put more than one 199 00:10:08,548 --> 00:10:10,858 character there. It can't be like CH equals AB. 200 00:10:10,859 --> 00:10:13,280 That's not allowed. It's just a single character. 201 00:10:13,279 --> 00:10:16,539 The funky thing, and you might say, so Mehran, why are you telling me about this 202 00:10:16,539 --> 00:10:20,069 thing called characters right after you talk about enumeration? 203 00:10:20,070 --> 00:10:23,850 Because it turns out in the computer, the computer really doesn't know about 204 00:10:23,850 --> 00:10:27,860 characters in some deep way that you and I know about characters. 205 00:10:27,860 --> 00:10:31,050 All characters inside the computer are actually just numbers. At the end of the 206 00:10:31,049 --> 00:10:31,789 day, we can 207 00:10:31,789 --> 00:10:35,389 refer to them as these characters in their niceties for us, but to the computer, 208 00:10:35,389 --> 00:10:37,199 they're actually an enumeration. 209 00:10:37,200 --> 00:10:40,650 A happens to be some number and B happens to be some number and C happens 210 00:10:40,649 --> 00:10:41,689 to be some number, 211 00:10:41,690 --> 00:10:45,410 and so now it's time for you to know what those numbers are. 212 00:10:45,409 --> 00:10:47,839 So if we just look at the slides real briefly, 213 00:10:47,840 --> 00:10:48,920 there's this thing called 214 00:10:48,919 --> 00:10:55,919 ASCII. Anyone know what ASCII stands for? 215 00:10:56,419 --> 00:11:00,019 It's American standard code for information interchange, 216 00:11:00,019 --> 00:11:01,649 and we'll just 217 00:11:01,649 --> 00:11:03,399 do it as 218 00:11:03,399 --> 00:11:07,500 a social. The basic idea for this is that somewhere, some time ago, someone came 219 00:11:07,500 --> 00:11:08,909 along 220 00:11:08,909 --> 00:11:12,709 and said we're gonna create a standard enumeration of all the 221 00:11:12,710 --> 00:11:14,120 characters that exist, 222 00:11:14,120 --> 00:11:16,350 and so here's the first 127, 223 00:11:16,350 --> 00:11:19,430 as it turns out, character enumeration, which is 224 00:11:19,429 --> 00:11:22,500 the part that's mostly been standardized. All the rest of the stuff, there's still some 225 00:11:22,500 --> 00:11:25,539 debate about, but this one, everyone's standardized on. Now 226 00:11:25,539 --> 00:11:30,009 this little diagram that's up here is actually in octal numbers, and you're like 227 00:11:30,009 --> 00:11:33,198 octal numbers? It's base eight. Computer sciences think that way. 228 00:11:33,198 --> 00:11:36,169 We think in base two. We think in base eight and we think in 229 00:11:36,169 --> 00:11:36,729 base 230 00:11:36,730 --> 00:11:40,200 16. Most people think in base ten. Yeah, that's why most people aren't computer 231 00:11:40,200 --> 00:11:41,000 scientists. 232 00:11:41,000 --> 00:11:45,019 Here is base eight. 233 00:11:45,019 --> 00:11:46,720 234 00:11:46,720 --> 00:11:51,250 I had this roommate at Stanford 235 00:11:51,250 --> 00:11:54,958 who thought everyone should count in base 12, because base 12 was divisible 236 00:11:54,958 --> 00:11:58,948 not only by two but by also three and four, and ten wasn't, and he would just try 237 00:11:58,948 --> 00:12:01,439 to convince people of this all the time. I was like 238 00:12:01,440 --> 00:12:02,880 that is so wrong. 239 00:12:02,879 --> 00:12:06,210 Now he works for the Senate, which I 240 00:12:06,210 --> 00:12:08,480 wouldn't be surprised if we have some resolution someday. 241 00:12:08,480 --> 00:12:12,350 The United States will now count in base 12. 242 00:12:12,350 --> 00:12:13,500 But anyway, 243 00:12:13,500 --> 00:12:17,159 the basic idea here, as you can see in this, is that first of all, the character 244 00:12:17,159 --> 00:12:18,458 A 245 00:12:18,458 --> 00:12:20,049 is not the number one, 246 00:12:20,049 --> 00:12:24,079 and there's actually a distinction between the uppercase A 247 00:12:24,080 --> 00:12:27,660 and the lowercase a. Another thing that's also kind of funky is you might notice the numbers up here, 248 00:12:27,659 --> 00:12:30,389 like zero, one, two, three - the digits. 249 00:12:30,389 --> 00:12:35,419 The number zero is not the same thing as the character zero. The character zero 250 00:12:35,419 --> 00:12:39,439 just has some enumeration that's some funky value. Who knows what that is. 251 00:12:39,440 --> 00:12:43,320 It would actually be 60 in octal notation, which turns 252 00:12:43,320 --> 00:12:45,640 out to be 48. 253 00:12:45,639 --> 00:12:48,480 That's not the same thing as the number zero, so it's important to distinguish 254 00:12:48,480 --> 00:12:51,009 that the number zero that we think of as an integer 255 00:12:51,009 --> 00:12:53,639 is not the same thing 256 00:12:53,639 --> 00:12:57,689 as the character zero which we would put in single quotes, 257 00:12:57,690 --> 00:13:00,130 258 00:13:00,129 --> 00:13:03,129 and that's true for all digits. You don't need to care about 259 00:13:03,129 --> 00:13:06,600 what the actual numbers are. If you really do care about memorizing it, 260 00:13:06,600 --> 00:13:08,200 the way you can read the chart is 261 00:13:08,200 --> 00:13:10,759 the row on the chart is like the first 262 00:13:10,759 --> 00:13:14,959 two digits, and then the third digit is given by the column. So A is actually 101 in octal 263 00:13:14,958 --> 00:13:17,948 notation, if you really care about reading this, which 264 00:13:17,948 --> 00:13:20,549 makes it the value 65, but 265 00:13:20,549 --> 00:13:24,379 we don't really care. We just think of it as A 266 00:13:24,379 --> 00:13:26,580 inside single quotes. 267 00:13:26,580 --> 00:13:29,360 There's a couple things that that gives you, though, that are useful, and the 268 00:13:29,360 --> 00:13:34,529 couple things that it gives you that are actually useful 269 00:13:34,529 --> 00:13:36,279 is it guarantees 270 00:13:36,279 --> 00:13:37,549 271 00:13:37,549 --> 00:13:41,669 that the letters little a through little z are sequential. 272 00:13:41,669 --> 00:13:46,099 It guarantees that uppercase A through uppercase Z are 273 00:13:46,100 --> 00:13:50,500 also sequential, and it guarantees that the digit zero through the digits nine are 274 00:13:50,500 --> 00:13:51,720 also sequential. 275 00:13:51,720 --> 00:13:54,639 That's all you really need to care about. Other than the fact that these 276 00:13:54,639 --> 00:13:57,970 guarantees are given to you, you don't care about what the actual numbers are up 277 00:13:57,970 --> 00:13:58,970 there. 278 00:13:58,970 --> 00:14:01,220 There's a couple other things that look a little weird up here that I'll just 279 00:14:01,220 --> 00:14:04,779 kind of point out. There are some characters that are like these 280 00:14:04,779 --> 00:14:08,629 backslash characters 281 00:14:08,629 --> 00:14:10,330 282 00:14:10,330 --> 00:14:13,509 that are \c and \n. The backslashes are just special characters that are treated by the computer as a single 283 00:14:13,509 --> 00:14:16,629 character, so \n is new line. It's like a return. 284 00:14:16,629 --> 00:14:17,739 285 00:14:17,739 --> 00:14:20,850 \c is the tab character, and if you're really interested in all these 286 00:14:20,850 --> 00:14:21,190 things, 287 00:14:21,190 --> 00:14:25,020 you can look them all up in the book, but those are the only ones you need to know. 288 00:14:25,019 --> 00:14:28,579 You might wonder hey, Mehran, how do I get a single quote, because if I'm trying to put 289 00:14:28,580 --> 00:14:30,699 characters inside quotes - if I want 290 00:14:30,698 --> 00:14:32,199 little 291 00:14:32,200 --> 00:14:35,210 ch over here to be a single quote, do I write it like three quotes in a 292 00:14:35,210 --> 00:14:36,450 row? No. 293 00:14:36,450 --> 00:14:38,570 That will confuse the computer to no end. 294 00:14:38,570 --> 00:14:40,000 The way you actually do it 295 00:14:40,000 --> 00:14:44,309 is you put in a backslash and then a single quote and then another quote, and 296 00:14:44,309 --> 00:14:46,808 this is what's referred to as an escape character. 297 00:14:46,808 --> 00:14:50,639 When it sees the backslash, it says treat the next character as a little 298 00:14:50,639 --> 00:14:53,930 character and not as any special symbol that, for example, closes 299 00:14:53,929 --> 00:14:55,969 the definition of a character or something. 300 00:14:55,970 --> 00:14:59,800 So this whole thing is just the single character 301 00:14:59,799 --> 00:15:03,049 apostrophe or single quote, and it's inside single quotes, so that's how it knows 302 00:15:03,049 --> 00:15:05,729 that this is supposed to be the character single quote as opposed to 303 00:15:05,730 --> 00:15:08,700 the closing of the character. 304 00:15:08,700 --> 00:15:11,490 A couple other things that are sort of funky. What happens if you want to 305 00:15:11,490 --> 00:15:14,860 actually do a backslash? Backslash is actually 306 00:15:14,860 --> 00:15:16,430 307 00:15:16,429 --> 00:15:19,359 just backslash backslash. 308 00:15:19,360 --> 00:15:22,949 If we put that inside quotes, that would be the single character backslash. 309 00:15:22,948 --> 00:15:25,849 There's a couple others that are worthwhile. Double quote - same thing. We would 310 00:15:25,850 --> 00:15:28,709 put a backslash and then a double quote if we wanted to have the single double quote 311 00:15:28,708 --> 00:15:32,169 character. Not a huge deal, but you should just know that if you want to put 312 00:15:32,169 --> 00:15:37,329 apostrophes inside some text that you're writing or something like that. 313 00:15:37,330 --> 00:15:41,200 How do we actually get these characters? 314 00:15:41,200 --> 00:15:44,810 Rather than getting single characters, so before we talked about over 315 00:15:44,809 --> 00:15:49,189 here our friend, read 316 00:15:49,190 --> 00:15:52,850 int, which reads in a single integer, you might say hey, do we have a read 317 00:15:52,850 --> 00:15:54,870 char? Can I read a single character at a time? 318 00:15:54,870 --> 00:15:55,990 Not really. 319 00:15:55,990 --> 00:15:57,029 320 00:15:57,029 --> 00:16:02,279 What I end up doing is I read a whole line at a time from the user and then I can break 321 00:16:02,279 --> 00:16:03,509 up that line. I'm 322 00:16:03,509 --> 00:16:05,939 going to have some string, 323 00:16:05,940 --> 00:16:10,320 and that string S, I would read a line from the user, and that's going to be a 324 00:16:10,320 --> 00:16:13,440 whole bunch of characters that are stored inside that string S, 325 00:16:13,440 --> 00:16:16,030 and I can pull out individual characters 326 00:16:16,029 --> 00:16:18,250 using something called char at. 327 00:16:18,250 --> 00:16:22,970 So I can say CH equals S 328 00:16:22,970 --> 00:16:24,940 dot, and so the string 329 00:16:24,940 --> 00:16:31,400 class or the string objects have a method called char at, 330 00:16:31,399 --> 00:16:34,569 and you give it a number and it gives you the character at that position. So I could 331 00:16:34,570 --> 00:16:36,030 say char at 332 00:16:36,029 --> 00:16:40,350 and as computer scientists, we always start counting from zero, so I could say char 333 00:16:40,350 --> 00:16:41,278 at zero, 334 00:16:41,278 --> 00:16:45,090 and that's the very first character in the line the user actually entered. 335 00:16:45,090 --> 00:16:48,530 I can do a print lin on that character directly, 336 00:16:48,529 --> 00:16:50,829 and it'll just write out that first character. 337 00:16:50,830 --> 00:16:53,829 The way to think about this - let's say it read a line, S, 338 00:16:53,828 --> 00:16:56,298 and the user gave me a line and they typed in 339 00:16:56,298 --> 00:16:58,569 hello. 340 00:16:58,570 --> 00:16:59,849 Then they hit enter. 341 00:16:59,849 --> 00:17:03,969 Hello gets broken up into a series of characters where this is character zero, 342 00:17:03,969 --> 00:17:05,679 one, two, three, four, 343 00:17:05,679 --> 00:17:08,850 and that return that the user types is thrown away. It's cleaned up for me 344 00:17:08,849 --> 00:17:10,909 so it gets rid of it automatically. 345 00:17:10,910 --> 00:17:15,279 If the length of the string that the user typed in is five, 346 00:17:15,279 --> 00:17:18,899 it's actually indexed from character zero to four. So char at zero would 347 00:17:18,900 --> 00:17:20,790 give me the H character out of here. 348 00:17:20,789 --> 00:17:24,569 That's a critical thing to remember. We start counting from zero as computer scientists. 349 00:17:24,569 --> 00:17:28,149 Any questions about that? 350 00:17:28,150 --> 00:17:30,350 We have our friend over here. 351 00:17:30,349 --> 00:17:32,230 Let's see if I can actually get 352 00:17:32,230 --> 00:17:36,039 this all the way out of the way. 353 00:17:36,039 --> 00:17:38,700 We have our friend over here that tells us, 354 00:17:38,700 --> 00:17:42,460 hey, the letters are guaranteed to be sequential in the lowercase 355 00:17:42,460 --> 00:17:45,210 alphabet, in the uppercase alphabet and the characters 356 00:17:45,210 --> 00:17:47,190 of the digits zero through nine, 357 00:17:47,190 --> 00:17:48,700 so how can I use that? 358 00:17:48,700 --> 00:17:52,380 It turns out you can actually do math on characters, and you're like oh, Mehran, the whole 359 00:17:52,380 --> 00:17:55,100 point of having characters was that I wouldn't have to do math on them. Well, you can 360 00:17:55,099 --> 00:17:57,969 actually do math on characters. It's kind of fun. 361 00:17:57,970 --> 00:18:01,390 Let's say we want to convert a character to a lowercase character. 362 00:18:01,390 --> 00:18:05,520 We might have some method. It's going to return a character, which 363 00:18:05,519 --> 00:18:07,259 is a lowercase version. 364 00:18:07,259 --> 00:18:09,160 I'll call it two lower 365 00:18:09,160 --> 00:18:13,558 of whatever character is passed into it. So it's passed in some character CH, 366 00:18:13,558 --> 00:18:16,809 and what it's going to return is the lowercase version of that character. 367 00:18:16,809 --> 00:18:19,899 So the first thing I need to check is hey, did you give me an uppercase 368 00:18:19,900 --> 00:18:20,950 character? 369 00:18:20,950 --> 00:18:24,220 If you give me an exclamation point, what's the lowercase 370 00:18:24,220 --> 00:18:25,829 version of an exclamation point? 371 00:18:25,829 --> 00:18:28,009 A period. No. 372 00:18:28,009 --> 00:18:31,509 We can't lowercase punctuation. It just doesn't work. It's like what's the lowercase 373 00:18:31,509 --> 00:18:32,690 version of a period? A comma. 374 00:18:32,690 --> 00:18:35,019 What's the lowercase version of a comma? A 375 00:18:35,019 --> 00:18:36,829 space. What's the 376 00:18:36,829 --> 00:18:38,460 lowercase version of a space? 377 00:18:38,460 --> 00:18:42,940 Yeah, somewhere, it stops, and it stops here. 378 00:18:42,940 --> 00:18:45,900 379 00:18:45,900 --> 00:18:47,519 380 00:18:47,519 --> 00:18:51,679 How do we check that this thing is uppercase? Well, these are really 381 00:18:51,679 --> 00:18:54,590 numbers, and we're guaranteed that they're sequential. So we can treat them just 382 00:18:54,589 --> 00:18:57,409 like numbers. We can do operations on them like they were integers. We can 383 00:18:57,410 --> 00:18:58,808 say if CH 384 00:18:58,808 --> 00:19:02,480 is greater than or equal to uppercase A 385 00:19:02,480 --> 00:19:02,950 and 386 00:19:02,950 --> 00:19:07,190 CH is less than or equal to uppercase Z, 387 00:19:07,190 --> 00:19:10,250 if it falls into that case, then we know that it's an uppercase character ecause 388 00:19:10,250 --> 00:19:13,740 they're guaranteed to be sequential. If it doesn't fall into that range, then we 389 00:19:13,740 --> 00:19:16,870 know that it's not an uppercase character because it's outside of the sequential 390 00:19:16,869 --> 00:19:18,949 range of uppercase characters. 391 00:19:18,950 --> 00:19:21,960 So what do we do? We're going to return something, 392 00:19:21,960 --> 00:19:25,090 which is a lowercase version of that character if it's an uppercase character. 393 00:19:25,089 --> 00:19:28,449 How do we convert it to lowercase? 394 00:19:28,450 --> 00:19:30,130 Anyone 395 00:19:30,130 --> 00:19:37,130 want to venture a guess? 396 00:19:41,890 --> 00:19:45,330 We could. How do we know how much to add? 397 00:19:45,329 --> 00:19:48,079 It's on the chart, but we don't want to use the chart, because we don't want to have to 398 00:19:48,079 --> 00:19:54,619 remember what's in the chart. 399 00:19:54,619 --> 00:19:58,639 Yes. For that, A. 400 00:19:58,640 --> 00:20:01,750 It's the difference between the uppercase and the lowercase character. So 401 00:20:01,750 --> 00:20:03,230 think about it this way. 402 00:20:03,230 --> 00:20:06,690 First thing we want to do is figure out - I'm going to explain this a slightly different 403 00:20:06,690 --> 00:20:10,230 way, which is first we want to figure out which character of the uppercase 404 00:20:10,230 --> 00:20:11,329 alphabet you typed. 405 00:20:11,329 --> 00:20:16,279 So we take the CH you gave us and subtract from an uppercase A. 406 00:20:16,279 --> 00:20:19,609 If we do that, if CH was uppercase A, we get zero, which is the first 407 00:20:19,609 --> 00:20:23,189 letter of the alphabet. If it's B, we get one. If it's C, we get two. 408 00:20:23,190 --> 00:20:27,140 This will give us in some sense the number of the letter in the uppercase 409 00:20:27,140 --> 00:20:28,110 alphabet. 410 00:20:28,109 --> 00:20:29,998 Once we get that number, we need to 411 00:20:29,999 --> 00:20:33,379 figure out what the corresponding letter is in the lowercase alphabet. So 412 00:20:33,378 --> 00:20:35,869 translate according to that chart into the lowercase alphabet. 413 00:20:35,869 --> 00:20:39,199 I don't want to memorize that chart. How do I know the starting position of the 414 00:20:39,200 --> 00:20:42,150 lowercase alphabet? 415 00:20:42,150 --> 00:20:46,960 It's lowercase a, so I just add lowercase a, which is the same thing, 416 00:20:46,960 --> 00:20:49,650 basically, as taking the difference between the lowercase alphabet and the 417 00:20:49,650 --> 00:20:50,759 uppercase alphabet. 418 00:20:50,759 --> 00:20:54,460 But if I do that, basically this portion tells me figure out which letter, 419 00:20:54,460 --> 00:20:58,860 in terms of the index of that letter, and then offset it by the appropriate amount 420 00:20:58,859 --> 00:21:02,869 to get the corresponding letter in the lowercase alphabet, and that's what I return. 421 00:21:02,869 --> 00:21:05,889 Otherwise, what happens if I'm not in the uppercase alphabet? 422 00:21:05,890 --> 00:21:08,740 I just say hey, I'm going to give you back - you wanted lowercase version of 423 00:21:08,740 --> 00:21:12,170 exclamation point. Not happening. You get exclamation point back. I have to 424 00:21:12,170 --> 00:21:15,930 still give back a character. I can't say I'm gonna give you back 425 00:21:15,930 --> 00:21:17,470 this big 426 00:21:17,470 --> 00:21:20,610 giant goose egg, and it's like sorry, thanks for playing. I can't do that because 427 00:21:20,609 --> 00:21:21,990 goose egg is not a character. 428 00:21:21,990 --> 00:21:26,220 I just return CH 429 00:21:26,220 --> 00:21:33,220 unchanged. We can do a little bit of math. 430 00:21:35,940 --> 00:21:38,110 It doesn't matter. I just get 431 00:21:38,109 --> 00:21:39,029 the offset, 432 00:21:39,029 --> 00:21:41,420 but I'll still give you a B for that. 433 00:21:41,420 --> 00:21:44,778 The other 434 00:21:44,778 --> 00:21:47,759 thing we also want to think about is we can not only do this kind of 435 00:21:47,759 --> 00:21:49,829 math on characters, we can even 436 00:21:49,829 --> 00:21:52,689 count through characters. 437 00:21:52,690 --> 00:21:55,820 You remember in the days of Yore 438 00:21:55,819 --> 00:22:00,379 when you learned your little alphabet, like the little alphabet song? 439 00:22:00,380 --> 00:22:00,630 440 00:22:00,589 --> 00:22:04,428 I thought for five years of my life L M N O P was one letter. 441 00:22:04,429 --> 00:22:06,509 Totally screwed me 442 00:22:06,509 --> 00:22:08,710 up. That's just 443 00:22:08,710 --> 00:22:10,509 the American educational system. 444 00:22:10,509 --> 00:22:14,648 If we have some character like CH, I can actually have a four 445 00:22:14,648 --> 00:22:18,449 loop counting through the characters. I can say start at uppercase A 446 00:22:18,450 --> 00:22:21,090 as long as the character is 447 00:22:21,089 --> 00:22:24,039 less than or equal to uppercase Z 448 00:22:24,039 --> 00:22:28,379 CH++. I treat it just like an integer, and now what I have is a four 449 00:22:28,380 --> 00:22:31,870 loop that's index is a letter that counts from uppercase A through 450 00:22:31,869 --> 00:22:32,619 uppercase Z. 451 00:22:32,619 --> 00:22:34,929 I can do this lowercase. I can do it with 452 00:22:34,930 --> 00:22:36,460 the digits from zero to nine, 453 00:22:36,460 --> 00:22:39,700 but I can treat these things basically just the same way I treated integers, because 454 00:22:39,700 --> 00:22:44,049 underneath the hood, it's an enumeration. They really are 455 00:22:44,049 --> 00:22:46,869 integers. Other things to keep in mind 456 00:22:46,869 --> 00:22:48,559 is characters are 457 00:22:48,559 --> 00:22:52,980 a primitive type. This type CHAR is like an integer or like a 458 00:22:52,980 --> 00:22:56,930 double. It's referred to as a primitive type. It's not a class. You don't get 459 00:22:56,930 --> 00:22:58,130 objects of type CHAR. 460 00:22:58,130 --> 00:23:01,210 You actually get these low level things called [inaudible] the same way you got integers, 461 00:23:01,210 --> 00:23:05,048 which means when you pass characters, you get a copy of the character. It also 462 00:23:05,048 --> 00:23:08,538 means that a character variable like CH is not an object, so it doesn't 463 00:23:08,538 --> 00:23:11,819 receive messages. It can't get messages. But 464 00:23:11,819 --> 00:23:14,379 it turns out there's a bunch of operations we'd actually like to be able 465 00:23:14,380 --> 00:23:16,400 to do on characters, 466 00:23:16,400 --> 00:23:19,390 and so java gives us this funky class 467 00:23:19,390 --> 00:23:20,810 called 468 00:23:20,809 --> 00:23:22,500 character, 469 00:23:22,500 --> 00:23:26,529 and character actually has a bunch of static methods. They're 470 00:23:26,529 --> 00:23:28,410 methods that you can apply to characters 471 00:23:28,410 --> 00:23:31,808 but you don't call them in the traditional sense of sending a message. 472 00:23:31,808 --> 00:23:34,680 This is how they work. If I have 473 00:23:34,680 --> 00:23:35,929 CHAR CH, 474 00:23:35,929 --> 00:23:40,409 I can say CH equals - I give it the name of the class instead of the 475 00:23:40,409 --> 00:23:41,790 name of the object. 476 00:23:41,789 --> 00:23:44,759 So I say character got 477 00:23:44,759 --> 00:23:48,309 and there is, for example, something called two uppercase 478 00:23:48,309 --> 00:23:52,659 that gets passed in some character and returns to me the uppercase version of 479 00:23:52,660 --> 00:23:55,900 that character. Just like we wrote two lower here, you can imagine you could do a 480 00:23:55,900 --> 00:23:58,370 very similar kind of thing with slightly different math 481 00:23:58,369 --> 00:24:00,000 to create an uppercase version. 482 00:24:00,000 --> 00:24:02,359 It does that for you. 483 00:24:02,359 --> 00:24:05,099 This method is part of a class called character, 484 00:24:05,099 --> 00:24:08,858 but it is what we refer to as a static method. 485 00:24:08,858 --> 00:24:10,819 It's not a method that 486 00:24:10,819 --> 00:24:13,480 every object in that class has. 487 00:24:13,480 --> 00:24:17,210 It's a method that just the class has, so the way we refer to it is we give it the 488 00:24:17,210 --> 00:24:20,429 class name and then the method name, because this CHAR thing is 489 00:24:20,429 --> 00:24:22,419 not an object. 490 00:24:22,419 --> 00:24:25,710 It turns out there actually is a class called character that you ca create 491 00:24:25,710 --> 00:24:26,390 objects of, but 492 00:24:26,390 --> 00:24:29,280 we're not gonna get into that. We're just gonna use these little things called 493 00:24:29,279 --> 00:24:30,599 CHARs, which are our friend. 494 00:24:30,599 --> 00:24:34,439 There's a bunch of useful methods in this character class, and I'll just go over a few of them 495 00:24:34,440 --> 00:24:36,919 real briefly. 496 00:24:36,919 --> 00:24:37,970 Real quickly, 497 00:24:37,970 --> 00:24:41,819 you can check to see if a character is a digit, is a letter or is letter or 498 00:24:41,819 --> 00:24:45,089 digit. That's good for validating some inputs if you want the user to type in 499 00:24:45,089 --> 00:24:48,359 something in particular. These taken characters are Booleans. 500 00:24:48,359 --> 00:24:54,319 Question? 501 00:24:54,319 --> 00:24:57,089 We're not gonna worry about letters from different alphabets for now, 502 00:24:57,089 --> 00:25:01,139 but in fact, they're all supported. The numbers just get bigger from what they 503 00:25:01,140 --> 00:25:04,050 could be. Though I only showed you the first 127 letters, it turns 504 00:25:04,049 --> 00:25:04,589 out that 505 00:25:04,589 --> 00:25:07,740 the standard that java uses actually supports over one million characters, and so 506 00:25:07,740 --> 00:25:10,599 you can have all kinds of stuff like Chinese and Arabic and all that. In 507 00:25:10,599 --> 00:25:13,549 508 00:25:13,549 --> 00:25:16,849 terms of other things you can do with characters, 509 00:25:16,849 --> 00:25:19,779 you can check to see if a character is lowercase or uppercase, and all these 510 00:25:19,779 --> 00:25:20,619 at this 511 00:25:20,619 --> 00:25:24,369 point are trivial. I know how to write these myself. In fact, you do, and 512 00:25:24,369 --> 00:25:25,339 you could, but 513 00:25:25,339 --> 00:25:29,049 they're so easy to write that they're just kind of given to you for free as well. 514 00:25:29,049 --> 00:25:33,119 A couple others like [inaudible] white space. That was actually convenient. It checks to see if a 515 00:25:33,119 --> 00:25:36,699 character is either a tab or a space or a new line. 516 00:25:36,700 --> 00:25:43,330 Question? 517 00:25:43,329 --> 00:25:44,649 It returns a Boolean. 518 00:25:44,650 --> 00:25:47,950 It just says the thing that you gave me is a letter or a digit. For example, 519 00:25:47,950 --> 00:25:49,069 here is the digit 2. 520 00:25:49,069 --> 00:25:51,259 So it would return true for that, 521 00:25:51,259 --> 00:25:58,259 except it might hit someone else along the way. 522 00:25:59,940 --> 00:26:03,120 Yeah, if it's either a letter or a digit. It doesn't let you know which one. It's just 523 00:26:03,119 --> 00:26:05,579 if it was a letter or a digit. 524 00:26:05,579 --> 00:26:08,879 It's not punctuation is kind of the idea. 525 00:26:08,880 --> 00:26:12,760 And then finally, two lowercase and two uppercase, you actually just wrote two 526 00:26:12,759 --> 00:26:15,440 lower yourself, but you also get those. These are all in the book, so you don't need 527 00:26:15,440 --> 00:26:19,580 to worry about copying them down, and the slides will be posted on the website. 528 00:26:19,579 --> 00:26:21,220 So 529 00:26:21,220 --> 00:26:23,789 characters are kind of fun because we can do math on characters, and it's kind of like 530 00:26:23,789 --> 00:26:26,990 oh, that's sort of interesting, but it gets more fun when you can put a whole 531 00:26:26,990 --> 00:26:28,740 sequence of characters together 532 00:26:28,740 --> 00:26:31,740 into our friend, the string. 533 00:26:31,740 --> 00:26:33,140 It's time 534 00:26:33,140 --> 00:26:35,830 to bring the string back out. 535 00:26:35,829 --> 00:26:37,779 536 00:26:37,779 --> 00:26:39,460 Time to polish off the string. 537 00:26:39,460 --> 00:26:42,519 I actually wanted to see if I can do this just for laughs. I want to see how far it 538 00:26:42,519 --> 00:26:44,259 will go. 539 00:26:44,259 --> 00:26:48,450 540 00:26:48,450 --> 00:26:55,340 541 00:26:55,339 --> 00:26:56,339 All 542 00:26:56,339 --> 00:26:58,490 right. 543 00:26:58,490 --> 00:26:59,220 Strings - 544 00:26:59,220 --> 00:27:01,750 I'm gonna try that again by the end of class. 545 00:27:01,750 --> 00:27:03,230 546 00:27:03,230 --> 00:27:05,250 547 00:27:05,250 --> 00:27:06,700 Our friend, 548 00:27:06,700 --> 00:27:07,519 549 00:27:07,519 --> 00:27:10,009 the string class. 550 00:27:10,009 --> 00:27:13,509 Strings, in fact, are a class and there are objects associated with strings, as you 551 00:27:13,509 --> 00:27:14,789 sort of saw before. 552 00:27:14,789 --> 00:27:17,618 So we could have, for example, a string 553 00:27:17,618 --> 00:27:21,658 that we declare called STR, and we might read that in from the user using 554 00:27:21,659 --> 00:27:25,360 our friend read line that you just saw an example of previously, and we pulled out 555 00:27:25,359 --> 00:27:26,319 individual characters. 556 00:27:26,319 --> 00:27:28,649 Here, we're going to read a whole line. 557 00:27:28,650 --> 00:27:31,169 It turns out there's a bunch of things that we would like to be 558 00:27:31,169 --> 00:27:33,590 able to do on strings. 559 00:27:33,589 --> 00:27:35,859 The key concept with strings is 560 00:27:35,859 --> 00:27:40,449 a string is what we refer to as immutable, 561 00:27:40,450 --> 00:27:44,269 which means that a string cannot be changed in place. 562 00:27:44,269 --> 00:27:49,378 If the user types in hello and I say, yeah, hello is kind of fun, but I really 563 00:27:49,378 --> 00:27:50,740 like Jell-O, 564 00:27:50,740 --> 00:27:54,118 and so I want to get rid of the H and replace it by a J, 565 00:27:54,118 --> 00:27:58,148 I cannot do that in a string. So if you worked with other languages where you can 566 00:27:58,148 --> 00:28:00,729 directly change the context of the string, 567 00:28:00,730 --> 00:28:01,259 568 00:28:01,259 --> 00:28:03,658 not allowed in java. They are immutable, 569 00:28:03,659 --> 00:28:06,930 which means if I want to go from hello to Jell-O, 570 00:28:06,930 --> 00:28:13,590 what I need to do is somehow create a new string that contains Jell-O. 571 00:28:13,589 --> 00:28:17,500 I might take some portion of this string and add some new character 572 00:28:17,500 --> 00:28:19,868 to it, and I'll show you some examples of how we might do that, 573 00:28:19,868 --> 00:28:23,709 but the key concept is strings are immutable. They cannot be changed in place. 574 00:28:23,710 --> 00:28:27,409 When we do operations on strings, what we're actually gonna do is create new strings 575 00:28:27,409 --> 00:28:30,120 that are modifications of the previous strings we had, 576 00:28:30,119 --> 00:28:34,169 but we're still creating new strings. 577 00:28:34,170 --> 00:28:37,250 I'll show you some examples of methods for strings in just a second, but I want 578 00:28:37,250 --> 00:28:41,308 to contrast between strings and characters just real briefly before we 579 00:28:41,308 --> 00:28:44,798 jettison our friend the character and deal all with strings. 580 00:28:44,798 --> 00:28:45,798 So CHAR 581 00:28:45,798 --> 00:28:48,450 as we talked about is a primitive type. 582 00:28:48,450 --> 00:28:52,210 It's not a class versus string 583 00:28:52,210 --> 00:28:56,720 is a class, so we have objects of the string type. 584 00:28:56,720 --> 00:28:59,009 If I were to have CHAR 585 00:28:59,009 --> 00:28:59,538 CH 586 00:28:59,538 --> 00:29:01,869 and I were to have string 587 00:29:01,869 --> 00:29:05,568 STR and I want to do something like converting to uppercase, 588 00:29:05,568 --> 00:29:08,129 for CH I have to call 589 00:29:08,130 --> 00:29:10,280 character dot two uppercase CH. I don't actually pass this message 590 00:29:10,279 --> 00:29:12,619 to CH, ecause CH is not a class. It's a 591 00:29:12,619 --> 00:29:19,619 primitive type. I need to 592 00:29:21,589 --> 00:29:25,529 call this funky class and say hey, class character, let me use your two 593 00:29:25,529 --> 00:29:27,980 uppercase method to make this character. 594 00:29:27,980 --> 00:29:32,669 In string, there actually is a string operation two uppercase, 595 00:29:32,669 --> 00:29:35,629 and the way that works is I could say string equals 596 00:29:35,628 --> 00:29:40,219 STR, so the receiver of the message is actually an object. 597 00:29:40,220 --> 00:29:43,730 It's this string thing. I'm not writing out the whole word string. 598 00:29:43,730 --> 00:29:45,930 I'm saying STR dot 599 00:29:45,930 --> 00:29:47,200 two 600 00:29:47,200 --> 00:29:48,519 uppercase, 601 00:29:48,519 --> 00:29:51,370 and I pass it in STR. 602 00:29:51,369 --> 00:29:54,219 Now here, things might look a little bit funky. The first thing that looks 603 00:29:54,220 --> 00:29:57,210 funky is you say hey, Mehran, if you're telling the string 604 00:29:57,210 --> 00:29:59,490 to convert itself to uppercase, 605 00:29:59,490 --> 00:30:03,128 why do you need to assign it back to itself? Why can't you just say hey, string, 606 00:30:03,128 --> 00:30:04,668 convert yourself to uppercase? 607 00:30:04,669 --> 00:30:09,640 Why wouldn't that make sense in java's model? eCause 608 00:30:09,640 --> 00:30:12,110 strings are immutable. That's just beautiful. 609 00:30:12,109 --> 00:30:14,219 610 00:30:14,220 --> 00:30:16,809 611 00:30:16,809 --> 00:30:20,389 The basic idea is strings are immutable, so I can't tell a string to 612 00:30:20,390 --> 00:30:22,549 change itself to an uppercase version. 613 00:30:22,548 --> 00:30:24,660 I can say hey, string, 614 00:30:24,660 --> 00:30:28,150 create an uppercase version of yourself. You haven't changed yourself and give me 615 00:30:28,150 --> 00:30:29,769 that uppercase version. So 616 00:30:29,769 --> 00:30:35,859 it says 617 00:30:35,859 --> 00:30:39,089 oh, okay. I say string, create an uppercase version of yourself. It 618 00:30:39,089 --> 00:30:41,899 says here you go. Here's the uppercase version, and it's all excited. 619 00:30:41,900 --> 00:30:45,360 It's like oh, here's an uppercase version of me. It's gonna be like me and my 620 00:30:45,359 --> 00:30:46,209 uppercase version, 621 00:30:46,210 --> 00:30:49,240 and what do you do? You just say yeah, it's not you anymore. I'm just slamming 622 00:30:49,240 --> 00:30:51,829 it over you with your uppercase version. 623 00:30:51,829 --> 00:30:54,668 So I've replaced the old string with it's uppercase version, 624 00:30:54,669 --> 00:30:57,809 but for a brief, gleaming moment in time, 625 00:30:57,808 --> 00:31:02,200 I had this thing was a separate string until I signed it over here. I wasn't 626 00:31:02,200 --> 00:31:06,210 actually overwriting or changing 627 00:31:06,210 --> 00:31:09,110 STR. If I assigned it to some other string like string two, I would actually have the 628 00:31:09,109 --> 00:31:14,689 original, unchanged from the string two, which would be a different kind of thing. 629 00:31:14,690 --> 00:31:18,029 A bunch of things you can also do on strings - [inaudible] you've 630 00:31:18,029 --> 00:31:21,069 actually seen before. I'll show you one more example of [inaudible], but you've 631 00:31:21,069 --> 00:31:23,720 been doing it this whole time. 632 00:31:23,720 --> 00:31:27,539 I have string S1, which will be CS106. 633 00:31:27,539 --> 00:31:32,779 I have string two. It's okay for a string to be a single character 634 00:31:32,779 --> 00:31:34,039 A. 635 00:31:34,039 --> 00:31:37,859 It is distinguished from the character A by having double quotes around it. So a 636 00:31:37,859 --> 00:31:41,769 character always one character. A string can be one or more characters. As a matter of fact, it can 637 00:31:41,769 --> 00:31:44,700 be zero characters. It can have double quote, double quote, which is the empty 638 00:31:44,700 --> 00:31:45,529 string. 639 00:31:45,529 --> 00:31:48,049 So I can create a new string, 640 00:31:48,049 --> 00:31:48,809 string 641 00:31:48,809 --> 00:31:53,678 S3 by just [inaudible] using the plus operation. So I can say 642 00:31:53,679 --> 00:31:55,720 I got 643 00:31:55,720 --> 00:31:57,000 an 644 00:31:57,000 --> 00:31:58,680 plus 645 00:31:58,680 --> 00:32:00,480 string two 646 00:32:00,480 --> 00:32:01,640 plus 647 00:32:01,640 --> 00:32:02,820 N 648 00:32:02,819 --> 00:32:04,109 plus 649 00:32:04,109 --> 00:32:07,509 string one plus string two. 650 00:32:07,509 --> 00:32:09,480 What is that gonna give me? 651 00:32:09,480 --> 00:32:11,288 I got an 652 00:32:11,288 --> 00:32:12,670 A 653 00:32:12,670 --> 00:32:13,410 in 654 00:32:13,410 --> 00:32:15,650 CS106 A, 655 00:32:15,650 --> 00:32:19,030 and it just [inaudible] them all together. 656 00:32:19,029 --> 00:32:25,809 Be happy it's not like CS106 657 00:32:25,809 --> 00:32:28,970 F. I don't know what's going on in 658 00:32:28,970 --> 00:32:32,480 CS106 659 00:32:32,480 --> 00:32:36,019 X. We're just dealing with the A here. It's amazing 660 00:32:36,019 --> 00:32:38,740 how small the difference is between an A and an 661 00:32:38,740 --> 00:32:42,309 F. Just kidding. It's actually a huge, wide gulf. I'm sure all of you will get As. 662 00:32:42,309 --> 00:32:43,509 [Inaudible]. 663 00:32:43,509 --> 00:32:45,849 664 00:32:45,849 --> 00:32:48,689 Another thing you might want to do with strings is say hey, the 665 00:32:48,690 --> 00:32:52,740 user's giving me some particular string like I do a read line over here. 666 00:32:52,740 --> 00:32:55,089 Can I actually 667 00:32:55,089 --> 00:32:58,019 check to see if that string is equal to something? So one thing I might be 668 00:32:58,019 --> 00:33:03,299 inclined to do is say hey, is that string STR equal equal to some other string, 669 00:33:03,299 --> 00:33:06,099 like maybe I had some other string up here, S2. And 670 00:33:06,099 --> 00:33:10,319 I might say is that equal to S2? 671 00:33:10,319 --> 00:33:14,000 Turns out, this looks like a perfectly reasonable thing to say. 672 00:33:14,000 --> 00:33:17,979 Bad times. This is not how we check for equality with strings. 673 00:33:17,979 --> 00:33:21,419 It's actually valid syntax. You will not get a complier error 674 00:33:21,419 --> 00:33:22,309 when you do this. 675 00:33:22,308 --> 00:33:27,329 What this is actually doing is saying are STR and S2 the same object? 676 00:33:27,329 --> 00:33:31,519 Not do they contain the same characters but are they the same actual object, 677 00:33:31,519 --> 00:33:33,619 which most of the time when you're comparing two strings, 678 00:33:33,619 --> 00:33:37,159 they're not the same actual object. They are two different objects that may 679 00:33:37,160 --> 00:33:39,070 contain the same characters. 680 00:33:39,069 --> 00:33:41,269 So how do we actually test for equality? 681 00:33:41,269 --> 00:33:44,779 Well, there is a little method we use that is a method of the string class 682 00:33:44,779 --> 00:33:47,569 called equals, and the way we write it is kind of funky. 683 00:33:47,569 --> 00:33:50,279 We take one of the strings and we say S1 684 00:33:50,279 --> 00:33:56,170 dot equals and then we give it as a parameter the other string, STR. 685 00:33:56,170 --> 00:34:00,769 So what it says is it basically sends the equals message to string one and says 686 00:34:00,769 --> 00:34:02,359 hey, string one? 687 00:34:02,359 --> 00:34:06,029 You know what your own contents are. Are they happen to be equal to the contents 688 00:34:06,029 --> 00:34:08,480 of this other string I'm passing you as a parameter? 689 00:34:08,480 --> 00:34:12,760 And this returns true or false if these two strings contain the same characters. It 690 00:34:12,760 --> 00:34:14,389 is case sensitive. 691 00:34:14,389 --> 00:34:18,019 There's another version of it that's called equals ignore case that is 692 00:34:18,018 --> 00:34:20,878 case insensitive, but this version is case sensitive. The other one you can 693 00:34:20,878 --> 00:34:23,368 also see in the book. It's not a big deal. 694 00:34:23,369 --> 00:34:27,789 So these are some very useful methods. I'll show you some more useful methods of strings 695 00:34:27,789 --> 00:34:29,249 very briefly. 696 00:34:29,248 --> 00:34:31,828 We'll talk about some of these in more detail next time, but I want you to 697 00:34:31,829 --> 00:34:33,769 see them briefly right now. The 698 00:34:33,768 --> 00:34:35,349 three made its way back up 699 00:34:35,349 --> 00:34:37,278 700 00:34:37,278 --> 00:34:39,028 here. The 701 00:34:39,028 --> 00:34:41,579 string class 702 00:34:41,579 --> 00:34:44,419 has some methods like the length of a string. How many characters does that 703 00:34:44,418 --> 00:34:47,588 string contain? So for a particular string like STR, you take STR 704 00:34:47,588 --> 00:34:50,708 dot length and it would give you back as an integer the number of characters in 705 00:34:50,708 --> 00:34:51,908 the string. 706 00:34:51,909 --> 00:34:56,349 CHAR at you already saw. You give it some index starting at zero and it gives 707 00:34:56,349 --> 00:34:59,869 you back the character at that particular index. 708 00:34:59,869 --> 00:35:02,469 There's a couple things you can do - substring, where you can pull out a 709 00:35:02,469 --> 00:35:05,329 portion of the string. Remember our friend hello? 710 00:35:05,329 --> 00:35:11,059 At some point, we want to say oh, just slide this over. 711 00:35:11,059 --> 00:35:13,689 Where we have some string 712 00:35:13,688 --> 00:35:17,588 that I'll call STR, which may be a set hello. 713 00:35:17,588 --> 00:35:22,259 What the substring method actually does is it says give me back a piece of yourself 714 00:35:22,259 --> 00:35:27,509 and the piece of yourself is determined by some indices P1 and P2. 715 00:35:27,509 --> 00:35:31,498 P1 is the beginning index of the substring you want to get. 716 00:35:31,498 --> 00:35:35,968 P2 is the end of the substring you want to get but not counting P2. 717 00:35:35,969 --> 00:35:38,340 So it's exclusive of P2. What does that mean? 718 00:35:38,340 --> 00:35:41,729 So if I say string 719 00:35:41,728 --> 00:35:45,778 dot substring where 720 00:35:45,778 --> 00:35:50,048 I start at position one, that's gonna start at the E, ecause that's position 721 00:35:50,048 --> 00:35:50,670 one. 722 00:35:50,670 --> 00:35:55,789 Then I say go up to 723 00:35:55,789 --> 00:35:57,739 three. 724 00:35:57,739 --> 00:36:02,159 What it actually gives me back is L 725 00:36:02,159 --> 00:36:05,358 as a string, and so I can assign that somewhere else. Maybe I have some other 726 00:36:05,358 --> 00:36:06,170 string S. 727 00:36:06,170 --> 00:36:08,709 It does not change the string STR, ecause it's immutable, 728 00:36:08,708 --> 00:36:11,769 but what it gives me back is starting at this position 729 00:36:11,769 --> 00:36:15,809 up to but not including this position. It's kind of funky. That's just the way it is. 730 00:36:15,809 --> 00:36:19,019 There's a version of it that only has one parameter, and if you specify just one 731 00:36:19,019 --> 00:36:21,320 parameter, it's [inaudible] start at a particular location. 732 00:36:21,320 --> 00:36:24,590 Give me everything to the end because sometimes you want to do that. You just want to say give me 733 00:36:24,590 --> 00:36:28,139 everything from this position on to the end of the string. A 734 00:36:28,139 --> 00:36:30,170 couple other things - equals you just saw. 735 00:36:30,170 --> 00:36:33,099 This lets you know if two strings are equal to each other. You might say hey, Mehran, I don't 736 00:36:33,099 --> 00:36:36,239 want to just check to see if they're equal. Can I do greater than or less than? 737 00:36:36,239 --> 00:36:39,519 Well, you can't do greater than or less than using the greater than or less than 738 00:36:39,518 --> 00:36:43,418 signs. Those will not work properly. What you do is you use a function called 739 00:36:43,418 --> 00:36:47,458 compare to, and the way compare to works is it actually gives you back an integer. 740 00:36:47,458 --> 00:36:49,480 That integer is either negative, 741 00:36:49,480 --> 00:36:50,809 zero or positive. 742 00:36:50,809 --> 00:36:54,340 If it's negative, that means one string is less than the other string lexigraphically. 743 00:36:54,340 --> 00:36:56,180 If it's zero, it means they're equal, 744 00:36:56,179 --> 00:36:59,809 and if it's positive, it means one string is greater than the other string 745 00:36:59,809 --> 00:37:02,208 in terms of the ordering that you actually - 746 00:37:02,208 --> 00:37:04,408 what you send the message to 747 00:37:04,409 --> 00:37:06,340 and the parameter 748 00:37:06,340 --> 00:37:09,380 that you actually pass. It allows you to essentially check for not only equal to 749 00:37:09,380 --> 00:37:14,588 but greater than or less than as well in lexigraphic order. A 750 00:37:14,588 --> 00:37:16,619 couple other things very quickly - 751 00:37:16,619 --> 00:37:20,849 index of allows you to search a string for a particular character or 752 00:37:20,849 --> 00:37:23,710 some other substring, so you tell a string, hey, string, 753 00:37:23,710 --> 00:37:29,010 I want the index of the character E in your string, and if I asked hello, the 754 00:37:29,010 --> 00:37:31,489 string STR for the index of E, 755 00:37:31,489 --> 00:37:34,259 the character E, it would give me back a one. 756 00:37:34,259 --> 00:37:37,838 So if it finds it, it gives you back the index of the first instance that it found 757 00:37:37,838 --> 00:37:41,768 it. So if I ask for the L, it'll give me aback a two. It won't give me back the three. 758 00:37:41,768 --> 00:37:45,988 Or I can pass in some substring like EL and say hey, where's EL located and it'll 759 00:37:45,989 --> 00:37:47,920 say it's located starting at position one. 760 00:37:47,920 --> 00:37:51,858 If it doesn't find it, it gives me back a negative one to say it's not found. 761 00:37:51,858 --> 00:37:55,328 So just a few things that you should see, and we can kind of put these together in some 762 00:37:55,329 --> 00:37:56,519 interesting ways. 763 00:37:56,518 --> 00:38:00,939 Let's actually put them together in some interesting 764 00:38:00,940 --> 00:38:04,188 765 00:38:04,188 --> 00:38:07,618 ways. One of the things that's common to do is you want to iterate through a whole 766 00:38:07,619 --> 00:38:10,210 string to do something on every character in the string. 767 00:38:10,210 --> 00:38:12,449 So you might have some for-loop, and 768 00:38:12,449 --> 00:38:15,829 it's I equals zero. You're gonna count up to the length of the string. 769 00:38:15,829 --> 00:38:20,539 So if our string is called STR, we'd say as long as I is less than 770 00:38:20,539 --> 00:38:23,499 STR's length, 771 00:38:23,498 --> 00:38:25,709 and then we would add our little friend, 772 00:38:25,710 --> 00:38:27,108 the I++ 773 00:38:27,108 --> 00:38:29,498 out here at the end. 774 00:38:29,498 --> 00:38:30,798 775 00:38:30,798 --> 00:38:33,019 This is going to go through 776 00:38:33,019 --> 00:38:36,338 essentially indexing the string from zero up to the number of characters that 777 00:38:36,338 --> 00:38:37,048 it has, 778 00:38:37,048 --> 00:38:40,800 and then inside here we might say CHAR CH equals 779 00:38:40,800 --> 00:38:47,559 STR dot CHAR at 780 00:38:47,559 --> 00:38:51,719 sub I, and what that means is one by one, you're gonna get each character in the 781 00:38:51,719 --> 00:38:54,999 string in CH, and then potentially you do something with those characters and then 782 00:38:54,998 --> 00:38:57,778 you're done. 783 00:38:57,778 --> 00:39:01,389 So something you also sometimes do along the way is you do some work and you say hey, 784 00:39:01,389 --> 00:39:04,669 I want to build up some resulting string. Like, maybe I want to take a 785 00:39:04,668 --> 00:39:07,588 string and create a lowercase version of it. 786 00:39:07,588 --> 00:39:11,849 So if I had some string STR and I wanted to build up some lowercase version 787 00:39:11,849 --> 00:39:15,100 of it, I would say, well, I need to keep track of what my result is. 788 00:39:15,099 --> 00:39:19,389 So I'll start off with some string result that I'll set to be the empty string. 789 00:39:19,389 --> 00:39:22,829 So it's two quotes in a row. That's the empty string. It means there's no characters in 790 00:39:22,829 --> 00:39:25,460 that string. It's still a valid string, but there's no characters. There's not even 791 00:39:25,460 --> 00:39:26,778 a space there. 792 00:39:26,778 --> 00:39:30,309 Then for every character that I'm gonna get in this loop, 793 00:39:30,309 --> 00:39:32,048 what I'm gonna do is I'm gonna 794 00:39:32,048 --> 00:39:36,088 tack on some version of it to result. So I might say result 795 00:39:36,088 --> 00:39:40,048 plus equals, which means [inaudible] onto the end of result, essentially what 796 00:39:40,048 --> 00:39:43,489 this is really doing. Yeah, what it's 797 00:39:43,489 --> 00:39:47,449 really doing is creating a new string with an extra something added to the end 798 00:39:47,449 --> 00:39:50,328 and reassigning that back to result. 799 00:39:50,329 --> 00:39:52,548 So I might say result 800 00:39:52,548 --> 00:39:55,018 plus equals 801 00:39:55,018 --> 00:39:57,668 character 802 00:39:57,668 --> 00:40:01,679 dot two uppercase 803 00:40:01,679 --> 00:40:03,298 of CH. 804 00:40:03,298 --> 00:40:07,690 Let me erase this over here so we don't get anymore confusion. 805 00:40:07,690 --> 00:40:10,699 What this is actually gonna do 806 00:40:10,699 --> 00:40:14,778 is it goes through this string, character by character, pulls out the 807 00:40:14,778 --> 00:40:16,010 individual character, 808 00:40:16,010 --> 00:40:19,420 converts it to uppercase and pins it onto the result that I'm building up. 809 00:40:19,420 --> 00:40:23,259 So at the end, what the result will be is an uppercased version of the string that 810 00:40:23,259 --> 00:40:25,019 I'm originally processing. 811 00:40:25,018 --> 00:40:28,128 That's an extremely common thing to do with strings - not necessarily 812 00:40:28,128 --> 00:40:29,199 converting them to uppercase, 813 00:40:29,199 --> 00:40:32,839 but pulling out characters one at a time, doing something on each character 814 00:40:32,840 --> 00:40:35,989 and building up some result as a result. 815 00:40:35,989 --> 00:40:42,989 Any questions about that? 816 00:40:46,559 --> 00:40:49,739 So I'm not checking the length of the result. I'm checking the length of 817 00:40:49,739 --> 00:40:56,739 string, but the length of result, if I were to actually compute it, is zero. 818 00:40:57,409 --> 00:41:00,659 Yeah, I need to put the double quotes here to say that results starts off empty. 819 00:41:00,659 --> 00:41:07,528 Otherwise I don't know what result starts 820 00:41:07,528 --> 00:41:08,329 as. 821 00:41:08,329 --> 00:41:10,849 Last but not least, 822 00:41:10,849 --> 00:41:15,410 two uppercase and two lowercase I mentioned returns to you a new string that 823 00:41:15,409 --> 00:41:19,210 is the upper or lowercase version of that string. So basically, we've just 824 00:41:19,210 --> 00:41:22,900 written over there the equivalent of two uppercase if we returned whatever the 825 00:41:22,900 --> 00:41:23,949 result was. 826 00:41:23,949 --> 00:41:27,289 So let me show you one final example before you go, 827 00:41:27,289 --> 00:41:30,759 and that's reversing a string. You might say how might we reverse a string? Here's 828 00:41:30,759 --> 00:41:32,748 a little program that reverses a string. 829 00:41:32,748 --> 00:41:34,848 It's gonna read in some line from the user 830 00:41:34,849 --> 00:41:37,619 and it's going to call a method reverse string 831 00:41:37,619 --> 00:41:42,099 that as you can notice over here, reverse string can't change the string in place 832 00:41:42,099 --> 00:41:44,890 because it's immutable, so it's gonna return to us a new string that we're 833 00:41:44,889 --> 00:41:45,808 gonna pass 834 00:41:45,809 --> 00:41:48,169 or we're gonna store in this thing called reverse. 835 00:41:48,168 --> 00:41:49,230 So we call - 836 00:41:49,230 --> 00:41:51,769 we ask the [inaudible] string. We're going to enter 837 00:41:51,769 --> 00:41:55,219 stressed, because I think by week four of the quarter, by the end of it, most people are feeling a 838 00:41:55,219 --> 00:41:56,588 little stressed, 839 00:41:56,588 --> 00:42:00,048 and so we're going to call reverse string, and reverse string comes over here 840 00:42:00,048 --> 00:42:03,358 doing this funkiness that we just talked about. It starts off with 841 00:42:03,358 --> 00:42:04,340 some result, 842 00:42:04,340 --> 00:42:06,199 which is the empty string, 843 00:42:06,199 --> 00:42:10,619 and it's gonna count through every character in our original string, but 844 00:42:10,619 --> 00:42:13,189 it's gonna do it in a way so that 845 00:42:13,188 --> 00:42:16,058 by adding the characters one by one 846 00:42:16,059 --> 00:42:17,570 from the beginning 847 00:42:17,570 --> 00:42:22,289 and pinning them on, it's going to actually append it to the front instead of the back 848 00:42:22,289 --> 00:42:24,349 and that way, it's gonna reverse the string. 849 00:42:24,349 --> 00:42:27,579 So that's the difference with this. 850 00:42:27,579 --> 00:42:31,339 Here, we're saying take whatever you have in result before 851 00:42:31,338 --> 00:42:34,119 and the thing you're gonna add is a character at the beginning instead of at 852 00:42:34,119 --> 00:42:34,880 the end, 853 00:42:34,880 --> 00:42:38,419 which ends up reversing our order. So let's just go through this real quickly. 854 00:42:38,418 --> 00:42:40,719 I starts off at zero. It's less than length. 855 00:42:40,719 --> 00:42:45,588 I get the character at zero, which is gonna be an S, and I add that to result, 856 00:42:45,588 --> 00:42:49,828 so nothing exciting going on there. Result was empty before. Now it's just the 857 00:42:49,829 --> 00:42:50,789 string S. 858 00:42:50,789 --> 00:42:55,230 Add one to I. Still less than length, and what I now get in the character at one 859 00:42:55,230 --> 00:42:56,380 is the T. 860 00:42:56,380 --> 00:42:59,690 I'm gonna add the T to the beginning, right, so I'm gonna say T 861 00:42:59,690 --> 00:43:03,500 plus whatever's in result, so now I have TS in my result. The T doesn't 862 00:43:03,500 --> 00:43:05,588 go on at the 863 00:43:05,588 --> 00:43:08,960 end. I'm pre-pending it, basically, and then I add one. I'm gonna 864 00:43:08,960 --> 00:43:12,720 get the R now added to the beginning and I keep doing this, adding the 865 00:43:12,719 --> 00:43:16,939 characters one by one at the beginning and at the end, I now have the value 866 00:43:16,940 --> 00:43:20,429 that's greater than the length. Even though the length is eight, I have 867 00:43:20,429 --> 00:43:24,098 the value that's eight, and so it's not less than the length, and so I 868 00:43:24,099 --> 00:43:25,548 return desserts, 869 00:43:25,548 --> 00:43:28,829 and back over here, stressed still remains stressed. 870 00:43:28,829 --> 00:43:33,940 People are still stressed out. Now they're just eating a little dessert along with it. 871 00:43:33,940 --> 00:43:40,849 872 00:43:40,849 --> 00:43:43,840 You can actually multiply and divide characters, 873 00:43:43,840 --> 00:43:50,539 it's just sort of meaningless if you do. If you do, you sort of get - yeah. 874 00:43:50,539 --> 00:43:51,009 Sorry. 875 00:43:51,009 --> 00:43:54,048 I'm sure you won't get an F, but you won't be 876 00:43:54,048 --> 00:43:57,239 multiplying characters, either. 877 00:43:57,239 --> 00:43:58,309 One final thing - 878 00:43:58,309 --> 00:44:02,140 we've still got a few minutes. Remember, we talked about how 879 00:44:02,139 --> 00:44:04,469 education is one of those things that if you get less of it, you're 880 00:44:04,469 --> 00:44:05,298 happy? 881 00:44:05,298 --> 00:44:05,849 882 00:44:05,849 --> 00:44:09,200 If you bought a car and the car had three doors, you'd be really upset if it 883 00:44:09,199 --> 00:44:10,788 was supposed to be a four door car because you'd be like 884 00:44:10,789 --> 00:44:13,439 I paid $10,000.00 for 885 00:44:13,438 --> 00:44:14,868 this car and it's only got three doors. It's missing one of the doors. 886 00:44:14,869 --> 00:44:18,650 But if I let you out of here five minutes early, you'd be like 887 00:44:18,650 --> 00:44:24,150 rock on! Less education! Same money. I've 888 00:44:24,150 --> 00:44:27,239 never been able to understand - well, actually, I did understand that 889 00:44:27,239 --> 00:44:28,940 about 15 years ago when I was sitting there. 890 00:44:28,940 --> 00:44:31,398 But now I don't understand it anymore. 891 00:44:31,398 --> 00:44:34,538 I want to give you one more quick example, 892 00:44:34,539 --> 00:44:41,329 which is computing a palindrome. 893 00:44:41,329 --> 00:44:44,679 So what we're gonna do is write a function that computes whether or not something is a palindrome. Do 894 00:44:44,679 --> 00:44:46,229 you know what a palindrome is? 895 00:44:46,228 --> 00:44:48,259 How many people know what 896 00:44:48,259 --> 00:44:51,798 a palindrome is? It's a word or phrase that is the same forward or 897 00:44:51,798 --> 00:44:55,568 backwards. Most people know what it is. Now, I will ask you the more difficult 898 00:44:55,568 --> 00:44:56,469 question - 899 00:44:56,469 --> 00:45:02,349 who created the world's longest palindrome? 900 00:45:02,349 --> 00:45:05,528 No, it was not me. 901 00:45:05,528 --> 00:45:08,898 It was, actually, however, my old boss, 902 00:45:08,898 --> 00:45:11,278 interestingly enough, a guy named Peter 903 00:45:11,278 --> 00:45:15,039 Norvig, who claims to have created the world's largest palindrome on November 904 00:45:15,039 --> 00:45:18,919 23, 2003, and it's a palindrome that is 905 00:45:18,918 --> 00:45:21,608 17,259 words long. 906 00:45:21,608 --> 00:45:22,608 Yeah. 907 00:45:22,608 --> 00:45:26,578 He did it with a computer. He's 908 00:45:26,579 --> 00:45:27,539 909 00:45:27,539 --> 00:45:29,298 910 00:45:29,298 --> 00:45:33,768 actually a 911 00:45:33,768 --> 00:45:36,248 912 00:45:36,248 --> 00:45:40,358 wonderful guy, but created the 913 00:45:40,358 --> 00:45:43,449 world's longest palindrome, and you could probably create one that's longer using a 914 00:45:43,449 --> 00:45:44,650 computer. 915 00:45:44,650 --> 00:45:47,719 But how do you actually determine that something is a palindrome? We're 916 00:45:47,719 --> 00:45:51,338 going to just do a little bit more math on strings than we might have otherwise 917 00:45:51,338 --> 00:45:55,058 done. Here's a function that computes is something a palindrome when we pass it a 918 00:45:55,059 --> 00:45:55,880 string. 919 00:45:55,880 --> 00:45:59,338 Now, to figure out if something's a palindrome, it has to be the same going forward 920 00:45:59,338 --> 00:46:03,719 and backwards, which means to do that, one simple way of doing that is to say hey, 921 00:46:03,719 --> 00:46:05,989 something like the word racecar 922 00:46:05,989 --> 00:46:07,478 is a palindrome. 923 00:46:07,478 --> 00:46:09,239 924 00:46:09,239 --> 00:46:12,869 One way I can figure out if it's a palindrome is the first letter equal to 925 00:46:12,869 --> 00:46:14,180 the last letter? 926 00:46:14,179 --> 00:46:17,598 If it's not, I stop immediately and if it is, 927 00:46:17,599 --> 00:46:20,880 then I check the next letter with the next to last letter, and I keep doing 928 00:46:20,880 --> 00:46:21,599 this. 929 00:46:21,599 --> 00:46:24,539 Now, the question is how long do I keep doing this? 930 00:46:24,539 --> 00:46:29,339 How long do I need to keep doing this for? 931 00:46:29,338 --> 00:46:32,389 The length divided by two. I don't need to do it the whole length because eventually, 932 00:46:32,389 --> 00:46:35,980 I'll just cross over and I'll just redundantly check the other side. 933 00:46:35,980 --> 00:46:39,909 But if I check halfway this way, it means that if I was comparing the 934 00:46:39,909 --> 00:46:42,658 characters pair wise, I also checked halfway that way. 935 00:46:42,659 --> 00:46:45,818 The other thing that's fun about this is if I happen to have something 936 00:46:45,818 --> 00:46:46,788 that has a length 937 00:46:46,789 --> 00:46:51,079 three, like the word bib, 938 00:46:51,079 --> 00:46:54,690 the middle character is always equal to itself. I don't need to check it. As a matter 939 00:46:54,690 --> 00:47:00,229 of fact, if I check the length three and divided it by two, I get the integer one, which means 940 00:47:00,228 --> 00:47:03,338 you really only need to do one check of the first letter and the last 941 00:47:03,338 --> 00:47:06,340 letter, and if those match up, don't even bother checking the middle. 942 00:47:06,340 --> 00:47:08,900 So it works nicely for 943 00:47:08,900 --> 00:47:13,039 words that have an odd number of letters by just optimizing out 944 00:47:13,039 --> 00:47:14,849 that middle character. 945 00:47:14,849 --> 00:47:17,019 If I actually have four 946 00:47:17,018 --> 00:47:22,548 letters 947 00:47:22,548 --> 00:47:24,748 948 00:47:24,748 --> 00:47:29,138 like 949 00:47:29,139 --> 00:47:30,509 noon, 950 00:47:30,509 --> 00:47:32,528 951 00:47:32,528 --> 00:47:33,699 952 00:47:33,699 --> 00:47:35,849 953 00:47:35,849 --> 00:47:39,749 you actually have four divided by two. You need to do two checks, and 954 00:47:39,748 --> 00:47:42,518 that will actually check all the characters you care about. So that's what we do here. We 955 00:47:42,518 --> 00:47:43,959 have a for-loop 956 00:47:43,960 --> 00:47:46,728 up to the length divided by two. It's doing integer division, 957 00:47:46,728 --> 00:47:50,528 and what we're gonna do is we say is the character at the beginning or at 958 00:47:50,528 --> 00:47:53,760 index I equal to the character at the end? 959 00:47:53,760 --> 00:47:57,310 Because we start counting from zero, the character at the end, if it has a length of 960 00:47:57,309 --> 00:47:59,059 nine, is actually at index eight, 961 00:47:59,059 --> 00:48:00,688 which is why we have this extra - 962 00:48:00,688 --> 00:48:04,038 when we do the subtraction, we add an extra one. It means subtract off an 963 00:48:04,039 --> 00:48:07,370 extra one ecause if your length is nine, you really want the eighth character. 964 00:48:07,369 --> 00:48:13,339 So basically, as I increases, we check increasing characters from the front and 965 00:48:13,340 --> 00:48:16,459 decreasing characters ecause we're subtracting off I from the back. 966 00:48:16,458 --> 00:48:20,358 As soon as we find the pair that doesn't match, so as soon as these two 967 00:48:20,358 --> 00:48:21,799 things are not equal, we say 968 00:48:21,800 --> 00:48:25,318 hey, I don't need to check anymore. I returned false in the middle of the function, which 969 00:48:25,318 --> 00:48:28,269 means the rest of the function doesn't need to worry about. All the local variables 970 00:48:28,269 --> 00:48:31,880 get cleaned up and I return false, because as soon as I find it, I don't even say hey, let me 971 00:48:31,880 --> 00:48:34,700 just check the rest just for laughs. Oh, you were just 972 00:48:34,699 --> 00:48:36,210 one character away. 973 00:48:36,210 --> 00:48:39,230 No, we just put you out of your misery immediately and we return false. 974 00:48:39,230 --> 00:48:41,670 If you manage to make it through this for-loop, 975 00:48:41,670 --> 00:48:45,130 it means you never hit a case where the two characters you were checking were not 976 00:48:45,130 --> 00:48:47,800 equal to each other, because if you did, you would have returned false. 977 00:48:47,800 --> 00:48:49,419 So if you never return false, 978 00:48:49,418 --> 00:48:54,440 you're good to go. You completed the whole loop, and so you'll return true at the end. 979 00:48:54,440 --> 00:48:57,119 Any questions about that? 980 00:48:57,119 --> 00:48:58,130 Alrighty. 981 00:48:58,130 --> 00:49:03,969 I will see you. Have a good weekend. I'll see you on Monday.