Intro to Java Programming - Course for Absolute Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hey guys, and welcome to a brand new tutorial  series. So in this series, I'm going to be   teaching you Java from the beginning. So starting  at the absolute basics, setting up an environment,   how to create variables, methods, like all  that stuff, and then move the old way up into   the advanced stuff. This is a beginner tutorial  series. So I'm going to treat this as if you have   never learned any programming language before.  Okay, so let's go ahead and get started. The first   thing we need to do is download a ID E. And this  is where we're going to be writing our Java code,   okay, so it's called Eclipse, that's what I'm  going to use use whatever you want. But if you're   beginner, I recommend you just follow along with  this, because some of the things I'm going to do   are specific to eclipse. So go to this link,  it's in the description down below, and just   click on download. And then obviously, 32 or 64  bit, I recommend 64 bit as most of you probably   have a 64 bit machine. Once you download this,  if we go Sorry, I've already got an open here,   but if we go to our downloads, we will see  that it should give us something like this,   double click on it, wait for this to boot up  might take a second. Okay, it's going it's going,   let's see. And once we're in here, we're simply  going to select Eclipse IDE. for Java developers,   we don't need to be using any of this other  stuff. Just this one right here. Okay,   so we're going to install this, I already have it  installed. So I'm not going to do that. Nobody's   gonna ask you for a project directory, something  like that. So if it asks you for that, just choose   wherever on your computer, it doesn't really  matter. Okay, so once you've had that installed,   you chose your project directory. Now we're going  to be in Eclipse. Now to start a new project, what   we need to do so it should look something like  this, by the way, you know, click on File, New,   and then Java project. Now here, I'm just gonna  name mine tutorial. And then let's see here, you   can just leave all the rest of this stuff blank.  If you want to change location, go ahead. Alright,   click Finish. And there we are. So once we have  this, it's going to pop up in our left bar,   the package explorer, I'm just gonna shrink  this a little bit. And what we're going to do   is we're going to go down to source or Oh, I  already have a package name tutorial, one second,   I'm going to create a new one really quickly, and  then walk you through this so it's fresh. tutorial   one. Okay, finish. Alright, we got tutorial  one, and we get source. Okay. Now, inside of   the source, what we're going to do is we're going  to right click on this, we're going to click New,   and we're going to click on package. Now, I  recommend you just leave this package name   the same as your project. So I'm going to do that  and click Finish. And then inside of your package   name, I'm going to click New, and plus. Okay, so  once I click class, it's going to pop up here,   it's going to allow you to select the package and  the source folder, just leave those blank. And   the name you can name is whatever you want. In my  case, I'm going to name it main, okay, type names   discouraged. Okay, so let's name it main with  a capital M. All right, just because you have a   like that. And then we're going to do is make sure  you check on this public static void Main, we need   to check mark this because we're gonna need this  function created us so that we can start Alright,   inherited abstract methods, you can uncheck  that. Or actually, we'll check that doesn't   really matter for right now. Okay, so now we're in  and we've got a little bit of code going here. And   I'm going to explain what this is. And then we'll  get into just coding like a really basic thing,   just printing a few things to the screen before  we move into the next tutorial where we talk   about data types. So Java is an object oriented  programming language, meaning that everything   happens inside of something called classes, and  everything is known as like objects. All right.   Now, you're not gonna understand very much of this  right now, if you're beginner, but for some of you   guys that are more advanced, you should come to  understand what this means. So classes are these,   okay? And everything in Java is written in a  class or an interface or something else. But in   our purpose, just think everything is written in  a class right now. Okay. Now inside of this class,   we have a special method is what it's called. And  these methods are what I'm highlighting right now,   inside these curly brackets, public static, void,  main, okay, you don't have to know what all that   what those words mean, just know, this method  is important. And this is where we're gonna be   writing all of our Java code in for right now. So  if you try to write code outside of these little   curly braces, and not inside of this method,  that's what we're calling it right now. Okay,   your code is not going to execute properly unless  you know what you're doing, which in our case,   we don't yet. So all the code we're writing goes  inside of this public static void Main inside of   these curly braces. All right. Now the first  thing I'm going to get us to do is just print   something to the screen. So to print something  in Java, we're going to type system.out.my   computer's lagging one second dot print, ln,  and this simply stands for print line. Okay,   now inside of these brackets here, we can print a  string or whatever we want to print. So a string   in Java is anything encapsulated by two quotation  marks. Whenever we're printing something,   we want to do it in two quotation marks, at least  for right now. And in this case, I would just say   the classic hello world with an exclamation point,  okay. Now, at the end of every line, we write in   Java But with exception of lines that start or end  with curly braces, we need to put a semicolon. And   now you can see once I put the semicolon, that  our system dot out dot print ln gets highlighted,   we can see that it now has syntax highlighting. In  syntax pretty much means any code that we write in   the program. So we have to make sure that whenever  we're done writing a line, we terminate that with   a semicolon. Okay, so now we've officially written  our first program, all it does is print HelloWorld   to the screen, but how can we run this. So first,  we're gonna have to save so I'm going to hit Ctrl   S. And to run this, there's a little green button  up here and says run main dot java. So we're going   to click that. And you can see that down here, a  little thing popped up called console, we printed   to the screen, hello, world. Now that we've  officially created our first Java application,   very simple. And that is how we go about setting  up a new class. And everything that we write   for right now, again, remember is going to happen  inside this method. That's what we're calling it,   I'm gonna explain that later. public static  void Main, we have to put it in here. Now if   you want to print another line, you want to  print something else after we can do that.   I'll show you how that works. So I'm gonna say  System dot out dot print, ln. And I'll say like   hello world, too. And if I save this, and run  again, we get hello world, and hello world too.   So you can see where we are right where we left  off with printing two lines, hello world and hello   world to to the screen. And in this video, I'm  going to be going over variables and data types,   the absolute fundamentals, we have to understand  before we can move on to anything more complex.   So let's talk about what a variable is. Now a  variable just like in mathematics is something   that holds a value. Now in our case, that doesn't  have to be just a number, it could be a string, it   could be a Boolean, and integer, like we're going  to talk about the different values. And those   are what are known as data types, or objects. So  let's start by just creating a variable. To create   a variable, we need to first figure out what  type our variable is going to be. In this case,   I'm going to start with an integer. And then  we're going to go through all the different   types and talk about the differences. So to start,  we're gonna type int, declaring that this variable   that we're about to type out is going to be an  integer, we're then going to give a variable   name. Now the variable name should contain  typically just letters and underscores. Now,   I believe you can use numbers as long as it's at  the end. But in our purpose, we're not going to   do that when we create a variable name, you have  to make sure you do not use any spaces. And you   don't use any special characters as well. So I'll  go through a few examples of valid variable names   and invalid variable names. So a valid variable  name, for example, could be Hello, that's valid,   because we don't have any spaces. We don't have  any special characters, and there's no numbers   and Okay, now, an invalid variable name would  be something like, Hello, name, okay, this whole   thing as our variable name is invalid, because  it contains a space. Now, if I try to put like a,   what do you call it percent sign like this, or  I tried to put like an Asterix, that is not a   valid variable name. So don't use any special  characters. You can end with an underscore, you   could start with an underscore if you want to. But  we're not going to be doing that. In this case.   Typically, when you create a variable, if you want  to make a name, be like to words, the way you do   it is you type like Hello, and then you would do  a capital and then the next word, or you would do   an underscore representing a space. And then next  word, okay, so that's not a valid variable names.   Not super important right now. So I'm going  to create my first variable int hello world,   and that's going to be equal to by just putting  an equal sign, whatever integer I want. So in this   case, we're gonna say five. And then remember,  in Java whenever we end the line, so this is   just saying my integer Hello, world is equal to  five, we need to terminate with a semicolon. So   there we go. And we see we get int, our variable  name is now highlighted in yellow. And there's no   issues. So if I let's just see what it says this  very, is never used. Okay, that's fine. So we've   officially created a variable, the variable name  hello world now points to the integer five. So to   prove this to you, I am simply going to print  out the value of hello world. Like this, okay,   so I'm gonna say system dot out to comment is  System dot out dot print ln. And what we're gonna   do is we're gonna say, okay, so hello, underscore  world. That's our variable name. And that variable   holds five. So when we print this, we should be  printing five. The way this works in the computer,   right? Is it says, Okay, hello, world. Let's  see where hello world is. It's up here. Alright,   hello, world is equal to five. So we print that.  Okay, so there we go. And we get five printed   down here in the bottom of the screen. Now I  want to show you something quickly and this   is just going to go this is a really important  fundamental thing a lot of people don't talk about   in programming tutorials. It's kind of like order  of operations or execution. So I am going to now   put my variable underneath my System dot out dot  Print lm. Okay, so I'm first going to print hello   world. And then I'm going to set hello world equal  to five. Hopefully you guys have realized what   the issue might be here. If not, I'll talk about  it really quickly. And the problem is that I am   trying to print the value of hello world before  I've declared it. Because the in programming   the way things work is unless you otherwise  specify, we're going to be reading from top to   bottom and left to right, just the way you'd read  a book, right? So we first read this line system,   dot out dot print ln a Hello World, and then we  create hello world. So here, we don't know what   hello world is. So if we tried to do this, you  can see it warns me there's an error. Yes. Okay,   you know what, there's an error. Let's proceed  anyways. And you can see that down here,   we get a bunch of red text. And it says, What  do you call it? Hello, world cannot be resolved   to a variable, because we haven't yet declared  that variable. So we have to make sure whenever   we're referencing or using variables that we  first have them declared somewhere above our,   like the line that we're using them in. Okay. So  there we go, we've created a variable hello world,   we print that to the screen. Now, let's create a  few different variables and talk about what these   types are. So in case I didn't really specify  int stands for integer. And that pretty much   stands for any number that does not have a decimal  point. So as soon as I put a decimal like this,   like 5.0, this actually becomes a new data type.  And that data type is called a float. Now float   stands for floating decimal point, which means  any number that has a floating decimal point,   so I'm gonna say float. Let's see, num. Two  is going to be my variable name is equal to,   and in this case, I'm going to say 5.0. What's  the issue here, add casts from double. Okay,   one second where I said double. So sorry, I made  a mistake there. We're just gonna type double here   as the name Okay, not float. Don't worry about  float right now. We'll talk about that later. So   anyways, we have double num two equals 5.0. Now  double is exactly what I was explaining before,   anything that has a floating decimal point. So  even though this is still the exact same values,   this in terms of mathematics, because it has  a floating decimal point in our computer,   is referenced as a double. Okay? Now, there's some  other data types as well, that we're going to talk   about. So we have int, we have double, we also  have something called Boolean. Now, Boolean,   is true or false. And these are gonna be really  important in the next few videos that we talked   about. So Boolean, I'm just gonna say B as my  variable name, is going to be equal to, and in   this case, I'm going to say true. And again, make  sure I'm putting my semicolon. Now, there's only   two values that a that your boolean data type  can have. That is true, or false. Now why these   are important is because based on the value of a  Boolean type, we're going to be doing things later   in our program. Like if B was equal to true, we're  going to print something, otherwise we won't print   something. So that's really important. So another  data type that we have is known as char, no char   is any character. Okay, so this is a little bit  different from string that I talked about in the   last video, but I'm going to talk about string  as well. So I'm gonna say char C, is equal to,   and whenever we create a character, there's two  ways that we can do it, we can do with a Unicode,   which you probably don't know what that is. Or  we could do it with single quotation marks. So by   just simply hitting the single quotation marks, we  can put one letter in the single quotation marks,   and that is a character because that's what char  stands for a character. So like something on   your keyboard, in quotation marks, now character  could be, for example, nine in quotation marks,   so that's still a valid character. Because it's  in quotation marks is different than the number   like nine if you're, if I were to type that  up here, okay? So any letter or character that   goes in quotation marks is a char. Now we have  to be careful, because chars can only be one,   one character, if I tried to, like 03, you can see  that we're getting red text and Java's yelling at   us, and it's saying no, that's not allowed,  because we can only have one thing. Alright,   now I just want to show you I want to go over  again really quickly what the main differences   between this nine and this nine, up here we've  declared, we're going to have an integer named   hello world, and it's set equal to nine. Okay,  that's what it is. This is a character. And   since it's in quotation marks, this is not the  same thing as nine. The way like the data types   really do matter in this sense, okay. So I'm just  gonna change this back to like h something for   now. Okay. Choice equals h. So that is extremely  important. Now string, this is another data type.   It's a different kind of data type, which we'll  talk about in a second. But to make a string,   we do string I believe with a capital We'll see.  And then we give a name. So for our variable,   in this case, I'm just gonna say str is equal to,  and then it this time for string, we use double   quotation marks, okay? And I'm just gonna say  Tim, in here. So chars, single quotation marks,   and strings are double quotation marks, okay? And  strings can contain anything in them. So I could   have like 673 underscore star, that's a completely  valid string, we can put any characters as long as   they're with inside of these quotation marks.  Okay? Same thing with the character, if you put   a nine in a string, this is different than a nine  like this. Okay? So these are our main data types,   there are a few other ones, but I'm not going  to go into them right now. Because they, they're   a bit more complex than than we can handle right  now. So pretty much though, you might notice that   this string is highlighted as a different color  than these and see it is orange doubles, orange,   Booleans, orange chars orange, why is string  blue? Now the reason it's blue? It's because   it's a different type of data type. These four  data types I talked about up top here, are known   as primitive. And, wow, I really just did that  I'm used to typing Python, primitive data types,   okay. And what that stands for, is not changeable.  Okay, and we're going to talk more about primitive   data types and not primitive data types later  on. But just know that these ones that I talked   about here are known as primitive in Java. And  this one is not primitive. Okay. So anyways,   let's just see now, printing out like some  of these data types, like what we get when   we print them to the screen, right. So if I  print, for example, C to the screen, well,   you should guess what we're gonna get, we get H,  because let's see, if we print, str, we get nine,   and so on. Now, last thing I want to show you  with variables is that we can actually make   variables equal to other variables, and we can add  variables, we can subtract variables. And so we're   gonna be doing a lot in the next video, but I want  to just quickly introduce you. So for example,   I want to create a new integer, Okay, I'm gonna  say, new integer, and I'm going to call it,   let's just call it Tim, why not. Now I want  this integer to be equal to nine. Okay,   but I don't want to type nine out, what I'm  going to do is I'm actually going to type Hello,   underscore, underscore world like this. Now what  we've essentially done is we've taken the value   from hello world, and we've assigned it to Tim.  So now if we were to print Tim to the screen,   we get the value nine. Now if I want to print  hello world to the screen, you can see that we are   still getting the value of nine. So now Tim points  to hello world, which points a nine, right, so   Tim is equal to nine and hello world is equal to  nine. And we can do that and we can add variables   together, we can subtract variables, we can do all  kinds of cool things. And that's really the point   of variables, right. So we just have to make sure  when we're doing this, for example, that we don't   do something like this. String temp equals hello  world, you can see here now it says cannot convert   from int to string, because the datatype string  is obviously not the same as nine. So we can't   do that until we learn about something called  typecasting, which will be in a future video.   Now in this video, I'm going to be adding on to  some of the stuff I did with variables in the last   video. And I am going to be talking about basic  operations like plus minus subtraction, division,   exponential, all of that fun stuff. So without  further ado, let's get started. I want I'm first   going to do is just talk about another way that  we can create variables in Java. So earlier,   you saw me do something like this index equals  six, okay, this is fine. This works, we already   know this. But there's another way that we can  actually create this variable. And to do that,   we can actually omit this equal six. So if we  do this, and we just say int x, what happens   now is we've declared that x is a variable,  it exists, but we've not given it a value. So   we've declared it but we have not initialized it.  I'll show you what I mean by this by just doing   System dot out dot print ln, and we're simply  going to print x and just see what we get. Oh,   probably helped I spelt print correctly. Okay.  Wow, I really messed something up, print ln,   okay, run this. And you can see, we are already  warned that there is an error. So we'll proceed   but and it says the local variable x may not have  been initialized. So before we can actually use   the variable x, when we set up like this, we  have to initialize it. Now to do that any line   underneath this declaration, we can just say x is  equal to and then whatever value you want it to   be as long as it's an integer value. So in this  case, I'm going to say x equals six. And now if   we run the program, everything looks fine, and  we get six. Now, obviously, we can do this with   every data type. So if I do like char, and char  x, right, and that's fine, I say char x equals   single quotation marks, f like that. Okay, then we  can print that out, and that will work fine. Now,   what I want to talk about now is operations.  So I am going to create a few variables,   I'm going to say integer x equals five. Don't  forget your semi colon, integer y equals seven   and I'll say int Zed is equal to 50. Okay, I'll  create one more variable. And that's where we're   going to start talking. So what I want to do now  is I want to sum all of these variables. So 56,   seven, and five. But I don't want to write like 56  plus seven plus five, okay? Because Yeah, that's   the value of these variables, this works fine. But  what if I were to change this variable y? Well,   that means I have to go down here and I have to  change this as well to whatever I change y to what   if I want to change x and y? Well, that means I  have to change both of these. So there's a really   cool thing that we can do. And we can just use the  variable names and just add them up. So we can say   x plus y, plus Zed like this, okay. And now our  variable sum stores the value of the sum of these   three variables. And to prove it to you, I will  print it to the screen, and you can see that we   get 68 as our sum. Now, Furthermore, if we wanted  to take the difference of all these and subtract   them, all we have to do is simply replaces with a  minus sign, and we get negative 58. Okay, now to   multiply things, this is another operator, we can  do multiplication like this, replace this one with   multiplication, and we'll multiply all these out  together, you can see we get 1960 as our value to   divide, we can use the forward slash. Now I'm not  going to do that right now, because that is kind   of a different topic. There's a weird thing with  Division I have to talk about. But that's how you   do that. So those are four basic operators. Now,  these work just like they work in math, in terms   of order of operations, right? So we're gonna  start off with exponents, and then go bracket   or brackets, exponents, division, multiplication,  and so on through the process. Okay, I assume you   guys know order of operations. So that means  if I do something like x times y minus said,   What's first going to operate is x multiplied  by y. And then we're going to subtract said from   whatever this value is. Now again, if I switch  this and I go like y, multiplied by said, what's   going to happen first is y multiplied by Zed, and  then the subtraction is going to happen. Now, for   example, if we have the operators that have the  same presidents are the same, I don't know like   order of operation level, I don't know what to  call it. It's just gonna order operate from left   to right, so it's going to do x multiplied by y  multiplied by Zed. So this is a common occurrence   a lot people to understand this. If we do a  division sign, we're first gonna multiply x times   y, and then we're gonna divide by Zed afterwards.  Okay, so that's how that works. Now when we're   whenever we're doing operations like this, we  can also add brackets in. So if I wanted to, for   example, say x multiplied by y, and then divided  by Zed, I could do something like this by throwing   brackets in here. So now it's showing 100% that  we're going to multiply this for we divide. And   same thing, if I did this, now, whatever's  in the brackets is going to happen first. And   then I can divide by said, and obviously we can  embed more brackets in here. So I can say like,   multiplied by y, times y, okay? Like, get out  as many brackets and layers as you want. And   that's totally acceptable. And that totally works.  Okay. So now, we are going to talk about division   more specifically, and then go into a few more  operators because division works a little bit   different. In Java, is it just in general, okay.  So if I want to divide 56 by seven, so I want to   divide Zed by y, okay? And I some just do you,  okay? I'm going to call this year, and I print   this out to the screen. 56 divided by seven, gives  me a value of eight. That is because our data type   for the variable u, which is holding the value of  Zed and y, or is that divided by y is an integer.   So it actually isn't able to give us a floating  point number, although we know that this number   is a floating point number, or is it a? Let's  see, let's make sure this is not messed up. 57   divided by this guy. Yeah, so we can't give us a  floating point number because this is well an int   datatype. So it assumes that we want an integer  in return. So therefore just going to give us the   value and terminate all the decimals guy. So say  this numbers like eight point something just gives   us eight, right? removes the remainder. Now if  I want to get decimal points, what you will want   to do most the time, I could try to do something  like this. Okay, double of u equals Zed divided   by y. And you mean Okay, that makes sense double  like it's going to give us the floating point.   But watch what happens around this, we just get a  point out, well, we know that 57 divided by seven   is not 8.0. It has some decimal component to it  that I couldn't tell you right now. But why aren't   we getting that? Well, that is because the two  data types that we are dividing are both integers,   which means when we get a value back from this,  it's going to be an integer value. And then all we   do is convert it into double because we have this  double here. Okay? Just by adding that dot zero,   so how can we ensure that we get a floating point?  Well, there's two things we could do, we could   first change the bottom to be a double. And we  could change the top to be a double. So I could   do like this, it's like double. And I could change  this to be a double. Alright, and if we have both   of these double, and we try this, now I can  see that we get our decimal point, okay? And it   shows up and gives us like, whatever that is. Now,  what if I just make one of these, so I say int y,   and this is double i'd let's try this. Now, you  can see we still get our floating point. That is,   because if one of the values that we're dividing  here is a double, it's going to automatically make   the whole thing a double. So one of them is a  double, everything becomes a double. And this   is the way it works for all the operations. So if  you have double u, and in this case, we say like   x times y, or let's say x times Zed, since Zed is  a double x is not, we're still gonna get a double   value. So if we run this, you can see that we get  this little point oh, so if one of our operands is   a double, then that means everything is going to  be a double when we use it or whatever. Okay. Now,   exponent, whether we can explain it is we have  to bring in module I believe, but it's like math   dot how. And then in here, you put the, what do  you call it, the base and the exponent, there's   not a star star like in most languages, okay. I'm  gonna say, instead of D is equal to math dot pal.   And here, I'm simply going to raise x to the power  y. Alright, so we get math dot pow, let's see what   is it saying? convert doc. So let's do this. int,  int, int. cannot convert form double to form. It's   interesting why it's telling you that let's just  try this maybe. There we go. Okay, so whenever we   get X, X, exponents, apparently they have to be  in the form double. Okay, I didn't know that. But   all right. So now if I want to print out D, what's  going to happen is we get the exponent for this,   okay? So math dot pow, this is your base. And  this is what you're raising the quantity. So   five to the seven apparently is equal to that.  Okay, so that is pretty much it for operators,   I guess. Obviously, you can have as many operators  in one line as you want, you now understand how   things work in terms of doubles. And instead  of one of the operators in the whole chain of   operators that are operators variables that you're  adding, subtracting dividing whatever is a double,   then that means you're going to get a double  value back for 100%. If all of them are integers,   that means you're going to get an integer value  back out, same thing here. So I have, let's change   y back to double. And let's change you to be int.  Okay, so it says int you x times set. So now,   that's x times x times y. Okay, so now you can see  we're getting an error here, cannot convert from   double to Ent. And that is because we're trying  to say that the integer u is equal to x times y,   but y is a double value. So when we get a value  back here, it's going to be double. So we can't   convert that into an integer just by doing this.  And there's another way that we can do it that I'm   going to show you in a second, okay. So we would  have to make sure that this stays testable. And   it's nice in this ID, it tells you when you made  a mistake like that because a lot of times you   might not really see that in your program. Okay,  now I'm quickly going to go over something called   typecasting. We're going to talk about this  a lot more later. But I'm just going to show   you like fairly quickly how this works. I'm  just gonna delete this line. And we're going   to turn these back into integers. Okay, so if I  want to do something like x divided by y, okay,   and I want to make sure that I'm getting that  value, right, the decimal point value, okay, like   so a double, rather than converting these like the  declaration of a variable to a double. So maybe we   can do called typecasting and to typecast, we are  changing inline, without changing the declaration,   the type of the variable, the way to do this is  to simply put in brackets, the type that you want   to convert your variable into, and then directly  afterwards is the variable you want to convert.   So in this case, it's double. And then we have y.  Okay, so if I run this now, and I print u, instead   of D, you can see that we get the decimal value  that we were looking for. If I remove this double,   right, then we do not get that or we do just go  to those double, but you guys see the point. Okay.   So in this video, I'm just gonna be teaching you  one more operator that I forgot to mention in the   last video. And we're gonna be talking about  getting input from the user using something   called a scanner. Now this scanner is going to  allow us to grab like text, numbers, strings,   all that fun stuff from the user and then do  something with that input. And that is kind of the   basis of progress. All right, based on what a user  does, we want to do something in most cases. So,   first thing that we're going to do is just teach  this other operator, it is the remainder operator   still going to take the very long excuse, I'm just  gonna say int x is equal to 56. modulus. And this   is what the name of the operator is modulus,  it's simply a percent sign, pick a number. In   this case, I'm gonna pick five. Okay, so what this  remainder operator does, it's called the modulus.   Okay? Some people like to call a remainder  operator, and it's simply a percent sign gives   us the remainder of this division. So this  division is actually equal to, would you call   it 36 divided by five should give us 11? Okay, 11  remainder one is what this division actually is   equal to. So if we print x, we should get a value  of one. And there we go. And that's literally all   the remainder operator is gives you the remainder  of a division. I don't think I need to talk about   it too much more, I'm sure you guys want to get  into the input. So I will leave that alone for   now. Okay, so let's get rid of this line. And now  let's talk about how we can get input. So using a   scanner is what we're going to do so well, first  thing we need to actually do is at the top of your   program, you'll notice I have a new line here that  says import Java dot util dot scanner. I need you   guys to type this out for me, Java dot util dot  scanner, make sure you add your semicolon, because   this is what we're going to be using to get input.  And whenever we use certain tools in Java, we need   to import them at the top of our program so that  we can simply just type like certain data types.   And we'll get into this later when we talk about  classes and methods and all that stuff. Okay,   so once we've done that, we need to set up a  new scanner object. So we're gonna type scanner,   the name of the scanner, I'm just gonna say SC is  equal to a new scanner. And inside of the scanner,   we're gonna type System dot gains brackets. This  might seem confusing, but also is doing is it   saying scanner is a dump type just like string  as the data type, just like boolean data, okay,   it's a different type of data type, but it is in  a data type. Its name is sc. And instead of typing   like one, or like putting quotation marks and  typing a string, we're gonna say what's equal to   a new scanner. And what the scanner is going to be  is system.in, which means typing on your keyboard.   And there's different types of scanners, which  we'll get into later, not in this video. So once   we've set up our scanner, we need to actually use  the scanner. So how can we do this? So what I'm   going to type now is I'm going to say string scan  is equal to sc dot next. And I think we can just   use next Yep. So what this does is we're creating  a new variable string, it's called or it's a   string type called scan. And it is going to get  the next stream of input from the scanner object.   Okay, so SC dot next is what allows us to get a  string from the user. So I'm just going to print   out scans after we get it because I want to show  you what happens. I'm just gonna run the program   and illustrate and we'll talk about it. So now  you can see if I go down here, my cursor shows up,   I'm able to type this because when type will  Okay, watch, watch runs. When I hit Enter,   it prints Hello, underneath it. So hello, which  we typed in was the input. You got scanned in by   the scanner object using SC dot next, it got  stored in the variable scanned, and then we   were able to print scan out to the screen. Okay.  Pretty straightforward. That's how it works with   strings. Now watch this. If I type one, one works  fine. But I just need to show you that now I'm   going to do this with the MTA. So I'm gonna say  int scan equals SC dot Next, well, what's what's   happening? What's wrong? Well, the issue right  now is we are trying to turn a string which is,   which is what this returns to us into an integer.  So we can't actually do that. Because what per   se would proceed, we just run into an error,  we cannot convert from string to int, because   what this method gives us is a string. Now, if we  wanted to be able to get an integer from the user,   for them typing, we have to use a another method,  okay. And this one is called next it. Now you can   see we're getting no more red lines, everything  seems to be working fine. sc dot next int. And if   we run this, then we type like 54. That works fine  prints that out for us. What would happen? So if   I try to type in something like Hello, so there's  no errors right now, like Java is not telling us   there's anything wrong. If I type Hello, we get an  issue. Now that's because this was expecting me to   type in a integer. And what happens in here like  this little line of code, what it actually does,   is it tries to convert what I'm typing in,  which automatically comes in as a string into an   integer. So when it tried to do this, it tried to  convert Hello into an integer and no one not you,   not you, not you or me knows how to convert  Hello into an integer so it threw us an error.   So now that that's not allowed, you can't do that.  So I'll show you what we can do now if we want to   get Booleans and other types, and I'll show you  a way to work around an error like this. Okay?   So if we want to get a boolean value, we could  type Boolean scan equals Next, add, what do you   look at this Boolean? That's literally the name to  get a Boolean SC next Boolean. Okay. So let's run   this. Now, note a Boolean has to be true. spelled  correctly or false with lowercase is I'm pretty   sure. So true, that works fine. But if I try to  type like Hello, or five or something in there,   that's gonna crash, it's going to give us an  issue. Okay? So Boolean lets you do that one,   once you get a double could do double equals  next. And guess what this one is? Next double,   there you go. And now it's going to expect a  number of some sorts, like six would work fine,   but I'm going to undo 6.3. And it prints out  6.3. Let's actually just test six at six. Yeah,   just give us 6.0. So that works fine. Okay, to  getting doubles. Now, I want to work around the   issue of what happens like we get that crash,  okay, we don't we don't want that crash. So   how can we fix that if we're per se, when it's  expecting a double, I type in like, hello. Well,   what we should do is always get a string. So we  should always say a C dot next. And always turn   this into a string. And that's because a string  can be anything like anything we type in here is   fine to be a string, because remember, a string  is simply anything in double quotation marks. So   if I type true, although Yeah, we know it's a  Boolean, it's in the quotation marks. So it's   really a string. Okay? If I type one, that's a  string, type 1.6, it's a string, like anything   can be converted into a string, pretty much. So  that works fine for us. Now, once we have it as   a string, we can then convert it into an integer.  Sorry, I had to see a quick cut there. But what   we're going to do now is we're going to attempt  to convert this value into a different type. So   in our case, I want to convert it into an integer.  So the way that we can do that is we first need   to set up a variable. So I'm gonna say index  in this case, is equal to scan. Now, I'm going   to put something with this scan, I can't just say  index equals scan, I can't, I can't just do that,   it's not just gonna be able to convert that for  me, it doesn't know how to do that. So what we   need to do here is actually do dots, and are  certainly to type here. integer dot parse, int,   okay. And then inside of the brackets for parse  int, we're going to put scanned, and what this   is going to allow us to do is well convert into an  integer. And if you guys just highlight over this,   you don't know what it does, you can actually  read through the definition. So in this case,   it's going to say parses the string argument  as a signed decimal integer, the character in   the string must be old decimal digits, except the  first character may be an ASCII minus sign. Okay,   so we can tell you it tells you exactly what this  does, it tells you what integer does integers a   class. So yeah, it'll convert that for us, so  that if I wanted to print x to the screen, that   would work fine. Now, the thing is that when this  is what we're gonna be doing in the next video,   is, we still run into the same issue because  again, like now, if I type something like Hello,   we still get a crash, because how do we can  convert Hello into an integer? Well, we should   really first check if the value is an integer.  And I'm going to be showing you in the next video,   I believe, the next one or the one after that, how  we can actually do that using if statements and   else statements and error catching later on. So  stay tuned for that. But this is the way for right   now. If you know, you're going to be getting an  integer that you can convert it in, or obviously   you could just do next int and turn this to an  int like that, and everything works fine for you.   So in this video, we're going to be  talking about comparison operators,   like greater than less than or equal to not really  talking about chaining conditionals together,   which you'll see if you don't really understand  what it is right now throughout the video,   and how we can use those Boolean variables to do  certain things for us. So that's what we talked   about in the first few videos. And if you guys  don't really know what the point of Boolean is,   well, this video will show you. So without further  ado, let's get started. So I'm just going to start   off by typing a few numbers here because it's  just the easiest way to compare things to start,   and then we'll do some other stuff. So I'm going  to create three variables that are integers.   X is six, y is seven ends, that is 10. Now I'm  going to create a boolean variable. So Boolean,   else, call it compare, and we'll just set that  blank for right now. So what I want to do is I   want to compare x and y and Zed using some  different comparison operators. So in Java,   I'm going to write them out here. We have a belief  for main comparison operators four or five. So   we'll type them out and we can see, so the first  one is greater than, and then we have less than   we have equal to, oops, we have greater than or  equal to less than or equal to and not equal to.   Now I know I just went through those fast but  they're pretty straightforward, this is simply   going to state whether something is greater than  the other thing, this is less than the other thing   or whatever way you have it right? This is, is  equal to so the double equal sign is different   than the single equal sign, double equal sign is  when you're comparing two values to see if they're   the same. And the single equal sign like this one  here, is if you're assigning a value, so make sure   you remember that because a lot of people when  they compare things, they forget to add the other   equal sign, and they just use one and they end up  running into some issues. Okay, so greater than   or equal to pretty straightforward, less than  or equal to same thing and not equal to as an   exclamation point and an equal sign. Now I'm going  to go through these and show what all these do. So   I saw Actually, I'll leave that there. And because  I just remember that I have been forgetting to   teach you this. If you do two slashes in Java,  this stands for a comment. And a comment is simply   something that the computer is going to ignore.  And it's just there for the programmer. So they   can look at it and be like, Okay, this is what  this line of code does. Whatever the computer   doesn't care, so whenever it sees a line that  has starts with two slashes, this is how you do   a comment. It just ignores that line and moves  on. So that's simply what a comment is. And it   just Gray's out in here to show us that that's a  comment. Okay, so compare. So I want to compare x   and y. And I want to set see if x is less than  y. So say, if x is less than y, and what this   statement right here that we're typing here is  going to return a value of either true or false.   So we can look at this, we say, well, six is not  less than seven. Yes, it is. So that should give   us a value of true. So if we print compared to the  screen, then you can see we get true like that.   Now, if I flip this around, and I say greater  than, okay, what do you think we're going to get?   Well, we get false, it's pretty straightforward to  compare the two values to see if they're the same,   we can do two equal signs. And in this case, we  get false. And now this this comparison operator,   some people get confused by. But if I do not  equal to, what this is going to tell me is if   the two values on the left side and the right side  of the comparison operator are not the same, so if   they're not the same, I get true. If they are the  same, I get false. So in this case, they're not   the same. So we should be getting true. Okay. And  I guess I can show you greater than or equal to,   but they're pretty straightforward in terms of  how they work, at least on numbers. Okay? Now,   these work fine. So greater than or equal to less  than or equal to on numbers. But what if I want   to compare strings, so I'm just going to change x  and y, I'm gonna make two strings here. I'll say,   oops, string, x, will say hello, and string  y. And this is equal to pi. Okay? So see,   now I'm getting this blurred out, or this red  line here, it's saying the comparison operator   greater than or equal to undefined for strings. So  the only ones that I'm allowed to do on strings,   at least for right now that we're going to talk  about, are two equal signs, or not equal to,   okay? And not equal to is simply going to say,  right, if the strings are not the same, and if   they are the same, so in this case, I say not the  same, I'm gonna get true. If I try to see say,   if they are the same, I get false. Now, I want you  to just show you one thing here, if I type Hello,   and I add a capital O at the end of Hello like  this, do we think that this is the same as Hello   with all lower cases are not? Well, I'll show  you. It is not so capital letters do matter. In   program, right, let's assume with variable names,  like the capital variable Y is different than the   lowercase variable y. Okay, so that's how we  can compare those. And I want to see actually,   can we use greater than net, we can't use  greater than or less than on strings either.   Okay. So what I'm going to do now is I want to  chain multiple conditions together. So I'm going   to show you the end operator, the OR operator,  and the NOT operator. And these are how we can   add multiple conditions together to get one  like main condition. If that makes sense,   I'm going to change these back to two numbers  might be faster just to type them out. index   equals what I have six into y equals, let's just  do like 23. Why not? Okay, so now I'm going to   compare multiple things at once. So I first want  to compare if x is greater than y, but I also want   to compare if Zed is greater than x, say I want to  check two things perfectly viable. In many cases,   you'd want to do that. The way that we can do that  in Java to compare if two things are the same,   is using this an operator AND AND operator is  looks like this. So it's these two and signs,   okay? And then we have to add another condition  after this. In this case, I'm going to say if   Zed is arch, say Zed is less than y, okay? You can  see that's fine. We're not getting any red lines.   And what this does is it's going to compare the  operation on the left side, it's going to do the   comparison on the right side, and then we're going  to end up getting two values. So in this case,   x is greater than y. So six, is that greater than  y? No, we're gonna get false. So I'll just type   it out as if this is what's happening in the  computer, right? And that is less than y. Yes,   it is. So we get true like this, okay? Now, we  have a false and we have a true, but we have   this and operator in between them. What this and  operators can do is it's going to check if both of   the conditions on the left side and the right side  are true. If they're both true, it returns true to   us. If they're both pretty one of them is false,  or both of them are false, then we get a false   value. So in this case, since this one is false,  we're going to get a false value because both of   them have to be true for this to evaluate to true.  So I'll show you if I do this. Okay, and I print   this to the screen, you can see we get a false  value, because this is saying, if this and this   are true, then the whole thing is true. Okay. Now  another operator that we have is the OR operator.   And the OR operator is to straight up lines like  this, I don't know what they're actually called,   the key for that, but anyways, so this is gonna do  kind of, it's similar to end. But in the other way   around, if one of the two conditions are true,  the whole thing is going to be true. Otherwise,   if both of them are false, it's false, right?  So the way that we can determine this again,   right is we'll do this condition, we'll see what  this is equal to, and then we'll see what this   is equal to. And if one of them are true, we get a  true value. So let's run and there we go, you see,   we get a true value. Now, we have a knot operator.  Now what not does is simply going to reverse   anything. And this is what I like to think about  it, whatever you have, if you see the knot, just   reverse it. So what this does is it's going to say  if this condition is not true, then we get false.   Or if this condition is false or not true, then  we get true other way around. If this condition   is true, then it's false. Because it's checking if  it's not true, I hope that makes sense, you kind   of just have to play with it to make sense of it.  But in this case, since we get a true value, and   we have this exclamation point, which is our NOT  operator is going to give us false. So you can see   here we get false. Now if in here I were to put a  false value. So let's literally I'll just show you   by typing in the value, I say not and then false  in there, you can see that we get a true value.   So it's simply just reverses like anything that  you're looking at. Now I want to show you that   we can actually combined all of these operators  together. So what we can do is we can use like   ands and ORS and we can have an infinitely  long chain of a condition that will eventually   evaluate to true. Now this is actually some kind  of like math problems you have to do typically in   university, I'm gonna have to do it next semester,  where you have like a ton of different conditions,   and you have to figure out if you're going to  get true or false based on them. So I'll show   you like a pretty basic example. So I'll say if x  is less than y, and y is greater than Zed, or Zed   plus two is less than five. Or what do you call  it? Like x plus seven is greater than what? Okay,   so I just typed a bunch of conditions, right?  How do we determine which ones which of these   are going to evaluate first? Well, that is a good  question. And typically, you'd never type it like   this. Because just looking at this, like even  I'm looking at it right now after I just typed   it in. I'm like, wow, okay, how do I determine  which one of these is going to happen first,   typically, you put things in brackets, so I would  say something like this. It's like x and y. Okay,   so I have this first conditional, right? You put  in brackets, it's going to evaluate this, and in   this case, we'll get I'm not going to bother doing  the actual evaluation. But let's say we get true,   okay? Or so we have true, or whatever this  evaluated with this is. So what this is   going to do now is it's going to look at said plus  two, if that's less than five, if that's true,   then we get true or and then whatever this is,  and then we could treat this as if this is in   brackets like this. Okay. So now the way that this  would work, is we do everything in brackets here,   we get a value of like true or false. We do  everything in brackets here, we get a value of   true or false. And then we'd see if either of them  are true, and return that value. So you know what,   let's just print it to see what we get. If we get  true, okay, I don't even know how that worked to   be honest. And yeah, that's how we could do  that. And we can also throw knots in here   too. So if I want to throw a knot like this,  then we can do that. Now typically, you're not   going to see massive chain conditionals like this,  because of the exact problem we're running into,   it's difficult to determine what they are by just  reading it. And you'll see when we go into if   and else statements in the next video how we can  use these conditions to evaluate certain things,   I just wanted to show you that you can combine  multiple things. And for example, I don't only   have to use variables, you can see that here I put  Zed plus two less than five, that's a perfectly   valid condition that works perfectly fine, can  change this is equal to five, right? And you can   add constants, like I could just say two is equal  to five if I want it to, but that's fine. You can   compare with constants. And with strings, just  remember you can do less than or equal to, or   sorry, not less than not the not equal to or equal  to, and you can compare floats with intz. And   you'll see as we continue going through the video,  it's too long to go through all them right now.   In this video, we're going to be going over if,  else and Elif statements in Java. So quickly,   just I want to fix a really small mistake I made  in the last video. So if you guys don't know,   I am like a Python programmer typically. So I'm  doing Java, obviously. But Python is like my   first language. So I'm used to being able to use  two equal signs on pretty much anything because   that's what you can do in Python. But in the  last video, I showed you using two equal signs   on strings. Now, that's not incorrect to do that,  but it's not going to work the way you think right   now. So if you want to compare if two strings are  logically the same, like the actual string values   have the same, what you should actually do is say  you have a string. So in this case, you have to   do dot equals Okay. And then inside of this equals  here, you're going to put the string that you want   to compare it to, or you're going to put another  variable, which is a string. So in this case,   I want to say if like, my scanner object, which  I've just typed out before this video, is equal   to Hello, this is how I would do it with this dot  equal sign, not the two equal signs, you the two   equal signs isn't wrong, but it's going to give  you a different answer. And we will talk about   that in future videos, but it's too advanced to go  into right now. So just remember, if you want to   compare two strings, use dot equals My apologies  about that from the last video. So what we want to   do now is we're going to do it using if and else  statements. So pretty much if something happens,   do this, otherwise, do this. And this is the  basis of programming. Using conditions based   on like user input, or based on certain events  that happened, we're gonna do different things,   right? So they're really important to understand.  So first of all, what I'm doing here is I'm just   getting, I'm just setting up a new scanner object,  I'm just going to get input from the user, we've   already talked about this. And what I want to do  is I want to check that user's input, and based   on what they type in, I want to do something.  So the basic syntax for if analysis in Java is   you simply type if you put brackets, and inside  of these brackets is going to be your condition.   And this is why I spend so much time talking about  conditions the last video, the condition here is   if this condition evaluates to true, whatever's  inside of what I'm going to show you here,   whatever's inside of these curly braces is going  to happen. So let's start with the condition. When   I say if s dot equals not two equal signs, and  in this case, I'm going to say, Tim. So if the   user types in Tim, when we're when we prompt them  for input, then I want to do something. And what's   going to happen is inside of these curly braces,  and this denotes a block. So this is known as like   an if statement, or an IF block. So this block  simply goes to syntax, if then we have this,   these brackets inside the brackets as a condition,  the condition can be as long as it wants as long   as you want, as long as you're going to get a  true or false value back from it. And then we   have these curly braces, so an open brace and a  closed brace. And inside of these braces, anything   that I typed in here is going to happen if this  condition is true. So let's just do a quick little   test here. And I'm going to do system dot out.  dot print ln. And in this case, we'll just say,   you typed Tim, okay, what types you types? you  typed Tim? Okay. And let's run the program and   see what happens. So when I run this, if it loads  up, That's odd. Okay. Give me one second. Okay,   so we're back, I accidentally closed my console.  And yeah, we running into some issues with that.   So I got that up and running now. Let me  just restart this quickly. Okay. Wow, okay.   I don't know why. That's why But anyways, let's  just type out. What are we going to say here,   let's type hello and see what we get? Well, we get  nothing because we didn't type Tim. So let's run   it again. Let's try it. And this time I type Tim,  and it says, you typed Tim. So there we go, our   basic if statement is working. Now I want you to  just put another system dot out over here so that   we can see what happens when we have like just  with this example. Okay, I'm just going to print,   I'm literally going to print print. Okay. So now  what I want to show, what happens here is like,   what is the program going to run? So if we type  Tim, we're gonna get this you type Tim. But are   we still going to get this print out here? Or are  we not going to get it? Well, let's test this out   and see, okay, so in this case, I typed Tim. So  when I typed him, it says you type Tim and then   it says print, okay? Now the reason that happens  is because this print is not within this if block,   right? So we're just going to execute simply  after this if block happens now, same thing,   right? If I type, like Hi, it's still going to  print out print to the screen. Because right?   It didn't go through this if block but this is  not inside of the if block or the if statement   or whatever. So that executes. Okay, so that's  pretty straightforward. Now I'm going to show   you else and elsket rest start with else. So else  is very straightforward. It can only come after,   and else if or and if so, like the start of  the block has to be if and then this else You   can't just have it like alone. It has to come  after an if statement or an elsif statement,   which we're going to go into in a second. Okay.  There's different ways to format this. Some people   like to put the else like this Has the closing  brace than the open brace. I personally like   to do it like this. Some people like to do it like  this, it's however you want, okay? This just works   fine. Just make sure that your curly brace doesn't  look something like that all the way up there,   because that's kind of hard to read. Okay. So  what I'm going to do now is I'm going to put this   print statement, and I'm going to put it in my elf  statement, right? Now, the way this else works is,   if this happens, that's fine. We're going to go  in here, we're going to print this out, and then   we'll move down to the end of the block. So we'll  go to here as our next execution line. Now, if   this doesn't happen, so other words, or else, then  we print this, so we're either printing you type   Tim or print, we're never printing both because  one of them is if and one of themselves. Alright,   so let's test this out. If I type Tim, we you type  Tim. Okay, if I type anything else, so like some   random letters, and I hit Enter, you get print.  So is LS works is it's meant to do something if   this condition is not true, right? Okay, so that's  pretty straightforward. I don't think any to talk   about that anymore. The next one I'm going to  show you is elsif. Now this is a way that we can   add multiple if conditions in one kind of block.  Okay, so what I can do now is I can do like elsif.   And now it's the exact same syntax as regular if  statement, except you just have this else before.   And obviously, since this is an else it has to  come after an initial if statement. So you can   have as many of these else ifs as you want. Now,  first, I'll just put a condition in here. So say,   s dot equals, and in this case, let's just say  like, Hello, okay. And then in here, we'll just do   system, dot out, dot print, ln, and we'll just say  hi, because we want to greet them back. They said,   Hello. Okay, so again, the way this is gonna work,  is we're gonna go through, we're gonna check,   we're gonna get our scanner input, we get  us say, okay, what's s is s equal to Tim,   if it is we're gonna print this, and then we're  gonna move down after the last l statement,   we're not going to even bother checking if it's  something else, because if it's equal to Tim,   then we know that we shouldn't bother looking at  the L sips. Right. Now, if it's not equal to Tim,   what we're going to do is we're going to go  through and we're going to check reinstate k LS F,   that's our next block, is it equal to Hello,  if it's equal to Hello, we're gonna print Hi,   we're gonna move on with our lives, you're gonna  move down here, we're not even going to bother   going to the else. Okay? Now, if it's not equal to  Hello, we're going to go into the else statement.   And we're just simply going to print print,  there's no condition, it just automatically   going to happen. If you print anything other than  Hello, or Tim, we're going to print print. Okay,   so let's try this out. And let's type Hello.  And you can see we get Hi, all right. Now,   if I print type anything else in here, like some  random letters, we get print. Okay. And that is   how that works. Now I can show you I can add as  many else ifs as I want. So if I copy this, and I   simply paste it down here, gonna have another LCF.  And in here, I could have Hi. And then in here,   I could type Hello. And we could keep going.  We could do as many else ifs, as we want. Just   know that whenever you have a kind of a block  that looks like this, it has to start with an   if statement. And it does actually doesn't have to  end with an else. So in else just is should be the   last thing so if you have any else ifs else will  be your last thing like I couldn't do an lF here,   and then try typing like that's, that's not  okay, you can't do that. But if I omit this else,   that's perfectly fine. So now I have if we type  Tim, we type Hello, or we type Hi, let's run this.   And let's type none of them. Let's, let's type a  bunch of letters, we get nothing printed to the   screen because we don't have that l statement.  Now, if I type Hi, you see we get Hello. Because   right we went through wasn't equal to Tim. It  wasn't equal to Hello. So we printed Hi. Now   I want to show you this. This is a bad example.  But it it'll work. If I put Hello here. And I put   Hello here. Which one are we going to print? So  remember, we're only going to print one of these   things. We're not going to print more than one.  So which one? Is it high? Or is it Hello? Well,   let's test it out. If I type Hello in here, we  print high. Now the reason we're printing high   is because this else if is above this one, and  this is the first one that's going to be looked   at when we type something in just the way I kind  of went through it sequentially. So since we print   high here, we're like, Okay, well that happened.  So there's no point in checking this. And we just   move on. Okay, straightforward. Now again, I could  do more if statements down here, you could keep   typing like do a bunch of stuff, just understand  you can have as many elsif as you want. You don't   need to know stuff. Like we could just do an IF  and then else. But every time you have kind of   like an if statement or block starting it has to  start with an if it could have as many elesis as   you want. And the last thing has to be an else.  If you're adding else you don't have to have that   else there. So anyways, I think that's all I'm  going to show for if statements elsif and else   they're pretty straightforward. Any condition can  go here. So anything that I showed you before,   like you could have a really long chain  conditional a ton of stuff. Same thing here,   you could have any condition like it doesn't  matter, anything that gives you true or false   value, you could also literally just type  true. And like that would always happen, okay?   In this video, we're going to be kind of taking  a step back and just going through everything   that we've already learned summarizing that,  putting that into a program to make sure that   you guys really understand all of that. And we're  also gonna be talking about nested statements. So   how you can kind of add things within other things  like so if statements within other if statements   variables. And the way I'm going to do this  is just by creating a simple program. And what   it's going to do is it's going to ask the user to  input their age. And then based on that age, we're   going to do certain things with that, we're going  to tell them like, I don't know good examples,   like on a roller coaster, like if you're 13 or  older, you can ride otherwise you can, so we're   going to do something like that. Okay, so I'm  going to keep these scanners up here that I have,   because we want to get the user's age. But um,  first, before I set up the scanner is I'm just   going to print a line to the screen here. So  I'm just going to say system, dot out dot print,   and so print ln. And in this case, I will just  say, input your age simply okay? So this way,   it should, if I'm doing this correctly, make  it so that we just get the age right after this   line. Okay, you'll you'll see when we do that in  a second. Anyways, what I'm going to do now is I'm   just going to kind of start typing, and we'll go  through how this works out. So I'm gonna say if,   and we're actually just going to convert this  to int first, I think string to an int. Yeah,   so using that parse into that I was talking about,  so we'll say int, age is equal to s.or integer,   I always forget how to do this integer dot  parse int. And then in here, we need to type s,   so we're going to convert whatever they type  into an integer in this case, so we're going   to assume that they type in an integer. So we'll  say if age is greater than or equal to 13, then   we will simply print System dot out dot print,  ln. You can write Okay, exclamation point. Now,   if they're not older than 13, that must mean  that they are less than 13 or younger than 13.   So in this case, we will simply print that they  cannot write system dot out. dot print ln. And   you cannot write exclamation point. Okay, pretty  basic program, we kind of have gone through all   this work. So let's test it out. input your age  five, you cannot ride. Okay, let's try this now.   input your age 43. You can ride awesome. Okay,  so everything's working fine for right now. Now   what I'm going to do is I am going to add a elsif  here and I'm going to change kind of the problem   that we're looking at. So I'm going to say now I  want to classify the person based on their age as   either an adult, a teenager, or just like younger  than a teenager. Okay? I don't know, what do you   call those? Whatever, younger than teenagers.  Okay, so if we are greater than or equal to 18,   I want to print you are an adult, okay? Now  otherwise, so if they are not greater than 18,   we want to check if they are less than 18. So or  if they're, actually we'll do it in a in a cool   way, that's gonna make sense here in a second. So  else, if age is not greater than or equal to 18,   we know it's less than 18. So all we actually  have to check is if they are older than 13,   right? We're greater or older than or equal to 13?  Because that's what a teenager would be. And the   reason we don't have to check if they are, what  do you call it younger is, or if they're older,   is because we already know that they're gonna  be younger than 18. So know where they're at   within that range. So I already butchered that  explanation, but that's okay. So system, dot out   dot print, ln. And in this case, we'll just say  you are a teenager, okay, like that. And then in   this last case, well, we know they're not older  than or equal to 18. We know they're not older   than or equal to 30. And that meaning they must  be less than which is you are not a teenager or an   adult, okay, like that. So let's test our program  out, make sure everything is working. So if we   input an age of 14, we get you are a teenager.  Awesome. Okay, let's try it again. We input an   age of 19. Get you're an adult. And if we input an  age of zero, yet, you are not a teenager, we're an   adult. Now, I want to show you what happens if  I input like a negative number, like what if I   do like negative 98? You are not a teenager or an  adult because right we have this L statement. And   that's not greater than or equal to 13. It's not  greater than or equal to 18. So we get you're not   a teenager or an adult. Okay. Now, that is pretty  straightforward. What I'm going to show you now   is how we can ask like another question or how we  can check something else. So what I'm going to do   here is I want to say, if they're 18, and only  if they're 18. Want to ask them a question?   What should the question be? I want to ask them  what their favorite food is. Okay? So I'm simply   going to do now exactly what I've done up here,  except just throw it inside of this if statement.   So if this happens, we're going to do this and  this is going to show you nesting. Okay? So we're   gonna say SC, or we'll say, string is Windows to  integers. We're gonna assume that our string and   guess Kazumi food Yeah, we'll say string. Food  is equal to SC, dot next line, like this, okay?   And what I'm going to do is I'm going to print  up here again, what we're looking for, so I'll   just copy this in a second. And in this case, I'll  say, input your fav food, okay? Now what I want to   do is I want to check what their favorite food is.  So I want to say if their favorite food is pizza,   I'll say mine too. Otherwise, we'll say that's  not my favorite food or something like that. Okay,   we'll say if, and we have our conditioning here,  we'll say if food dot equals, and in this case,   we'll simply type pizza. Right? Then we will  system dot out, dot print, ln. And we'll say   mine. Wow, inside of the coats, hopefully. Say  mine too. And then otherwise, so the else, what   we'll do is we'll simply type system, dot out dot  print, ln. And in this case, we'll say, not mine.   Okay? So this is showing you now it looks a bit  more complicated, but that we can actually put if   statements and else statements, we can do things  with inside of the if statement. And I'll show you   that this works. So if I type, I'm 19. It's gonna  say input your fav food, okay, my favorite food,   let's try pizza, says mine too. Awesome. That's  working. Now, if I type something else, so I typed   like for, it just tells me I'm not a teenager,  and adult because we don't have anything else   happening inside of this if statement, or this  else over here. Okay. Now, that's really cool.   And that allows us to do some neat things. And  you can continually nest This is called nesting,   putting a statement inside of another statement,  you can continue to do that as much as you want.   So I just wanted to show that to you. Because  a lot of people think that that's not possible,   they don't understand that you're able to do that,  because they haven't been shown that right? So   again, if I want to ask another question, I could  do that inside of here. And I can just keep going.   What is an array? Well, an array is a collection,  pretty much of elements, that is a set size. So   rather than having like a variable equal to one,  you'd have an array and it would have a bunch of   different values in there that you can index  at certain points. And you'll see when I start   talking about them here, if you're a little bit  confused, but how to create an array is to first   you have to declare the type that you want your  array to be. So all the values in the array,   at least right now have to be the same type. So in  this case, I'm going to create an integer array.   And to declare that this is going to be an array,  you simply do these little square brackets, use a   in square brackets, the name of your array in this  Gizmo is a new arr standing for array is equal to,   and then in this case, you're going to say new  int, square brackets. And inside of the square   brackets, you have to define how many elements or  how long your array is going to be. In this case,   I'm just gonna say five. Okay, so you can  see that's fine. not getting any issues here,   just because we're not using new AR. But yeah,  this is the syntax. So whatever type you want   it to be, you type the type. So if I wanted to  be string, I do string. And then this obviously,   would have to be the same string like that. Okay,  square brackets, name of your array equals new,   and then the type square brackets how  long you want the array to be. Now,   there's another way to declare this, I'll show  you in a second. But this is like a set length,   like you can't change this length. So you have to  make sure that when you're declaring your arrays,   you know how many elements you want to have.  Because you can't if you make an array length   zero, then there's no point in using it. You can't  add, you can't add anything past like that length,   okay? And what it's going to do is it's going  to, by default set all of these elements to like,   no, so they have no value, but they exist, like  they're there. They just aren't holding anything   right now. Okay? Well, you can kind of think of it  as you're initializing, like five containers that   are old gonna hold something, but they're not yet  holding anything. So is a good way to visualize.   So we're gonna keep the string array for now.  And I'll show you how we can add things to array   and change values and print them out and see  what that looks like. Okay, so the way that you   index things in an array and array is going to be  it's going to look like these curly braces. Okay,   so I'm just gonna type some stuff out and just  follow with merit. So my string array I want to   have Hello. I need double quotes. Hi, Tim. Maybe I  should have done last values bill, and Joe. Okay.   Now these are the five values in my string array.  This don't type this out with me. This is just   showing you an example. Now the way that we start  counting in computers are actually let's first say   this, every element in our array is going to have  an index, okay? And the index is going to be a   number that represents its position. So in this  case, you'd say like this is position one, this   is position two, and this would be position five.  Now, if I were to ask for position zero, or sorry,   position one, it would give me Hello. But the  thing is, that seems logical. But in computers,   we actually start counting at zero, meaning  that position one, which I'm saying is Hello is   actually going to be position zero. And that means  that whatever the length of my array is, the so in   this case, five, the last position in my array is  going to be the length minus one. So in this case,   four, so Joe would be position four. And these  are known as indexes, like index zero is Hello,   index one is high, index two is Tim. And this  allows us to really easily grab different elements   without having to know their value, we just know  the position of them in the array. Okay, so 01234.   That's how we count in computers. Okay, so how can  we actually access these different elements? Well,   I'm going to create a variable and just say  string x equals, and in this case, I want to   get like value two in my array. So first of all,  I haven't actually added anything to my rate, but   we'll do that in a second. But how I would do that  is I would type like new AR, and then I put square   brackets and then in here, I put the position. So  position, so I want to get Hello, I think that was   the first or second position I will see anyways,  I would type one. And what this would give me is   the second value in my array, because remember, we  start counting at zero, so the first value would   be zero, and the last value would be four. Now  maybe this is going to look a little easier once I   start adding things into my array. So the way that  we can actually add things into the array is what   we've declared an array and we set it has length  five, which means that these elements already   exists, or those buckets, there's not holding  anything. So what we have to do is we have to   say new AR zero is equal to, and in this case,  let's give it a value. So let's say hello, okay,   put our semicolon, there we go. We've just set  position zero, index, zero, whatever, to Hello.   Okay, so now if I copy this a few times, I can  set the values of all the elements in my array. So   I did 012, and three, and here, I can just change  these. So say hi, I'll say, Tim, I will say, Bill.   And we can add one more, but we don't have to. So  if we let's, let's just add one more for the sake   of it. And new AR four is equal to what we call it  geo up. I don't know why it's doing that for me.   Okay, jus like that. Alright, so it may say string  x equals new AR four, then that's going to be   equal to Joe. So to print this out to the screen,  now I can do system dot out, dot print, ln. And in   this case, I will say x, and let's see what we  get. We get Joe, right, because we added all of   these into the array. Now if I just comment this  out by doing that, let's see what happens when   we do a new AR for let's see what we get, we get  no, now that's because remember what I said when   we initialize this array, we set it of a length of  five and we say we Okay, these are our containers,   we've got five containers, five buckets, they're  gonna hold values, we don't know what the values   are yet. Alright. So if we haven't set a value  for the fourth index, or the fourth position,   well, we don't know what it is. So we're just  going to return no meaning nothing is there   right now. Okay. So yeah, so those are, that's  how you add things into an array. Now there's,   I'm going to create another array and show you a  kind of an easier way to do this, if you're just   gonna like statically type in numbers, okay,  so I'm gonna say int summary into an integer   edge to save me from doing all these quotation  marks, I'm gonna say, and let's say num is equal   to new it square brackets. Or actually, we don't  need that, because we're gonna do the other way,   we're just gonna put two curly braces. And in  here, we're going to type out our array. So in   this case, this is now my number array. So I put  it in curly braces, and I have an array of two   354, six and six. Now again, just to recap, two  would be at index 03 would be at index one, okay?   Because that's where we start counting. And now,  if I wanted to, instead of having x equal to new   error, I'm going to say nums four, which should  be six, right? And what's our error here? Change,   this has to be an int, my bad. index equals lumps  four, we print this out, you can see we get the   value six. So that's another way to create an  array, if you're just going to be like typing in   all of the numbers like that, okay. And obviously,  you can create arrays of different types like int,   we could do string we do float, we do double,  so you say like a double array of nums to is   equal to and then in here, like 2.0 3.0 There you  go. We now have a Wow, really forgot that. Okay,   well, you need But that works. Okay, we now have a  double array at index zero, we have 2.0. at index   one, we have 3.0. And yeah, so arrays can get  fairly complicated, they're not really the best   thing to be using for a lot of example cases. But  for right now we have to understand them. in later   videos, I'm going to be talking about collections,  which are going to allow us to do some cooler   things rather than like indexing elements like  this. There's some cool methods that we can use   on those. But for now, I would just want to  make sure that we do understand arrays, okay.   So in this video, I'm going to be talking about  for loops. Now, for loops are fundamental aspect   of every programming language. And  they're very important to understand,   I can almost guarantee you that any program you  write will use multiple for loops. So pretty much   a for loop is allows us to kind of automate a  task, do something a set amount of times. And   typically when you use a for loop, you're using  that because you know how many times you want   to do something or like a condition is going  to tell you how many times to do something as   opposed to what we're going to talk about in  future videos, which is a while loop. While   you're not really sure how long it's going to go  for. So you do something based on a condition,   but we'll talk about that when we get to it.  So let's start and just do an example of what   the for loop can really like solve for us,  for example, okay, so say this is a pretty   simple example. And you guys will see in a second  directory integer, C index equals zero and say   that I want to add one, and then I want to add  two to it. And then I want to add three to it,   and four, and five and six, and like an infinite  amount of times I want to add to that variable.   Well, we could do like x plus equals one, we could  do x plus equals to like in keep going and adding   things to x. And we could just keep copying and  pasting this down our program. But obviously,   that's terribly inefficient in terms of a typing  standpoint. And what if we wanted to change this,   what if every time we want the program, we want  to ask the user how many times they want to add   like a pattern like this to x? Well, then we would  have to constantly keep changing the numbers here,   right, so this is where for loop can kind of come  in handy. So the syntax for for that, I'm just   going to type it out, and then we will talk about  exactly what it does. So it has these brackets   here and in here, we're gonna put three things  typically, okay, so what you're gonna do is you're   going to first start off by declaring a variable.  So in this case, I'm gonna say int x is equal to   zero, okay? Now this can be called whatever you  want. Typically, people call it AI, I like to use   x, but you know, let's just use AI. And then what  you're gonna do for this next, so you're gonna put   a semicolon, and then the next thing you're gonna  do is you're going to set a condition. So I'm just   gonna do this and then talk about because it's  hard to kind of do it step by step, because they   all work together, less than or equal to 10. And  I plus plus. Okay, so what I've just done here,   actually, is I first started by declaring a  variable is equal to zero. And then I've said,   we're going to do this while i is less than or  equal to 10. And we're going to add one to I.   So pretty much the way this works, is every time  we execute what's in these little square brackets   here, is gonna have one added to it. So this is  what's known as the increment. And this comes at   the end of your for loop. So this is what you're  adding to the variable i, which you're declaring   here, every time you run the loop. Now this here  is your condition. And this is going to state how   many times a loop is gonna run. So in our case,  we're going to run the loop. And while i is less   than or equal to the value 10, meaning we're going  to start at the value zero, because I equals zero,   we're going to run this loop, then we're going to  come back up here, we're gonna say, Okay, what are   we doing? We're adding one to AI, then we're gonna  check this condition, so i is not equal to one,   we're gonna say, well, is one less than equal  to 10? No, it's not. And then we're gonna loop   through, and we're gonna continue looping through  until eventually, we get to the point where we add   to i, it's equal to 1111. Well, that's greater  than 10. So we break out of this loop, and you   guys will see when I start printing stuff to the  screen exactly how this works. But I hope that   was a decent explanation of the way to do this.  So what I'm gonna do is I'm just going to print   out here and I'm going to tell you right, now  what's going to happen, right, so we're starting   at zero. So the first loop here, where I come  through is equal to zero, we're gonna print zero,   next loop, we come up, we add one, two, I check  the condition, we're okay, we can keep going.   We're at one, and we're going to print from zero  to 10. So watch when I run here, we get zero all   the way up to 10. Okay, that's how the for loop  works. So we execute whatever is in between these   little curly braces 10 times, or 11 times in  this case, because zero to 10 is 11. Right? Now,   we can change this increment, we can change this  condition, there's a lot of things that we can   do in this for loop. So here we say for int i  equals zero, I could do for int i equals five,   and I can start at the value five and now we're  going to start we're going to go from five to 10   looping 123456 times, right? We're printing six  different values, okay? So that's if you can do   that If I tried to do something like i equals  11, well watch what happens, nothing prints to   the screen. And that's because 11, well, that's  greater than or equal to 10. So the loop doesn't   even run one time, right. So let's go back, and  let's start at zero. And now let's show what we   can do in terms of incrementing. So to increment  here, we can do i plus equals, and then any value   we want. So in this case, if I do five, we started  zero, we're going to add five, we're going to   add five again. So we get 05, and 10. And you can  change this to whatever value that you want. Okay,   same thing here with the condition. These can  also be variables, I feel like I don't need to   tell you that. But if I do something like int  x equals five, then I could do i plus equals x,   right, and we can add that integer to it, we  can add x as the bound here. So less than or   equal to this could be greater than this can be  greater than or equal to. So I'm just going to   quickly go over one thing that a lot of people  get confused with, for with for loops. And it's   when we're going to stop and when we're going to  start. So when we say less than or equal to 10,   this means we're going to start at this value. And  now assuming we're adding one, okay, we are going   to stop but include 10, meaning that I will hit  the value 10, we will print 10 to the screen. Now,   if I remove this equal sign, this is only going  to happen while i is less than 10. Which means   that if I is 10, well, 10 is not less than 10. So  we are not going to print 10. So if I run this,   you can see we only get up to the value nine. Now  the reason I'm talking about this is because what   we typically want to do with loops is we want to  loop through something or look at some data. And   typically that data is in the form of an array.  So now in the slide talked about arrays before,   I'm going to create an array, so an integer  array, let's call it Arr, is equal to, and let's   just give it some values here. So like 157345,  okay, this is going to be our integer array. Now,   what do I want to do? If or how am I going to do  this, if I want to look through my array and look   at look at these values, and maybe check them  values equal to seven or values equal to five   or something like that? Well, the way that I can  do this, and this is like a dynamic way to do it,   is I could count the length of the reg and say,  123456, guys, six elements. So I would put is less   than six year. But a more useful way to do this  is to just get the length of the array. And the   way that I can do that is just to do a arc dot  length, or I actually don't need this bracket,   sorry. So we'll just do this in this way, say the  user had typed in a bunch of elements, we had put   them in an array, we don't know how many elements  they type in, per se. So we're going to use this   length so that we can change this loop and this  loop will never crash, it will always work.   Because we're just simply getting the length of  the array. Now if I were to do equal here, I hope   you guys realized this would cause us an issue.  And that's because we have six elements in the   array, right? But it's going to allow us to get i  to the value six, because that's the length of the   array. Now what happens if I try to do this arr  of six? Well, we should know from the last video   that that will actually crash our program. And  that's because when we start we start at zero, and   our last element in the array is actually going  to be index five, right, because we go to zero   all the way to the length minus one, which is  five, so race six does not actually exist. So   what I'm going to do now, is I'm just going to  write a little program inside this for loop, and   I'm just gonna say if the value is equal to five,  we're going to print it out. So how do I do this,   I'm gonna say if arr index i is equal to the value  five, then we're simply going to be a system,   dot out dot print, ln, I just realized this comma  there, alright, semi colon, and we're simply going   to print the value. So in this case, we could  print air I, which we know is gonna be five,   or we could just type five because we know  that it's there. So in this case, let's just   do found a five exclamation point, okay, that's  what we'll print to the screen. So again, the   reason this is gonna work is because we're looping  through starting at zero, and going to but not,   but not including the length of the array, make  sure you remove that equal sign, otherwise you're   gonna get a crash, okay? So that way, we're going  to look at every single element in this array,   we're going to check its value. And then if we  find a five, we're gonna print it. So let's see   if this works. found five and found a five. Now  for wanting to be more precise, and say like where   I found this five, I could say found a five at  index, and we'll just put a plus sign and then we   can put pi. And what this is going to do is it's  actually just going to automatically convert this   into a string for us and print it out with this  index. So we're going to put a space here just   so they don't get smushed together. But it says  found a five at index one and found a five at   index five and we know this is true. Index  one is here, index five is here. And that's   a really simple way that we can loop through  a list and look for a certain value. Okay.   Well, I'm going to be showing you a different kind  of for loop which is known as a for each loop. And   this is going to allow us to loop through the  elements of a list or a collection data type,   and do something with those elements, I'm also  going to be showing you ways that we can break   out of the loop at certain points. So if the  condition is not met, or like, we just want to   get out of the loop the way that we can do that,  and I'm gonna be showing you some common examples   of where we use loops. So let's go ahead and get  started. So you can see that I have two arrays,   here one's name names, and one is named AR, I just  added this names one is just a blank, blank array   for right now. Okay, so what I first want to do  is in the last video, I looped through this array,   and the way I did that was by having a variable  i, we waited until it was, what do you call it   greater than the length of the list? And then once  it was, we simply broke out of that, that loop,   right? Okay. So an easier way to do this, is to  do something like this. So we're going to have   the same exact syntax, except in these brackets,  something is going to be different. Okay? What   I'm going to do if I want to loop through every  element in this list is I can do something like   this, I can say for element colon arr. Now what  this is actually going to do, I have to do this   sorry, string element, or I can't say string  because that's something we type into element   in our array. So what I'm doing now is I'm saying  that every time we loop through this loop, right,   what we're going to do is we're going to declare a  new variable called element, and it is going to be   equal to the next element in our array. So in this  case, when we first loop element is going to be   equal to one, and then elements going to be equal  to five. And that's going to be equal to seven,   and then three, and then four, and then five.  And this is a way easier way if I wanted to,   like print out the element, or look at certain  elements to do so rather than having to have   like a counter variable i and then index all of  the different elements, especially if we don't   know the length of the array, or we don't want  to do dot length, because this is automatically   going to go from start to end. Okay. Now, I'm  going to show you because, obviously, we need an   example to really understand this. But the first  example I typically like to do is just printing   out all these elements to show you that is indeed  working. So in this case, we'll say element,   okay. And we'll just print this to the screen and  see what we get. So in this case, you see, we get   157345, right, starting at the beginning going  to the end, the way this works with the colon is   again, we're just grabbing the elements in order,  and we're using those as a variable element. Now,   a lot of times when you want to do with this is  you want to be checking the element, but you also   want the index as well. So you want the element  and you want the index. Now we could use what   we used in the last video where we just have a  counter variable, and that way we have the index   and the element because we can do like arr of  one like all that, okay, I. But a way that people   typically do this is with like an outside counter  variable that you increment yourself. So I'll show   you how this works. So what we're gonna start  off by doing is just creating variables called   int count equals zero. And then within this loop,  we're going to increment count ourself. So now,   every time that we run the loop, we're adding  one to count. So that count is keeping track of   the index of the element. So if I print out the  element plus we'll do is I can't do that plus a   space plus our count, you can see that this is  keeping track of the index. So what's happening   here says one is that index 05 index one, and it  keeps track of all these different indexes for   us. And that's a really easy way to do that.  I just wanted to show it doesn't really make   sense for this example case, but I just wanted  to show you because there will be situations   where you want both the element and the index,  and it's easier to get the element just by doing   this with this colon. Okay. So again, when you're  doing a for each, this is what this is known as,   because it's going through for each element in  the array, we are doing something, okay. Alright,   so the next thing that I want to do is I want  to show you how we can populate an array using a   for loop. So right here, for example, we have a,  what he called a new string array that's empty,   right, like all these elements are null,  and we want to populate them. So the way   that we would go about doing this, and this is  a common example of what you want to do this,   while I'm showing you this is we create a variable  and say again, int i equals zero, we will say,   well, i is less than and then names dot length,  right? And then we'll do semi colon, and we'll   simply add one to it. So this is the exact same  thing as we done before. But I'm going to show you   how we can actually add the elements because it's  a really common use case. So what I'm going to do   here is I'm actually going to use the scanner to  get a new variable or to get like a name from the   user. So I'm just going to say scanner SC equals  new scanner and then we need system.in Okay,   and actually think I should probably not declare  this every loop but put it Up here, because we're   just going to use the scanner, right? So we'll  say string, input equals SC dot next line like   this. And this way, every time we run the loop,  we're going to get input from the user. And you   know what, let's also just print out here, system,  dot out, print ln and just tell the user we want,   I should let us do print not ln, we'll say, input  like that. So that way they know what to type in.   Okay, so input, we're getting input. And now  what I'm going to do is I'm going to add the   user's input into our array. So how can I do this?  Well, this is really easy, I can just do names, i   is equal to input, right? So we're just declaring  that whatever index we are in the loop right now,   so whatever value by well, that value in the list  or sorry, the list, I keep calling it lists, it's   an array I'm going to use to Python is going to be  equal to whatever we typed in. And then if we want   to print out the entire array, what we can do is  we'll use another for loop. So we'll just say for,   and then this time, it's going to be a for each  loop, right? So we'll say for an a Stanford name,   string, and, and names, okay, then we will simply  just do System dot out dot print ln, and we will   print every value of n. Okay, so let's two good  examples. Let's run this and just make sure   everything's working. So we run this input, I'm  just gonna type Hello. I guess I wanted names in   that, Tim, Bob. Joe, Bill. And then there we go.  So we ran that five times. And we get Hello, Tim,   Bob, Joe. And Bill. Awesome. There we go. So that  gives us all the values that we need. Okay. Okay,   so now what I want to show you guys is how we can  break out of the loop. So if we're in the loop,   and we're going through, and we're doing  operations, and we've not yet met the condition,   where are like, so the loop is gonna keep  going like is not greater than whatever value   or whatnot. Okay, then we want to break out of it.  How can we do that? So how can we just get out of   it in the middle of looping? Well, there is this  magical little keyword called break. And what this   does, is whenever this is encountered inside of a  loop, it simply breaks out of the loop. So in this   case, what would happen is, since I have break  at the end, we would do these first three lines,   then we just break. And this would only happen one  time, no matter what no matter what any of this   stuff said, this loop would only happen once. Now,  I'm going to show you just down here in this loop,   how we can break out based on like a certain  condition. So for example, say we encountered the   word end. Or what do you call it? The word Tim,  what am I saying? And I was just reading that,   if we encounter like the word Tim, while  we're reading through the list of names,   then we just want to break out like we don't want  to print any more names anymore, we just want to   break out. So the way that I can do this right now  I can say well, we're gonna we're gonna print Tim,   but we'll break after Tim Okay, so what I'm gonna  do, I'm gonna do an if statement, I'm gonna say if   and equals equals or not equals equals dot equals,  okay. And in this case, we'll just simply put Tim   in here, then what we're going to do is we're  going to break. And in this case, Now, whenever   we encounter Tim, we're simply breaking into the  loop. And that means we will not print the rest of   the names. So if I run this string, this console  up here, and we run and we say, Hello, let me say   name. And then we say, Tim, and I'll just show you  this Tim, and Tim. And I run this here, you see   we get Hello, name and Tim. And these other two  Tim's do not print. And again, that's because once   we hit m, and n is equal to 10, we simply break,  okay, and we get out of the loop. And that's like   a really easy way to just break out of the loop.  And this is going to work for a while loops that   we do in what do you call it the next video.  Now typically break is not something you want   to be using a lot like you only use this if you  really have to, it's better to just make these   conditions work the way they should, and have the  loop run, like as many times as it needs to run,   you know what I mean? So just try not to use this  too much. But I mean, there's nothing really wrong   with using it, people just are frowned upon it in  the programming world using the word break. Okay.   We're gonna be talking about while loops. Now,  while loops are very similar to for loops,   anything you do with the for loop, you can  indeed do with the while loop. That being said,   they do have different use cases in terms of  where they're most useful. But just remember,   anything you do with the for loop, you can do with  a while loop, anything you do with the while loop   you can do with the for loop, they're completely  interchangeable. And if you only had one of   them in programming, you'd still be able to do  everything. It just might be not as I don't know,   nice to code, that's the right word for it, or  elegant is probably the better word. So a while   loop is going to work when you typically don't  know how many times you want to loop through   something. So it's kind of like a question.  Like, you know, it's going to be based on this   condition. But you're not really sure like, if a  for loop is more like, you know, you're gonna do   it a set amount of times a while loop is it could  change based on the program based on user input,   okay, something like that. So what we're gonna do  here with this while loop is I'm just gonna set   one up, and then we'll just talk about exactly  what it does. So in this case, what I want my   while loop to do is I want it to just continually  keep asking the user for input until they give   me like a certain word or like one or two words.  Okay, so in this case, what I'm gonna do, I'm just   gonna say, let's see, maybe just get an answer  to be faster, say int, x equals SC dot, next int   like this. Okay? And what I'm gonna do is I'm  just gonna keep looping through until they type   the number 10. Okay, so all I can do this as you  just type the keyword Well, in here, and then in   these brackets is going to be your condition. Now,  this is why I focus so much about conditions in   the first few videos, because they go a lot with  if loops while loops for loops. So whenever while   this condition is true, then we're going to run  the loop. So in this case, I'm going to say, well,   x does not equal 10. Okay. Yeah, exactly. That's  true. So if they type in 10, then you will not   continue to do this. Otherwise, we will continue  to do this. So that means I'm also going to have   to get the INT in here, because every time that we  run this loop, we want to continue getting x. So   in this case, we already have X, fine, we just do  x. And what I'm gonna do is if they don't type in   10, I'm simply going to tell them like type in 10.  So we'll just say, dot print, ln, in this case,   I will just say, type 10 dot, dot, dot. And  now what we're also just going to print out,   because I like to do this, we'll just do system  dot out, dot print, ln, and in this case, we'll   say type a number. Cesar knows what we're looking  for. Okay. And that means we're also gonna have to   print this here. And it should just be print, not  print and print ln my apologies. Okay. So again,   the way this is gonna work is we're just  going to continually keep looping through   this until eventually the user types in  10. In that case, we will break it, okay,   and we will not do this anymore. So let's run this  type of number, let's type four. So it's type 10,   data, type a number, type five doesn't work, type  10. There we go. And we broke out of the loop,   and it no longer asked us for that number. Now,  these are actually really simple. It just wow,   this condition is true. Do everything that's  in here. And that's all you really have to know   about while loops. Now again, like we can what  I was saying we can do everything that with for   loops with while loops, because we can also count  and Wallops as well. So just like I did before,   I could do something like int count equals zero.  And then every time we run this loop, we could   just say count plus plus. And then maybe at the  end of our loop, we wanted to do like, tell them   how many times they messed up. Okay, what am  I saying print system dot out, dot print, ln.   And in this case, we'll just say you tried, tired,  you tried. And then plus couch. plus, times scan,   make sure we add a little space here. Alright, so  now if we run this, and we'll say like 123 and 10,   you tried three times until like, eventually, you  got it. Okay. So that's like a really simple way   to do stuff with while loops. Now, you guys  might notice that this code here like this,   these lines are repeated up here. So I have this  index and this type of number. So how can I do   this without repeating those lines? Because  I don't want to have to type like, what if I   wanted to ask a ton of different questions and  then do the wallet? Well, I don't want to have   the same section of code appearing multiple times.  So there's actually something called a do while   loop. So what I'm going to do is I'm just going  to erase all this. And I'm going to set up this   a do while loop. And it's pretty much what it says  we're going to do everything while this is true.   And you'll see how it works in just a second.  So I'm going to type the keyword do brackets,   and now everything in this brackets is what is  going to happen. Wow. The condition that I put   down here is true. So the condition here is going  to be the same. Well, x does not equal 10. We're   going to do everything in here. So now you can see  this is working fine. There's no issues. And what   I'm going to simply do is I'm going to take this,  these two lines here, I don't need that scanner.   So we're not going to redefine that every time.  And I'm going to paste them in here. And what   this is going to allow us to do now is x does not  equal zero. Create local, gosh, okay. Let's just   do this. int x and then x. Okay, perfect. There  we go. So sorry, that was just a quick issue.   But what this is going to allow us to do is we're  automatically going to do this once no matter what   this is going to happen once because we're going  to do this and at this point x is not equal to 10.   So we're automatically going to do this once which  means we don't have to have it up top and then   down below. So we do that. And then we check the  condition. If it's true, we'll do it again. Yeah.   And if it's not, then we will break. So we can  do this, we'll say type a number, let's do four,   let's do five, let's do negative 00. Number, okay,  and 10. And there we go, we break out of the loop,   and everything is working fine. And that  is pretty much it for while loops. Again,   if you wanted to set one up to look like a for  loop, then you'd literally just have to create a   variable in this case, say like index equals zero,  you can set up a while loop. So you say a while   and you say, x is less than or equal to 10. And  then in here, you just say x plus equals one,   or plus equals two, or whatever value you want to  add to it. And then you can just do what you would   do in the for loop underneath your increment.  Or you could put the increment, typically you   put it at the end. So at the end of the loop,  you're adding one and then you're checking, okay.   Now in this video, I'm going to be talking about  sets and lists. Now sets and lists are from   something known as the collection interface from  Java, I believe, that's what it's called someone   correct me if that's wrong, but I think that's  the name. And they are fairly complicated. Now,   I'm just going to talk about really the basics  of them, there's a lot more to do with sets   and lists. I know, I'm probably gonna get some  comments from some guy who says, Wow, you really   didn't talk about a lot of this, you didn't. But  I can't explain all of it to you guys right now.   Because it's very difficult to understand, if  you don't know a lot about data structures,   which in our case, we don't. So I'm just going to  talk about what a list is, what a set is quickly   in a very basic standard implementation of them.  Now, the reason I'm doing this is because whenever   you're programming stuff, like a lot of Java  tutorials you watch probably won't talk about   these, because they're really complicated.  But you're going to need them to do certain   things. So for example, with an array, what I  talked about before, how you could like, you   had to set the size of the array, like you had to  know how many elements you wanted, you don't need   to do that with a set or with a list. And that's  the main advantage of it. Because a lot of times,   we're gonna be adding things into per se a list or  set, and we don't know how many things we're gonna   be adding. So we can't define like an array of  that length, if that makes sense to us. So let's   actually just get started, we're gonna talk about  sets first, and then we'll move into lists. So I'm   just gonna create a set, and I'm just gonna type  it out and then kind of explain exactly what I'm   doing, I'm just going to call this set t equal  to new, new hash set, and integer. Okay, so this   already probably looks confusing. And if you're  typing this out with me, you're getting these red   lines. Now, what we should do to get rid of these  red lines is we just have to hover over them and   click Import set by like clicking on whatever it  says there. If you don't have you're not using   this Id just literally type import Java dot util  dot set. And for hash set, same thing, we're gonna   click on it and import it, just because this isn't  like built into main, the Java functionality.   Alright, so we have a set. Now what is a set?  How do we create it? Well set is a collection of   unordered elements that are unique, meaning that a  set cannot contain the same element twice, and it   doesn't know where that element exists. So you can  kind of think of a set as just like a big bubble,   and you kind of just throw things into it. And  it doesn't really know where they are, they just   knows that they exist there. Now you also have to  understand that can only be unique things. So for   example, I've created this integer set. And this  is the way that you do it. So you type the keyword   set. In these little like greater than sign  less than sign river, you want to call it tags,   if you're talking HTML type integer, and I have  the name T, and I'm setting that equal to new   hash set. Now you don't have to understand what a  hash set is just know that it's like the standard   implementation of a set. And then same thing, the  types are integer and then these brackets here.   Now inside of these brackets, if you had another  set you had created, like say a set created a set   called like W, I could put w in here. And assuming  it had any elements in it, this set would get all   of the elements from W and start with that and  then you could add things into it and whatnot.   Okay. So to add things to set, I'm just gonna  do this. We'll go through and see how it works.   You simply type dot add, so I'm going to say T  dot add. In this case, I'm going to add five.   And we'll just copy this and we'll add like a few  different numbers into our sets just because it's   seven, and what five again, and add nine. Okay?  Now what I'm gonna do is I'm just gonna print   this set out to show you what it looks like. Now  remember, I said unordered collection of unique   elements. Okay, so let's print this out. And you  can see that we get five, seven and nine. And   notice that this odd right here actually didn't do  anything. And that's Because since we already have   a five in our set, when we try to add another five  to it, since it already exists, it doesn't care   and it just doesn't add it. Alright, now I'm gonna  add another element. In this case, let's add like   negative negative zero, negative eight, alright  as our element. And you can see that we don't   have any specific order. Now, see, when I printed  this, like I had added 579, and negative eight,   so you think that should be the order that we  get it or it should come in some sorted order   or something like that. That doesn't happen with a  hash set. Okay, standard hash set. And that's just   because again, we're just a bubble, we just know  that things exist, we don't care where they exist,   or how many times they exist, we just care if they  exist. Now to see if something exists in a set,   and this is typically something you want  to do like this is a main operation,   checking if something exists, that's why you use a  set, you can type T dot contains, or like your set   named contains, and then any element in here that  you want. So in this case, I can do like, if five   is contained in the set, and it's going to be able  to tell me that really fast. Now, I can't really   explain to you why sets are so fast. But just know  that whenever you're looking for something in a   set, you can do that very quickly, like very fast,  like in constant time, it doesn't matter how big   the set is, the set could be 2 million elements,  or it could be five elements, it will take you the   same amount of time to look if something exists  in the set. Okay, and same thing with adding and   removing things from the set that happens almost  instantly. As opposed to with arrays, the larger   the array gets, the longer it's going to take us  to look through and find certain elements, okay,   just, we have to understand that. So what I'm  gonna do is create a variable, I'm just gonna   call Boolean, x is equal to this. And what I'm  going to do is I'm simply just going to print   x. Now let's see if that is contained. And in  this case, yes, it is, five is contained. Again,   if I do something like zero, then obviously we  get false, it's not in there. To remove something,   we can type T dot remove, or set name dot remove,  and then whatever the element is that we want to   remove. So in this case, we want to remove like  nine from my set, I would do that. And then if I   print my set, so in this case, t, which get five,  seven, negative eight, because we removed nine,   those are kind of like the standard operations,  to I'm trying to think if there's any other ones,   okay, so there's one more, or there's a few more,  but I don't know which ones are important, you can   clear an entire set by just doing dot clear. So  just simply remove everything from it like that.   To see if a set is empty, you can do.is empty, and  this will literally just tell you if it's empty   or not, to get the length of the set, you can do  dot size, and this will tell you how many elements   so in this case, value, size, change this to an  int, and just print x here, then we get a size of   three. So like, again, if we had nothing in there,  the size would be zero, so is empty, and that are   kind of like similar in the sense that you can  just check by the size. Um, and yeah, so that's   a hash set. A hash set is like the standard set.  And that's the way that it works. We explained it   to you. Now they also have a tree set. And we also  have a linked hash set. Now whenever you use these   things, they're gonna pop up here, I'm not really  gonna talk about what these do too much. I'm just   gonna kind of show you the way a tree set works.  Now tree set is similar to set in the sense that   you can only have the or a hash set, sorry that  you can only have unique elements. But these are   actually ordered and they're ordered in a tree  data structure. You don't have to understand what   this means just know that it's like it shows  up ordered. what's what's our problem here,   just want to read this error. Tree set cannot be  resolved to a type. Interesting. Okay, Teresa,   well, let's just try this. That still worked.  Okay. So anyways, I don't know why I was showing   me there. But Teresa is going to actually  give us an order. So if I type T like this,   you can see that we actually have an order  for the set now. So negative eight, five,   and seven. Now like arrays, like we can't just  index the set. And we can't just say like t zero,   like that doesn't work doing that, like we were  able to do with the race. But when we print it   out, or when we look through the set, it maintains  this order in the katri functionality that I can't   really explain to you. Now linked hash sets are  this. So linked hash set again, we're gonna have   to import this up there. It's linked hash set. And  this is similar to a basic set it just faster on   certain operations. So I'm not I don't really  want to talk about them too much, because I   feel like I'm already confusing you guys, but  just understand if maybe you know a bit about   like operations and speed and time complexity.  They have a linked hash set, a regular hash set,   and a tree hash set or tree set. Whatever I typed,  okay? Alright, so that's enough for sets. Don't   worry if this is a bit confusing, like, we're,  we're not going to be using these too much,   I just want to show you cuz I feel like you  guys are going to need to do this for some of   your projects or whatnot if you're learning this  for a reason. Now, we also have lists now lists,   these are easier to understand, don't worry. But  there is again, two types of to create a list,   what we're going to do is we're going to type  array list, we're going to give it a type in this   case, I'm just gonna give my integer type. And  notice how I'm not typing int I'm typing integer,   just that's important that we need to use integer  here instead of int. Okay, so ArrayList integer,   I'm going to call this one Tega and equals  new array list. And we'll type integer,   and we'll put brackets and there we go. Now we're  gonna have to import this so important ArrayList   up there. And we now have an A list. And what  a list allows us to do is it's, you can almost   think of it as the exact same thing as an array,  except it's a bit slower. And it can change sizes,   so we can add things to it, we can remove things  from it. And if we get like the end of the array,   or the end of the list, sorry, we can add another  element into it, it doesn't matter. So we can have   like a dynamically sized array. These have pretty  much identical properties to the set. So to add   things, you can do t dot add. And you can add like  an element you want to add like one right? Okay,   that works fine. To remove things T dot remove.  Now to index things, because just like an array,   we can actually index things with the list  because this is going to care about the position,   it's not going to just care that it exists like  a set, what we can do is we can do t dot get,   and then we can give an index, right? So even it's  showing, it's always saying index right here. So   I do dot index zero, then that would give me one,  right because one is added in there. Now to like   put thing to put something or to set something on  a certain index where you do this is T dot set,   and you type the index and the element. So I want  to set index one per se to be equal to like five,   then that's where I would do it, index one is  five, okay, we print out t here just to show   you what it looks like. Okay, sorry, that's we  can't do that we have to do t, we have to add   something. So to set something like this, sorry,  you have to actually have something added at the   index. I forgot I didn't add that. So I'm just  gonna add to what I'm doing now essentially,   is what this set actually does is you have to  have something already existing at that index.   And this is going to change that index for you.  So this is to change an already existing index.   And before I was trying to set one, when we didn't  have that in so that was my mistake. But anyways,   there we go. So now that works. We've changed  to equal to five and twos up position one,   right. So that's the way that that works. I've got  a thing up here, I'm just going to read through a   few of these set. Yeah, okay, so that's the basic  ones. Again, we have dot size, so T dot size,   we can do t dot empty, or is empty. And this  is actually really useful when it's called sub   list. And what this allows us to do is get from  certain indexes. So T dot sub lists, let's see at   argument, yeah, so we just need an int. So this is  going to allow us to get within a certain range.   So say I add like these a bunch of times. Okay,  so we have six elements. Now, what, what I want to   do is I want to get from like index one to index  three, and I want to print that to the screen. So   let's just take this and put this here instead. So  let's get rid of TIG or the semicolon, and print   out and see what we get. So we get here now is  five and one because although our list actually   has what do you call it, like six elements in it  from here, we can just get the elements from 123,   not including three. So if I type like 124, then  you should see we get one more element in here,   we get five one and two, because that's from from  index one to index four, but not including index   four. That's what we're grabbing, we're getting  all those elements in the the sub lists like that.   Now, this is ArrayList. We also have linked list.  Again, this is another data structure that I can't   really explain to you just understand that it is  faster at certain operations than an ArrayList.   So for any of you to understand the difference  between this you have linked lists and ArrayList.   And you guys, I recommend you to play with  those and figure them out on yourself. Anyways,   we're coming up on about 15 minutes now almost.  And that's pretty much all I wanted to talk about   with sets and lists. Now I know this video  might have been a bit confusing. There's a   lot of stuff that I wasn't able to explain to you  guys. But just understand that if you're trying   to do something and you don't know how long you  want something to be like a list or an array.   You don't know how long you want it to be. It's  a different amount of time. If you don't know,   you would just use an ArrayList the way that I  showed that to you before, so ArrayList like that,   because then you can add things, you can get  things, you can set things. And you don't have   to worry about like having a designated length.  The reason you use a set is when you don't care   how many times something exists or where it  exists, you just care if it exists. And the   reason a set is better is because it's simpler.  And it's a lot faster to do operations on in   terms of like a computer efficiency standpoint,  that's when you would use a set. I'm going to   be talking about hash maps, or like Java maps. The  reason I say it like that is because there's a few   different implementations of maps, similar as to  how there's different implementations of lists,   and like ArrayList, and all that stuff. So  maps, if you're coming from Python, like me,   you can think of them as dictionaries. And I'm  only talking about three different types of maps.   So a hashmap, a tree map, and what was the last  one, I got it written down here, linked hash map,   sorry, just forgot about that one. So pretty much  a map is known as a key value pair in any other   languages, they're typically called dictionaries,  or hash tables, or hash maps or whatnot. Okay, so   to create a map, and I'm just gonna do this, and  then we'll talk about exactly what it does, we're   just gonna create a map M is going to equal to new  hash map like this, okay. And this is your simple   syntax, now to we have to import this. So I'm just  gonna import Java dot util. So just by clicking on   it, we have to import map as well. So we'll import  the map there. So you see those. It's all coming   up now for us. Okay, so now that we have that, I  want to talk about exactly what a map is. Now map,   like I said, is a key value pair, meaning that  it's similar to like a list and an array in the   sun, so you can index things. But instead of  indexing them by numbers, you index them by   keys. Now keys can be anything that you want. A  key could be a string, a key could be an array,   actually, I'm not sure if it could be an array,  but it could be a string, it can be a number,   it can be a char, it can be a float, what like  pretty much whatever you want can be the key.   And that key links us to a value. So the easiest  way to kind of demonstrate this is just to do it,   and then to talk about what's really happening. So  to put a new value into a hashmap, or into a map,   what you do is you have to specify a key, and  a value. So you type whatever the name of your   map is, in this case, m dot put, and then you  need to put a key in a value. So in this case,   for my key, I'm going to type Tim, and it is going  to lead to the value five, so I type Tim, and then   the value is five. So I put that in there. And  now I'll just show you what it looks like if we   print this out to the screen. Because I think it  will give us a decent representation system. What   am I saying System dot out dot print ln Sorry,  guys more tired today. And we'll print em and   see what we get. So in this case, you can see we  see this a curly braces as Tim is equal to five,   meaning that if we're to index Tim, we get the  value five. Now, how do we do that? So how do we   actually get a value based on a key, the way  we can do this is if you put square brackets   next to your map, are actually you can't do like  this Python, we have to m dot and then get Sorry,   I'm still used to the Python syntax here, guys,  we type m dot get Okay. And then we put the key   in here. So in this case, I'm going to put Tim as  my key. And what you should already be predicting   what we're going to get out to the screen. In this  case, we get the value five and not as because   the key Tim leads us to the value five. Now to  put another thing into our map, we can do this,   right. So we do Tim we could do like Joe, and Joe  doesn't have to point to a number, it could point   to another string. And that string could be like,  I don't know x, okay. And we can just do whatever   we want. Like we can have the the key one and or  the key 11 and the key 11 points and 999. Okay,   and now if I just print em out, you'll see that we  don't get any issues. And that this works fine, we   get Joe equals x Tim equals 511 equals 999. Okay,  now, this is an extremely fast data set, meaning   or don't type, whatever you want to call it,  meaning that everything from adding to removing   to overriding to getting happens in constant time.  Now again, like I talked about in my stats video,   you don't really have to understand what that  means. But if you do, that's great. And just   know that this does happen in constant time. Now  I'm going to show a, I still show the tree map   and the what was the other map talked about linked  hash map first, and then we can kind of talk about   the differences between them. So with a hash map,  as opposed to the other type of maps, I'm going to   show you this one does not retain an order. So all  of these maps can only contain unique elements or   unique keys meaning that if I try to add another  key, so I try to add 11 nine Try to add this to be   998. Instead of adding another key that's equal to  11. We're simply overriding this already existing   key that is 11. So in this case, you can see that  we override it and we get 998. You cannot have to   have the same keys existing in the map. But you  can have to have the same values if you'd like to.   Okay, so sorry, that kind of got me off track.  But that was important to understand. So this   hashmap does not guarantee the order in which  we add things in. So you can see that I added   Tim, and then I added Joe, and then I added 11.  And it showed me Joe, Tim and 11. Now this is not   any kind of sorted order, because how do we sort  numbers and strings and all that stuff together?   It's not in the order that we added it in. So what  order is it in? Well, it is in no order. And that   is why this hashmap is extremely fast, because  it does not keep track of the order of elements   when they go in. So just know that if you're  trying to like look through the map, because   you can iterate through maps, and I'm going to  show you that in a second. It doesn't keep it in   the correct order. Okay. Now, the next map that  we're gonna talk about is the tree map. Okay,   now this tree map is what we're gonna have to  import it, of course, is similar to the tree list,   or the tree set, or whatever one I showed you in  the earlier video. In that when we add things in,   it's actually going to keep it in a sorted order.  So if I run this, I'm actually curious to see what   kind of sort we're going to get. Okay, so we  don't get anything. So that's, that's actually   a good area to run into. Whenever we're using a  tree map, the types that we add to the map have   to be the same, meaning that the keys have to be  the same datatype. At least I'm pretty sure. So   I'm gonna just gonna see if I remove this, if this  is going to work for us. Yes, so in that case,   we do get in a sorted order, we get Joe and then  we get Tim, because obviously j is before T. And   that's where it's going to sort strings. So if I  try to put like an A value in here, so it's Yeah,   it's even true on a key value, I'll literally  just put like a is equal to b. And I guess we   can't set equal we have a comma. What's our air  semi colon, always forgetting those are? Okay,   so we'll run this. There we go. So you see, we get  a first because obviously a well it's first letter   in the alphabet. So that's going to show up for  Joe. And for Tim. So that sorts the order for us.   And that means that the data types for the keys  that we pass in have to be the same. So we can use   numbers, we can use strings using whatever data  type you want, there's got to be completely the   same for that map. Now, the other type is a linked  hash map. And what this does, is it's similar to a   list in that it keeps the same order that you add  things into it. So in this case, if I print out m,   you can see we get Tim Joe and then a and that's  because it's actually going to maintain the order   in which we added elements. So it's gonna say,  Tim, that was first element. So that's gonna   be the first thing that we show. And then Joe,  that's gonna be the first thing and then a nursery   second, and then a, this is gonna be the third  thing, because that's the order in which we added   them in. And those are the only main differences  other than, like, the speed of which these things   run out, that you have to kind of understand  right now, okay, and typically, whenever you do   anything, you're just gonna be using a hashmap,  you don't really need, like a linked hash map,   or a tree hash map, or tree map, whatever it's  called for any of the stuff that we're gonna   be doing right now. So what I'm going to do now,  is I'm just gonna show you a few methods, how we   can kind of clear the maps, how we can like remove  elements, and some useful things you might want to   do. So what we can do to get all the key values  or to get like a certain key value, we can say,   Actually, I'll just show this one first. Well  we can actually do is is just cool things that   are going to be really useful to you. And dot  contains value. So this is let's see, this method   contains values not available for type map. Hmm,  interesting. Match contains value object. Yeah,   so we just have to put something there. Okay.  So m dot contains value. And then in here,   we're just going to put like any value on so in  this case, B. So what this is going to do is it   is actually going to check for us if this value  exists in the map. And remember, the values are   all these these second elements here. And they  are linked up or like attached to the key. So   it's going to tell us if b exists. Now we can do  the same thing with keys and the keys are more   useful. And I'll show you why in a second contains  key and in this case, for the key I could put like   contains the key five, right and that will give us  a true or false value similar to what we're doing   with sets and lists in the last video. Okay.  Now the reason keys are more useful is because   if I try to do something like m dot get, and I  put the key, let's say five, well the key five   does not exist, the value five exists, but the  key five does not exist. So watch what happens   when I run the program. m dot get five actually  let's just see what this is printing out to us.   Because I'm curious if this is gonna crash. Let's  Let's see here. I'm not getting five No, yeah,   so this isn't gonna actually crash for us. If we  try to get Ah, what do you call it a key that does   not exist in the map, but it's going to return us  a no value because this key does not exist in the   map. That's actually interesting. I didn't know  that in Python, if you try to get a key from map   or from a dictionary, and it doesn't exist, you  actually get an error. So that's interesting to   know. Anyways, I guess another method I could  show is, let's see here, dot values. So what   this is going to do is it actually just prints out  all of the values in the map. So if I copy this,   and I simply print it down here, we get m dot  values. And in this case, it'll just give me x,   b, and five, which are all these and you can  see, it gives me that in no particular order,   it's important to remember, values, let's see  if there's any other ones we can use. We can use   clear so I just got a text document beside me cuz  I always forget all these to show you, I always   forget which ones. So Am I clear? Obviously,  this is just going to remove everything from the,   what do you call it the map. So we get an empty  map. And I believe the last one is, is empty. And   this one like that is simply just going to tell us  if the map is empty. Or if it's not up to you know   how much time we had 10 minutes. So I actually I  am not going to show you an example of using these   maps. But if you want to think about this, and  maybe try programming this yourself using the maps   that I just showed you be good exercise. And that  is given a string, or like an array of characters,   count all of those characters into a map. So  have a key that's equal to, let's say the letter,   and then the value for that key is equal to the  how many times that letter occurs, or how many   times that character occurs, and try to do that  yourself. And if you're able to accomplish that,   well, then you're definitely learning and you're  understanding the stuff that I'm explaining.   What I'm going to be doing is showing you some  common problems you're going to want to solve   and how you can do that with the information  I've showed you so far. So kind of bringing   everything in now, mixing it all together, and  using all the things. And this is a really good   way to kind of like apply all the knowledge that  we've just learned into solving like a few common   computer problems. Because a lot of the time  you guys learn how to do all this stuff, but   you don't know how to kind of mix it all together.  And that's what I'm trying to do in this video. So   I'm also going to be showing you some like useful  methods and some things that I may have forgotten   to talk about in previous videos. So yeah, that's  what this video is going to be about. So in the   last video, I mentioned that a good exercise to  do would be if you have like, I don't know a word,   and you want to count or like a sentence or a  string, you want to count all the letters and   store them in like a hashmap and have the letter  as the key and the value being how many times they   appear. So I figured for any of you guys that  were brave enough to attempt that on yourself,   I would show you a solution here quickly to doing  that. And for any of us who didn't see that,   make sure you pay attention, because this is  really useful, it's going to help you also to   understand kind of how maps work and how you can  use them. So what I'm going to do first of all,   is I'm just going to create a string, let's  call str is equal to and I'll say, Hello,   my name is Tim. And I am Cool. All right,  very creative string name anyways. So we're   going to do that just because we're going to  count all the letters in the string. Now the   human way to count this would be to go through  and literally read like each letter and be like,   okay, so I have an L how many times the L appear.  But I will show you how we do this with maps and   for loops, like very efficiently in computing.  So what I'm going to do is create a for loop,   what we're going to do is we're going to loop  through the string. And the way that we can do   this is because if I try to do like for char  our like x because it's going to be character,   right. And in str you see we get this red line,  it says we can't iterate over a string. So the way   that we can do this is actually a cool method and  a useful method. And it is going to convert the   string into a character array for us. And the way  that we can do that is by doing angas dot.to char   array. And this is if I literally just print this  out for you, if you want to see what it looks like   System dot out dot print ln is simply just going  to put every character so including the spaces   into a character array for us. So let's just run  this. And you can see it just is literally just   printing this entire thing, but it's actually  in an array, and this is going to allow us to   loop through it. So you'll see in a second. Okay,  so what I'm going to do now is every character,   I'm going to see if that key already exists in  the map. If it does, I'm going to get the value   because that value is gonna be how many times it  exists. So in this case, say I'm looking for like   m it already exists in the map, I'm just gonna  add one to it and then overwrite that key with   a new value. And you'll see what I mean here in  a second. So we're gonna say if m dot contains   Key of x, then what I'm going to do is I'm going  to say, old, we'll say int old equals m dot get,   and then the key is going to be x. And then what  we're going to do is, what's the issue here,   cast int, if I need this, forgetting all my  brackets, int old equals m dot get x, and x   should be interesting one second, saying I have to  cast this to an integer. Okay, let's just do this,   I don't think we actually need to do this, but  let's just cast it to an int. All right, anyways,   and then we'll say, dot quotes. And for the key  is going to be x two, that's going to be the   character. And we're gonna say old plus one. Now,  what this should do for us is just increment the,   the amount, so that's going to go up by one  now otherwise, so if that key does not exist,   what we're going to do is we're going to put  into our map, a new key, and the keys value   is going to be one because we just found the  first occurrence of that letter, and the key   is going to be x standing for the letter. And  that should work. Now after we do that, I want   to see the counts of all these letters. So to do  that, it's really help I spelt system correctly,   we're going to print the line, and we're just  going to print them. Okay, so quickly, I know   this fast, and you might not really understand  exactly what's going on. But we're just looping   through every letter in this string, we're going  to check, the first check is going to see if the   map contains that letter already. So as a key,  if it does not, what we're gonna do is we're   simply going to add in a key, and it's going to be  equal to that letter, and it's gonna have a value   of one. Because we just found it occurs one time,  we already know that. Now, if for some reason, or   if that key does exist, what we're going to do is  we're going to get that previous count. And then   we're going to add one to that previous count and  override the key. So like, if the count is four,   it goes up to five. Pretty straightforward. So  if I run this, you can see we get a space, well,   there's eight spaces, we get a, there's three AC,  there's one D, there's one, and it goes through   an account all of the different letters for  us. And if we wanted to confirm those correct,   we could go through and count them like  that. And just by looking at it here,   it does look as though that is correct. So yeah,  that's a really common way to use a map. Um, yeah,   it's really useful. And there's a lot of cases in  which you want to do something like that. Okay,   so the next thing I want to show you guys is  something I forgot to do in the last video. And   this is really just gonna take like 10 seconds.  But pretty much to remove an element from a map,   you probably already guessed, but it's literally  just m dot remove, and then you just type the key.   So I just felt like I had to say this, because  I watched back my other video, and I realized I   forgot to say it. So to remove an element, you  do m dot remove, and just put the key. So if I   want to remove like all the spaces, which actually  wouldn't be a bad idea to remove from our counts,   then if I do that. Interesting why it's not  letting me remove that. Let's try that maybe, oh,   it's because it's not because of character. My bad  Sorry, guys, I've been doing string a character   space is different. Yeah, so that it actually  removes the character, the space from our program,   or from our map, whatever. Okay. Now, the next  thing I want to show actually is sorting. So   sorting is something you typically want to do on  arrays. So I'm actually going to remove all this,   and I'm going to create a new array, an  integer array. And I say int x equals,   with cases do like three, and let's do a  bunch of different values and see if we   can get this to sort for us. Let's do negative  99. semicolon there for us. And to sort this is   actually really easy, all we have to do is just  type arrays, dot sorts. And then what it takes,   it takes two arguments. Now the first argument is  obviously going to be our array. So in this case,   x, and then the other two arguments are  actually optional. And what these do sources,   three arguments not to what these arguments do is  from a certain index, so if we want to sort just,   let's say, like this part of the list that's  highlighted, we can actually do that. And the   way that we would do that is we would type one  because we're gonna start at one. And then if   we were going to 71234567. Funny enough, we will  actually type seven. Now I know this is index six,   but the thing is, it's going to work up but not  including to that index. So if we type seven, it's   going to that seven, but it's only going to sort  up to like the actual number seven. Okay? And what   this does, and I believe is actually just changes  the list, we don't need to say like x equals   Arrays dot sort, we just say Arrays dot sort. And  then what we can do is we can do system dot out,   dot print ln, and we can print x. And let's see if  it did end up sorting that section of the list for   us. Okay, so we actually, man of course, we can't  do that. We're gonna have to set up a basic for   loop to print these out just because when we try  to print arrays, I forgot we get that little loop,   or we get that message because that's the memory  address. But anyways, let's say for int, i and x,   and then we'll simply just print out I, it's got  to make it complicated for us, doesn't it? And   instead of printing, ln, we're just gonna print.  And we'll just add a comma here just to make it   separated. Okay, so let's run that. So there we  go. So it actually did sort this middle section   of the list for us, right? So we got 1234567, you  see eight and zero, were not touched and negative   99 was not touched. Now, if you want to sort the  entire thing, and you don't want to worry about   these indexes, you can just put x, in this case,  if we do this, we're going to get an 899, zero   all the way up to eight. And yeah, so you could  sort like from three to five, or three to six,   or whatever, okay. And you can see, it's only  going to sort like a few of the elements in the   list for us, or in the array for us. So that's a  really useful way to sort things at quite quickly,   actually, in quite easily using just Arrays  dot sort. And remember, you don't have to do   like x equals Arrays dot sort. Because what it's  actually going to do is just sort in place all the   elements of x and swap them around, rather than  creating like a new version of x that is sorted.   Where you're going to be introducing object  oriented programming, and talking about what   objects are and getting a little bit into  classes and methods. So I think the first   thing to really talk about is what is an object.  So an object essentially is just an instance of a   certain data type. So we know we have data  types, like int strings, bool, scanners,   like all kinds of different things that we can  use, like ArrayList, hash table, hash, list,   whatever, or hash map all that stuff, okay. And  those are all known as data types, right? We're   classes if you want to say that. So whenever we  create an instance, by doing something like this,   like scanner, SC equals new scanner, essentially,  what we're doing is pointing this variable SC   to a scanner object, okay. And whenever we create  any kind of variables, like just type like int   x equals five, we're essentially saying x  is equal to an int object of value five,   okay? These objects are what allow us to have  different properties for different data types.   So you know how we can add integers together, or  how we can do like, dot parse int, on on strings,   and we can have like a bunch of different methods  and stuff. Those are all specific to the type of   object that we're using. So, for example, we  can see here I have the scanner object, right,   so SC is equal to a new scanner object that's  taking system.in as an argument, okay. So we can   only use this dot next method on SC because it is  a scanner type. If I try to do X dot x, and I do   a little semi colon here, you can see that we're  getting can't invoke next method on primitive type   int, because well, that method doesn't exist for  int, okay. And that's like a really basic way to   kind of understand what objects are essentially,  whenever we're creating a new variable. So like,   even just doing like string like, str equals,  hello, what we're doing is we're saying, well,   str is actually equal to a string object with  the value alone. And because of, or based on   the different types of objects, we have different  properties, different attributes, things we can do   with the methods we can call in, that's just kind  of important to understand, you'll see more how we   create like objects and whatnot, that are specific  to like classes that we're going to make in later   videos, okay, so it'll all start to come together.  But just now kind of know that what I'm calling   things objects, essentially, that's like you have  a variable of a certain type, and it's equal to   something, you're creating an object of that  type. Okay, that's all we can have to understand   for now. So now let's talk about methods. So  some of you may already understand what methods   are because I think I went through a very brief  like explanation of them in previous videos. But   essentially, methods are anything that you call on  an object or on an instance of an object. So just   to clarify, when instances when we're creating  a variable like this of a new scanner object,   we can say that we're creating FC is now an object  like a scanner object, or we can say, se is a new   instance of scanner of like the type of scanner,  and those kind of are interchangeable. So if I say   instance, instead of object, they're very similar.  Okay, so just bear with me on that. So anyways,   when we call this dot next method, we call it  on the like, variable, right? So that's, that's   pointing to our scanner object. So our scanner  object allows us to use this method. Again, I   showed you before, if I tried to call that on, for  example, the variable x that was like an integer,   that doesn't work because that object doesn't  have that method associated with it. So a method   is anything really that's just like a dot, and  then whatever the method name is, and typically   brackets, for example, if we had like, an array  like so we have or let's do actually strings are   just another string probably should have deleted  all these. But that's okay. We'll say string H   equals Hello. And I can do something like H dot  length. And this is a method that simply returns   to us the length of the string. And again, this  works on our object, which is a string, but it's   not going to work on our SC, because well, what  is the length of a scanner. So if I type that, you   can see that we're getting this red line here. And  that's obviously not giving us anything, because   this length method is not defined for a scanner  type. So that's kind of a bit about like how we   call methods. And you guys have already seen a lot  of different examples of methods that we can use   on different objects and different data types.  Okay. And same thing when I say like data types   and objects, data types are kind of like what the  object is created off of. And you'll see that more   in in future videos. Just want to clarify in  case anyone's kind of confused, okay, so how   can we create our own methods? Well, currently,  since we don't really know anything about classes,   we're going to be creating methods inside of this  main class. Okay, now, this main class is special,   because it is it contains this method. And  remember, I was telling you guys at the beginning,   this method automatically runs whenever we run the  program. Well, that's different than other methods   we're going to have in classes we create in future  videos. So it's a bit hard to explain this method   per se. But this is a method because a method is  typically anything that sits inside of a class and   the class is what's going to define like our data  types. So scanner. In Java, we can't see it right   now, there's actually somewhere that says public  class scanner, and inside of that scanner class.   So if you do like brackets like this, it has a ton  of different methods that are different attributes   that we can use by calling them from within this  function. Okay. So if we want to create our own   method within this main class, what we can do,  and this is just the default way that we're   going to do for now, I'm going to show you why we  use certain keywords and stuff in future videos,   but it's a bit advanced right now. We're something  in type, public static, okay. And then whatever   return type we want, which we'll talk about in  a second. So let's get going to void the name   of our method. So in this case, I'm gonna say  like, Tim, okay. We have a public static void,   Tim has your parameters, again, talk about that  in a second. And then it's going to do something   in here. In this case, I'm just gonna say system,  dot out, dot print, ln, and we'll just say, when   you call up, it just says Tim, like that. Okay. So  what have essentially done here is create a static   method that we can call from anywhere within this  class, okay, and actually outside of the class as   well. So what we need to do to call this method  from inside this class is we can literally just   type the method name since case by type Tim. And I  put a semicolon like that, and we run the program,   you see that we get is it running? one second  here, sorry. Okay, so I figured out why it   wasn't working, because we have this SC dot next  coming in here. So I'm just going to comment that   out for right now. And we will run this one  more time. And you can see that we get Tim to   the screen. Now I know, I didn't really explain  what this is or how this worked. But essentially,   what I've done here is I've created kind of like a  function, okay. And it's known as a method. But in   other programming languages, you might see this  as a function. And this void keyword. What this   means here is it returns nothing, okay, because  in functions, we can actually return values,   which I'm going to show or sorry, functions  methods, we can return values, which I'm going   to show you in just a second. So this stands  for, we are going to not return anything,   we're just going to do something in here. So in  this case, we're just printing something to the   screen. All right, now we have Tim. And this is  the name of our method. And that's what we call   here to trigger this to run, right. So if I wanted  to pass some information to Tim, so say I wanted   to print whatever string I passed to Tim, well,  what I would do in here is I would type string,   and then let's just say str like that. And now  this means that whenever we call this Tim method,   we have to actually pass it one piece of  information, and that piece of information has to   be a string. Okay, so I'm going to show you right  here. So I type str, I put Tim and I put Tim in   here like this, okay, and we run the program. Now  we see we're printing out Tim, if I change this,   like with a bunch of M's can see if we run this  we're getting Tim. Now the way that this works, is   this is called a parameter. So str is a parameter  of type string. And that means that whenever we   call this function, we have to type in arguments  and what arguments are is what the parameter is   going to be when we get to the method. So anything  that goes in here in the call statement, which is   what this is known as where we're like triggering  the method to run is known as an argument. Okay,   so Tim is an argument. When we call this what's  happening is we're passing Tim as str so now it's   saying like str equals Tim like the other  whatever the string is okay. And then we're   simply going to print to the screen. Whatever  it is that we were passed. So in this case, 10,   right? Now we can actually do multiple parameters  as well. So if I wanted to do another parameter,   I wanted to pass two pieces of information every  time we call this function, I would say maybe int   and X, okay? And now it means that I have to pass  not only a string, but I have to pass an int. So   that means we have to type an integer in here.  In this case, I'm gonna do four, what I'm going   to do now we're going to say four. And we'll say  int i. And we'll say i is less than x. And we'll   say i plus plus, we're just going to print this  that many times. Okay, so let's see here. Boom,   okay, so System dot out dot print line, what's  more, we get in here, initialize variable,   got to do that, sorry. Okay, so now we pass an  int, and we pass a string. And what we're going   to do is simply print this as many times as like  the input that we typed in, okay. So you can see   that we now we get Tim four times to the screen.  And that's how that works. So we have arguments   in here, the two arguments are Tim and four. And  then we have our parameters, which are STR and   x. And when we pass our information in str gets  equal to Tim, and x gets equal to four. Okay,   and then in here, we can now use those values by  referencing STR and X, like I've done. So. Okay,   so I hope that makes sense. Now, in terms of this  public static, I'm going to talk about this in the   next video. For right now, all this means is that  you can call this not on an instance of the class,   which is kind of what we're doing here, when we're  just calling Tim, okay, because we don't have any   object that's like a class main, which you'll  see in the next video. Okay. So that's kind of   how we can create our own methods. Again, we can  create methods that return things. So that's what   I wanted to talk about now. So all I'm going  to do is going to create another method. And   I'm going to say this is public static. And in  this case, I want to return an integer value to   whoever's calling this Okay, or wherever this  is being called. So I'm going to type int,   because this is what I'm going to return and  the function is going to give back to us and   the method is going to give back to us. And I'm  going to say add two as the name. And we're simply   going to take an integer x as our one parameter  here, see what the issue is here. Method must   return Yeah, okay. So you can see, it's already  giving us a red line saying that we have to return   a value in this method, because we typed in the  fact that we were going to turn it okay. So what   I'm going to do here is I'm simply going to return  x plus two. And what this is doing now, semicolon   is it's just taking a value x, and it's adding to  it and it's returning the value to our program,   wherever we call it. So let's just do something  up here, and show you how this is working. So   I'm going to say, system, dot out, dot print, ln,  and here, I'm simply going to type add two, and   then give it a number, in this case, six. Now, you  can probably guess what this is going to give us.   But essentially, what's happening here is we're  calling add two, we're giving it the value six, so   our argument is six, it's coming in here, and the  parameter x is now set equal to six, we're going   to return so back to wherever we call this x plus  two, in this case, eight. So we get eight here as   a value, and we go, and we bring it up here. And  now this little line is equal to eight. So we're   going to print eight to the screen. And you can  see that we get eight as a value like that. Now,   obviously, in methods like this, you're probably  gonna do some more advanced things. And just   adding to that shows you how we can return values.  And I'll do one last example of returning maybe   like a string value. So public static string,  okay, and we'll just say, str as their name,   because I don't really know what to do. And  we'll take a string x, okay. And then in here,   we have to return a string value. So what we could  do is we could return x plus and an exclamation   point. But I think I need to put that in double  quotation marks, like that. So what we're doing   now is we're adding an exclamation point to  the end of our string, and we're returning   that. So if I call str here. Okay, so we'll I  don't know why keeps doing with my brackets,   we'll do str. And then inside our brackets, we'll  give it high, it should return to as high with   an exclamation point. And indeed it does. And  yeah, that's kind of how methods work basically   going to be going over classes when we talk  about creating classes, creating some methods,   constructor methods, all that fun stuff, creating  instances and objects. And yeah, so this is going   to be kind of an advanced video. Don't if you've  been following along so far, you'll definitely be   able to follow along with it. But just know that  now we're kind of getting into some harder aspects   of Java. So if you guys don't understand this,  please don't hesitate to join my Discord server,   ask me some questions, leave a comment down below.  Because this is absolutely fundamental. And you   have to understand this before you can really  start doing any serious programming. And that goes   for kind of all languages, but Java, especially  since it is an object oriented language, we need   to understand classes and how to create classes.  So without further ado, let's get started. So   so far, we've been working in this class. Hold me  now this is not really a true class. Because this   class, all it's doing for us, is just running  some code right away when we run the program.   And that's what we have this public static void  main function that automatically triggers whenever   we click this little green Run button. Okay, so  what I'm going to do now, and you can see that I   have some methods that I've added in here that we  just kind of use within here earlier. Okay. What   I'm going to do now is I'm going to create my  own class, from scratch from scratch, and we're   going to start coding. So to do this, we're going  to go to whatever our packages. So in this case,   tutorial, one for me, and I'm going to go new and  class. And I just did that by right clicking. And   now I'm going to give my class a name. Now, for  this example, I'm going to create a dog class.   And yeah, you guys can create whatever you want.  But I would recommend you follow along with me,   since you guys are most likely new to classes.  Okay, so now we have this thing, and it says   public class dog. And you can see we open up in  a new file. So whenever we create a new class   in Java, we have to actually create a new file  for that. Now, that's actually good, because it   makes it really easy to navigate between different  classes. Whereas in something like Python, you can   just have all your classes in one file. Okay, so  what we're going to do now, is we have this public   class dog. So what does this What does this mean?  Like what is a class or class is pretty much a   data type. And whenever we create an instance of a  class, like an object of a certain type, all we're   doing is we're just using all the information  within the class to like create an object. So   you can almost think of it as like a blueprint  for an object. Now, what problems do classes   fall for us? Like, why are they useful? So I'm  going to introduce combos a very simple problem,   right? So I want to create five dogs, okay, I want  to have five dogs, I want each dog to have a name,   I want each dog to have an age. And I want to,  at some point, be able to like call something and   print out each dog's name and age and like a nice  form. Okay. Now, we could do that we know how to   do that. If we went back into main here, what we  could do in this little, what do you call method   here is we could just type a bunch of answer you  say it's like dog one. And this is equal to four,   and that's dog one's age, we say int dog two,  and that's equal to five. Or we could go on and   create 10 variables, five for the age and five  for the names, right? And then we could go and   we could print out each one each time. But  that is incredibly inefficient. And what if   I wanted to have like 10,000 dogs? Well, what  would what would I do, then? Well, we could use   like lists to create or lists or raised to create  names and ages. But that's just not efficient,   it doesn't look as good in our actual coding. So  what we're gonna do is we're gonna use a class,   and you'll see how we can kind of accomplish this  problem. So within classes, we have methods and we   have attributes, okay? Now attributes are kind of  like variables that hold information for us. So in   this case, we want to have two attributes in our  dog class. And these are going to be the name and   the age to create our attributes. And so the first  thing we typically do when we create a class is   right at the top of our class, we're going to type  keyword privates. And then we're going to give a   data types. In this case, we can say string, and  then the name of our attributes, in this case,   name, okay, and this all we have to do, we're  just declaring that at some point in time,   we're going to have the attribute name, and  it's going to be storing information later   on in our program. Okay, we can also do private  int. And in this case, we'll do age. And this   is going to obviously represent the dog's age.  Now, if you want to have some other attributes,   we would, again, type private. And we could keep  going and type a bunch of attributes. Now, what   is this private keyword? And why do we have public  up here and private here? Well, what this private   keyword does is it ensures that this name and  this age are only accessible within this class,   meaning that if I tried to do something over  here, and I wanted to use this age and this name   from this class, I wouldn't be allowed to do that.  And the program would say, No, this is a private   attribute, you can't access that. Why do we do  that? Well, you'd find out in larger programs,   but we can also create public attributes as well  that are accessible to the other classes. Okay. So   if I wanted my things to be public, I could put  public for now whenever we're using attributes,   we're going to use private, and it's best practice  to use more private things, okay. And same goes   for methods, which you'll see in a minute. Okay.  So now we need to create something called a   constructor method. Now this, you typically  only create one of these, although you can   create multiple, what this is going to do is it is  going to run automatically whenever we call this   dog class. And the way we create this constructor,  and we typically need one of these when we have a   class is we're going to do public. And we're just  going to type the name of our class once again,   so excuse me as a public dog, like this, okay.  And you can see that now we're getting no errors,   everything's fine. And in here, we're going to  type the parameters or the information that we   need to be passed in whenever we're creating a  dog object. Now, in some instances, you may have   nothing in here, when you create a dog object, all  you need to do is just say you're creating a dog   object. You don't need to give any information and  that's fine. But in our case, we want to be able   to create an age and a name with our dogs. So what  I need to do here As I need to type string name,   and int, age, okay, just meaning that whenever we  create a dog object, we need to give it a name,   and we need to give it an age. Now what I'm going  to do is I'm going to set these values. So these   attributes equal to whatever we pass in. Okay? So  what I'm gonna do is I'm actually just gonna use a   keyword and it's, the keyword is this, it's kind  of hard. If you weren't watching, you think I'm   just like saying this is a keyword and pointing  something, but this like actually typed out. And   then I'm doing this dot name is going to be equal  to name. And this dot age is going to be equal to   age. So what is this, this this keyword actually  doing? Well, it is referencing the attributes of   the class, okay? So when we type this, it is going  to be looking up here to find all of our like   private attributes. And in this case, we have an  age and we have a name, alright. And that's how we   reference things that are a part of the specific  instance, we have this name and this dot age,   okay, and I'm going to explain more and more  of this as we keep going. It's hard to do it   in like small steps. Okay. So now that I have this  constructor method created, we can go on to create   one more method, and then we can actually start  using this class really simply Okay, so what I'm   going to do now is I'm going to create another  method, in this case, I'm going to type a public,   I'm not going to return anything, so I'm going  to use void, okay? And the name is going to be   speak. And what this is going to do is it is  simply going to say something or prints me up   to the screen. So I'm gonna say system, dot out,  dot print ln, why did that? Okay, print ln, and   all we're gonna do is we're gonna say I am, and I  want to say that the dog's name. So in this case,   we'll say this dot name, okay? And we'll say plus,  and I am, you know, plus, we'll say, this dot age,   plus years old, okay? So suddenly gonna say like  I am, whatever the name is, and I am, however many   years old, not great grammar, but that's fine. So  now, how do we actually create a instance of this?   Or how do we use this class? Well, from our main  class here, which we should still have open, and   you know, we can delete all these, we don't need  all that. So we'll get rid of all this stuff here,   we can actually create a object and to do that of  type dog, what we're sending individual type dog,   and we're gonna give it a name. In this case,  we're gonna say Tim, is equal to new dog. Okay.   And then remember that we have to give dog some  parameters, right? So or arguments because we   have the name, and we have the age. So what we  need to do is we need to give it a name and an   age. In this case, we can type 10. And I'm going  to type four. And now you can see no red line,   we're looking good. So our dog's name  is Tim, and it's age as for. Okay,   so that's great. Let's run the program, make  sure this is working. Everything's fine. Now,   what I want to do is I want to use that  speaker. So how can I use that? Well, what   I'm going to do is I'm going to type Tim, which  is the name of my dog or like the variable for it,   dot speak like this, okay, semicolon. And let's  see what happens. We get I am Tim. And, excuse me,   I am four years old. So the way that this worked,  right, is we created a instance of the dog class,   and the instance was named Tim, and it referenced  a dog object. Okay. So now when we created that   instance, we said, okay, so this instance, this  specific one is going to have an age of four,   and a name of Tim. All right. So it stored that  information up here in our private string and   our private int. And then later on, when we  decided we wanted to call this what said,   Okay, well give me the instance you're calling  it on. So in this case, we're calling it on Tim,   we're gonna say okay, so Tim, what's Tim's name?  Let's name is well, it's Tim. Okay, so we'll say,   Tim, what's its age, its age. That's four. So  we'll print for the screen. Now, we can obviously   create multiple dog objects. So let's copy this  and let's create a few more. We'll create three   dog objects. I'm gonna say this is Bill, and he  is seven. And let's say this is Bob, and he is   11. Okay, let's copy this and keep naming the  same thing. Let's go Bob. And let's go, Bill,   and then we can simply call the speak methods on  them. Okay, so we'll say bill, and we'll say, Bob.   Alright. And just to prove something, I'm gonna  say Tim dot speak again, and we'll talk about   why I do that in just a second. Okay, so let's run  this. Let's see, I am Tim and I'm four years old.   I'm Bill I'm seven. Then go on and you can read  through them. Okay. Now notice, when we call Tim   again, it still retains its age and its name.  When we do this, we're not actually changing.   Like this is not one variable. We actually now  have three different names right? We have Tim   we have bill we have Bob. And there's specific to  each of these variables that are storing that dog   object okay. So that means that we can hold like  unique values for each of our different instances   and we can have like infinite amount of instances  of a class okay. Alright, so That is about it,   I think I'm going to talk about quickly. I  already talked about private versus public.   Let's create some more methods in here and see  what they can do. And I'll talk about a bit more   about constructors because we're only at like,  11 minutes. So what I'm going to do now is I'm   going to create a another method. And I'm going to  call this get age. Okay? So I'm gonna say public.   And in this case, we'll say int, get H. All right.  And all we're going to do in here is simply return   the age to, let's see a return statement to  wherever we're calling from. So in this case,   all we'll say it will return this dot h. Now,  the reason I need to do this is because say,   I want to get the age of one of my dogs, right?  Like, say, I created it, I changed it around,   I don't know what it is I want the age. Well, we  can't simply do something like in other languages,   we'd be able to do like Tim dot age, right? Now  you see, when we do this, we get these little red   lines. And it says the field dog dot age is not  visible, and not as because again, it's private.   So it's not letting us see it from over here  in this main function only within this sorry,   method only within this class, can we actually  reference this variable, right. So to get the age,   what we can do is we can call Tim dot get age like  that, okay, and we can print that to the screen,   we say like, and x equals, I'm gonna say system,  dot out, print, ln, and x is already there. Great.   So now if we run this, we can see that we get  four down here, excuse me, at the at the bottom   of the screen. Now we can actually do the same  thing with like setting the age. So we wanted to   change the age at some point, maybe Tim got a year  older, well, what we would do is we have to create   another method in here, this case, we're going  to make it public. Again, we're gonna say public,   and we don't need to enter this time, because  we're just going to change something, we're not   going to return to say public void. And we'll say,  set age, okay. And then we're going to take an age   because we need to know what we're going to set it  to. And we'll just say this dot age equals age. So   just like we've done up here, is the exact same  thing, except we're just going to do it within   the method set age. So now, if I want to change  the age, so let's say instead of that, we'll say   set age, and we'll give it a value of 10. Then  we print out, let's just say Tim not speak again.   What's gonna happen now sorry, guys, I'm a bit  sick. If I keep coughing all over the place. What   I'm going to do is, oh, what's your air timeouts  at age 10? return type of set age to int. One   second here, sorry, public, let's try this. Insert  age, public void. I know I'm making a mistake over   here. Oh, okay. That's why I can't do I can't set  equal to a variable. I just call like Tim dots at   age by about on that guys. We do speak, we can  see now it says I am Tim and I am 10 years old,   and we've changed from the age of four. So that's  how we would go about changing these attributes   later on within the the class. Okay. Now I want to  do one last one, I want to create a private method   and show you what this is. Okay, so I'm gonna  create a private, void. Actually, no, let's do   it. Okay. And we're going to call this add to it,  it's not gonna make any sense with the dog class,   but just, it's just an example. Okay. And in here,  we're going to take actually won't take anything,   but we're going to return we're going to  return this dot age, plus two, okay? Now,   this is a private method, meaning that it  can only be used and seen within this class.   So say I wanted to use this add to, maybe I  could do this, I could do add to here, okay,   and just do a little semi colon. And there we go.  I could use add to, and that works fine. There's   no issues with that. Okay. And that's how you use  methods with inside of the class. So like speak,   I can easily every time we initiate the dog, call  the speak method as well. And to do it within the   class, you don't need to do like something dot  you just call the actual name, because it's   really visible within here, right? Now, what would  happen if I go over here, and I tried to use that,   so add two, so we'll say Tim, dot add two like  this. And you can see that, again, we're getting   a red line and says, This is not visible, you  can't use it, because it's a private method. Okay.   So in today's video, we're going to be going over  inheritance. So this has to do with classes. And   this is going to be how we can inherit methods  and attributes of previously made classes into a   new class, and then override methods and all kinds  of fun stuff. This is really important. This will   save you a ton of time. And yeah, so let's get  started. So you can see here I have this sorry,   this dog class that we created in the last video.  And pretty much the goal what I want to do,   at least in this video, the problem is, I want to  create a cat class that is identical to this dog   class, except in the speak method here. Instead of  saying I am whatever I just wanted to say meow. My   name is what I write right Just want to change  one of these methods to do something different.   But other than that, I want all of this to be  the same. So, intuitively, what we think we do   is, well, we just take all this, copy it, put it  into a new file called cat, and then just change   this to be what we want. Well, we could do that,  and that'd be fine. But when we have classes that   are like 1000s, and 1000s, of lines long, ideally,  we don't want to be repeating code and continually   typing like the same thing a bunch of times,  right, it also just makes it more difficult to   read. So what we're gonna do is we're going  to use something called inheritance. So I'm   going to create a new class, I'm gonna go new  class. And in this case, I'm going to call mine   cat, alright. And they'll just click Finish like  that. And then here we have public class cat. Now,   whenever we do this inheritance, what we can  actually do to inherit all of the properties,   all of the methods, everything from this dog  class is up here, when we define the class,   we can type extends, if we spell it correctly,  extends, and then the class doc like this, okay?   Now what this is going to do is it is going to  grab everything from dog class and apply it to cat   class. So this is known as our superclass because  it's kind of above the cat class. And then cat is   known as our subclass. So we have, you can also  call it a child class, or drive class. And this   could be also called a parent class, all right.  And that's because it's kind of underneath it's   getting everything from there, but then it's going  to be changing a few things, and maybe adding a   few methods to it. So typically, you start off  like the most abstract with a parent class,   and then your child classes or your drug classes  or your sub classes, because they're all the exact   same thing, just everyone likes to use different  words for them, are going to have some slight   modifications to them, and kind of work based  off of that. superclass, right? So or the parent   class. Okay. So what we need to do when we first  do this is you can see here, it already is telling   us, we need to use the constructor for dogs. So  we're inheriting from dog. But we need to use   the same constructor, otherwise, all this stuff.  And all these attributes will they're not going   to work unless we set up our class when we first  create it the same way as we did dog. So what I'm   gonna do is I'm actually just going to click on  this, and you can see it automatically generates   this constructor for us. Now, I could have typed  it out. But this is a Yeah, this is what it's   going to do. So since in our dog class, we get  name and we get age, we need to make sure that   we have a constructor inside of our cat class that  gets name and gets an age. And we have to actually   explicitly call our superclass constructor, which  is going to be the dog constructor, we're using   this formation or this syntax super. And then we  give it the name and we give it the age. Now, this   doesn't stop us from being able to type in other  properties or other attributes. So for example,   I could do something like int, and I don't know  food or something, it's like how much food they   should get, this works perfectly fine. And we can  go ahead and we can create another attribute here.   So we say private, or no, I'll just say food.  Okay, and this should say int food. And then down   here, we can go ahead, and we can just type this  dot food equals food. Okay, right. And that works   perfectly fine. And now we're gonna have, since  we're inheriting from this, we're gonna have age,   we're gonna have name, and inside of this cat  class, we're gonna have food as well. And that   works just fine. Okay. So just to know, let's  go over this again, because they've kind of gone   through pretty quickly. But all of these methods  here, we're going to be able to use within our   cat class, because we'll we're inheriting we're  grabbing all of them from the dog class. So this   cat class when we first extend it, and do nothing  is identical to the dog class. And then as soon as   we start changing a few things in here, it's gonna  become a little bit different, but still use all   this functionality. So we still have the attribute  age attribute name, we're setting them equal,   because we're gonna call this constructor method,  right from our, what do you call it super right   here. So we call this constructor. So we set up  age, and we set up name. And then we have speak,   we have get age, we have set age. Now before  I move any further, let me just prove this to   you. So if I go to main here, and I just  create a cat object, so I'll just say cat   CIM equals new cat. And remember, we have to give  it three parameters or three arguments this time,   because that's what we typed in. So for name, we  can obviously do Tim, age, we'll do 18 and food,   let's say they get 100. I don't know grams of food  or something, whatever you want to call it there.   Okay. Now I can use this speak method on Tim. So  you see, we're getting no errors here. Even though   in my cat class, there's no speak method. But  since there's one in dog, and we inherit from it,   we can use it. So I'll prove again to you  run this. I am Tim and I am 18 years old,   and that works perfectly fine. So now remember  I said though, I want this speak method to do   something else, I want to change it. So how can we  do that within the cat class? Well, the way to do   this is to just rewrite the method in here because  when we do that, we're simply going to overwrite   whatever's in the dog class. So the child class  or the subclass, whatever you want to call it.   Whenever there's something the same in here, it  automatically is going to override or overwrite   whatever is in the dog class. So I do public, void  speak. Okay, so This is the exact same name as the   other one. All right, and in here, I change I do  something, right, so I changed something. So I'm   just gonna say, system dot out, dot print ln. And  in here, what did I want to say like Meow? And my   name is what to say plus this dot name. And you  don't even add like a little food thing. And I   get fed. Let's see here, plus this dot food.  Okay, I don't know what's good enough for me,   what's the issue here? Change visibility of name  to protect it. Okay, second, I got to do something   here. Okay, this just needs to be sorry, public  and public. Sorry, this is just because we're not   actually able to see these attributes, because  they're defined in the dog class from within   our cat class. So we just need to change them to  public so that we're actually going to be able   to use them within our speak method, we can also  use something called protected, which I'm going   to talk about near the end of this video. Okay, so  right now, this is public, because I don't wanna   get into protected. But let's see what happens.  Now. If I run my Tim dot speak now and say meow,   my name is Tim and I get fed 100. Okay. And that's  because even though in the dog class we had speak,   since we wrote it again, in our cat class, and  cat class is the one that we're using, we're   simply going to take this one, and we're going to  override the the one in our dog class, okay? Now,   obviously, when we're doing inheritance,  you've already seen we can add our own methods,   we can add more stuff, and we do whatever we want,  right. So in here, I can add more methods, I can   say public void, I don't know, eat. And in this  case, we'll just have like, int, I don't know x,   like as how much they're eating. And we can say  this dot food, minus equals x, okay? And just   subtract from it however much they ate. And then  we can use eat in the cat class. But it's not   going to be seen in the dog class, right? So we  just have to understand that this does go one way,   all the stuff we do in the cat class is not going  to be visible to stuff in dog cost. because well,   dog does not extend cat, right. So it's kind of a  hierarchy where like dogs at the top, and then cat   branches off and we could have other things extend  the dog class as well. We'd have something extend   the cat class. And we can just keep going and  going and going and extending and extending and   extending. Also known as like inheritance, right?  So that's kind of it for inheritance. Now, I want   to show something called multiple constructors,  okay. So this is sometimes when we're setting up   a class, we want to be able to pass it like a  different piece of information and set up the   class differently, right. So for example, my  cat class here gives name, age and food. What   if however, I only wanted to give it a name and  age, well, we can leave both options available,   they don't have to be mutually exclusive, right?  I could give it either these three arguments to   set it up, or I could give it just two. And to  do this, we have to create another constructor.   So I'm going to do the exact same thing as above,  pretty well identical, except I'm just gonna leave   out one of the parameters here. Okay, so Second  Age. And then here, we'll simply call super,   and name and H guy. And you'll see no red lines,  everything looks fine. And what we're essentially   doing here is we're giving it another way to set  up the cat class another way to construct all of   our attributes and set up the cat class. So we can  either call the cat class using three parameters   or three arguments, or we can call it using two.  And you know what, maybe we could set one up only   using one, we're only using zero. So let's do  one with one just to show you how this might   work. Okay, so if I do string name, and then int,  age, okay. And then here I call super. Sorry, I   should just get rushed. actually get rid of this,  because we're only gonna use one right? My bad.   I'm going to give name. But what about age? What  am I giving super for for our age? Well, I don't   know. How are we going to set it up? If they only  give us one argument? Well, I would think that if   they don't give us an age, then we should just  automatically assume that their age is zero. So   let's do that I'm going to put in zero, just hard  code that in here. So that way, this means now say   for example, they only want to give us a name,  then we'll just automatically give them an age   of zero, they want to give us a name. And in age,  that's fine, we can do that. But maybe what we   should do in here, if they give us a name and age  is we should make sure that food is set equal to I   don't know, maybe let's give it a default value of  50. So if they don't define how much food the cat   eats, we're just going to assume that they eat 50.  Okay. And this is kind of a way you can do like to   know when I was like default parameters. If they  don't type something in just automatically set   a value for that. So that everything still works  fine. I hope that makes sense. So let's show now,   the three different ways that we can create a cat.  So cat, Bob equals new cats. And in this case,   we'll give it a name. So we'll say Bob, and we'll  give it an age of seven. And no red lines. This   works fine. Again, this defines a cat right  because that's another constructor that we had.   So let's do this one more time. And in this case,  let's just only type a name. We're going to name   this One do, like the name Joe. Alright. And  there we are. So now we could obviously do   like Bob speak. And we could do, Joe does speak.  And these are the three ways that we have that we   can define a cat. Okay, so Meow, my name is Joe, I  get fed zero, Bob, I get fed 50. Right, it can go   on like that. So again, when we have one, now we  just seen a quick error that maybe we want to fix,   we should probably give them food as well. So we  say this dog food equals 50. Okay, as like our   default value for how much they get fit. So that  is a way that you can do multiple constructors.   And now I'm going to talk about protected values.  So protected values are something that we can use,   similar to private and public. But they're, they  just act a bit differently. So pretty much if you   use the protected keyword instead of public  when you're defining things. So for example,   here in my dog class, I'm going to go up and  change public to protected. This means that only   things that are within the same package, or are  subclasses of the class can have access to this   pretty much like name. Okay, so that means any of  these files, or any of these classes within this   package are able to see this. But if I had another  package, which you typically do, if you have large   Java programs, anything in there would not be able  to see this. Okay, so with whenever we're using,   what do you call it whenever we're creating  like a main class or a parent class? pro? tid.   Wow. Okay. We're going to want to use a protected  keyword, at least for what do you call it, sorry,   our attributes so that we can change them access  them from within the sub class. All right. Now,   for these methods, it's fine. They're public  anyways, right? So we can make these protected as   well, if we didn't want another package to be able  to access them. Now, if we create private methods,   right, if we create a private method here, I'm  actually going to try this. I don't remember   if this is going to allow us to use this  now out here. So let's say like Tim dot,   let me know which one I changed. Get age.  Okay. So Tim dot get age. So int x equals See,   this is gonna work. Yeah. So again, this is not  going to allow us to use this, because well, we   made it private, right? If we made that protected,  it would allow us to use it here, but not outside   of the package. Now, I want to try this though,  and see if I can get this uses get age from within   our cat class. So if I do, for example, when I  want to speak, I'll say this get age Ed. Yeah,   so this isn't working as well. Which means  that, again, if we want to use those methods,   we have to make sure they're either public or  protected, rather than private, okay? The only   time you really get to create private methods  is if within the class is the only area you're   using the map that so for example, maybe you're  doing a math calculation, and you only do that   from within the class and you don't want anyone  outside of the class to be able to do that, then   you create a private method to do that, because  you could do that from within the class, right.   So in today's video, we're gonna be going over  kind of class variables and the keyword static.   So you may have seen that we've used static  and a few things in like our main thing here,   actually, I'm going to delete all this. It says  static right here. And no one's really explained,   I guess I haven't really explained what static  means. And that's what I'm going to be doing in   this video. So let's get started and talk about  class variables. So right now we have something   called attributes games, we have these two  attributes, which are specific to each instance of   our dog. So when we created multiple dog objects,  each dog had their own name, and their own age,   and other dogs were not affected when we change  one of the dog's name or one of the dog's age   unless that dog was that obviously, right.  And that's great, because we can have a ton   of different values that are specific to different  objects or different instances of this class. Now,   sometimes, we want to have variables that will  change or are the same for each instance. So   we can change them in one instance. And if we do  change it on one instance, they will change in the   other. Now, those are known as class variables,  not instance variables, because instance variables   are like kind of attributes, okay? The way that  we can create these class variables is we can do   something like this. So yes, let's say protected  again, say protected. And by the way, I'm just   in my dog class, I deleted a bunch of stuff out  of it, just to make it a bit easier to work in.   And I still have the cat class, but it's just  I'm not using it for this video. Okay. So we'll   say protected static int count equals zero. Now,  I know that that is a mouthful. But what we're   doing here essentially is we're creating a class  variable, which is an int, int count equals zero,   which is protected. Okay, I know it's a lot  of keywords. But what is the static keyword   do so what this is actually going to do is it's  going to allow us to statically change count,   which means it's not going to change for each  instance, it's only going to change or sorry,   it'll change like that. All together, right? So  what I was explaining before, and that's what   this static keyword is going to allow us to do.  So we don't actually care about what instance this   variable is a part of, we just care that it's a  part of the class. Alright. So if I want to change   this value x, well, I do need still need to call  this and I can say this dot count, equals, and   then or actually, I'm gonna say plus equals one,  okay? Because what I want to do with this count   here, consist, I feel dogs should be accessed in  a static way. Okay, let's just do this one second.   Okay, what I'm doing here, sorry, one second,  I'll explain this is I'm just gonna add one to   this count, because I want to keep track of how  many dogs we have. So that if I wanted to, I don't   know, like, see how many dogs have been created,  I could just simply call like dog count, and   then figure that out. Alright. So what I'm doing  here, instead of calling this count, which does   actually work, is I'm calling dog count. Because  since this is a static, variable, or static, yep,   static variable, class variable, whatever you  want to call it, it doesn't actually care about   what instance I'm changing it on, because it's  going to change for all the instances. So I can   actually use the name of the class to change it.  And that's why it gave me that little yellow line.   I'm not sure if you guys read that. It said we  should do this in a static way. Because Well,   it's a static variable. So I could, however, call  like this count, right? And that would work fine.   Because this will is an instance of the dog class  who knows where to look for the variable count,   but it's much better to do dog count, like plus  equals one. Okay? Now, I'm going to prove this to   you that this does indeed actually work. I'm not  just making things up. So let's create some dog   objects here. Say a Tim or dog. Tim equals new.  Wow, typing is not great today, new dog, and we'll   say 10, and score nine for age. And we'll just  copy this and what should we do? Let's do bill,   maybe, say, Bill, and that is new dog name. Bill,  age 10. Okay, now, since this static variable here   is protected, I can actually access it from me.  So what I'm going to do to change count in order   to print count out first of all, is I'll prove to  you that system, system dot out, dot print, ln, I   keep thinking, we're in Python here. And I'll just  simply print out dog dot count like this, okay,   and just see what we get as a value First of all,  okay, so we get to, so we already know that this   is indeed working, because well, when we added one  to count, if it was different for each one, then   it would just be one. But anyways, and if I want  to change dog count, what I can do is I can say   dog dot count, let's say equals like seven. Okay?  And that's fine. That works. If I print out, dog   count, we get seven. Now I'll show you two. If I  do like Tim dog count, this will give us the same   answer. So you can see we get seven. Even though  in Tim, we didn't do anything with Tim to change   count. Since it's a dog part of the dog class,  it has this count variable, and it's changing,   obviously, within 10 as well. Okay, so that's kind  of how the class variables work. Now time, let's   talk about static methods. Okay, so this was a  static variable, or class variable, I like to call   a class variable just because it's what I'm used  to, but you can call it whatever you want. Now   we're gonna talk about static methods. And what  that is. So it's very similar to this, kind of the   way that this works, but is a little different.  So I'm going to do here man type, public static,   void, display, okay. And in here, all I'm going to  do is simply do system, dot out dot print, ln, and   I'll just print. I am a doc. Okay. So what this is  doing now is I'm saying static void display. Now,   in here, what's going to happen essentially, is  we don't need an instance to call this. So I can   actually just call this on the dog class. So I  can say, like dog dot display, like this. And   since it's a static method, this works fine. And  this one didn't say I am a dog. Now, as opposed   to if this was a regular method, so let's just  make a regular method to kind of do a comparison   here. So public void, and we'll say, display to  okay. And we'll literally just take this and put   it word for word in here, just to show you the  difference. So now watch what happens if I tried   to do dog dot display too. You can see that we're  getting a red line and saying that we need to make   this static because we need an instance to call  display to meaning that we need to have first   create a dog object. And then we can use that  dog object to call display to or we can do like   this dot display too. And that works fine because  that's calling it on the instance right? Because   this represents the instance that we're using. So  the static is going to allow us to call it just   simply using dog but the thing was static is it  has no way to access are, what do you call it? Our   values are our attributes, right? So if I wanted  to access like a dog's name or a dog's age, well,   I can't do that for a static method like, I can't  type like this dot age, that just simply doesn't   work. Because if you think about it, well,  what what, what instance are we calling it on,   there's no instance, we're just simply calling  it on the dog class. So we have no idea what   age to look for, or what name to look for. So the  only time we use static methods is when we don't   care about the instance. And we just want to do  something and we want to store it within that   class. You can almost think of it as a function if  you if you know other programming languages. But   that is ideally how static works. So whenever you  want to build a call something and you don't care   about the like the instance, you don't care about  the attributes, you can make it static, and that   way you can call it without having an instance. So  yeah, so hopefully, that makes sense to you guys,   I'll quickly go over what this void does one  more time, in case anyone forgets, remember,   in our functions, we can return values, when you  put void, all this does is say we're not returning   anything, we're just going to do something,  right. So we could be like manipulating an array,   or in this case, printing something out to the  screen, okay, or maybe getting like input from   the user, but we're just not returning  any value back to the call statement.   So in today's video, we're gonna be going over  how we can compare objects, and how we can get   a string representation of different objects.  And you'll see what I mean in just a minute. But   these are really useful. And I guarantee you guys  will be using this a lot, especially if you have   objects, like maybe like point objects, or like  some kind of mathematical objects that you'd want   to compare, without having to make things too  complicated for yourself. Let's just say that,   right. So what I'm going to do here, and what I've  already done, I guess, is I have the student class   set up. And this is just what we're going to work  with right now. Because the other classes I had   in previous videos were already like finished, and  they had other stuff in it. So we have a student.   And currently the only attribute the student  has is a name, okay. Now, in my main Java here,   I've just created a few different students.  And then I'm just gonna be printing something   out to the screen. So that's why I have that  right now. So we can see student class, right,   we just create a name for a student. Now I just  want to show you quickly how we would go about   like comparing objects intuitively right? So say  I wanted to check if Joe and Bill were the same,   right? Like if I want to see if they were equal to  each other? Well, we know how to do that already.   So what I would do is I'd say like Joe is equal  to bill. Now, intuitively, you probably say, well   know, their names are not the same. So they're  not going to be equal to each other. And well,   that would be correct, but not for the reason that  you think and all demonstrate that to you. So we   have Joe and Bill compared to each other. So watch  what happens if I try to make these the same name,   right? So you'd think well, these would be  the same object, or they'd be the same thing,   right? Because they have the same name. But watch  what happens when I run, we still get false. Now,   why is that? Exactly? Well, pretty much we  use this double equal sign on two objects,   right? So like Joe, and Bill, the computer  doesn't know how to compare them. So by default,   what it does is it compares the actual object. And  you see here how we're creating a new student. So   a new object. And here we're creating another new  student, another new object, these are actually   two different objects, like in memory, okay, so  in your computer's Ram. These are two different   entities. And they're completely unique. Although,  yes, they have the same name. Since we can change   names, and we can do things. These need to be  separate objects. So when we compare here, it's   actually saying, well, we're going to look and see  if these are the exact same object, and well, they   are not. Now that's useful in some cases, because  sometimes you want to see if it's like the exact   same object. But in other cases, we want to see if  these objects have like the similar values, right?   So in this case, we probably want to compare the  two names of the students to see if they were the   same. And in that case, we would say, Well, yes,  these are the same student, we're going to say,   Yes, they are, they are the same, right? When we  do that, like double equal sign. So what I'm gonna   do now is I'll delete this. Let me show you how we  can go about doing that. So inside of our student   class, I'm going to add a method, okay. We'll call  this public, Boolean, not Boolean Boolean. And   we're gonna say equals, and then here, we're gonna  take a student object known as other, okay? And   then we'll go like that. And there we are. So what  I'm gonna do in here is when we want to compare   our students, okay, we're going to pass another  student, and we're going to compare their names,   right? So we're going to call this dot equals on  one of our students that already exists. We're   going to give it another student, and then we're  going to see if they're the same. So how do we   say we're going to check the same? Well, we're  going to look at their names, okay. So what I'm   going to do is I'm going to say if, and then we'll  say, this dot name, and I guess I need a brackets   here as well. always forget about those. If this  dot name equals equals Okay, and then we'll say,   other dot name, then what we'll do is we'll simply  return true. Okay? Now otherwise, so if these   names are not the same, what we will do is we  will return false like that. Okay. And that's all   we need to do for our dot equals. So now, what's  gonna happen right is we'll pass on one student,   we'll call this we'll check name, and then we'll  check the other student's name, see if it's true,   if it is, or if the same will return true,  otherwise, it will return false. So now we can   try this out. So these two are the same names.  So let's say is Joe dot equals, and then here,   I guess we will put a bill. So let's run this.  And you can see we get a value of true. And then   if I change bill, back to say, Bill, then we  can run this, and we can see we get false. So   now we officially have something checking for  equality between the names. Now we could create   more kind of ones like this, like equals grade,  like whatever if you want to do that. But this is   how we can check for equality between objects.  And this method is like really easy to add in.   Obviously, if you had a student with a bunch more  values, maybe you had like grade, maybe you had   other stuff like that, or like a last name, you  could check to make sure those are all the same.   Or you can just kind of customly do it the way  that you want to say, two different objects are   the same, right? Okay, so the next thing I want to  be able to do is compare things using like greater   than sign or less than sign, right? So I want to  see if like, Joe is greater than bill or bill is   greater than Tim. And we should intuitively like  do that based on alphabetical order, right? So I   can't really show you like a way to do it here cuz  I don't think it's gonna work for my show. Greater   than bill. Yeah, see, that just is undefined,  there's no way to do that. So we're going to do   is we're going to use a method called compare to,  okay, what we have to do to compare to, is we have   to, what do you call it, sir, we have to implement  something into our class. So when I say implements   at the top of our class, I'm gonna spell it  correctly. And we're gonna type comparable. And   then in here, we're going to do student. Now what  we're actually doing here, when we bring this in,   is we're actually implementing something called  an interface. Now, an interface I'm going to talk   about in I think, like two videos from now. But  pretty much it's just like a set of methods that   we have to use for the class to work. So when  we implement this comparable thing, that means   we're allowed to bring in a method that is going  to allow us to compare two objects. Okay? So what   we'll do now, is we will say, public and actually,  what is the thing for this the return type public   ins and compare compared to, and then in brackets  here, again, we're going to take students, and   this will be other, we'll put our brackets here  like this. Okay. So now again, we're gonna have to   return some kind of integer. And I'll show you how  that works. I just got to get it up on my other   screen, so don't mess this up. Okay. What I'm  gonna do now is I'm going to return this dot name,   dot compare to, and in this case, we're going to  compare it to another string. So the other string   will be other dot name, okay. So essentially,  what this is going to do is it is gonna compare   this name to this other name, and it's going to  give us an integer value saying kind of how far   away they are, right? So it's, it's kind of hard  to like explain. So I'm just actually going to do   it from over here. And you'll see what I mean.  So if I go Joe, dot compare to, and then against   to bill. Let's see what we get. We get a value of  eight. Now what this means is Joe is greater than   bill by a distance of eight, meaning that  when we compare these first two letters,   they are eight away, I believe, okay. So if we  want to check if Joe was greater than bill, what   we do is we check if this integer value returned  was greater than zero. So here greater than zero,   that's going to tell us true if Joe is greater  than bill and false if he's less. So we run this   and we get true. Now, let's see if we compare Joe  to Tim. Okay. So t obviously is ahead of j in the   alphabet, so we get a value of false, okay, and  if we get rid of this greater than zero, then we   can see exactly what we're actually getting as a  value. So negative 10, right? Because that's how   far away j is from T one, we're comparing them and  that's what the compared to does for us. Okay. So,   if we wanted to check if Joe was less than 10,  then we check if this value was less than zero,   in this case, we get true. So that's a really easy  way that we can compare different objects using   that compare to method Okay, so like this a public  int compared to and you just have to remember to   implement comparable students. And then you can  use exactly this return statement here. Now,   the last thing I want to show is a string  representation of an object. So once you First,   what happens when I just want to print out  my object to the screen. So I want to print   him. And maybe ideally, I'd like to see like  Tim's name, right come up to the screen. Well,   look what we get here, we get tutorial, one dot  student app, and then all these different letters.   What this is printing is actually the memory  location of our object. So all of our objects   are actually stored in memory, in RAM in like  really fancy ways that I can't really explain to   you guys. But anyways, this is like the address  for it's like, when the computer looks it up,   this is what it's actually using to find Tim and  find all its values. And obviously, it's telling   us where it is in the tutorial one, like folder,  file, whatever, okay? So this is no use to us,   we this does not help us as programmers at all  we want to see like something that's a string,   and gives us some valuable information. So  what I need to do is I need to implement a   nother method here, that's going to change this  to string. So we've used this.to string before,   but this is exactly what I'm going to use, we're  gonna say public string. And we'll say, to string   like this, okay. And what we're gonna do in here  is we're just gonna return a string representation   of our object. So what we can do essentially, is  I can just return this dot name, and this can be   what we're going to use to represent our object.  Now, typically, you might see people do something   like this, okay, and just follow me for one  second. They do student and they have a bracket,   and then we're gonna add this store name. And then  they add that with another bracket. So this way,   you'll see when I printed out to the screen,  what it looks like, okay, we get student, and   then Tim. And this is just simply telling us that  the object we're printing is a type of student,   and the name value is Tim. Okay. And we could  even get more fancy. And we could add, like,   double brackets around this name, if we want  to show that this dot name was a string type,   okay? Now for our purpose, I'm just going to  get rid of this student and this bracket here.   And you can see again, when we print this, it  goes, Tim. Now notice, when I'm printing this,   I'm not actually calling this two string method,  like I'm not, not doing.to string. That's because   Java actually knows that when we have this two  string method in here, this is going to represent   a string, right? So when we try to print it, it's  automatically going to call that.to string method.   Okay. So now, what we can do is we also just  call like two strings, so I can, if I wanted to,   I could just call.to string like this, and I  could convert it to a string. And this will work   the exact same way as before, you can see we get  bill, okay. Again, in here, you can change this up   however you'd like. In some cases, like say your  student had some grades, maybe you want to write   their name. And then after you have like an array  of all their grades or something like that, okay?   Now, inner classes are pretty, pretty easy,  actually, all they are, is really just a   class inside of another class or inside of a  method. And I'll show you some examples as we   go along. But since we already know all about  classes and methods and static and whatnot,   I'm not gonna explain too much of it, I'm just  gonna kind of show you how we can create instances   of inner classes in different ways. So I'm gonna  start by doing these incidents outer class here   that I've already created. can see on the side  here, I'm just gonna create another class called   intercourse right now, in a class is just gonna  simply have one method in it. And I'm just gonna   make this a public void display. And in here, all  I'm going to do is simply just display something   to the screen. So we know that we're actually in  the rights class here. Okay. Let's say this is an   inner class, okay? And what's our error here,  class at token interesting. must have made a   mistake somewhere here, one second guys. Oh,  sorry, I put these two brackets here. I don't   know why I added those anyways. Okay, so this  is an inner class. Now, outside of this class,   I'm going to create another method that's going to  be a part of the outer class. Okay, so this class   is actually contained within the outer class,  meaning that currently, since it's private, we   can only actually access it from within our class.  So if we want to create an instance of this, that   means we must need a method in our class to do  that. So in this case, I'm gonna say public void,   inner, and in here, we're going to take nothing,  and all we're going to do is we're gonna create an   instance of our inner class and just display Okay,  so we're just gonna say, inner class I N equals   new inner class like that. Okay? And then all  we're gonna do is just call i n dot display, and   make sure that this is working. Okay, so now our  class is finished, we have an inner class and we   have these void inner and all that does is create  an instance of our inner class and printed to the   screen. Again, note that this is private meaning  we can only access it from within In our class,   okay, so now if I go to my main dot java, I can  run a command so or whatever create an object so   we can see this. So first of all, what I have to  do is create an outer class object, so that we can   call that inner class. So outer class out equals  new, and then outer class. Now it is worth noting   that I don't have any constructors here, meaning  that I don't need to give any information to my   outer class or my inner class when I first call  it and that's why I can just leave these blank   brackets. Okay. So now if I want to see my inner  class, what I have to do is I have to do outdoor,   enter. Ok. So we'll call that method to create  the inner class, and then run that. So let's   run quickly here. And we can see we get this is  an inner class. Awesome. So that's working fine.   Now I'm just going to make this public. Now I'm  going to show you how we can do this from outside   of the class. So right now what we need to do is  we need to create an instance of outer class. And   then we need to call the method inner, to be able  to create the inner class. And that's because this   was private. But now that it's public, we should  be able to create an instance of it outside of the   class. So the way that we do that is we still  need an instance of outer class, because this   class is defined within outer class. So we need to  first create this so that this is kind of created,   right? As we did that, we have outer class.  Now what I can do is I can say, outer class,   dot inner class, okay. And we'll just call this  one i n equals lowercase equals out, which is   going to be our instance of outer class dot. And  then in this case, we're going to inner class   believe this is correct. But I guess we'll see  Oh, I probably need an equal sign. And we'll type   new new outer dot inner class. And let's see what  our little red line is create class inner class in   package out. Interesting one second, guys. Okay,  so that's what the issue, I knew it was something   like this, what we need to do is we need to do dot  new, and then inner class because we're creating a   new instance inside of this right of outer class  of inner class. Okay. So now what we've done is   we've created this i n, which is an object, which  is a new inner class. So now we can simply use i   n, which is an instance of inner class, right to  display. So now we can do a i n dot display. Okay,   so if we run this, see, this is an inner class,  and that is working fine. So those are two ways   you can get access the in a class. Now, right?  Now, you might be like, hey, well, why the heck   would I do this, because now it's just like a ton  more lines? Well, there's many times where you   don't really care about like this instance, and  you just want an instance of the object inside of   it. So in that case, this would make more sense to  do. And obviously, you will most likely have more   methods than just one you're gonna be using. Okay?  Alright, so now that we have that, I guess, let's   show creating this kind of inner class. So I'm  going to delete this inside of a method. So we can   actually create these inside of methods, which are  really useful because sometimes within the method,   we want to use a class and we don't want to have  to define like a new file for another class.   Okay. So we have this public void, enter. So I  guess let's just work with this right now. You   don't even leave in a class right now. And let's  show you how this works. So all I can do in here,   and I can say, we can do public private, in this  case, I'm just gonna say I'm going to say class   to class in a class. And then here, yes, that  works fine. We're just going to redefine display.   So we'll say, public void display. And now that I  think of it, I probably could have just copied all   my other code, but whatever, get some practice  typing. Print ln, and then in here again, this   should probably be a T, we will just type inner  class, okay. So this is going to work perfectly   fine. Whenever we call this void inner, then we  have this class inner class, we're going to create   an instance of it, and we're going to display it.  So if I run this, I guess, wow, I really should   just kept all that code. That's fine. Why is this  not giving me an autocrat outer class, out equals   new and outer class and brackets. And then in this  case, we'll simply just call out dot enter. Okay,   so now we can see what should happen is again,  we should print in our class. So there we go,   and we get in our class. Now pretty much you can  do this like wherever you want. The reason I'm not   saying this is public or private is just because  like it's already within this void. So to use it,   we're gonna have to call this void. So there's  no really there's not really any point in putting   like private or public But again, like if you  wanted to can type public class inner class,   or Oh, actually saying you can't do that. So I  guess I was right the first time. So since it's   inside of this void, you're only gonna be able  to use what we call this. So there's no point   in really giving it like a public or private  method, or class name, sorry, because in here,   we can still do public and private methods,  because well, outside of the class, we might   be using those methods, right. So you can see  obviously, since I'm creating this instance,   inside of the, the voids inside of inner,  and if I tried to do that outside of it,   it wouldn't work. Because this class is only  defined when we call the inner method, okay? Now,   interfaces are either unique to Java, actually,  because I haven't heard of them in any other   languages, but they're somewhat similar to  classes, they have to do with like inheritance.   And anyways, you guys will see as we go through  the video, but they're pretty useful. And yeah,   they're an important part of Java, so we need to  learn them. So what we're gonna do first of all,   is we're gonna create a new interface.  Now, just like we create a new class,   we're just gonna right click on tutorial one,  our package, go to new, and instead of class,   obviously, we're gonna click interface. Now,  I'm just gonna name this one vehicle for the   example that I'm going to be doing. So we'll click  vehicle. And that's all you need to do. There's   no like, you don't have to package generate  comments, that's fine to do vehicle. All right,   now we have an interface called vehicle. So  what is an interface? Well, pretty much it is   something that is completely abstract. Now, this  is kind of a new concept, but it means that we're   not actually going to like you can't create an  instance of an interface, all it's meant to do   is to be inherited from. So if we have a class  like car, for example, that I've created here,   car could implement the interface and use it.  But we're never going to create an instance of an   interface it just here, so that we can implement  it into different classes. hope that makes sense.   So inside of our interfaces, and you'll see  in a second, we can only have public methods,   and we actually don't define anything inside the  methods, or the the attributes. So when I create   a method in interface, and I'll just make one  now I guess, I'm going to do, let's see here,   void, speed up. And we'll take as a parameter  into a, that's all I do, when I create a method,   I don't put those, the brackets, I don't do  anything inside the method, I just create   this. Now, you could see that this is completely  abstract, because it doesn't actually do anything,   it just defines the fact that this method exists.  So similar to when you create a variable and you   don't give it a value, that's kind of what we're  doing here. Because we're gonna use this in our   car class. And you'll kind of have to go through  all that before you really get to understand. But   when you create a method, just know that you don't  give it any content, all you do is just say, like,   this method will exist in any of the classes that  implement this. Okay, so I'm going to create a few   methods here and already call it like a attribute.  And then we'll just move into actually using the   interface because the easiest way to understand  it, so I'm going to say, we have speed up, we're   gonna slow down. And we'll have to go change gear  as well. Just going a classic example that we're   gonna implement a vehicle from a car and you guys  will see what I mean. Okay, change gear. Okay,   and then I'm gonna add a attribute. Now, any  attributes that add in here, like variables,   whatever you guys want to call them, they have  to be final. Now, I'm not sure if you remember   in the last video, but final means that it's  a constant and it cannot change. Okay, like,   we can't change this value. So I'm going  to say final int, a equals and then let's   just say five. Okay, actually, or let's just go  gears, okay, like Final interviews equals five,   saying that each thing we're using is going to  have five gears. Now watch, if I try to just do   interviews equals five, that still works, but it's  not going to work in the way that we want. Okay,   so just make sure whatever you're doing  this, you make them final, if you want   to have like variables or whatever that you  want to use in all of the different classes,   they must be final. Okay. So right now, we've  defined three methods, and we have one attribute,   which is just called gears. Okay, so how do we  use this interface now? And how does that how does   that work? Well, the way we use it is we've typed  implements and you might have seen this before,   and then the name of the interface in this case,  vehicle, okay, you can see already that we're   getting a red line, we're getting an error.  So there's nothing wrong here because we can   implement it but car saying the type car must  implement the inherited abstract method. Okay,   so that means that since we've defined these three  methods here inside of our interface of vehicle,   we have to define them inside our car class. So  whatever methods we put here, if we implement   one of the interfaces, we have to define them,  because right now they're completely abstract.   And if we call them well, we have no idea what  to do because there's no content for them,   right? So we're gonna have to create a void  which is change gears. I'm gonna say public   void Change was calling viewers or gear.  Let's see change here. Okay, so change gear,   and then it's gonna have to take one parameter. In  this case, we're gonna say int gear, so like, what   gear do we want to change? Okay? And then we're  just gonna go up here, and we're just gonna say,   private int gear, because we're going to define  that when we when we change gears, right? Okay,   so now we're gonna go to and make another the  other one that we need. So let's just check here,   we need speed up. So we'll say public void speed  up. And in here we'll have speed as like what   speed are actually not speed, we're going to go  into change. So how much we're going to speed up   by, and then we'll do one more. So public void,  slow, down like this. And again, we need this   change. And there we go. So this now we're getting  no errors, because we've implemented all of the   methods that were abstract over here. Okay, now,  gears, we don't need to do anything with it. It's   just like, if we wanted to use it, we could,  okay. So what I'm going to do now is inside,   I'm actually gonna create another private int, I'm  gonna call this speed. Okay. What I'm going to do   now is I'm going to just just fill up these,  what do you call these methods, so I'm gonna   say when we change gear, we're just gonna say,  this dot gear equals whatever gear we're changing   it to. And then for speed, I'm gonna say, this  step speed, actually plus equals change. Okay,   and then down here, we'll say this speed minus  equals change, game. Change. Perfect. Okay. So   now what we're gonna do is we're actually just  gonna add one more method just to show how this   works. So we've implemented the things we need to  implement from our interface. And now what we're   going to do is we're going to just create another  method. So in this case, I'm going to make this   like a display method so that we can display our  car pretty easily. Okay, so public void, display.   And inside of here, I'm just going to print out a  few things to the screen. So System dot out dot,   print, ln, and in this case, we'll just say, I am  a car, comma, and then we'll just add, actually,   let's say going and how fast we going we're going  this start speed. If we want to get out like a   little kilometers per hour, so let's do that. And  I am in gear, and we'll just add whatever the gear   is. So this stuff stuck here. Perfect. Okay,  so let's now actually use this. So we have our   interface vehicle, okay, and we have our car. So  what we're gonna do now is we're gonna create car   objects, we'll say car. I don't know, let's say,  Ford. Okay, equals new car. Okay, perfect. And now   what we're gonna do is we're gonna say car dot.  Ford, I guess is what we need to do Jose for speed   up, and let's maybe change it to 10, we'll add 10  to it, I just realized that we're actually gonna   have to make this speed default at like zero, and  gears, let's just do that default zero as well.   Otherwise, you're gonna run into an air arshi  gear, I guess you can't really be in gear zero.   Can you just change that to one? Okay, perfect.  Okay, so for speed up, and I will say Ford change   gear, and let's just change the gear to Okay. And  then if we want to display our Ford, we can do   Ford dot display. Perfect. Okay, so let's run this  now and see if everything's working. So we say I   am a cargo in 10 kilometers an hour, and I am in  gear to perfect. So that's working fine. And we've   implemented all that stuff from our interface  vehicle interface. So now I'm going to show you   some cool things, we can add into our interfaces  that make them a lot more useful. So say, for   example, you've used a bunch of interfaces with  a bunch of classes you've inherited from them.   And now what you want to do is you want to add a  method to all of those classes that are inherited   from it, well, what you could do is you could do  the same thing here, and you could type something   like this. And then you could go into every single  one of your classes, and you could change them to   be whatever you want. But if you want the method  to be the same for all of them, we can actually   use something this is why interfaces are kind of  useful called default. Okay? So we type default,   and then maybe in this case, void, and let's just  say like outlook, we're gonna print something out   just to make it easy. What we'll do here is we'll  just, we can just create a method and we can use   this from any of the classes that inherit our,  our interface, okay? So I'll do here they'll   say system, dot out dot print, ln, in this case,  I'll just say default method, okay. I know very   creative. How did I just spell system? And I okay,  anyways, alright, I'm tired guys. I apologize   System dot out dot print ln default method.  Okay. So now that we have that, from within   this car class, we can use that default method.  So say when we display, we want to print that. And   then we want to do the default, what we could do,  I believe, is we can say, just out maybe. And yes,   that actually works fine. So now that we have out  because that is a method within our vehicle class,   and we implemented, let's see what happens when we  run our program, we get a cargo tank one mission,   our gear two, and then says default method. It's  using that default method that we created inside   of our interface. Now, I believe we can actually  use static methods as well. So this would be known   as something that's not a static method, because  we have to call it from within our, our class, but   we can actually create static methods within our  interface that we can then just call statically.   Like, we don't have to have an instance to  call them. So if you don't know what I mean,   just follow along for a second, as I'm going to  create a static, I don't know, let's just do like,   int, let's return something, let's change it up.  Let's just call this math. I feel like math is   a keyword. And that's fine, we'll just do math.  Okay, we're gonna take int B, and all we're gonna   do is we're just gonna return b plus not okay. I  know very basic, but that's all we're gonna do,   just to prove a point. So now, saying, I'm in  my main job, all right, and we get rid of this,   we don't create a car object. And I want to use  this static method inside of my interface. Well,   what I can do is I can do vehicle, dot, and then  you can see it already has this function coming   up here, this method dot math, give it a value,  like five, I'm just actually set this equal to a   variable. So we'll say x equals that. And then  we can System dot out dot and see what we're   actually getting as a value. In this case,  we should be getting 14, right? Yeah, so 14,   there we go. So let's see, you can create a static  method inside of your interface. So in some cases,   you can kind of think of these as functions.  All right, say you had unless you don't,   I'll just do one as an example. Because this is  kind of useful. If I go new interface, and we   call this math, okay, then inside here, you could  do a bunch of static methods that will do like   math operations for you. Like maybe you have the  quadratic formula, maybe you had, I don't know,   like Euclidean distance, I don't know a bunch of  different math formulas, you put them on this math   interface. And then when you want to access them,  you would come in here and you'd say, well, math,   dot maybe like square root like sq. RT, okay. And  then there, you'd be able to use all of those,   I want to call functions, but they're really  methods inside the, the interface really   easily. So that is a really good example  of when you can kind of use interfaces.   Now, enums are pretty much a collection of  constants that we can reference. And then we   can do things with those constants. And they  are pretty cool and just make your code more   readable. And yeah, you guys will see when we  go through the video. So the first step is to   create an email. Now to do that it's similar to  creating a class or an interface or whatever,   go to your package, right click, we're gonna  click New and then you know, so for e&m, I'm just   going to name mine. Actually, I'm just gonna name  mine level, because you'll see why it makes sense   for this example. So again, the syntax here is  is public e&m level, rather than like class or   interface or whatever, right? So in here is where  we're going to define our constants. Okay? Now,   this is really useful, because we don't have to  do like public static, and whatever like the name,   like we can literally just type whatever value  we want our constant to be in, typically,   it's a string. So in my case, I'm going to  go for level, I'm going to go in all caps,   you don't have to go all caps, we're gonna  go high, medium, and low. Okay, now, these   are the three constants, I'm going to store in my  level iums, we have high, medium, low. And yeah,   that's literally all I'm going to do right now,  for my e&m. It just means we have three constants   that we can reference from level and these are  the three. And obviously, you can do as many as   you want. And just keep going with commas on and  I think you can put them on the same line as well.   It just makes them more readable if you do them.  Like other lines. Okay, so every three constants,   now I'm going to show you how we can actually use  this email and you'll start to understand how it   works. So to use this enum, we have to create a  new instance of it. So to do this, I'm going to   say level, and we'll say LBL equals new. Actually,  I think it's like this was level dot, and yes, we   say a high, low and medium. So my guess we'll just  pick a low, and I'll put a semicolon. And now you   can see it's all showing up in different colors.  And this is working fine. So now that we have this   as a constant, we can do certain things with it,  right? So typically, you might want to check what   value your constant is like what value is levels.  So we can obviously use these in an if statement,   like so. So I say like if elvio equals equals and  then like it's level dot analyst, Start out low,   I guess. And we'll just work our way up to high. I  mean, say l if our elsif. And we'll say LBL equals   equals, and then level dots and medium. Okay,  we'll do something in there. And then finally   else, because if it's not lower medium, it must  be high, then we'll just use me. So in this case,   we'll just say system dot out, dot print, ln.  And we're just gonna print level for all of them,   because I want to show what this does. Okay?  What the heck did I just do there? Okay, anyways,   now we're just printing level each time. And  actually, let's just run this down and see what   happens. So you see, we get low. So when we print  out level, it's simply just gonna print like,   whatever the constant is that we have. And that's  because it has a little.to string method built in.   So we can convert these into a string. And I'll  show you what I mean by that. So pretty much   say we wanted to get this because right now like,  it's not a string, it's not an it's not a Boolean,   we don't really know what type it is, it's just  like in level it gets an enum. So to actually   get this as a string value, if we wanted to do  something with it, or store it, we can use.to   string. So if I say string, we'll say E. Yen,  why not? Why not? We'll say is equal to lbl.to   string. And that's just going to convert this to  a string for us. And then we can do all our string   operations or whatnot with that. All right, so  to get it to a string. Now another really useful   thing that you notice is we actually get all the  different values of enums and print that to the   screen. So where do system dot out? dot print ln.  And I believe the way we do this is to do level   dot values. Yes, there we are. So we go level dot  values. And if we hit that, or we type that in,   you're going to see when we run this, now, it  gives us what is actually giving us an array with   the values. So when we try to print that we're  getting like, you know, the memory address of   it. So once we have these values, so I'm actually  just going to store this an array so that we can   then iterate through it and print them out. What  I'm gonna do is we'll say, level array. And this   is the way you have to do it, let's say arr is  equal to and then level dot values. Case, that's   working fine. So what we're gonna do now is we'll  just create a quick for loop. So and I'll show you   how we can loop through all the different values.  So say, for E, or home, which would be this level,   level E, and then we'll say, in ARR, what we'll do  is we'll simply just print out, E, okay. So system   print out E, and then we can see all the different  constants that we're going to get. So just go   here, and you see we get high, medium, low, and  then just prints low. Again, just because we're   doing this if statement here, okay. And that's how  you can loop through all the different values. So   that's really cool. But you might say, Okay, well,  that's cool, too. But is this really that useful?   Like we just have these constants? Well, there's  a few other things that we can do inside of this,   you know, add some methods, and some instructors  as well, so that these constants actually point   to certain values, kind of similar to like a hash  table or hash map that I showed you before. So the   way that we can do that is we need to create first  of all a variable that's going to be private. So   in this case, I'm going to say private, and I'll  say, actually ends level LBL nom, okay. So this   is going to just be a private value, that's just  going to store the value of each of these. So like   low, medium, high value of all these levels.  So just set this value, what we need to do is   we need to create a constructor for our IEM. And  you'll see how this works in a second. So I'm just   gonna say public level in and then none. Okay? And  in here, what are we getting here? Remove invalid   modifiers legal modifier for short only private  instrument. Okay, so let's do this. Private,   sorry, my bad private, because we're just gonna  call it on here, you'll see in a second private   level int num, and we're just gonna say this dot  LBL, num is equal to num. Okay, there we go. So   now what I can actually do is you see how these  are highlighted in red. So since this is a private   constructor, that means we're going to define  this variable based on what constant we choose   for the enough. So I'm going to put brackets here  and I'm going to define highest three, medium as   two as low as one. Now, what this is going to do  is it's going to keep our same constants of high,   medium and low. But when we create a new constant,  it's going to activate this constructor of level,   and it's going to set this level non equal to  whatever value we put in the brackets here. So in   this case, 321, we could obviously have multiple,  what are called parameters here, and we could have   multiple arguments here and we can have a ton of  different values. And yeah, so the only issue is,   since this value is private, we're going to need a  way to access it from our main here because that's   probably where we're gonna want to use it. So that  means we're gonna have to create a few methods to   will change that level num and to get that level  num so we've done this before, but Actually,   I don't think I can do a public. Eye we'll see  if we can public gets level. And then in here,   we'll simply return. Guess it's gonna need to be  an int, public int get level will simply return   this dot, LDL num. Okay, yeah, so that does work  fine. I just can't do a public constructor. Okay,   so this is just going to give us the level num. If  we called get level on our keynote to change this.   What do you call it? level num. What I'm going  to do is I'm going to do set LBO. And so having   interior it's just gonna be a void. Okay, and all  we're gonna say is this dot LBL, num is equal to   num, and then we're gonna put in here, int, num,  Okay, perfect. So now what we're doing is we have   a way to not only get the level num, but to change  it. So now let's try using this out in our main   and see how this works. So I'm just going to get  rid of all this stuff, because we don't need this   right now. So right now we have our level and it's  level dot low. So to actually get the value of   this level, we can use something called get value  of or we can use that method that we had here. So   in this case, I'm going to say get level. So what  I'll do, as we'll say, system, dot out, print ln,   and in here, we'll say elvio. dot. And then what  was the thing we were using get level like this.   So now if we print this out to the screen,  we should be getting one. And indeed We are,   we're getting one. So now I'll show you this  thing. It's called get value of Okay, and is it   x value ever get value? I'm not sure we'll see. So  I'm pretty sure if we do something like this level   dot value of Yes, that is exactly what it is. And  then we pick a we type in whatever the enormous   so in this case, we could say, low, it's gonna  give us Let's see, what's the issue here. Okay,   so the issue is, I just got to put this in string,  I just forgot about that. So I do level dot get   value of and say I put a string in here. So like,  say someone was typing in and into the computer,   and they type in a string, right, and you want  to pass this in to get value of we see what we're   actually going to get is we get low, right?  Because that is the value of this string.   In the enum. Although it's not a string, it  can like determine that, okay, so that's what   value of is used for, I can't really give you  any good examples of them. Cuz I don't really   know any good examples of using value of but  for any of you that might see some use in it,   I figured I would show it to you. So I guess we  can do an example. Now just setting the level just   to make sure that all that is working, say elvio  dot set level. And in this case, let's say five.   And then let's just grab this again, actually.  And we can delete this row up here. And now I'm   just simply going to print out, record elvio dot  get level, and just make sure that everything is   changing and working fine. And there we go,  we get a level of five. So that is how we can   use itams. Pretty much they're used for when you  want to have a collection of different constants,   maybe you want to loop through, see what those  constants are. And it just makes a code kind of   readable, right? So we have level dot low, and  then that low can point to like five to one,   whatever. And that's all stored within our enum  right here. Obviously you can have as many items   as you want and you'll really see the use of  them further on in Java when you're creating   like larger programs. Okay, so anyways, that has  been it for this video. If you guys enjoyed please   make sure you leave a like and subscribe  and I will see you again in another video.
Info
Channel: freeCodeCamp.org
Views: 977,045
Rating: 4.926837 out of 5
Keywords: java, fcc
Id: GoXwIVyNvX0
Channel Id: undefined
Length: 228min 24sec (13704 seconds)
Published: Tue May 21 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.