Java Tutorial - For Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody and welcome to this java course for beginners so before we get into the content let me just take a quick minute to give an introduction talk about who this course is for and where you can find some resources and just some other necessary information that i need to tell you so first of all this course right here is designed as i said for absolute beginners for people with no coding experience before or people with very little programming experience now of course if you're more experienced you're welcome to follow along with this and in fact there's timestamps in the description that you can click on that will show you all of the different things that i'm covering in this video now it's worth noting that this actual content so all of the content for this video was filmed over a year ago so i've had these videos on my channel for a really long time and what i figured i would do is re-edit them kind of make them a little bit nicer and put them into one larger video so more of you guys can find out about my java content and will be able to follow along with this course so that's pretty much all i wanted to say this course is designed for beginners people with little to no experience these videos are older but i've re-edited them and kind of put them into one larger video to make it easier to follow along with and well i hope you enjoy so with that said let's get into the content and learn the java programming language so before we dive in i need to thank jetbrains academy for sponsoring this video jetbrains academy provides amazing resources that teach you how to code in a project-based approach using jetbrains academy you can follow along with learning tracks related to java python kotlin and web development each track has a catalog of awesome projects you can create and that are designed for programmers of all levels you can solve simple problems in the jetbrains online code editor and then start learning professional development tools by creating your projects in the jetbrain ides this will allow you to develop the skills to use a professional ide and improve your coding at the same time jetbrains also provides another great feature called the knowledge map this gives you an entire view of all of the learning content and allows you to see how single concept topics are connected you can also use the knowledge map to plan out your own learning goals and track your own progress get started using jetbrains academy by signing up for three months of access completely free from the link in the description in fact i'd encourage you all to install the jetbrains educational ide to use to follow along with this tutorial video unfortunately for me at the time of filming these videos this ide was not around and that's why you'll see me using a different one in this video with that said let's jump into the content so let's go ahead and get started the first thing we need to do is download a ide and this is where we're going to be writing our java code okay so it's called eclipse this one i'm going to use use whatever you want but if you're a 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 tv you probably have a 64-bit machine once you download this uh 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 now it is going to 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 i'm going to click on file new and then java project now here i'm just going to 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 the location go ahead all right 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 going to 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 just gonna create a new one really quickly and then walk you through this so it's fresh uh tutorial one okay finish all right we got tutorial one and we get source okay now inside of the source what we're gonna 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 class 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 will just leave those blank and the name you can name this whatever you want in my case i'm going to name it main okay type name is discouraged okay so let's name it main with a capital m all right just because java likes that and then what we're going to do is make sure you check on this public static void main uh we need to check mark this because we're going to need this function created so that we can start all right i inherited abstract methods you can uncheck that actually we'll check that it 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 program language meaning that everything happens inside of something called classes and everything is known as like objects all right now you're not going to understand very much about of this right now if you're a beginner but for some of you guys that are more advanced you should kind of 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 uh 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 those words mean just know this method is important and this is what 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 um not inside of this method that's what we're calling it right now okay uh your code is not gonna execute properly unless you know what you're doing which in our case we don't yet so all the code for 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 dot out dot 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'm going to say the classic hello world with an exclamation point okay now at the end of every line we write in java with exception of lines that start or end with uh curly braces we need to put a semicolon and now you can see once i put the semicolon that our system.out.println uh gets highlighted and we can see that it now has syntax highlighting uh and 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 ridden our first program all it does is print hello world to the screen but how can we run this uh so first we're gonna have to save so i'm gonna hit ctrl s and to run this there's a little green button up here and it says run main.java so we're going to click that and you can see that down here a little thing popped up called console and we printed to the screen hello world now 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 going to explain that later public static void main we have to put 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 going to say system.out.println and i'll say like hello world 2. and if i save this and run again we get hello world and hello world 2. now that has been the introduction tutorial in the next video i'm going to go into data types and primitive data types and we're going to talk about like integers and strings and all that fun stuff so if you guys enjoyed the video and you're excited for the next one make sure you leave a like and subscribe and i will see you there as you can see we uh are right where we left off with printing two lines hello world and hello world 2 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 an 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 going to type int i'm declaring that this variable that we're about to type out is going to be an integer we are 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 in it 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 try to put like an asterisk 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 two 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 enough about valid variable names it's not super important right now so i'm going to create my first variable into hello world and that's going to be equal to by just putting an equal sign whatever integer i want so in this case i'm going to say 5 and then remember in java whenever we end the line so this is just saying my integer hello world is equal to 5 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 uh is never used okay that's fine so we've officially created a variable the variable name hello world now points to the integer 5. so to prove this to you i am simply going to print out the value of hello world like this okay so i'm going to say system.out oh that's a comma isn't it system.out.println and what we're going to do is we're going to 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 all right 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 and it's kind of like order of operations or execution so i am going to now put my variable underneath my system.out.println okay so i'm first going to print hello world and then i'm going to set hello world equal to 5. 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 uh 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.out.println uh hello world and then we create hello world so here we don't know what hello world is so if we try to do this you can see it warns me there's an error i'm gonna say okay you know 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 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 going to say float uh let's see num 2 is going to be my variable name is equal to and in this case i'm going to say 5.0 uh what's the issue here add cass from double to okay one second we're just gonna say double okay 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 num2 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 value as 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 going to be really important in the next few videos that we talk about so boolean i'm just going to 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 uh that your a 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 now char is any character okay so this is a little bit different from a string that i talked about in the last video but i'm going to talk about string as well so i'm going to say char c is equal to and whenever we create a character there's two ways that we can do it we can do it with 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 um because it's in quotation marks it's different than the number like nine if i were to type that up here okay so any letter or character that goes into quotation marks is a char now we have to be careful because chars can only be one one character if i try to do like zero three you can see that we're getting red text and java is yelling at us and it's saying no that's not allowed because we can only have one thing all right now i just want to show you i want to go over again really quickly what the main difference is between this 9 and this 9 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 or something for now okay char c 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 going to say str is equal to and then at this time for string we use double quotation marks okay and i'm just going to 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 six seven three underscore uh 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 uh they're a bit more complex than that 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 in this orange doubles orange booleans orange charge orange why is string blue now the reason it's blue is 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 uh primitive data types okay and what that stands for is not changeable okay and we're gonna we're gonna 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 i'm 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 going to get we get h because that's c if we print str we get 9 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 going to 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 going to 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 9 okay but i don't want to type 9 out what i'm going to do is i'm actually going to type hello on your scroll 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 uh hello world to the screen oh 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 tim equals hello world you can see here now it says cannot converge from int to string because the data type 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 so anyways that has been it for this video i hope you now understand what variables are how they work and how we can create them in java in the next video we're going to be talking about operations so adding variable subtracting dividing doing stuff like that and then we'll be moving on to some more advanced stuff as always if you guys liked the video please make sure you leave a like and subscribe to the channel i will see you again in the next one 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 uh exponential all of that fun stuff so without further ado let's get started now what i'm first going to do is just talk about another way that we can create variables in java so earlier you you saw me do something like this into x 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 we just say int x what happens now is we've declared that x is a variable uh 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.out.println and we are simply going to print x and just see what we get oh i probably help if i spell to print correctly okay wow i really messed that one up print lm 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 it 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 we want it to be as long as it's an integer value so in this case i'm going to say x equals 6. and now if we run the program everything works fine and we get 6. now obviously we can do this with every data type so if i do like char and char x right that's fine and 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 5 uh don't forget your semicolon integer y equals 7 and i'll say into z is equal to 56 okay and i'll create one more variable and this is 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 7 and 5. but i don't want to write like 56 plus 7 plus 5 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 we can do and we can just use the variable names and just add them up so we can say x plus y plus z 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 replace this with a minus sign and we get negative 58 okay now to multiply things this is another operator we can do multiplication like this and i'll replace this one with multiplication and we'll multiply all these out together you can see we get uh 1960 as our value to divide we can use the uh forward slash now i'm not going to do that right now because that is kind of a different topic like 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 going to start off with exponents and then go brackets 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 z from whatever this value is now again if i switch this and i get like y multiplied by said what's going to happen first is y multiplied by z and then the subtraction is going to happen now for example if we have um the operators that have the same presence or the same i don't know like order of operation level i don't know what to call it um it's just gonna order it uh operate from left to right so it's gonna do x multiplied by y multiply by z so this is a common occurrence a lot of people understand this uh if we do division sign we're first gonna multiply x times y and then we're gonna divide by z 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 uh say x multiplied by y and then divided by z i could do something like this by throwing brackets in here so now it's showing 100 that we're going to multiply this before 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 uh in java is it just in general okay so if i want to divide 56 by seven so i want to divide z by y okay and i s some let's just do uh u okay i'm gonna call this u 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 z 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 let's see let's make sure this is not messed up 57 divided by this okay yeah so it can't give us a floating point number because this is well and into data type so it assumes that we want an integer in return so therefore it's just going to give us the value and terminate all the decimals okay so say this number's like eight point something just gives us eight right removes the remainder now if i want to get decimal points which you will want to do most of the time i could try to do something like this okay double of u equals z 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 if i run this we just get 8.0 well we know that 57 divided by 7 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 a 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 okay it's like double and could change this to be a double all right and if we have both of these double and we try this now you 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 into y and this is double 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 of the operations so if you have double u and in this case we say like x times y or let's say x times z since z is a double x is not we're still going to get a double value so if we run this you can see that we get this little point o 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 um the way that we can do exponent is we have to bring in module i believe um but it's like math dot pow and then in here you put the uh what do you call the bass and the x the exponent there's not a star star like in most languages okay i'm to say ins of d is equal to math.pal and here i'm simply going to raise x to the power y all right so we get math.pal let's see what is this saying convert okay so let's do this cannot convert form double to form int interesting why it's telling me that let's just try this maybe there we go okay so whenever we get x uh x exponents apparently they have to be in the form double okay didn't know that but all right uh so now if i want to print out d what's going to happen is we get the exponent for this okay so math.how this is your base and this is what you're raising the exponent to so 5 to the 7 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 in so if one of the operators in the whole chain of operators or offers 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 now same thing here so i have let's change y back to double and let's change u to be int okay so it says into you x times it 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 int 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 as double and it's nice in this ide 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 going to 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 the decimal point value okay like so a double rather than converting these like the declaration of our variable to a double something 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're looking for if i remove this double right then we do not get that or we do just because it says double but you guys see the point okay so anyways i think that is going to be it for this video in the next video what are we going to be covering got to look at my guide here we're going to be talking about input and output so how can we actually get input from the user in the console and then doing things based on that input printing them to the screen adding subtracting whatever okay so if you guys enjoyed the video as always please make sure you leave a like and i will see you again in the next one 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 going to 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 programming right based on what a user does we want to do something in most cases so the first thing that we're going to do is just teach this other operator it is the remainder operator it's not going to take me very long in this case i'm just going to say intex 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 going to pick five okay so what this remainder operator does it's called 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 uh what do you call it 56 divided by 5 should give us 11. okay 11 remainder 1 is what this division actually is equal to so if we print x we should get a value of 1. 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 the first thing we need to actually do is at the top of your program you might notice i have a new line here now this says import java.util.scanner i need you guys to type this out for me java.util.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 going to type scanner the name of our scanner is going to say sc is equal to a new scanner and inside of the scanner we're going to type system dot in okay in this brackets this might seem confusing but all this is doing is it's saying scanner is a data type just like string is the data type just like boolean's 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 going to say well it's equal to a new scanner and what this 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 uh use the scanner so how can we do this so what i'm going to type now is i'm going to say let's say string scanned is equal to sc dot next and i think we can just use next yeah so what this does is we're creating a new variable string it's called or it's a string type called scanned and it is going to get the next uh stream of input from this scanner object okay so sc.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 so i'm just going to run the program and illustrate and we'll talk about it so now you can see if i go down here my cursor uh shows up and i'm able to type this because i'm going to type hello okay watch watch what happens when i hit enter it prints hello underneath it so hello which we typed in was the input it got scanned in by the scanner object using sc.next it got stored in the variable scanned and then we were able to print scanned 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 i just need to show you that now i'm going to do this with indicate so i'm going to say int scan equals sc.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 say we 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 in we have to use a another method okay and this one is called next int and now you can see we're getting no more red lines everything seems to be working fine sc.nextint and if we run this then we type like 54 that works fine prints that out for us watch what happens though 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 and said no 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 and would you look at this boolean that's literally the name to get a boolean sc.net boolean okay so let's run this now note a boolean has to be true spelled correctly or false with lower cases 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 going to crash it's going to give us an issue okay so boolean that's how you do that one if we want to get a double you 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 just gonna do 6.3 and it prints out 6.3 let's actually just test 6 vd6 yeah it just gives a 6.0 so that works fine okay um to getting doubles now i want to work around the issue of uh what happens like we get that crash okay we don't we don't want that crash so how can we fix that if for 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 sc.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 mark so it's really a string okay if i type 1 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 take 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 way that we can do that is we first need to set up a variable so i'm going to say into x in this case is equal to scanned now i need to put something with this scan i can't just say in x equals scanned i can't i can't just do that it's not just going to 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 or sorry we need to type here integer dot parse int okay and then inside of the brackets for parseint 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 if you don't know what it does it you can actually read through the definitions in this case it's going to say parses the string argument as a sine decimal integer the character in the string must be all 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 uh integer is a class so yeah it they'll convert that for us so then if i want to print x to the screen that would work fine now the thing is though and this is what we're going to 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 uh 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 inch like that and everything works fine for you anyways that has been it for this video uh and again in the next video we're gonna be going over conditional statements and then probably we'll be going into uh if and else is the one after that anyways if you guys enjoyed as always please make sure you leave a like and subscribe and i'll see you again in the next video so in this video we're going to be talking about comparison operators uh select greater than less than equal to not we're going to be talking about chaining conditionals together uh which you'll see if you don't really understand what that 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 they're integers uh x is six y is seven and z is ten now i'm going to create a boolean variable so boolean and let's call it compare and we'll just set that blank for right now so what i wanna do is i want to compare x and y and z using some different comparison operators so in java i'm going to write them out here we have i believe four main comparison operators four or five so i'll type them on 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 is an exclamation point and an equal sign now i'm going to go through these and show what all these do so i guess 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 sim it's just there for the uh the programmer so they can look at it and be like okay this is what this line of code does um 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 uh it just ignores that line and moves on so that's simply what a comment is and it just grays out in here to show us that that's a comment okay so compare so i'm going to compare x and y and i want to say 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 6 is that less than 7 yes it is 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 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 just going to make two strings here i'll say oops string x i will say hello and string y and this is equal to high 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 is 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 going to get true if i try to say if they are the same i get false now i want 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 or not well i'll show you it is not so capital letters do matter in programming right and let's say 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 no 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 and 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 into x equals what did i have 6 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 z is greater than x say i want to check two things perfectly valuable 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 and operator and the 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 z is let's just say z 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 6 is that greater than y no we're going to get false so i'll just type it out as if this is what's happening in the computer right and then is z 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 operator is going to do is it's going to check if both of the conditions on the left side and the right side are true if they are both true it returns true to us if they're both or if 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 two straight up lines like this i don't know what they're actually called uh the key for that but anyways so this is going to do kind of it's similar to and 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 not operator now what not does is simply going to reverse anything and this is the way i like to think about it whatever you have if you see the not just reverse it so what this does is it's going to say if this condition is not true then we get uh false or if this condition is false sorry so 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 it is going to give us false as 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 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 orders 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 going to 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 z or z plus 2 is less than 5 or what do you call it like x plus 7 is greater than y 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 i'm like wow okay how do i determine uh 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 condition now 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 z plus 2 if that's less than 5. 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 we get true okay i don't even know how that worked to be honest and yeah that's how we could do that we can also throw knots in here too so if i wanted to throw a knot like this then we can do that now typically you're not going to see massive chained 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 want 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 z plus 2 less than 5. that's a perfectly valid condition that works perfectly fine could change this is equal to 5 right um you can add constants like i could just say two is equal to five if i wanted 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 not equal to or equal to and you can compare floats with ins and you'll see as we continue going through the video it's too long to go through all of them right now anyways that has been it for this video in the next video we're gonna get into if else and l-if statements and yeah if you guys enjoyed please make sure you leave a like and i will see you in the next one in this video we are going to be going over if else and l if statements in java so quickly uh 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 are the same what you should actually do is say you have a string so s in this case uh 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 if 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 equals 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 be doing using if and else statements so pretty much if something happens do this otherwise do this and this is the basis of programming uh using conditions based on like user input or based on certain events that happen we're going to 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 and else is 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 spent so much time talking about conditions in 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 i'm going to say if s dot equals not to 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 the 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 type 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.out.println and in this case we'll just say you typed tim okay wow types you types you typed tim okay and let's run the program and see what happens so when i run this uh if it loads up that's odd okay um give me one second okay so we're back i accidentally closed my console and uh yeah we're running into some issues with that so i got that up and running now uh let me just restart this quickly okay wow okay i don't know why that's white but anyways let's just type out i don't know 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 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 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 uh what happens here is like what is the program going to run so if we type tim we're going to get this you typed him but are we still going to get this print out here 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 typed him and then it says print okay now the reason that happens is because this print is not within this if block right so it's just going to execute simply after this if block happens so 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 else if we're going to start with else so else is very straightforward it can only come after an else if or an if so like the start of the block has to be if and then this else you can't just have it like a loan it has to come after an if statement or an else if statement which we're going to go into in a second okay uh there's different ways to format this some people like to put the else like this so it has the closing brace and then 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 out 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 else statement all 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 uh 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 them is else all right so let's test this out if i type tim we get you type tim okay if i type anything else so like some random letters and i hit enter you get print so the way this else 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 i need to talk about that anymore the next one i'm going to show you is else if 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 else if and now it's the exact same syntax as a regular if statement except you just have this else before and obviously since uh 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 uh s dot equals in this case let's just say like hello okay and then here we'll just do system dot out dot print ln and we'll just say hi because we want to create them back they said hello okay so again the way this is going to work is we're going to go through we're going to check we're going to get our scanner input get s say okay what's s is s equal to tim if it is we're going to print this and then we're going to move down after the last else statement we're not going to even bother checking if it's uh something else because if it's equal to tim then we know that we shouldn't bother looking at the elsif's 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 we're going to say okay else if that's our next block is it equal to hello if it's equal to hello we're going to print hi we're going to move on with our lives we're going to 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's 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 so like some random letters we get print okay and that is how that works now i can show you i can add as many lsifs 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 here i could type hello and we could keep going and we could do as many lsips as we want just know that whenever you have 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 an 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 um 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 else statement now if i type hi you see we get hello because right we went through it 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 is it high or is it hello well let's test it out if i type hello in here we print hi 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 well we're like okay well that happens 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 lsifs as you want you don't need an elsef like we could just do an if and an else but every time you have kind of like an if statement or block starting it has to start with an if um it can have as many elses as you want and the last thing has to be an else if you're adding the l so you don't have to have that else there so anyways i think that's all i'm going to show for if statements else ifs 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 uh chain condition with a ton of stuff same thing here you could have any condition like it doesn't matter anything that gives you a true or false value you could also literally just type true and like that would always happen okay so anyways that's been it for this video if you guys enjoyed and learned something please make sure you leave a like and subscribe and i will see you again in the next one in this video we are 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 going to 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't 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 i'm 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 instead of print ln and in this case i will just say input your page simply okay so this way it should uh if i'm doing this correctly make it so that uh we just get the age right after this line okay 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 after so i'm going to say if and we're actually just going to convert this to an int first i think string to an int yeah so using that parseint that i was talking about so say int of age is equal to s dot or integer i always forget how to do this integer.parseint and then in here we need to type s so we're going to convert whatever they type in to 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 uh system.out.println you can ride 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 ride system.out and you cannot write exclamation point okay pretty basic program uh we kind of gone through how 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 do 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 ages 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 teenager is okay so if we are greater than or equal to 18 i want to print u r and 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 well we'll do it in a cool way that's going to 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 or greater or older than or equal to 13 because that's what a teenager would be okay and the reason we don't have to check if they are uh what do you call it younger is or if they're older is because uh we already know that they're going to be younger than 18 so no we're they're 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 13 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 if we input an age of 19. get you an adult and if we input an age of zero yet you are not a teenager or 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 else 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 i 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 um 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 i'm going to say sc or we'll say string i guess we can just do ins because we're going to assume that our string i guess because only food yeah we'll say string food is equal to sc dot next uh line like this okay and what i'm gonna do is i'm gonna print up here again what we're looking for so i'll just copy this save me 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 i'll say that's not my favorite food or something like that okay so we'll say if and we have our condition in 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 quotes hopefully say mine to and then otherwise so the else what we'll do is we'll simply type system dot out dot parent ln and in this case we'll say not mine okay so this is showing you now it looks a bit more complicated but 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 uh i'm 19 it's going to say input your fav food okay my favorite food let's try pizza it says mine too awesome that's working now if i type something else i type like four it just tells me i'm not a teenager an 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 and this is called nesting putting a statement inside of another statement you can continually 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 or 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 could just keep going but anyways i think that that probably gives you guys a good um explanation of how that works and shows you that i don't need to go any further so with that being said i'm going to end the video here in the next video we're going to be getting into looping which is some more advanced stuff some cooler stuff and we're continually moving through and we're going to be getting more advanced now uh getting out to kind of the more basic stuff of java that being said if you guys enjoyed the video please make sure you leave a like and subscribe and i'll see you in the next one okay so i know i said in this video i was going to be talking about for loops and looping but i actually forgot that i haven't yet talked about arrays so we're going to be going into arrays in this video and that will lead us really nicely into for loops and wallops in future videos because that's kind of one of the main things you do with arrays is you loop through them and you look at things so we first need to know about arrays before we can really move into loops or at least so that it makes more sense in this series so anyways 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 grade 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 you say in square brackets the name of your array in this case i'm going to say 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 going to say five okay so you can see that's fine not getting any issues here just because we're not using new ar um but yeah this is the syntax so whatever type you want it to be you type the type so if i want to be string i do string and then this obviously i 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 null uh so they have no value but they exist like they're there they just aren't holding anything right now okay the way you can kind of think of it is you're initializing like five containers that are all gonna hold something but they're not yet holding anything it's nice it's a good way to visualize so we're gonna keep the string array for now i'm gonna show you how we can add things to array um and change values and print them out and see what that looks like okay so the way that you index things in an array an array is going to be is going to look like these curly braces okay so i'm just going to type some stuff out and just follow with me right so my string array i want to have hello uh i need double quotes hi uh tim maybe i should have done less 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 uh or 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 uh 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 so like index zero is hello index one is high index two is tim this allows us to really easily grab different elements um without having to know their value we just know the position of them in the array okay so 0 1 2 3 4. 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 i'm just going to 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 array 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 uh 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 well we've declared an array and we said it has length five which means that these elements already exist or those buckets they're just not holding anything so what we have to do is we have to say new ar 0 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 uh 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 do zero one two and three and here i can just change these so i'll 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 4 is equal to uh we call it geo oh i don't know why it's doing that for me okay geo like that all right so if i say string x equals new ar 4 then that's going to be equal to joe so to print this out to the screen now i can do system.out.println 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 uh let's see what happens when we do new arr4 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 going to hold values we don't know what the values are yet all right 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 null 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 gonna create another array and show you a kind of an easier way to do this if you're just gonna like statically type in uh numbers okay so i'm going to say int this this time we're going to do an enter edge to save me from doing all these quotation marks i'm going to say inside let's say nums is equal to new int square brackets or actually we don't need that because we're going to do the other way we're just going to 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 2 3 54 6 and 6. now again just to recap 2 would be at index zero three would be at index one okay because that's the way we start counting and now if i wanted to instead of having x equal to new arr i'm gonna say nums four which should be six right and what's our error here change this has to be an int my bad and x equals nums4 we print this out you can see we get the value 6. 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 ins we could do string we do float we do double so you say like a double array of nums 2 is equal to and then in here you like 2.0 3.0 there you go we now have a uh well really i forgot that okay well you need that but that works okay we now have a double array at index zero we have 2.0 at index 1 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 and in the next video i'm going to go through looping through arrays and we'll be talking about that more using for loops and while loops so as always if you enjoyed please make sure you leave a like and i will see you in the next video 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 what for loop is it 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 well 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 um and just do an example of what a 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 but i create an integer uh let's see into x 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 two like and 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 run 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 fourth 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 going to put three things typically okay so what you're going to do is you're going to first start off by declaring a variable so in this case i'm going to say intex is equal to zero okay now this can be called whatever you want typically people call it i i like to use x but you know what let's just use i uh and then what you're gonna do for this next so you're going to put a semicolon and then the next thing you're going to do is you're going to set a condition so i'm just going to do this and then talk about it because it's hard to kind of do them step by step because they all work together uh less than equal to 10 and i plus plus okay so what i've just done here actually is i've first started by declaring a variable i it's 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 i 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 the loop is going to run so in our case we're going to run the loop and well i is less than or equal to the value 10 meaning we're going to start at the value 0 because i equals 0 we're going to run this loop then we're going to come back up here we're going to say okay what are we doing we're adding 1 to i then we're going to check this condition so i is not equal to 1 we're going to say well is 1 less than or equal to 10 no it's not and then we're going to loop through and we're going to continue looping through until eventually we get to the point where we add to i it's equal to 11. 11 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 going to do is i'm just going to print i here and i'm going to tell you right now what's going to happen right so we're starting i at 0. so the first loop here we're gonna come through i is equal to zero we're gonna print zero next loop we come up we add one to i check the condition we're okay we can keep going we're at one and we're gonna print from zero to ten 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 0 to 10 is 11. all 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 4 into i equals 0 i could do 4 into i equals 5 and i can start at the value 5. and now we're going to start and we're going to go from 5 to 10 looping one two three four five six times right we're printing six different values okay um so that's if you can do that if i try 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 you want so in this case if i do five we're gonna start at zero we're going to add five we're gonna add five again so we get zero five and ten 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 in x equals 5 then i could do i plus equals x right and we can add that integer to it we could add x as the bound here so less than or equal to this could be greater than this could 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 uh 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're 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 9. 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 and this is why i talked about arrays before i'm going to create an array so an integer array it's called arr is equal to and let's just give it some values here it's like 1 5 7 3 4 5 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 if a value is equal to 7 our value is equal to 5 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 array i could say 1 2 3 4 5 6. okay 6 elements so i would put i is less than six here 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 r dot length or i actually don't need this bracket sorry um 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 realize 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 6 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 5 right because we go 0 all the way to the length minus 1 which is 5. so ray 6 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 going to say if the value is equal to 5 we're going to print it out so how do i do this i'm going to say if arr at index i is equal to the value 5 then we're simply going to do a system dot out dot print ln i just realized there's a comma there all right semicolon and we're simply going to print the value so in this case we could print ari which we know is going to be 5 or we could just type 5 because we know that is there so in this case let's just do found a5 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 move that equal sign otherwise you're going to 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 5 we're going to print it so let's see if this works found a 5 and found a 5. now if i wanted to be more precise and say like where i found this 5 i could say found a 5 at index and we'll just put a plus sign and then we can put i 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 i don't get smudged together but it says found a five at index one 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 now in the next video i'm going to talk about looping through arrays in a different way that is easier than this and it's called four each okay that'll be in the next video and then after that we're gonna go into while loops and then once we finish while loops we'll be going into collections like sets and stuff like that and then getting into all the object orientated program anyways if you guys enjoyed the video please make sure you leave a like and subscribe and i will see you again in the next one so in this video 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 uh or like we just want to get out of the loop the way that we can do that and i'm going to 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 uh arrays here once named names and one is named ar i just added this names one it's just a blank uh 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 uh what do you call it greater than the length of the list and then once it was we simply broke out of that uh that loop right okay so a 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 to do this sorry string element or i can't say string because that's something we type int 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 1 and that element's going to be equal to 5 and then it's going to be equal to 7 and then 3 and then 4 and then 5. 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 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 one five seven three four five so 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 what 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 have i um 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 going to start off by doing is just creating a variable that's called int count equals 0 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 just i guess 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 at index zero five 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 want 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 four each this is what this is known as because it's going through four each element in the array we are doing something okay all right 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 uh what do you call it 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 one i'm showing you this is we create a variable we're going to say again into equals zero we'll say well i is less than and then names dot length right and then we'll do semicolon and we'll simply add one to i so this is the exact same thing as we've done before but i'm just 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 dot in okay and i 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 of input equals sc dot next line like this and this way every time we run the loop we are going to get input from the user and you know what let's also just print out here system.out.println and just tell the user uh we want actually we'll just do print not ln we'll say input like this 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 um index we are in the loop right now so whatever value of i well that value in the list or sorry the list i keep calling it list it's an array i'm just used to python it's 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 four and then this time it's going to be a for each loop right so we'll say four and let's just stand for name i should need string n and names okay then we'll simply just do system.out.println and we will print every value of n okay so that's two good examples let's run this and just make sure everything's working so we run this input i'm just going to type hello oh i guess it wanted names didn't it 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 uh 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 or like so the loop is going to keep going like i is not greater than whatever value or what not okay then we want to break out a bit 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 encounter the word n um 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 going to we're going to print tim but we'll break after tim okay so what i'm going to do in the dual if statement i'm going to say if n 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 are simply breaking out of the loop and that means we'll not print the rest of the names so if i run this just drag this console up here and we run and we say hello and we 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 tims do not print and again that's because once we hit tim and n is equal to tim 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 wallops that we do in what he called 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 uh 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 in the programming world using the word break okay so anyways that's been it for this video in the next video we're going to talk about while loops which is very similar to for loops but they just work a little bit of a different way again if you enjoyed the video please make sure you leave a like and subscribe and i will see you in the next one so in this video we are going to 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 the 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 um 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 going to 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 going to do here with this while loop is i'm just going to set one up and then we'll just talk about exactly what it does so in this case uh 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 of two words okay so in this case what i'm gonna do i'm just gonna say let's see here maybe just getting answers be faster say ins x equals sc dot next int like this okay and what i'm going to do is i'm just going to keep looping through until they type the number 10 okay so while i can do this is you just type the keyword wow 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 two 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 wow x does not equal 10 okay um yeah exactly okay that's true so in if they type in 10 then it 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 to find we just do x and what i'm going to 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 and you know what we're also just going to print out uh because i like to do this we'll just do system.out dot parent ln and in this case we'll say type a number so the user knows what we're looking for okay and that means we're also going to have to print this here and it should just be print not print println my apologies okay so again the way this is going to 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 loop through this anymore so let's run this type a number let's type four it says type 10. type a number type 5. it 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 um 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 uh with for loops with while loops because we can also count in 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.out.println and in this case we'll just say you tried tired you tried and then plus count plus times okay make sure we add a little space here all right so now if we run this and we'll say like one two three and ten 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 intex 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 while loop 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 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 while 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 because 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 uh 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 or uh 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 zero is that a number yeah okay and ten 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 sell one up to look like a for loop then you'd literally just have to create a variable in this case say like into x equals zero you can set up a while loop so you say wow and you say x is less than or equal to ten and then in here you just say x plus equals one or plus equals two or whatever value you wanna 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 but that's pretty much been it for while loops they're very simple uh in the next video what are we going to go over let me check my sheet here we're going to go over sets and collections like we're going to talk about hash tables and stuff um getting a bit more advanced and moving away from some like the basic stuff in java that being said if you guys enjoyed the video please make sure you leave a like and subscribe and i will see you again in the next one 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 like the basics of them there's a lot more to do with sets and lists i know i'm probably going to get some comment from some guy who says wow you really you didn't talk about a lot of this 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 and 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 um because they're really complicated but you're going to need them to do certain things so for example with an array uh 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 uh 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 a 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 going to create a set and i'm just going to 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 so we need 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 uh to get rid of these red lines is we just have to hover over them and click import set uh by like clicking on whatever it says there if you don't have we're not using this ide just literally type import java.util.set and for hash set same thing we're just going to click on it and import it just because this isn't like built into main uh the java functionality all right so we have a set now what is a set how do we create it uh well a 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 it 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 uh in these little uh like greater than sign less than sign wherever you want to call it tags if you're talking html you 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 type so integer and then these brackets here now inside of these brackets if you had another set you had created like say i said created a set uh 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 a set i'm just going to do this we'll go through and see how it works right you simply type dot add so i'm going to say t t.add in this case i'm going to add 5 and we'll just copy this and we'll add like a few different numbers into our sets in this case i'll add 7 and we'll add 5 again and then we'll add 9 okay now what i'm going to do is i'm just going to 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 ad 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 uh it doesn't care and it just doesn't add it all right now i'm gonna add another element in this case let's add like negative negative zero negative eight all right as our element uh and you can see that we don't have any specific order now see when i printed this like i had added five seven nine 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 a 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 uh you can type t dot contains or like your set name dot 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 gonna 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 going to do is create a variable i'm just going to call boolean x is equal to this and what i'm going to do is i'm simply just going to print x now to see if that is contained and in this case yes it is 5 is contained again if i do something like 0 then obviously we get false it's not in there to remove something we can type t dot remove or our set name dot remove and then whatever the element is that we want to remove so in this case if i want to remove like nine from my set i would do that then if i print my set so in this case t we just get five seven negative eight because we removed nine those are kind of like the standard operations uh two 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 dot 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 if i do 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 um and that's the way that it works the way i 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 going to pop up here i'm not really going to talk about what these do too much i'm just going to kind of show you the way a tree set works now a tree set is similar to a 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 tree set well let's just try this um that still worked okay so anyways i don't know why i was showing me an error but tree set is gonna 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 arrays but when we print it out or when we look through the set it maintains this order in like a tree 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 set and this is similar to a basic set it's just faster on certain operations so i'm not like 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 uh they have a linked hash set a regular hash set and a tree hash set or tree set or whatever i typed okay all right so that's enough for sets um 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 because 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 so to create a list what we're going to do is we're actually going to type array list we're going to give it a type in this case i'm just going to 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 t again equals new arraylist and we'll type integer and we'll put brackets and there we go now we're going to have to import this so import arraylist up there and we now have 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 to like the end of the array or the end of the list sorry we can just 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 in any element you want so you have like one right okay that works fine to remove things t dot remove uh 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 it's even it's showing it's always saying index right here so i do dot index 0 then that would give me 1 right because 1 is added in there now to like put thing put something or to set something at a certain index what you do this is t dot set and you type the index and the element so i wanted to set index one per se to be equal to like five then that's the way 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 in so to set something like this sorry uh you have to actually have something added at that index i forgot i didn't add that so i'm just going to add two so 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 we didn't have that in so that was my mistake but anyways there we go so now that works uh we've changed two to equal to five and two is that position one right so that's the way that that works i've got a thing up here i'm just gonna read through a few of these uh 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 and it's called sub list and what this allows us to do is get from certain indexes so t dot sub list let's see add argument yeah so we just need ins 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 i want to do is i want to get from like index 1 to index 3 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 t get rid of the semicolon and print out and see what we get so we get here now is five and one because although our uh list actually has what do you call it's like six elements in it from here we can just get the elements from one to three not including three so if i type like one to four 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 uh the sub list like that now this is array list 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 um at certain operations than an arraylist so for any of you that 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 uh 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 um 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 uh or an array you don't know how long you want it to be it's a different amount of elements like 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 anyways i'm sorry about the confusing video in the next one i am going to be talking about hash tables which again are similar to this but then after that we're gonna be moving into object oriented programming getting into classes and that stuff's not as hard it is just a bit more information to remember anyways that's been it for this video if you guys enjoyed please make sure you leave a like on the video and subscribe to the channel if you are not and i will see you again in another one so in this video i am 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 those different implementations of lists and like array lists and all that stuff so maps if you're coming from python like me you can think of them as dictionaries and i'm going to be talking about three different types of maps so a hash map a tree map and what was the last one i got it written down here uh linked hash map sorry i 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 going to do this and then we'll talk about exactly what it does i'm just going to create a map m it's going to equal to new hash map like this okay and this is your simple syntax now to you have to import this so i'm just going to import java.util so just by clicking on it and we have to import map as well so we'll import the map there so you see 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 a map like i said is a key value pair meaning that it's similar to like a list and an array in the sense that 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 could be a number it can be a char uh it could be a float 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 5. so i type tim and then the value is 5. so i put that in there and now i'll just show you uh what it looks like if we print this out to the screen because i think it will give us a decent representation system dot what am i saying system.out.println sorry guys i'm a little tired today and we'll print m and see what we get so in this case you can say we have c this the curly braces says 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 actually you can't do like that this is python we have to m dot and then get sorry i'm still used to the python syntax here guys we type m.get okay and then we put the key in here so in this case i'm going to put tim as my key and well you should already be predicting what we're going to get out to the screen in this case we get the value 5 and that is 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 uh 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 to 999 okay and now if i just print m out you'll see that we don't get any issues and that this works fine we get joe equals x tim equals five eleven equals nine nine nine okay now this is an extremely fast data set uh meaning or data type whatever you wanna 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 sets 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 guess i'll show the tree map and the uh what was the other map i talked about linked hash map first and then we can kind of talk about the differences between them so with the hashmap 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 and i try to add this to be 998 instead of adding another key that's equal to 11 we're simply overwriting this already existing key that is 11. so in this case you can see that we overwrite it and we get 998. you cannot have two of the same keys existing in the map but you can have two of 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 hash map does not guarantee the order in which we add things in so you can see that i added 10 then i added joe and then i added 11 and it's showing me joe tim and eleven now this is not any kind of sorted order because how do we sort numbers and uh 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 hash map 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 uh it doesn't keep it in the correct order okay now the next map that we're going to talk about is the tree map okay now this tree map is well we're going to import it of course is similar to the tree list or the tree set or whatever one i showed you in the other video and 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 data type at least i'm pretty sure so i'm just going to see if i remove this if this is going to work for us yes so in that case we do get in assorted order we get joe and then we get tim because obviously j is before t and that's the way 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 showing me a key value i'll literally just put like a is equal to b and i guess we can't say equal we have to say comma uh what's our error semicolon i'm always forgetting those aren't i okay so we'll run this there we go so you see we get a first because obviously a well that's the first letter in the alphabet so that's going to show up before joe and before 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 could use numbers we could use strings using whatever data type you want they have to be completely the same for that map now for the other type is a linked hashmap 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 going to say tim that was the first element so that's going to be the first thing that we show and then joe that's going to be the first thing and then a or sorry 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 um other than like the speed of which these things run at uh 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 hashmap or a tree hashmap or treemap whatever it's called for any of the stuff that we're going to be doing right now so what i'm going to do now is i'm just going to show you a few methods how we can kind of clear the maps how we can like remove elements and some useful things that you might want to do so what we can do to get all of the key values or to get like a certain key value we can say actually i'll just show this one first what we can actually do is there's cool things that are going to be really useful to you and uh doc contains value so this is let's see this method contains values not available for type map interesting match contains value object yeah so we just have to put something in there okay so m.contains value and then in here we're just going to put uh 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 always the 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 can 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.5 actually let's just see what this is printing out to us because i'm curious if this is going to crash for us or not let's let's see here m.5 no yeah so this isn't going to actually crash for us if we try to get a uh what do you call it a key that does not exist in the map but it's going to return us a null 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 a map or from a dictionary and it doesn't exist you actually get an error so that's interesting to know uh 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 xb 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 um we can use clear so i've just got a text document beside me because i always forget all these to show you i always forget which ones so m.clear obviously this is just going to remove everything from the uh 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 empty now how much time we have 10 minutes so i actually i am not going to show you an example of using these maps but uh 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 so anyways that's 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 the next one so in this video i'm actually not going to be teaching you guys anything new so if you're more interested in just like speeding through and learning everything i suggest you just move to the next video but what i'm going to be doing is showing you some common problems that 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 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 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 hash map 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 uh brave enough to attempt that on yourself i would show you a solution here quickly to doing that and for any of you guys 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 um so we're going to do that just because we're going to count all of the letters in this 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 does it l up here but i'm gonna show you how we do this with maps and for loops like very efficiently um in computing so what i'm gonna do is create a for loop what we're gonna do is we're gonna loop through the string and the way that we can do this is because if i try to do like for char or like x because it's going to be a 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 this string into a character array for us the way that we can do that is by doing uh i guess a dot dot 2 char array and this is if i literally just print this out for you if you want to see what it looks like system.out.println is simply just going to put every character so including the spaces into a character array for us so let's just run this uh and you can see it just it's 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 going to 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 going to add 1 to it and then overwrite that key with a new value and you'll see what i mean here in a second so i'm going to say if m dot contains key of x then what i'm going to do is i'm going to say old i will say int old equals get and then the key is going to be x and then what we're going to do is what's the issue here m add cast and if m i need this for getting all my brackets into old equals m 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 end all right anyways and then we'll say m.put and for the key is going to be x because that's going to be the character and we're going to 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 key's value is going to be 1 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 probably helpful i spell it system correctly we're going to print the line and we're going to print m okay so quickly i know i did 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 going to do is we're simply going to add in a key and it's going to be equal to that letter and it's going to have a value of 1. because we just found it 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 1 to that previous count and override the key so like if the count is 4 it goes up to 5. pretty straightforward so if i run this you can see we get space well there's 8 spaces we get a there's three a's c there's one d there's one and it goes through and it counts all of the different letters for us and if we wanted to confirm if that was 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 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 going to take like 10 seconds but pretty much to remove an element from a map you probably already guessed but it's literally just m.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.remove and just put the key so if i wanted 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 it's because it's a character my bad sorry guys i've been doing string a character of a space is different uh yeah so then it actually removes the uh that character the space from our program or from our map whatever okay now the next thing i wanted 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 i'm going to say int x equals in this case let's do like three and let's do a bunch of different values and see if we can get this to sort for us uh 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 sort and then what it takes is 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 sorry so there's three arguments not two what these arguments do is from a certain index so if we wanted 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 1 because we're going to start at 1 and then if we were going to 7 1 2 3 4 5 6 7 funny enough we would actually type 7. now i know this is index 6 but the thing is it's going to sort up but not including to that index so if we type 7 it's going to that's 7 but it's only going to sort up to like the actual number 7 okay and what this does and i believe is actually just changes the list we don't need to say like x equals arrays.sort we just say arrays.sort and then what we can do is we can do system.out.println 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 uh 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 a raise i forgot we get that little loop or we get that message because that's the memory address but anyways let's say for uh int i in x and then we'll simply just print out i it's got to make it complicated for us doesn't it uh and instead of printing ln we're just going to 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 1 2 four five six seven and 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 negative 99 0 all the way up to 8. 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 out quite quickly actually and quite easily using just arrays.sort and remember you don't have to do like x equals arrays.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 so anyways i know this has been like a quick video but i just wanted to show you and for some of the guys that might have been struggling um how we can kind of incorporate some of these things together and how to create like a basic problem-solving program maybe a way you go about doing that hopefully this was useful to you guys let me know in the comments down below and in the next video we're gonna be getting into object-oriented programming and that's gonna be the rest of the tutorial series uh it's a bit more complex but it's definitely like the heart of java and it's something that we have to understand so if you guys enjoyed the video please make sure to leave a like and i will see you in the next one so in today's video we'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 ins strings bulls scanners like all kinds of different things that we can use like arraylist hash table hash list whatever hash map all that stuff okay and those are all known as data types right or 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 uh pointing this variable sc to a scanner object okay and whenever we create any kind of variables like just typing like and x equals 5 we're essentially saying x is equal to an int object of value 5 okay and 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 um 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.next method on sc because it is a scanner type if i try to do x dot next and i do a little semicolon here you can see that we're getting can't invoke next method on primitive type in 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 hello and because uh or based on the different types of objects we have different properties different attributes things we can do with them methods we can call uh and 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 when 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 uh previous videos but essentially methods are anything that you call on an object or on an instance of an object so just to clarify what instances when we're creating a variable like this of a new scanner object we can say that we're creating sc is now an object like a scanner object or we can say sc is a new instance of uh scanner of like the type 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.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 uh 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 uh an array like say we have or let's do actually a string so we're just going to take another string probably shouldn't have deleted all these but that's okay we'll say string h equals hello and i can do something like h dot at 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 a ton of 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 it 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 simply going to type public static okay and then whatever return type we want which we'll talk about in a second uh so in this case i'm going to do void and then the name of our method so in this case i'm going to say like tim okay so we have a public static void tim it has zero parameters again talk about that in a second and then it's going to do something in here in this case i'm just going to say system dot out dot print ln and we'll just say when you call that it just says tim like that okay so what i've 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 so in this case if i type tim and i put a semicolon like that and we run the program you see that we get is it running system that out one second here sorry okay so i figured out why it wasn't working because we had this sc.next coming in here so i'm just going to comment that out for right now and we'll 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 uh 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 ms you 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 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 that or 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 tim right now we can actually do multiple uh 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 here in this case i'm going to do 4. what i'm going to do now is i'm going to say 4 and we'll say int i and we'll say i is less than x let's say i plus plus we're just going to print uh this that many times okay so let's see here boom okay so system.out.printline what's what are we getting here initialize variable ah gotta do that sorry okay so now we pass an end and we pass a string and what we're going to do is simply print this as many times as like the int that we typed in okay so you can see that 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 and 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 i'm 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 so the method's going to give back to us and i'm going to say add to 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 an int okay so what i'm going to do here is i'm simply going to return x plus 2. and what this is doing now semicolon is it's just taking a value x and it's adding 2 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 2 and then give it a number in this case 6. 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 called 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 going to do some more advanced things than just adding two 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 it keeps doing with my brackets we'll do str and then inside our brackets we'll give it high it should return to us high with an exclamation point and indeed it does and yeah that's kind of how methods work basically and in our the next video we're going to be talking about classes and creating our own kind of data types and moving forward and that's what we're going to continue be doing with the rest of the tutorial series so if you guys enjoyed the video it helped you out please make sure you leave a like and subscribe and i will see you in the next one so in today's video we're going to be going over classes we're gonna be talking about creating classes creating some methods constructor methods uh all that fun stuff creating instances and objects uh and yeah so this is gonna be kind of an advanced video uh 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 called main 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 why 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 package is 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 it'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 well a class is pretty much a 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 solve for us like why are they useful so i'm going to introduce kind of 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 uh at some point be able to like call something and print out the each dog's name and age in like a nice form okay now we could do that we know how to do that um if we went back into main here what we could do in this little what do you call it method here is we could just type a bunch of ants we could say into like dog one and this is equal to four and that's dog one's age we say inch dog two and that's equal to five and we could go on and we could create 10 variables 5 for the age and 5 for the 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 what would i do then well we could use like lists to create or lists or arrays to create names and ages but that's just not efficient it doesn't look as good um in our actual coding so what we're going to do is we're going to use a class and you'll see how we can kind of accomplish this problem so within classes uh 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 h to create our attributes since the first thing we typically do when we create a class is right at the top of our class we're going to type the keyword private and then we're going to give a data type so in this case we're going to say string and then the name of our attributes in this case name okay and this is all we have to do we're just declaring that at some point in time we're gonna have uh the attribute name and it's gonna be storing some information later on in our program okay we can also do private int uh and in this case i'll do age and this is gonna obviously represent the dog's age now if you want to have some other attributes weave it 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 would 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 i need to create something called a constructor method now this you typically only create one of these although you can create multiple and 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 in this case i'm going to say 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 dog so what i need to do here is 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 going to do is i'm actually just going to use a keyword and it's the keyword is this it's kind of hard if you weren't watching you'd think i'm just like saying this is the key word i'm 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 uh attributes of the class okay so when we type this uh 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 all right and that's how we reference things that are part of the specific instance we have this dot name and this dot h okay and i'm gonna explain more and more of this as we keep going uh 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 print something out to the screen so i'm going to say system dot out dot print ln why did that okay print ln and all we're going to do is we're going to 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 another plus we'll say uh this dot age plus years old okay so it's simply going to say like i am whatever the name is and i am uh 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 dog 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 simply going to do is we're going to type dog and we're going to give it a name in this case i'm going to say tim is equal to new dog okay and then remember that we have to give dog some parameters right so or argument 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 h in this case i'm going to type tim 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 hs4 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 speak method 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 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 well it 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 well it's name is well it's tim okay so we'll say tim what's its age it's age that's four so we'll print four to 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 we'll say this is bob and he is eleven okay let's copy this and oh 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 all right and just to prove something i'm going to 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 me say i am tim and i'm four years old i'm bill i'm seven and 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 they're specific to each of these variables that are storing that dog object okay so that means that uh 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 all right 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 another method and i'm going to call this uh get age okay so i'm going to say public and in this case we'll say int get age right and all we're going to do in here is simply return the age to uh let's see a return statement to wherever we're calling it from so in this case i'm going to say i'm going to 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 um these little red lines and it says the field dog.h is not visible and that is 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 and say like and x equals i'm going to say system dot out print ln and oh x is already there great so now if we run this we can see that we get 4 down here excuse me at the uh at the bottom of the screen now we can actually do the same thing with uh like setting the age so say 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 in this case we're going to make it public again we're going to say public and we don't need int this time because we're just going to change something we're not going to return so we'll 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 it's 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 h and we'll give it a value of 10. then we print out uh let's just say tim.speak again what's going to happen now sorry guys i'm a bit sick if i keep coughing all over the place uh what i'm going to do is oh what's our error tim dot set age 10 return type of set age to int one second here sorry public let's try this inset h 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 have to call like 10.78 my bad 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 uh 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 going to create a private uh void actually no let's do int okay and we're going to call this add two it's not going to make any sense with the dog class but just it's just for an example okay and in here we're going to take actually we won't take anything but we're going to return we're going to return this dot age plus 2 okay now this is a private method meaning that it can only be used and seen within this dog class so say i wanted to use this ad to maybe i could do this i could do add two here okay and just do a little semicolon and there we go i could use add two and that that works fine there's no issues with that okay and that's how you use methods within side of the class so like speak i could 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 watch what happens if i go over here and i try to use that so add two so i'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 i think that's going to wrap it up for this video on classes hopefully you guys now understand kind of how you can create classes uh how to create methods somewhat and in the next video we're going to go more in advance we're going to talk about inheritance we're going to talk about multiple constructors and then we're just going to keep on going again more and more advanced as we go so as always if you guys enjoyed the video please make sure you leave a like and subscribe and i will see you again in another one 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 like 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 this speak method here instead of saying i am whatever i just wanted to say meow my name is whatever right so i 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 we'll be 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 thousands and thousands 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 going to do is we're going to use something called inheritance so i'm going to create a new class i'm going to go new class and in this case i'm going to call mine cat right and i'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 dog 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 super class 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 a derived 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 drive 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 super class right so or the parent class okay so what we need to do when we first do this as 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 well 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 going to 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 super class constructor which is going to be the dog constructor using this formation or this syntax super and then we give it the name and we give it the h 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 uh int and i don't know food or something it's like how much food they should get and this works perfectly fine and we can go ahead and we can create another attribute here so we say private i don't know let's just say food okay and this should say int food and then in 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 going to have name and inside of this cat class we're going to have food as well and that works uh just fine okay so just to uh i don't know let's go over this again because then i've kind of gone through it pretty quickly but all of these methods here we're going to be able to use within our cat class because what 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 going to become a little bit different but still use all this functionality so we still have the attribute age the attribute name we're setting them equal because we're going to 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 and 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 tim 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 say 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's in the dog class so if 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 change something so i'm just going to say system dot out dot print ln and in here what did i want to say like meow and my name is let's say plus this dot name and you know we'll even add like a little food thing and i get fed um i don't know let's see here plus this dot food okay i don't know it's good enough for me what's the issue here change visibility of name to protect it uh okay one second i gotta do something here i can't 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 want to get into protected but let's see what happens now if i run my tim.speak now we can 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 dot 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 uh 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 this does go one way uh all the stuff we do in the cat class is not going to be visible to stuff in dog class 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 we could just keep going and going and going and extending and extending and extending also known as like inheriting right so that's kind of it for inheritance now i want to show something called multiple constructors okay so there is sometimes um 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 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 going to leave out one of the parameters here okay so we'll set into age and then here we'll simply call super and name and h okay and you can 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 uh 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 ant age okay and then here i call super oh sorry i should just get i should actually get rid of this because we're only going to use one right not bad i'm going to give name but what about age what am i giving super for uh for our age well i don't know how are we going to set it up if they only give us one uh 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 0. they want to give us a name and an 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 weight you can do like it's known as 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 say cat bob equals new cat 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 joe like the name joe all right and there we are so now we could obviously do like bob.speak and we could do joe.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 see a quick error that maybe we want to fix we should probably give them food as well so we say this.food equals 50. okay as like our default value for how much they get fed 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 um what do you call it whenever we're creating like a main class or a parent class protected wow okay we're going to want to use the protected keyword at least for what do you call it sorry our attributes so that we can change them and access them from within the subclass 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 because i don't remember if this is going to allow us to use this now out here so let's say like tim dot i don't even know which one i changed uh get age okay say tim dot get age say int x equals see if this is going to 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 the package now i want to try this though and see if i can get this use this get age from within our cat class so if i do for example when i want to speak i'll say this don't get aged and 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're really going to create private methods is if within the class is the only area you're using the method 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 anyways i think i am probably going to end it here in the next video we're going to be talking more about classes doing some more advanced stuff and then in the future we're going to get into like interfaces and some other cool java stuff so again if you guys have any questions feel free to join my discord server or leave a comment down below uh and as always if you enjoyed the video please make sure you leave a like and subscribe and i will see you again so in today's video we're going to be going over uh kind of class variables and the keyword static so you may have seen that we've used static in a few things in like our main thing here actually i got to delete all this uh 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 okay so 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 changed one of the dog's name or one of the dog's age unless that dog was them 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 dog 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 them in 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 i'm gonna say protect it again say protected uh and by the way i'm just in my dog class i deleted a bunch of stuff out of it uh just to make it a bit easier to work in and i still have the cat class but it's just uh 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 named count equals zero which is protected okay i know it's a lot of keywords but what does this 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 um or sorry it it'll change uh like 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 dog class all right so if i want to change this value x well i do need still need to call it this and i can say this dot count equals and then actually i'm going to say plus equals one okay because what i want to do with this count here i'm going to say static field dog 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 going to 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 right 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 yeah 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 dot count right and that would work fine because uh this was an instance of the dog class so it 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 i'd say tim or dog tim equals new wow typing is not great today new dog and we'll save tim and let's go 9 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 main so what i'm going to do to change count now or to print count out first of all is i'll prove to you that system system dot out dot println 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 two so uh we already know that this is indeed working because well when we added one to count uh if it was this 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 you can say dog dot count uh let's say equals like seven okay and that's fine that works if i print out uh dog.count we get seven now i'll show you two if i do like tim.count uh 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 account since it's a dog part of the dog class it has this count variable and it's changing obviously within tim as well okay so that's kind of how the class variables work now time to talk about static methods okay this was a static variable or class variable i like to call it class variable just because that's what i'm used to but you can call it whatever you want now we're going to talk about static methods and what that is so it's very similar to this kind of the way that this works but it is a little different so i'm going to do here i'm going to say public static void display okay and in here all i'm going to do is simply do uh system dot out dot parent ln and i'll just print i am a dog okay so what this is doing now is i'm saying static void display now in here what's gonna happen essentially is we don't need an instance to call this so i can actually just call this on the 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 will indeed 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 let's say public void and we'll say uh display2 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 try to do dog.display2 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 display2 meaning that we need to have first create a dog object and then we can use that dog object to call display 2 or we can do like this dot display 2 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 with static is it has no way to access our uh what do you call it our values or 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 from a static method like i can't type like this.age that just simply doesn't work because if you think about it well what what uh 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 be able to call something and you don't care about the uh 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 uh the call statement so anyways as always if you guys have any questions please make sure to leave a comment down below or join my discord server and if you enjoyed the video please make sure you leave a like and subscribe and i will see you again in another one so in today's video we're going to 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 i don't know make things too complicated for yourself let's just say that all right so what i'm going to do here or what i've already done i guess is i have this 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 just 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 going to 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 our student now i just want to show you uh 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 we 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 said well no 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 i'll demonstrate that to you so we have joe and bill compared to each other so watch what happens if i try to i 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 uh exactly well pretty much when 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 are 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 we 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'd 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 uh the same student or we're going to say yes they are they are the same right when we do the like double equal sign so what i'm going to 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 i'm going to call this public boolean not boolean boolean and we're going to say equals and then here we're going to take a student object known as other okay and then we'll go like that and there we are so what i'm going to do in here is when we want to compare our student 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 did 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 uh brackets here as well i 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 going to happen right is we'll pass uh 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 they're the same will return true otherwise you'll 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 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 um 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 so maybe you had like grades maybe you had other stuff like that or like a last name you could check to make that 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 because i don't think it's gonna work right like joe greater than bill yeah see that that just is undefined there's no way to do that so what we're going to do is we're going to use a method called compare to okay and what we have to do to compare two is we have to uh what do you call it sir we have to implement something into our class so we're going to say implements at the top of our class and we're going to spell implements correctly and we're going to type comparable and then in here we're going to do student now what we're actually doing here when we uh 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 uh to compare two objects okay so what we'll do now is we'll say public and actually what is the thing for this the return type public ins and compare two and then in brackets here again we're going to take student and this will be other and then we'll put our brackets here like this okay so now again we're going to 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 i don't mess this up okay so what i'm going to 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 going to 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 uh 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 again let's do bill let's see what we get so 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 wanted 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 uh j is from t when we're comparing them and that's what the compare t 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 compareto method okay so like this a public and compare to and you just have to remember to implement comparable student and then you can use uh exactly this return statement here now the last thing i want to show is a string representation of an object so i want to show you first what happens when i just want to print out my object to the screen so i want to print tim and maybe ideally i'd like to see like uh tim's name right come out to the screen well look what we get here we get tutorial1 dot student at and then all these uh different letters what this is printing is actually the memory location of our object so all of our objects are actually stored in memory uh 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 uh using to find tim and find all its values and obviously it's telling us where it is in the tutorial one uh 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 another method here that's going to change this to a string so we've used this dot to string before but this is exactly what i'm going to use we're going to say public string and then we'll say tostring like this okay and what we're going to do in here is we're just going to 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 going to add this dot name and then they add that with another bracket here so this way you'll see when i print it out to the screen what it looks like okay we get student and then tim and this is just simply telling us uh that the object we're printing is a type of student and the name value is tim okay and we can even get more fancy and we could add like double brackets around this dot 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 dot 2 string that's because java actually knows that when we have this tostring method in here this is going to represent a string right so when we try to print it it's automatically gonna call that dot two string method okay so now uh what we can do is we can also just call like two string so i can if i wanted to i could just call dot two 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 wanted to write their name and then after you have like an array of all their grades or something like that okay so anyways that has been it for this tutorial in the next tutorial we're going to be talking about inner classes then we're going to be going into interfaces and enums and some more advanced stuff that i think is pretty cool in java uh if you guys enjoyed the video as always please make sure you leave a like and subscribe and i will see you again in another one so in today's video we are going to be going over inner classes now inner classes are pretty uh 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 going to explain too much of it i'm just going to kind of show you how we can create instances of inner classes in different ways so what i'm going to start by doing is inside this outer class here that i've already created you can see on the side here i'm just going to create another class called inner class right now inner class is just going to simply have one method in it and i'm just going to 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 uh in the right class here okay and i'll say this is an inner class okay and what's our error here class at token hmm interesting i must have made a mistake somewhere here one second guys oh sorry i put these two brackets here 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 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 going to say public void enter and in here we're going to take nothing and all we're going to do is we're going to create an instance of our inner class and just display okay so we're just going to say inner class i n equals new your class like that okay and then all we're going to 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 this void inner and all that does is create an instance of our inner class and uh print it to the screen again note that this is private meaning we can only access it from within our our class okay so now if i go to my main.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 elder 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 out dot enter okay 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 so we did that we have outer class now what i can do is i can say elder class dot inner class okay and we'll just call this one i n equals oops lowercase equals out which is going to be our instance of outer class dot and then in this case we're going to type 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 out 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 for the issue i knew it was something like this what we need to do is we need to do out 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 in um which is an instance of uh 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 we can access the inner class now right now you might be like okay 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 would most likely have more methods than just one that you're going to be using okay all right so now that we have that i guess let's show creating this kind of inner class stuff 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 inner so i guess let's just work with this right now you know we'll even leave in a class right now and i'll show you how this works so all i can do in here then say uh we can do publicly private in this case i'm just going to say i'm going to say class let's see class inner class and then here yes that works fine we're just going to redefine display uh so we'll just say public void display and now that i think of it i probably could have just copied all my other code but whatever got some practice typing print ln and then in here again uh well 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 have just kept all that code but that's fine why is this not giving you the autocorrect outer class uh out equals new and we go outer class and brackets and then in this case we'll simply just call out dot inner okay so now we can see what should happen is again we should print uh inner class so there we go and we get inner 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 going to have to call this void so there's no really there's not really any point in putting like private or public um but again like if you wanted to you could type public class interclass 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 going to be able to use it when 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 void so 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 so i think that's kind of been it for inner methods there are like anonymous inner methods and some few a few like other cool things you can do with it but i don't find them particularly useful so i'm not going to teach them here especially in like a beginner's tutorial but if you guys are interested in learning about them i will leave a link in the description just for any of you guys that are curious and as always if you enjoyed the video please make sure to leave a like and subscribe and i will see you again in another one hey guys and welcome back to the java programming tutorial so in today's videos we're going to be going over interfaces now interfaces are uh i think they're unique to java actually because i haven't heard of them in any other languages but they're somewhat similar to classes uh they have to do with like inheritance and anyways you guys will see as we go through the video but they're pretty useful um and yeah they're an important part of java so we need to learn them so what we're going to do first of all is we're going to create a new interface now just like we created a new class we're just going to right click on tutorial 1 our package go to new instead of class obviously we're going to click interface now i'm just going to name this one vehicle for the example that i'm going to be doing so i'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 just 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 do 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's 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 um public methods and we actually don't define anything inside the methods or the uh the attributes so when i create a method in an interface and i'll just make one now i guess i'm gonna 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 the the brackets i don't do anything inside the method i just create this now you can 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 going to use this in our car class and you'll i kind of have to go through all of it before you're really going 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 what do you call it like a attribute and then we'll just move into actually using the interface that's the easiest way to uh understand it so i'm going to say we have speed up we're going to have slow down and we'll have uh let's go change gear as well okay just going a classic example that we're gonna implement a vehicle from a car and you guys will see what i mean okay uh change gear okay and then i'm gonna add a attribute now any attributes that i add in here like variables or 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 into gear equals five saying that each thing we're using is going to have five gears now watch if i try to just do int gears equals five that still works but it's not going to work in the way that we want okay so just make sure whenever 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 type uh implements and you might have seen this before and then the name of the uh the interface so 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 is saying uh 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 a 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 we have no idea what to do because there's no content for them right so we're going to have to create a void which is change gears i'm going to say public void change what did i call it gears or gear let's see change gear okay so change gear and then it's going to have to take one parameter in this case we're going to say int gear so like what gear do we want to change to okay and then we're just going to go up here and we're just going to say private int gear because we're going to define that when we when we change gears right okay so now we're going to go to and make another uh the other one that we need so let's just check here we need to speed up so we'll say public void speed up and in here we'll have int speed as like what speed or actually not speed we're gonna go int uh change so how much we're gonna 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 gonna do now is inside i'm actually gonna create another private and i'm gonna call this speed okay what i'm gonna do now is i'm going to just just uh fill up these uh what do you call these methods so i'm gonna say when we change gear we're just gonna say uh this dot gear equals whatever gear we're changing it to and then for speed up i'm gonna say uh this.speed actually plus equals change okay and then down here we'll say this.speed minus equals change okay and change perfect okay so now what we're going to do is we're actually just going to 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 uh just create another method so in this case i'm going to make this like a display method so that we can uh display our i don't know car pretty easily okay so public void display and inside of here i'm just going to print out a few things the screen so system dot out dot and in this case we'll just say i am a car comma and then we'll just add uh actually let's say going and how fast are we going we're going this dot speed and if we wanted to we could add 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 dot uh the 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 going to do now is we're going to create car objects we'll say car uh 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 uh or ford i guess is what we need to do we'll say four dot speed up and let's maybe change it to 10 we'll add 10 to it and 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 we're going to run into an error um actually gear i guess you can't really be in gear zero can you let's change that to one okay perfect okay so four dot speed up and we'll say four dot change gear and let's just change to gear two okay and then if we want to display our forward we can do four dot display perfect okay so let's run this now and see if everything's working so we say i'm a car going 10 kilometers an hour and i am in gear two 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 if we type default and then maybe in this case void and let's just say like out like we're going to print something out just make it easy what we'll do here is we'll just um we can just create a method and we can use this from any of the classes that inherits our our interface okay so i'll do here it'll say system uh dot out dot println in this case i'll just say uh default uh method okay i know very creative how did i just spell system am i okay anyways all right i'm tired guys i apologize system.out.println 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 i am a car going 10 kilometers an hour and i am in gear 2 and then it says default method and 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 uh our class but we can actually create static methods within our interface that we can then um 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 uh i don't know let's just do like ins let's return something let's change it up let's just call this math actually i feel like math is a key word that's fine we'll just do math okay we're going to take uh int b and all we're going to do is we're just going to return b plus nine okay i know very basic but that's all we're going to do just to prove a point so now say i'm in my main.java 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 gonna actually set this equal to a variable so we'll say into x equals that and then we can system dot out that 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 say 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 actually you know 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 uh will do like math operations for you like maybe you have the quadratic formula maybe you had um i don't know like euclidean distance like i don't know a bunch of different math formulas you put them all in this math interface and then when you want to access them it would come in here and you'd say well math dot maybe like square root like sqrt 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 uh really easily so that is a really good example of when you can kind of use interfaces so anyways that's been it for interfaces if you guys continue on with java you will see that in many cases especially making games and stuff you're going to be implementing other people's interfaces so it's good to know kind of how to create them how they work in the back end typically you probably won't be creating any of your own interfaces unless you have like large programs and a lot of abstraction and inheritance but anyways they're good to know so as always if you enjoyed the video please make sure you leave a like and subscribe and i'll see you again in another in today's video we're going to be going over enums now enums are pretty much a collection of constants that we can reference and then we can do things with those constants and uh they are pretty cool and just make our code more readable and yeah you guys will see when we go through the video so the first step is to create an enum now to do that it's similar to creating a class or an interface or whatever go to your package right click and click new and then enum so for enum i'm just going to name mine actually i'm just going to name mine level because you'll see why it just makes sense for this example so again the syntax here is a public enum 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 and 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 in all caps i'm going to go high medium and low okay now these are the three constants i'm going to store in my level enum so we have high medium low and yeah that's literally all i'm gonna do right now for my enum it just means we have three constants that we can reference from level and these the three and obviously you could 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 it more readable if you do them like other lines okay so there we go three constants now i'm going to show you how we can actually use this enum and you'll start to understand uh how it works so to use this enum we have to create a new uh instance of it so to do this i'm going to say level and we'll say lvl equals new uh actually i think it's like this it goes level dot and then yeah so we say high low and medium so in my case i'll just pick 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 uh what value your constant is so like what value is level so we can obviously use these in an if statement like so so if i say like if lvl equals equals and then like it's level uh dot and it will start at low i guess and we'll just work our way up to high and we'd say l if or else if and we'll say lvl equals equals and then level dot 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 something here so in this case we'll just say system.out.println and we're just going to print level for all of them because i want to show uh what this does okay oh what the heck did i just do there okay anyways okay so now we're just printing level each time and actually let's just run this now and see what happens so you see we get low so when we print out level it's simply just going to print like whatever the constant is that we have and that's because uh it has like a two 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 like it's 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 dot tostring so if i say string let's say e end why not why not i will say is equal to lvl lvl.2string 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 that's how you get it to a string now another really useful thing with enums is we can actually get all the different values of enums and print that to the screen so i do system dot out dot print ln and i believe the way we do this is we go 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 well it 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 i'm actually just going to store this in array so that we can then iterate through it and print them out what i'm going to do is i'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 okay so that's working fine so what we're going to 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 4e hormone which should be s 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 will print out e and then we can see all the different contents that we're going to get so let's go here and you see we get high medium low and then just prints low again just because we're doing the silly if statement here okay and that's how you can loop through all the different values so that's really cool um but you might say okay well that's cool tim 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 enum we can add some methods uh and some constructors 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 uh actually int level lvl num 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 to set this value what we need to do is we need to create a constructor for our edium and you'll see how this works in a second so i'm just going to say public level ins and then num okay and in here actually what are we getting here remove invalid modifiers legal modifier for enum contract only private increment okay so let's just do this private sorry my bad private because we're just going to call it on here you'll see in a second private level into num and then we're just going to say this dot lvl num is equal to num okay there we go so now what i can actually do is you see how these are highlighting 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 enum so i'm going to put brackets here and i'm going to define high as 3 medium as two and 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 numb equal to whatever value we put in the brackets here so in this case three two one we could obviously have multiple uh what he 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 going to want to use it so that means we're going to have to create a few methods to we'll change that level num and to get that level number so we've done this before but actually i don't think i can do a public and we'll see if we can public get level and then in here we'll simply return i guess this is going to need to be an int public into get level we'll simply return this dot lvl num okay yeah so that does work fine i just can't do a public construct okay so this is just going to give us the level num if we call get level on our enum okay now to change this uh uh what do you call it level numb what i'm going to do is i'm going to do set lvl and instead of having interior it's just going to be a void okay and all we're going to say is this dot lvl none is equal to num and then we're going to put in here into okay perfect so now what we're doing is we have a way to not only get the level number 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 for right now so right now we have our level and it's level.low so to actually get the value of this level we can use something called getvalueof 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 is i'll say system dot out dot print ln and in here we'll say lvl dot and then what was the thing we were using get level like this so now if we print this out of the screen we should be getting one and indeed we are we're getting one so now i want to show you this thing it's called get value of okay and is it it's value every get value of 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 uh we type in whatever the enum is so in this case we could say low it's going to give us let's see what's the issue here okay yeah so the issue is i just got to put this in string i just forgot about that so i do level dot get value and say i put a string in here so like say someone was typing in and um into the computer and they type in a string right and you want to pass this into get value 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 because 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 of just setting the level just to make sure that all of that is working say lvl 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 from up here and now i'm just simply going to print out uh we call it lvl 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 enums 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 our code kind of readable right because we have level.low and then that low can point to like five two one whatever and that's all stored within our enum right here obviously you can have as many enums 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 you
Info
Channel: Tech With Tim
Views: 138,516
Rating: undefined out of 5
Keywords: tech with tim, java tutorial, java, tech with tim java, java course, java for beginners, java tutorial for beginners, beginner java tutorial, learn java, java course for beginners, java beginner tutorial, java full course, java full tutorial, java course for beginners 2020, java programming
Id: Yv_4RXyLjL8
Channel Id: undefined
Length: 236min 12sec (14172 seconds)
Published: Sat Dec 19 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.