Kotlin - Full Beginner Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys everybody the name of geo welcome to my channel in this video i'm going to be starting my new kotlin tutorial series we're pretty much going to be starting from the humble beginnings of not knowing kotlin to better heights of getting a better understanding of how kotlin functions and why a lot of people are trying to use it so hope you guys enjoy all right so first things first we're going to need is an ide i'm going to go with the intellij id because of the fact that it is created by the creators of kotlin which is jetbrains so i believe they will provide the best support for their product there are two versions of this id there's the ultimate edition and there's the committee edition the good thing about the community edition is that it's free so i'm gonna go with that and if you look below you guys can see the things that each of these products support and both of them support kotlin so the community edition should be just enough for we're going to use all right so the second thing you guys will need is the java development kit or the jdk for short currently it's on version 15 and it includes things such as the interpreter the compiler and the jvm the cool thing about colin is that just like java it runs on the jvm so you don't really have to give up anything to run kotlin if you guys go down you'll see different versions for different os well i mean it's the same thing but if you have a different os there's a different installer in my case i have the mac os so i either use this mac os installer to get it if you have windows you just use this windows installer and you know continue on with linux so once that's finished downloading or installing you just go through the instructions uh usually i leave the location of the jdk as default whoever wherever the installer chooses so that's just pressing the next button over and over again until it's finished and then once you have the jdk and have your ide installed we're good to go and also i'll leave a link in the description to both the ide and the jdk all right so now that we have intellij and the jdk installed i'm going to you guys to open up intellij i mean you guys also click on new project check this check box for kotlin jvm for the project sdk choose the jdk that you guys just installed in my case i have version 14 but you could have an older version or you could have a newer version in my case i have 14. uh for the library to use use the kotlin java runtime and press next then we're going to need a name for our project in my case i'm going to choose collin tutorial 2021 press finish and then boom you guys should see on the left your file structure first we have the idea folder which is intellij specific this holds information regarding your project such as navigation history the current files open and debugging configurations then we have the source folder which will be used for um writing our kotlin files and code and logic they'll be placed in here then we have the iml folder this is to you know this is also intellij specific it holds metadata metadata for the project then we have external libraries which holds the jdk jar files and the kotlin java runtime jar files all right now we're ready to start writing some kotlin code so like i said source is going to be used to hold our kotlin files and logic so let's right click source hover over new click kotlin file slash class so what i want to do is create a kotlin file to hold our entry point into our application so what i'm going to call this file is application start press enter cool so the difference between java and colin is the fact that we don't even have to make a class to get started really all we need is a function so in this case to make a function in kotlin we use the word fun simple and you guys can see that you know try to recommend some other stuff but we just need everyone simple very simple um and then we choose a name for the function there's a keyword in kotlin that let's call it know that this is going to be the entryway into our application so we call this thing main this is the main entryway it's the main gate this is the main door this is how we'll get into this application then you know for functions we'll need a uh parentheses so we just open and close parentheses and then we have curly braces within this curly braces this is going to run the logic that we put inside this curly braces this is the body of our function so i think it's the international standard to print out something when you're learning a language for the first time or when you're building an application for the first time just to see that it's working and that it's running and then i'm not lying to you guys so the first thing i'm gonna do is to print out hello world in my case i want a new line after so i'm gonna put print ln so this like calling no i want to print something but i want to add a new line after i print that thing so then i put print ln open close parentheses and to print out text and call it i have to use double quotes and really that's in most languages but in calling there's double quotes and then i place whatever text that i want to print out within those double quotes so i'm gonna just put hello world with exclamation point because i'm serious and then you click this play button right next to this function run application start which is you know the name of our file and then if my computer is so slow you should see it pop up soon boom hello world so let me just go over this again we made a file called application start.kt we made a function and to make a function of kotlin use the keyword fun then you specify it with a name to let caller know this is going to be the entryway into our application we have to use the keyword main for the function then we open and close parentheses uh for the function and then we open up a body of the function so that you can see what exactly we're trying to run on its way into our application so within those curly braces we want to let colin know to start off i want to print out hello world and i want to print out with a new line hello world so yeah that is how you get an issue into kotlin and that is how you print something in column with a new line all right so we dabbled a little bit with println to print out some data and add a new line but i just want to explain to you guys there are two different type of functions that we can use to print out data onto the screen so first we have prints and the second one we have is println let me show you guys exactly what this looks like so we have print this is just to output data onto the screen all right it is not restricted to just strings or just text you can print out all these data types that you guys see boolean bytes charts doubles all of this and i'm gonna go over that a little bit more later on but i just wanna let you guys know you're not restricted to just text you can print out different types of numbers as well so in this case we're just going to print out seven all right and then let's print out one more thing let's print out 14 so i can show you guys the real difference between print and println so we just print this out we will see 7 and 14. print just prints out data onto the screen nothing more nothing less now if we were to use something like println this also prints things onto the screen same type of data types as well but the ln portion of it adds or appends a new line onto the printed message or after the printed message so in this case we'll have a prompt let's put my name is let's locate that y and this thing does not want okay there it is and then put a colon for for looks and then let's put another println to put my name and then let's run that all right so boom you guys can see it prints out 7 and 14 like before and it also prints out my name is but then because of the ln it appends a new line right after this message and you guys can see that giovanni is added onto a new line you guys can see that my name is is still on the same line as the numbers because print does not add a new line after it prints out text it just prints out to the screen no instructions and then after this pranlin which is my name is it just adds a new line and then giovanni is placed on your line and if we were to put a prince right under giovanni let's put tech with geo you guys will see that this is also on a new line even though this is pritz it is on a new line because of the fact that giovanni was appended with a new line after the message was outputted so tech with geo takes that and is added onto the new line so it is all about preference do you just want to output that onto the screen or do you want to output that onto the screen with a new line do you want to play the row by row it's all about convention alright so variables are namespaces in memory that holds different types of data calling uses two keywords to specify how this data will be held or how it could be manipulated in memory so the first keyword we're going to use is val well let's call it know that this data will be read-only once we pass information to this variable the mem the data cannot be changed so we start off with the keyword which is val which lets us know that it's read only then we specify a name for the variable so in this case i want to hold a value that will hold the day of uh birth which is the number so i'm gonna call it day of birth i use camelcase which is first start off with a lowercase letter for the first word and then uppercase for the next couple words then i use a colon and then pass in ins this will let colin know that i want a variable called days day of birth which will hold a ins which is a number and we'll pass in 11. and then we'll have another variable which is val read-only month of birth and then colon and it's which will let caller know that this is going to be an integer a number and then pass in seven now to show that i'm not lying that this information is actually in day of birth and month of birth we're going to print this out let's use println because i'm gonna make it a prompt so month of birth cola then we'll use plus to concatenate it we're gonna go into the thing called string templates later on um but for now i'm going to show you guys that you can literally just add on a variable to a string or a number whatever message that you're going to put into this print just add a plus to concatenate it so month of birth and this weekly line that you're gonna get will let us know that you know you can use the template but we're gonna go over that later on in the series and then print another print length to print out the day of birth so we're gonna put day of birth colon concatenate it with the variable day of birth and then if you play that you will see month of birth and you will see day of birth so it is holding the information that you're assigning it to this variable and yeah this equal mean is pretty much assignment operator you're assigning 11 today of birth of type ins which is read only because of val and you're assigning seven to this variable of type int using read only this cannot be changed your day of birth cannot be changed your monthly birth cannot be changed that is a fact all right so the second key word we'll be using is var a var lets colin know that the information that we're about to store can be changed later on the information is mutable so we can read the information and we can write the information it's pretty much the same approach as val which is we just choose a name for the variable which in my case i want to choose name because sometimes the name that your parents chose is just not right it's not it's not right for you so in my case i want to change my name and this variable is going to hold a string which is text and the name that i want to change to is bobby all right and then i'm going to use one more var and we're going to call this last name and we're going to infer we're going to let colin know that this is going to be a string which is also text and the last name is going to be some people um some people change their last names you know maybe they get married some people don't it's really up to you if you want to let the person change the last name or not in my case free reign bobby boucher i'll let you guys you know figure out where that's from so now we can print out you know our first and last name actually just use ellen so our first name or my first name oh actually my new first name is and then you concatenate that with name actually doesn't make it more specific first name first name and then print and then my new class name colon then add last name now let's print that oh well i can't spill i guess last let's run that again but as you guys can see it's a vowel and then that's printing out the first prompt and then our var is printing out my new first name is bobby and my new last name is boucher but the cool thing about avar is that you know maybe i want to change that again let's change first name to pops pops blue shade and then let's grab that first name again and then if we print that and run that you will see boom my first name is pops but the initial one was bobby so it only changes when it gets to this line after that that becomes the new value that we assigned to first name so before if we use vowel we should read only once we even think about changing it and trying to assign it a new value colin already sees that yo um no that's not what you agreed with initially you said it's vowel it cannot be assigned reassigned it'll let you know do you want to change it to far which is mutable it could be changed so you got to think about these things when you're creating an application like do you want the information to be changed um or do you want to just stay the same value once the information is being assigned to it by convention it's recommended that you use val but if you know the information is going to be changed use var alright so the cool thing about calling is that we can let kotlin infer the type of the variable so before we were specifying the type that we want to hold into this variable but we can literally just remove all of this we can remove the data type that we specified it to and we can let kotlin infer the type so in this case we were to run this we can see that it works perfectly kotlin was able to see that this is a string without us specifying it now the issue is if we try to pass in another type this will cause a problem the literal or integer literal does not conform to the expected type which is string so because we instantiated this originally with a string which is bobby we can't just make it switch into another data type because that's not the contract or that's not what we agreed on we let caller infer that this is a string so it has to maintain that data type which is a string so let's leave it as a string but this is a very powerful tool with colin sometimes we just you know we don't have to if we know exactly um what to expect with the type we can let colin infer it and that saves some time all right so let's start talking about data types in kotlin so the different data types that we have are numbers booleans characters and strings the first type we're going to go over are the integer types so first let's talk about the byte this is a data type that is 8 bits and can hold a small number of values so the minimum value is negative 128 up to 127. i will add a little chart so you guys can see you know the different types and the sizes but a bit is the smallest integer type and to create one let's make a file or variable that is a type valve meaning it's not mutable let's call it is byte and we will use a colon to specify the type which in this case it is a white and we are going to live life on the edge and place the smallest value that a bike can hold which is negative 128. now if you guys try to go less than that this is gonna give you a little warning where it says that it's a mismatch this is really an integer because the range is past a byte can hold so you just got to make sure that if you're going to use a byte which is very small you got to make sure that you're only going to store a very small number because if you try to put something bigger than what it is there will be a problem so the next one we have is a short type this is a 16 bit data type and to make one we just use we're going to use vowel which means not mutable again and this is bothering me all right and then ish short and we're going to specify that as shorts and we're going to make that equal to the max value of short which is 32 600 or 32 767. if you try to put anything bigger than that it will also throw an error so just make sure that you know you want to hold something that is 16 bits or something that's up to 32 767 or within the range of negative 32 768 that range of numbers the next one we're going to go over is an integer type or int type to be exact um so it's going to be val is int this is a 32-bit data type i mean it can hold a very decent amount of of uh numbers and we're going to make that equal to let's make it 1 million so another way as the numbers get bigger another way to specify a number or a big number in in kotlin to replace commas you can use an underscore this will make it more visually pleasing to see um you know a big number like this because before it would just be without these underscores and it's just how many zeros do we have right now but we can put these underscores to pretty much mimic a comma and it just makes it you know an easier experience so yes this is an int it is 32 bits and usually if you don't specify a type if it's within the range colin will infer an integer to be in type so int is the default type for numbers now the next one and the last one of the introductory type is the long and this is the biggest um biggest um type within the integer type it is 64 bits it's double of an inch so if you know you're gonna store something of a huge huge huge amount then you use a long and to do that you just use the long data type and then you can place a big number in this case we'll put 20 billion and they could make it still more than that so now to specify that this is a long you need the l at the end of the number to let god know that this is a long now if we were to use a number that is still within the range of an ins kotlin will infer this value as a ant if we didn't specify the l at the end column would say okay this this could be an n so it might be an answer but if we really want it along to make sure that this is a long you have to put an l at the end obviously if it's out of the range of an inch colin will refer this as a long but just to be sure if you know you went along for sure just put an l at the end so these are the four integer types what makes them different is just a range of numbers that they can hold so you just got to figure out you know how big the number you're going to use or how big can a number get within your application all right so the next data type we're going to go over are the floating point types these are pretty much decimals now we only have two of them so we're going to go over the first one which is the float type i'm going to call this is floats we're going to specify it as a float we're going to make it equal to 3.14 now you know very basic pi but with float you have to add a suffix of f just like long add a suffix of f to let colin know that you're trying to use a float type a float type is 32 bits and we have a second one called double which is double which is 64 bits and to get caught this is double we can specify it as a double and we'll make this 3.4 and i don't know i didn't memorize all the numbers but we can just spam it but yeah this is double the size of a float now with the float and the double by default kotlin will infer that it's a double it's a little bit different than with the long where if it's in the size of an inch or in the range of an int it will just infer it as an inch but if it gets big then it will just infer it as a long in this case a double can hold anything that a float can hold so if we don't specify it as a float by using f it's always going to store it as a double so if you really want to float if you really just want 32 bits then you have to specify it as a float and you have to use f um so or call it would infer it as a double but yeah now what makes it different is that because of the fact that a float is only 32 bits if we try to push it you know a little bit too far there will be a cutoff because of the fact that a float can only hold six to seven decimal digits so we have three here then we have another three and then we have this eight so after that and we'll print this out so you guys can see there will be a cut off so we can print out is floats and let's just use string templates here to make our life easier floats and then we'll add that right here let's print this out if we had to print this out there should be a cut off and it shouldn't be the same number that i have here so boom it cuts off at eight and it rounded that last seven to make it eight so it only held six values after the decimal point so if you want it to be bigger you just use a double and honestly we'll just take this whole number and put it in the double so you guys can see and even with double there is still a cutoff point my double can only hold up to 15 to 16 decimal digits but let's print this out call it this a double and then we'll print out is double if we run that boom we printed out everything because of the fact that double can hold way bigger amounts of numbers and we can also reach that limit too so honestly you just got to be careful to how precise you want it to be and honestly you guys can see that the last number here is eight there was a cutoff point somewhere honestly there's too many numbers for me to tell but just go by the chart you can go up to 15 to 16 decimal digits so you got to specify which one or how much you need to hold or how precise you want the number to be now it's time to go over the boolean type and this is probably the easiest one to understand all the boolean is is a value that returns either true or false i mean that that sounds pretty you know straightforward to me so let's see this in action so we'll just create a file we'll call this is true and we're going to make this of type boolean and we're going to make this equal to true it can't be one right yeah it can't be one can't be three it has to be true and let's create another one called false and perfect just true or false and later on when we start to get more into knows it also could be no just in case you know you're taking an input or you want to specify as null but for the most part boolean is just true or false and we'll just print that out because i didn't print out the others but actually just copy this oops copy that true they're gonna print out true true but and then we'll call that is true it's true or is false and then run that and boom i'm not lying obviously we still have to float in the double but we also have the true and false sounds good all right so the next type we're going to talk about is the character type and this just hold single characters nothing more nothing less so to do that we can just create a vowel called that is char and we'll just specify it as a char and this will hold legit a single character so in this case we call this a and i mean honestly just print that out call this char and then use is char and if we run that boom it's just an a now if we try to put anything else inside of it it's going to scream at you because if you're trying to use multiple characters then it'll be the next type we're going to talk about which is a string but let's talk about some special things we can do with a character characters have a special function now i'm not going to go over all of them but i leave a chart but we can do special things with a character type we can create a thing called a new line which we always use println to create a new line after you print something out we can also do this with a character and to do that we just use a backslash and an n this is to let you know kotlin know that we want to create a new line and we're doing that with a character now if i were to just add something random let's put the word random literally and we were to print this out i should see random on a new line which i do and there's some space because i have some space right here but yeah and then another one i can use is i can use backslash t and this means that i want to use a tab or put a tab so you should see a little gap boom so these are some special character sequences that you that are built in that you can use to do certain things so you're not just restricted to just one character but you also have sequences of characters that do special things so the last thing we're going to talk about is the string type a string is a sequence of characters and we use it a little bit when we print out but we place them within double quotes so to specify a string we just use you know let's create a vowel and our variable and we call it this is string and we're going to specify that it is a string and we're going to make this equal to this prompt hello i am a string and a string are immutable once you create a string you cannot change its value or assign a new value to it now we can transform it or change it a little bit how it looks but everything it stays the same within the string so first let me print out this prompt so you guys can see that this is actually a string and and i can just you know print this out as just a straight up string this is a string hello i am string because it is a string but let me talk about what i was talking about before so like i said it's immutable that means that once i create a string the value cannot change or i can't assign a new value so if i were to use a built-in method that comes with the string which is is or uppercase or two up to uppercase this will take the string and uppercase it and if i were to print out the same string right after it should stay the same because a string is immutable so if i run this i will see oh there i've got to put ln i always make that small mistake so if i were to run this again and if i were to use this i should see hello i am a string but then i'm also telling it to uppercase which i did but if i go back and print it again it stays the same so a string is immutable and it stays the same no matter what type of operations that you run on it so that's another cool thing you should know about strings are in kotlin we have operators the first type of operators we're going to look at is arithmetic operators these are operators that you usually see in your math class such as addition subtraction multiplication division etc so let's see them in action we're going to put them onto the screen so you guys can get understanding or see um the power of these operators all right so the first one we're gonna do is two plus two i think you guys know that the answer to that press play we should see four all right so that's addition let's write a comment there so you guys can see that it's you know addition is you know the usual plus sign the next one we're gonna do is subtraction can't spell and then we're going to also print that out and we're going to do 2 minus 2. put a space there for for looks and we should expect that to be zero okay cool that worked the next one is gonna be multiplication we're pretty much gonna do a square which is multiply um then we're gonna take that print ln and do 2 times 2 and we expect that to return 4. we use asterisks for multiplication and then let's do a division we're going to use a forward slash for this we're going to use prince ln and then 2 divided by 2 and that should return one because anything divided by itself should return one then the last one is modulus this returns the remainder of the division so in my case i want to use a weird example so you guys can get a gist of what i'm talking about we're going to do 13 modulus 5. we know 5 can go into 13 two times so the remainder should be i'll let you guys guess three so that's pretty powerful if you want to see if something is even or odd um if you guys are you know into um doing that um so one more thing i'm talking about you're not just restricted to just two numbers of course you could try to use multiple types of these arithmetic operators together so you could use two plus two and then you can plus that or minus that by seven this will actually work obviously if you have an addition and then you subtract seven by it is going to return a negative number and this does follow pemdas which is you know first it looks at the parentheses exponents multiply division addition then subtraction so it does two plus two first and then subtracted by seven which is then returning negative 3. now if we want to subtract it first we can use parentheses like i said to make sure that it focuses on that first and then add the value so 2 minus 5 or 2 minus 7 is negative five and then you add that by two i mean it's looking at negative three look at that um but if we turn that let's see let's turn that into nine it would do two minus nine and then it would add two so it's gonna be negative seven plus two which is negative five pretty much the same thing so yeah or we can just do nine what's something that comes first let's do divided by two let's make it weird because you're not just restricted to um you know just whole numbers so yeah let's make this weird let's make this 9.5 divided by two plus two just so you guys get the whole feeling of what this does and it should return a decimal number which is 6.75 so you guys see how arithmetic works let me just put pemdas which is parentheses exponent multiplication division addition and then subtraction this is the order that it looks at so yeah there's addition subtraction multiply division modulus and pandas that is your arithmetic operators all right so let's go over another set of operators that we can use in kotlin these are going to be the comparison and equality operators or we can call them boolean expressions because of the fact that when we use them they either return true or false so these operators are what we're going to use to compare or check if a value is equal to or not equal to another value so to start this off we're going to make a variable that's going to hold the age of bobby so let's you know give some context to bobby you know bobby's in college right now you know he's a sophomore or a junior he's about to be a junior and he is the age of 20. all right he is almost there but he's not just there yet you know the age of drinking in the united states is 21. so at certain bars they may ask for his id and see what age he is so this is the perfect thing to use for that scenario let's check if he is less than 21. so let's print out that logic so we're going to take the age of bobby which is holding the value of his age and that is 20 and we're going to check if he is less than the age of drinking united states which is 21. now we do know that his age is 20 and we are checking if he is less than 21. so what do you guys expect the value to be now if we return this right now we should know that it's going to return true because his age is 20 and we do know that 20 is less than 21 so it should return true so let me just put a comment right here that says less than operator because we use this to check if a value is less than another value actually let's let's extract this we know we're going to use 21 so let's make another variable called age of ranking n u s we're going to make that as well voice break and end as well and we're going to make that equal to 21. so that would have to keep writing 21 all the time we just paste that in major drinking and then let's do the greater than operator this would check if a value is greater than the age drinking in the us so let's print that out as well we'll check if the age of bobby is greater than the age of drinking in the united states and we do that using this greater than sign that you know most people probably you know are shown in you know basic math classes at a young age and then we're going to print out the age of drinking in the us which is equal to 21. and you guys can change that to 18. i know some places is 18. some places might even be 16. we're going to run that and we should also see false so true or false that is a boolean expression there's only two values that you should expect it to return so we we know that his age is less than 21 and we know that his age is not greater than 21. so then let's check if it's equal to so let's put equal to and that's one of the equality operators all right so we'll just print ln and age of bobby and you guys know that with one equal it means to assign you know a value to agent bobby we don't want that we want to compare so we use two equals to actually compare a value to another value so we're going to compare age of bobby to the um actually we're just basically age of drinking in the us which is 21. so is 20 equal to 21 let's see that that is false so this is powerful if you guys want to create a range of you know numbers to check so like this is pretty much going from uh i mean we can count negatives or not we're going to do a zero to up to the age of drinking in the us which is 21. if you want to see that range it actually is less than then you know it will do that if you want to see from pretty much 21 to infinity that's pretty much what greater than is doing and then if you want just one set value to check to compare then you know we do the equal signs but then we also have you know a mixture of both which is the less than or equal to value which is just prince ellen we're going to take age of bobby and we're going to check if it's less than which we take the less than from from above and then we put it equal to not two equals but just one you know and then you take the uh age of drinking in the u.s to check if it is less than all right or if it's equal to the age of drinking in the us so this would not just have a specific range but it'll also include the equal to check if it is 21 or less than 21. so let's run that what do we got what do you guys expect to see it's true because it is less than it is less than the age of drinking us which is 21. although it's not equal to it is less than so it's still technically true so now let's check if it's greater than or equal to the age you're drinking in the u.s so we just take the age of lobby check if it's greater than or equal to the age of drinking in the u.s we know that the age of bobby is not greater than um the age of drinking in the us and we know that it's not equal to as well so we should expect it to return false all right that's perfect now one more thing that i want to go over is the not equal to this will check for sure that a value is not equal to a value another value so in this case we know that the age of bobby all right let's print that out now let's put println we know that the age of bobby is not equal to the age drink in the u.s so since you know it's not equal to the age of drink in the us we should expect it to return true it's pretty much the opposite of equal to if we know that it's going to compare if it's equal to age of drink in the u.s we know that this is going to return false so if we just put the negation of that we know it's just going to return the opposite of that which is true because age of bobby is not equal to the age of drinking the us so these are some of the comparative operators and equality operators that you can use to add some logic to your code to check a certain range or a specific value to see if it's actually equal to or within a range of a value and you're not limited to just using uh you know a direct value you could just put the value into a variable and you can keep adjusting this and it will return you know right now if you guys see it says true false false true false true and i'm pretty sure if i run this it's just going to change a whole bunch um so yeah yeah it changed so if you guys want to add some some logic to your application um comparison and equality operators will be necessary all right so the next operator we're going to go over is the assignment operators you guys are probably thinking we've been using the sum operators this whole time you know equal but that's just the simplest form of it there are other assignment operators that we can use with some additional logic to it so let's just create a variable called x and this is going to hold the type int and it's going to equal to 0. i just want to create a base variable with a value that we're going to just continually add on to and this is just the simplest assignment operator next we're going to use the you know plus equal this is the dish edition assignment operator um and just as it looks it's just gonna equal plus equal and we're gonna assign it you know the number 20 and this is equivalent to x equal whoops lowercase x x equal x plus 20. so it takes the current value of x and it just adds 20 to it so plus equals 20. it just adds you real quick it makes your life simple so you don't have to write all this out so it's just plus equal you know because it takes the current value of x and it just adds on to 20. convenient and it's pretty much the same thing for every other arithmetic operator that we used so the next one we have is minus equal and this is going to be equal to x minus equal to 10. so we're going to cut that in half and that's going to be equivalent to x equals x minus 10. so it takes the current value right and then which is 20 because we just added 20 to zero we're going to subtract that by 10 because the current value of x is 20 at this point so that should equal to 10. the next assign operator we're going to use is the divide or actually let's do multiply multiply equal and this is you know following the pattern of above which is just going to be you know multiply which is the asterisk and then we're gonna make that equal and then you know let's choose a fun number let's do five because i want fifty um and then we're gonna do uh what that is considered and that's gonna be x equal x multiplied by 5. so the current value of x is 10 and we're going to multiply that by 5 which should make it equal to 50. and the next one we're going to do is divide equal which is going to be equal to x divide equal to let's do 2 and that should be equivalent to x equal to x divided by 2. actually no let's make it a whole number because this is gonna make it a decimal and we're holding it in right now so it can't really be a decimal so we're just going to divide that by 10 and then that should be that should bring it back to 5. um so you know that's what it's equivalent to it's equivalent to x equal x divided by 10 the current value of x is 50. we divided by 10 it should be 5 and then the last one we're going to use is modulus which is x modulus equal we're actually i didn't put the comment you know so that is modulus equal and then that's x oh that caps are gone x modulus equal and currently it's equal to five so if i do modulus five modulus returns to remainder session i'm just write this all out x modulus x is equal to x modulus and then five and then that should be equivalent to zero because x is holding five here and then if we match this by f if you do modulus by five then it shouldn't have a remainder so it should equal to zero and we're gonna print that all out so that you guys can see the values on each step oops probably should have to definitely be beginning but i was so in the zone i was like there's no point but i want you guys to see exactly how it does it and as you guys can see it really does shorten um and i'm just turn out the addition um beginning value so you guys can see it pretty much change values as it goes through each one so i'm going to run that and you guys see it starts at zero then it goes to 20 then it goes to 10 after we um you know subtract it by 10 then we multiply it by 5 which makes it 50 then we divided it by 10 which makes it 5 and then we use modulus to see what is the remainder so cool stuff if you guys don't want to write out this whole this whole thing this whole you know assignment operator we could just use the some operators to get the same logic going so these are the summer operators that you can use in kotlin all right so the next operators we're going to go over are the increment and decrement operators this is pretty much a shorthand way of increasing a variable value by 1 or decreasing a variable value by 1. so the first one i'm going to go over is increment and first before we do that let's create a variable and it's going to be called i and you guys are going to see i a lot once you start going into loops later on um but we're going to call i or we're going to make i hold an int value and the initial value is going to be 0. so to increments or to use an increment operator for this variable we literally just take the name of the variable and then we add plus plus this is shorthand for i is equal to i plus one or as we learned before it is the same as pretty much i plus equal one because you know i plus equal one is same thing as i equal i plus one but um you know i plus plus just literally just adds one and if you print that we should we should see that i is not equal to one we started with zero we use i plus plus to increment by one um and you know and it did that and there's a very shorthand way of doing it and as you guys get into loops this would be very useful the next one we're going to go over is decrement which pretty much is the inverse of increment you know it decreases the variable value by one so just as we use plus to add we use minus to subtract so we just put two minus signs for decrement this is equivalent to i equal i minus one which is the current value of i minus 1 or as we learned before i minus equal 1 which is you know also equivalent to i equal i minus 1. so this will decrement or decrease the value or subtract 1 by the value that i is holding and if we print that out print out i we should get the original value which is zero so this is a very shorthand way of doing it an either an even shorter way of doing it because it's shorter than this by i think one character or yeah one character for sure so if you guys know that you want to decrease it by one for sure or increase it by one for sure this is the perfect operator to use for that and i am pretty sure you guys will use it later on when you guys learn loops all right so the next operators we're going to go over is the logical operators these are used to combine boolean expressions so let's go over them real quick so we're going to create a variable and this is going to hold it's going to be called issue some you guys know it's going to hold the boolean oops boolean and it's going to equal to true now we're going to hold another we're going to have another variable that holds false and it's going to be called is false it's going to be a boolean equal to both so the first logical operator we're gonna go over is the and and this is specified by two ampersands and we're going to print this out so and is only true if both the boolean expressions are true so in this case we're going to use is true and we're going to use the two ampersands which specify and and we're going to put is false so you guys can see what i'm talking about so right now we have a variable that holds true and we have a variable that holds false and if you run that we will see that it is returning false because and is true and is false returns false now if we were to make this is true it will return true just by the rule of ends now it doesn't matter what order we put it in where to put is false here is still going to return false we put both false is still going to return false and needs both boolean expressions to be true and that is just a rule of ant the next logical operator we're gonna use is ore this is specified by two pipes and we're gonna print this out and or the rule of or is that only one of them have to be true for it to return true so we're gonna use is true two pipes to specify or and we're gonna use is false we're going to run that and we'll see that it returns true because this is true no matter if this is false at least one of them were true now if we were to put it false for both of them just by rule of or there was no true included in this statement so they so it returns false now also for or the order doesn't matter as well we can put his true on the other side and as long as she is in that statement that expression it will return true so either or you know and the last one we're going to go over is not this is just an estimation point not and we're going to print that out and this literally just takes one of them doesn't take two like the other logical operators it takes one of them and it essentially negates the value so if we have is true right and we prefixed it with the logical not operator this will make it the opposite of the value that is true is holding so if we run that we will see that it will return false even though is true is true it negates it essentially and if we were to put it false here then it will return true because the negated is false to return the opposite of the value that it holds so these are the three logical operators that colin has you have the and which both of the bullying expressions in this statement has to be true for it to return true we have the or which is you know just like just as it says either or has to be true for it to return true if it doesn't include a true then it returns false and then you have the not which essentially negates the value of the expression provided so you have a false variable or expression then if used not in front of that expression then it returns the opposite value so in this case this is is false if we negate it or we put not in front of it then it will return true all right so in calling we have a thing called string template this allows us to inject variables or expressions into a string let's take a look at an example to see how it is done so first let's create a variable let's call it slogan we'll make this a type string and it's going to equal to just do it oops so you guys know where this is from then that's a bonus if you don't that's okay and just like how we would normally do it let's print this out and we're going to create a little prompt what is your favorite slogan let's put a colon and before if we want to add this variable into it we'll use a plus and then the name of the variable which is slogan and that is concatenating it but if you guys hover over this it will say convert concatenation to template there is an alternative and that is string templates this allows us to inject it directly into the quotes and it will work perfectly fine so to start off or to create a string template you will literally just add a dollar sign and then use the name of the variable this will let colin know that you're trying to inject a variable into the string and in this case we have a variable called slogan and is holding the value just do it so if we run this we get what is your favorite slogan how should i put a question mark that's okay but then just do it we see that kotlin injected a variable into our prompt and we're perfectly fine we don't have to add a plus and then the variable and you know it's very convenient you know way of doing things instead of having to do all the extra stuff we are not just restricted to just variables we could also inject expressions and what i mean by expressions is you can use you know the methods that are that come with a string which is uppercase or two lowercase so let's see that in action and let me just put a expression injection um so let's see let's print ln again and let's put elise favorites quote or slogan and let's copy this and let's put these favorites slogan now what is my least favorite slogan um i'm just going we got the meat because i really can't think of any other slogan but i'm not going to forget the question mark this time and we're going to add this colon and we're going to inject an expression all right so right now we have this looking called we got to meets but you know i want it to be uppercase you know just to add emphasis so i'm gonna use a dollar sign and i'm gonna put open and close brackets because we wanna let kotlin know that we're gonna use an expression not just a regular variable so i'm gonna put the name of the variable which is least favorite slogan and i want to use a method that will uppercase this uh slogan so i'm going to put dot and then i'm going to put 2 uppercase and this will let kotlin know that uh this the values within this variable i want it to be uppercase as the name implies and if i run that boom what is your favorite slogan just do it and least favorite slogan we gots to meet so i mean this is pretty convenient i don't have to do any extra stuff i can just as i'm typing just inject it right in and it works perfectly fine and actually this is not really a question this is kind of what is your least favorite slogan that's perfect that's proper english alright so that's how you guys inject or use string templates to inject variables or expressions into a string all right so to write comments in kotlin we'll use first the single line comment which is two four slashes everything after the four the two four slashes is considered a single line comment so all right that if you're using an id you will see that you know everything after the four slashes is grayed out so this is to let you know that you know this is a comment this won't affect the code that you're writing and it just allows us to put our logic into words uh that we understand but everything after that you know on every everything after this after these two four slashes is considered a comment but if we were to go on another line we're back to writing code again but there is another type of comment which is multi-line comment which is forward slash asterix and then we would have to close it which is asterisk and another forward slash anything within these two asterisks is a comet so we can have the first line we can have a second line and more and more until we're out of this asterix or these closing asterisks and four slash anything after that we're back to coding so you know the single line comment is perfect if you want to keep things brief or maybe you have that you have a title that you want to put and then you have the multi-line comment if you want to describe um even more so usually you see these in the beginning of a class uh we have them in front of a pretty decent you know method uh so it just allows us to put our words into something that people understand maybe it's for your team maybe it's for yourself comments are perfect to add more knowledge or more understanding of what you intend on writing or what you already wrote all right let's talk about functions in kotlin functions are modules of code that does a specific task we use functions in kotlin or in any programming language to break down our code and to allow for a reasonable code so in our case we kind of already seen a function which is the main function this is the entryway into our application and we already know that if we were to put anything within these brackets the code will be ran when we run the application but we can also create other functions and to create a function in kotlin we use the keyword fun and then we have to choose a name in my case i want to make a function that will output a prompt to say hello to a user on start of the application so i will put hello prompt as the name of the function then use a parentheses and then open and close brackets now just like with a variable we could specify a type for a function so after these parentheses we could use a colon and then specify the type that we want to be returned by default in kotlin a unit type is returned unit means that no value will be returned from this so just like in java there's void which means that you know no value will be returned so if we have a double or string any of that we're not returning that a unit is the same logic there is no return type we're not returning a value but that does not mean we can't print a value so by default it is a unit but we do not have to put it we can let call it infer that it's a unit and we just put prints at ln and then we can put our prompt which is hello comma welcome to my app application so now that we have hello prompt we have to use it so we can go into this main function which is ran when we run the application and put hello prompt parentheses this will let colin know that we want to run this function right away we pre when we press this run button we should see after this builds and that might take a little bit it says hello welcome to my application that's pretty cool and we can do this as many times as we want and that's what allows for the reusability of a function we don't have to keep typing out print and land println per ln obviously this is a very basic case but we can literally have a whole bunch of code or not a whole bunch you should try to keep functions as small as possible but we can have reusable modules or functions and we can run them just like this and if i were to run this whoops you guys should see the prompt four times all right so in kotlin we can pass in values into a function to be used in our logic within the function so let's see that in action let's create a function that will double the value that we pass in so let's call this double the value for the function name let's put a parameter that we can pass into the function so in this case we'll just call it number value and we have to make sure that kotlin knows the type that the person is going to pass into it or that the person should pass into it so we're going to make sure that the person is required to pass in an int and then we want to return it into after we double it so we're going to make sure that this function has a return type of int open and close the brackets and we are good to go a little red squiggly that we see is to let you know that we have to use a return to return the integer type it says a return expression required in a function with a block body so we let colin know that we're going to return an end so we have to do that so we just use the keyword return use the inputs that the person's going to pass into this function and the name of that is going to be number value and then we're going to times that by 2. so we're going to return the number value that the person passed in to this function and we're going to multiply it by 2 to get double the value let's erase 3 of these so then the next thing i'm going to do is create a variable called double the file and i'm gonna make this an int and i'm gonna make that equal to doubled value and i'm passing my favorite number which is seven actually um let me just make this more specific hold seven twice and then if i print ln i just put hold 7 twice and i should expect to see 14. so let me explain this again we have a function that we created it has a parameter of type int called number value and the person can pass in a number which will take and we'll multiply by 2 to get double that value we return that value back after the logic is done and now this is pretty much a 14 this is holding 14 after everything is done we can assign that value into a variable which we have called hold seven twice and we can print that out and that's it that's what we see right here which is 14. amazing stuff right there with functions you're not just restricted to just one parameter we could just make another function and we're going to call this favorite food prompt we're going to open this up we're going to take the name of the user make this a string type and we're going to take another parameter which is going to be favorite food oops favorite food and this is also going to be a string we're going to make this a unit we can specify it or you can just like call it infer that it's going to be a unit but i'll specify it this time so you guys can see i open up the brackets and i'll just have a simple println and i am going to make a prompt that says hello let me use the string template here with name then put a comma and your favorite food is i use another string template oh whoops my favorite food is pizza but not in this case just put favorite food so it should say hello your name and your favorites got that food is boom so in the main function we could just put favorite food prompt we can pass in the name but let me show you a cool thing you can specify say for instance you don't know the order of the parameters if you know the name of the parameters you could just use name put an equal and pass in the value that you want to use in my case it'll be giovanni so that you don't get lost by the parameters you can specify exactly the only problem with this is that if you specify one parameter if you have multiple parameters you have to specify all of them so in this case i have to specify favorite food as well so in this case i use pizza and boom you guys can see i have a parameter name which is name is the first one i have favorite food which is favorite food right here the name of this parameter and i specify it as pizza we can even change the order because we're specifying it it doesn't even matter what order this is in no problems but you know if we were to just put pizza first here and then giovanni here this would mess this up because it will just see that oh you're passing pizza into name and you're passing giovanni into favorite food that's not what we want but we can make it more specific by passing in the actual name and assigning it a value that we actually want so let's just run this so we can see so boom you see hello giovanni your favorite food is pizza and it's not even in the correct order as you would expect but if you were to erase this and just pass in how it is right now you'll see hello pizza your favorite food is giovanni kind of trippy but you know now you guys know that there is an alternative if you know the exact name of the parameter this makes much more sense also if you think that the user is you know going to play with your application and not passing the value that you expect or not passing any value you could put a default so i could just put guest here and you could just put what's something that everybody likes hmm you can buy ice cream i hope you're not lactose but you can put a default value just by assigning a value to the parameter and just in case nobody puts in in values let me see so if we just erase all of this and run it it'll say hello guess your favorite food is ice cream you don't have a choice this is the default values so that's that's pretty cool stuff you can either uh you know let it be free and let the person decide for themselves or you could place a default value just in case the user forgets or just doesn't put any values for the prompt or for any function that you intend on using it for so that is functions in kotlin hope you guys enjoyed conditional expressions are expressions that produce a result based on the criteria pass the first conditional expression we're going to go over is the if expression and just as the name sound like if a certain criteria is passed then we will run a certain amount of code based on what you specify within that block so instead of talking about it let's go over an example so you guys can get a better understanding of it so a good example that i wanted to use because i want to make something fun is a coin toss so you guys know in front of in in the beginning of a game like an nfl game they do a coin toss to see who gets the ball first who chooses to get the ball first and um you know you choose between heads or tails so what i wanted to do was make an interactive game where the user can input a choice between head or tails and based on the choice if i run a random number between 0 and 1 if their choice matches up with whatever was generated then you win if not then you lose so how i'm doing that is first i'm creating a variable called coin toss which is holding um heads or tails in that set of array we're going to talk more about arrays but it's just contiguous memory which holds data so i can retrieve this value based on the index or the order that is in so i can retrieve head by using the number zero and i can retrieve test by using the number one i print out a prompt which let the user know to choose with either heads or tails i take in some inputs which we're always talking about printing printing out using println print and all that but you can actually take an input in kotlin using readline function so it's literally just read line and then parentheses and this is going to be storing the value that the user chooses which is head or tail and i'm using string and um this is to let this question mark is used to let kotlin know that you know sometimes the user doesn't you know make mistakes so um sometimes the user might not make a choice and it might be no so we gotta be sure that that's okay and that question might let cotton know that you know it's okay to store no if the user passes in nothing and in this case we're going to print out the choice that the user makes and i want to generate a number between zero and one so this is how i do that this dot dot let's call to know that we want a range of numbers between zero and one so in this case it's only two numbers and i want to generate it a random number between zero and one so it's either gonna be zero it's gonna be one based on that i'm either gonna retrieve heads or head or tail so now we're gonna use the if expression which is if open parenthesis and within this parentheses i'm going to put the criteria that i want to check for so in this case i want to retrieve the value from the coin toss so i'm going to use coin toss which is this variable that's holding head or tails and then i'm going to use the random number that was generated to retrieve either head or tail based on whether the number of that was randomly chosen chosen is zero or one so i'm going to put random number so i use this uh to retrieve that and then i use double equal which we learned to compare with the user side choice then i open up a bracket so if this is correct if this is true we will run whatever is in here so in this case i just want to print out you we're right congrats that say something simple oh i've got the quotations because i'm printing out text congrats and then the if the if has a another expression that you can use called the else which if it wasn't met if this is false this will run you know if you know or else you know in this case the else case would be i want to print out you loss that's it so even if the person puts nothing you know it's still not going to equal to you know the choice because you can either be heads or tails if the person put one or if the person puts a number or put something random the else case will catch that which is perfect it's perfect for if the user doesn't put exactly what you want or if the person just loses all right so let's run this so if we press this play button and it's going to take a little bit we get the prompt choose head or tail in this case i want to choose head press enter and oh that was quick i lost um the choice that i picked was head and it just let me know that i lost let's run it again let me choose head again boom choice is head you are right congratulations so this is a pretty cool way to see how you know conditional expressions can be used in your application um if you want something to check um a criteria a certain criteria this can be used uh to execute a certain block of code if that criteria is met if not then we can use else to run something else all right so the cool thing about if or else expressions is the fact that we're able to return a value based on the criteria that we pass so before we did a little game where we checked is the person was of age to drink in the us and before we just printed out some you know some prompts whether it was true or false but what if i want to store that value so i can use later on in my application to block out you know information based on if the persons of age or not well now we can do that with the if or else expression because we can store that into a variable so first let's create a variable called age and we're going to make that a var because the person's age can change and we're going to make an int and we're going to let you go to 20 because you know i want to see if the person is now of age what will happen so then we're going to use var and is of drinking age boom and then we're gonna put it as a boolean because it's either gonna be true or false and then we're gonna see this expression in action so this do if and then let's put the criteria if the age is greater than or equal to 21 then we're going to put prandtl on first so we want to print out congrats well not congrats because it's not really a congrats type of thing enjoy your drink that's all it is and then we're gonna put true as what's going to be returns now you're still probably seeing this red and you're thinking oh man this guy does not know what he's doing but in this case um it's pretty much called letting you know that yes you have an if but what happens if the person is not greater than or equal to the age of 21. we're gonna use the else for everything else and we're gonna print out yeah not missing out just wait and we are going to return false boom red squigglies are gone so if the person is greater than or equal to 21 then we'll print this out and then we'll store true into this variable if the person is not then we will print this out and we will store false granted we do not need these prints i'm just doing that you know just so you guys can see but um yeah now we can print out whether that is you know correct um so our view of the us drinking page and then actually it's a question uh but then i'm just gonna you know put on uh what was actually stored into is of drinking age and we're gonna put this out let's see what happens that's gonna take some time and then boom look you're not you're not missing out just wait and it's actually you are you of the direct the u.s drinking age and it's saying false now if i put 21 which is you know equal to is that greater but it's equal to 21. let me run this then boom enjoy your drink it is true and we are good to go so that's pretty useful if you want to you know return a value to be stored into a variable the if or else expression can be very useful all right so the next thing we're going to go over is the one statement we have the one statement we have the one expression but we're going to go over the one statement first now this allows us to check multiple criterias and whatever criteria is passed first that is what we run with now to show an example of this we're going to create a hot or cold type of game where i'm going to pre-select a number and we're going to let the user know if they're hot or cold towards you know choosing the right value now i already have a prompt that lets the user know to pick a number between 1 and 50 and we're going to take in that value and we're going to store it as a user number choice now to create a when statements we just use the keyword when open up right parentheses and we pass in the usernumber choice and then we just open up some brackets and now it's time to start writing some criterias so the first criteria that i want to put is just a range so the number that i want to pre-select is 30. so the range that you know i want to select first is 1 through 25 and that's going to let the user know that they're cold so we're going to do in which let's kotlin know that we want to first take this you use a number choice which is just you know the number that the user specified between 1 and 50 and we wanted to see if it's in this range of numbers that we're about to specify so 1 through 25 is the range that we're going to use to you know let the user know that they're codes and we're going to take this user number choice to check if it's within this range and if it is then we'll print out and we have to use this arrow to you know if we have to use this arrow to let uh kotlin know that if it is within this criteria we want it to run something specific so in this case we wanted to print out that you know it's it's cold or you are cold grab the cap now the second range i want to do or the second criteria that i want to specify is 26 through 29. i mean the user is very close so that means they're hot so i'm going to use this arrow to point to what it needs to run if that person is within that range and i want to print out you are now if they pick exactly 30 which is the pre-selected number then we just pat we just put the exact number and this will check if the user number choice is you know equal to 30 and if so we just want to print out you are correct with a lot of estimation points now we have the other side now if it's 31 to 35 or if it's in the range of 31 to 35 and these two dots you know specify that there's a range so this is the first number and this is the second number 31 and everything in between to 35 and then if that is if that number is within that range then we'll print out you are hot i've got to put the quote you are out and then the last range check is from 36 to 50. but the arrow if it's within 36 to 50 then you print out that you are codes one last thing is just like with the if statement we can also have a fallback if it doesn't meet any of these criterias and we can use an else and then we'll use arrow to specify what we want to do if it's none of these if it passed none of these criterias so in this case we'll just print out doesn't apply so let me just run this back doesn't apply so we take the user number choice which is the input of the user which is a number or which should be a number between one through fifty if the number hit is within one through twenty five then we print out you are code if the number is between 26 to 29 you are hot if it's equal to 30 which we don't have to use in because this is not you know a number within a range is exactly the number 30. if it's exactly 30 then you print out you are correct if it's in 3135 you print out your hot and if it's 36 to 50 you print out your code and if it's something random that the user put in like if it's a character or something we just print out it doesn't apply so let's run this your code okay since this is not in a loop it's not gonna like continually run uh we're gonna talk about loops later on but every single time we want to run it you know we have to just play it again so let's try 28 oh well i'm hot okay let's try 33 oh i'm hot again let's try 49 your codes oh man but now i know the magic number is 30 you are correct that's cool now if it's a number that you know we don't expect like it's not in the range that we expect we have to fall back else and we can use the number 99 to see this if we put that doesn't apply because it's not within the range that we specified obviously we may have cases where you know a user puts in a a letter or something um and you know we're not you know we're not going over that this is just a perfect instance of it and as you guys go on throughout the series there'll be the fallback options for that but if it's not within the range that we specified we have the else that will put does not apply so this is a when statement you know this is pretty cool we can have multiple criterias and if a criteria is met then we have we can do an action based on that and if it doesn't we can have a fallback that prints out or does something else all right so just like how there's an if expression we also have a one expression and this will return a value if any of the criterias are passed so in this case we're going to create a calculator i already have some setup code we have two prompts that will let the user know to pick a number or integer to be specific and this is just a fallback just in case no number is you know picked this is called the elves operator we're going to talk about it later on um but you know there's also other things that need to be checked like you know if the person actually puts a number which we didn't do here but we're just going to act like everything is perfect we're just going to put normal numbers and then we have another prompt that will actually use it to pick an operator which you know we went over all these operators before which is just addition subtraction multiplication division and modulus now we're going to take this operator and we're going to use a wet expression to return the value based on the two numbers that the user specified so to do that we'll create a variable called answer and we're going to make this equal to a 1 expression so we're going to open up a parenthesis we're going to pass in the operator open up the body and then the first criteria i want to specify is the addition criteria so we're going to put in a plus we're going to pass in an arrow to let colin know that this is what we're about to do this is what we want to return after we do it so we're going to do a num1 plus num 2. so if it's a plus or if the person passes in a plus then we want to point to this mathematics right here so num1 plus num2 and it will return the answer of this we don't even have to put return it will return this calculation then the next one we're going to do is we're going to do minus and then we're going to point to num one minus num two and we're gonna do multiplication which is the asterisks and then went to num1 times them two yeah we're going to do division which just can also be dangerous because what if the person uses zero as a second number but we're not doing checks for that we're just showing a very basic implementation so we're gonna do num one divided by num two and then we're gonna do modulus which will return the remainder if there is one um and then we're gonna do num1 modulus num2 they're gonna have else just in case it's none of these and we're just gonna print out actually oh yeah we're just gonna uh just return zero that's it and we should be good to go actually let me just print this out to make sure we actually have the right answer so your answer is we're going to use string template and oh that's well you're right your answer is and it should be the answer that is storing this i didn't specify a type because of the fact that i want to like call it interfere it because if we use division it can be a decimal so let's run this and we're just going to act like a top flight user we're not gonna add any funny business um but we're just gonna do five and another five and i want to multiply it because i want 25 and then boom your answer is 25. now we're gonna run this again we're gonna do five we're gonna do one we want to add this we want you know six boom your answer is six now we want to do 10 and we want to do seven because i wanted three so i want to subtract so boom three and one part sheet i lost count i think we need two more so we're gonna do division we're gonna do 24 and six let's do division boom four and then we have module lists i want to do five and five so this should return zero because there shouldn't be a remainder whoa now let's let's play uh the funny user a little bit let's do four let's do two but for the operator i just want to act funny let me put a little and your answer is zero we're not we don't we don't play that funny business so that's how you can use when to make a calculator this is a very basic implementation of it obviously there's some things that will cause this thing to wreak havoc like you know if i were to divide you know four by zero that would destroy this thing but you know obviously the more you go on the more you guys will learn um more ways to make sure that does not happen so let's continue on but this is a one expression all right so now we're ready to dive into loops so the first loop we're going to go over is the for loop and that allows us to iterate through a collection of data with the iterator so that includes ranges arrays and collections we didn't really talk about arrays and collections yet but we did dabble into ranges a little bit when we went over it in the when statement examples so we're going to use that in this example so the for loop allows us to iterate through a collection of data one by one so that we can see what's inside or we can manipulate it however we want to so to do an example real quick what you had to do is write out four open parenthesis and open brackets just like most things that we use so far now within this parenthesis for the for loop we have to specify the collection that we're about to the collection range array that we're about to iterate through so in this case i want to have a variable called item that is within this range of data so i'm going to make this one through 25. so what this is is this is a single instance of an item within this range all right so this can be one this can be two as long as this is iterating this is going to be an instance within this arrange and it's gonna make sense once i print it out so what i'm going to do is i'm going to print and then i'm going to have a string and this is going to be called index and then i'm going to have a little colon and we're going to do a string template of item this is just going to be a range that i talked about before in the one statement example uh these dot dots specified that i want a range of numbers between 1 and 25 so when i run this this will print out a whole bunch of prompts a whole bunch of text this has index colon and look at this it takes item item is just an instance of each item in this range or each number in this range so at first it's one because the first instance in this range is one and then it continues to iterate it prints this out go back to the top takes the next number in the range goes here print it out and then it goes back and back i keep going um back and forth until you get to 25 and then it ends because you're iterating throughout each one and you're passing that value into this item rate and we can use that to print it out or if you want to do some addition or if you want to do anything or later on when you do uh we when we use arrays you can use that value to retrieve a value an array which probably sound like gibberish right now if or if you have some you know background knowledge and arrays you'll know that an array uses an index to retrieve a value so this is going to be important for that type of stuff so if you're using collections arrays or if you just want to iterate through uh you know a range this is going to be wonderful and if you have used any other language this is a very similar or this pretty much is a for each loop so to repeat again we have item and this is in the range of 1 to 25 this item is just going to take a single instance of a number in this range and it's going to pass it um to be used in this block of code now in this case we have an item and the first instance is going to be one you go back to the top it's gonna be two and it's gonna keep iterating until it gets to the end of the range which is 25. interesting stuff all right so the next loop that we have is the while loop this loop will check if the criteria or the condition is true and if it is true the body within the block is ran now let's write something out to make for this to make more sense so we're going to create a variable called counter and this is going to be an int and it's going to be equal to let's make it 5. we're going to make a while loop which is just wow open parenthesis and then we're going to have the block of code that we're going to put within now what i mean the condition has to be true the thing that we place inside this parentheses has to be true for this loop to continue going on so the condition i want to set is i want this while loop to continue as long as this counter is greater than zero now if it is less than zero or if it's equal to zero then that means that we want this to end so if we print and then counter so we can see how this affects it and then we do counter minus minus so each time this runs it will subtract one from counter we run this we will see boom five four three two one and it just keeps going you know from the top to bottom top to bottom because it's running in a loop but once it gets to zero we got to stop because it's not greater than zero so that's a while loop it could be useful like when we did the little game before uh with the calculator we could place that inside of this while loop and if the person types in you know ends uh we can have a flag that triggers you know boolean that makes it true or false and we can end that while loop so there's a lot of things you could do with a while loop all right so when you're running the while loop the condition has to be true for it to run but you also got to be careful that there is an ending point because you can have instances where this continue running forever if this is never you know change or if it never changed the false so for this example i have a very extreme case where i'm legit passing true as the condition to this while loop but this is just to show you guys what can happen if you don't have an ending point to this while loop so if i run this as you guys can see it's just continue going and going and going and this is what you call an infinite loop because it's going to keep going and going going because the while loop never ends there it never is never going to be false so when writing loops you always got to be careful and in this case i just pressed stop but you always got to be careful because this can continue going and going going and if this is a real application this can crash your code so because at one point the counter is going to overflow so always be careful always make sure that the while loop can eventually turn or can eventually end because you have a thing called infinite loops all right so the next loop we're going to go over is the do while loop now that what makes this do well a loop different from the while loop is the fact that the body will be executed once no matter what so even even if the condition is false it will still run at least once so let's see this in action so we're going to have a variable that holds zero so i'm going to call the variable hold zero we're gonna make this equal to zero and we're gonna have a do while loop so we're gonna have do first and it's just gonna hold our body of code that we want to be executed and we're just going to have this print out you know let's code and then we're going to have the wow that will hold the criteria that checks what makes this run or continue running so we want to check if hold zero is you know not equal to zero we know that is equal to zero so obviously this should be false but let's see what happens whoa so as you can see it presses it prints it out no matter what we know that hold zero is equal to zero but with the do while loop this is going to run at least once you know no matter if the criteria is false so if you just want to check something real quick um or if you want to just take it using input to you know check uh something out this can be useful so the dual if you wanted to if you want your code to run at least once the check the dual loop is perfect all right so now it's time to go over ranges we went over a little bit and we used the when statements and the for loop this helped us create an interval of numbers that we can go through but let's dive in a little bit deeper and try to customize it just a little bit more so let's create an if to check if a number is within a range so i'm just going to create a vowel called y and i'm just going to make that equal to actually let's make it x is going to equal to 9 and we're going to check if x is in the range of one to ten if so then we'll print out on println yes x is in is within the range so this takes the value which is x which is 9 and checks and using this keyword in to check if x is in this range and we specify a range using two dots in the beginning of the two dots we use the beginning of the interval that we want which in this case is one and we have an ending of the interval which is ten so we're doing one through ten so is nine within one through ten we shall see yes nine is within or in this case it says with but yes it's within the range cool stuff that's a very basic implementation of it but uh that's that's a way you can create an interval and check if it's within or if it's in the interval now another use case for an interval is a for loop we can iterate through an interval so in this case we'll have another variable or another uh maybe we got a little variable because it's gonna be holding a value but we're gonna have y and we're gonna use n and then we're gonna use an interval of one through seven and it's not the same as how we have above in the in the if case we're checking if a value is within a range in this case in the for loop we're assigning each value within the interval 2y and as it gets assigned we do some well we can do some logic within the blocks like if you want to access our array value which will be going over next or if you want to do some manipulation some multiplication anything really you do that but then after you're done within this block it goes back to the top and y grabs another value within the range so let's print this out so you guys can get an understanding or see how that is working so once again i am printing a number and we're going to use some string template and we're gonna run this so once again i am printing a number and then boom as you guys can see this is y right here but y is always gonna change because of the fact that it runs this line it runs what it needs to but then it goes back to the top because it's a loop and it grabs the next value within the interval which is two three four five six and seven until he gets to the end and then the for loop is done so that's another use case for it now when using a for loop you're not just restricted to just going in an ascending order we can go into descending order too we can go from seven to one or whatever number you choose to whatever lower interval you choose as well so let's do that let's do four let's choose we already use y so let's use z in this case and if it's within um let's do seven down to one or actually yeah just like how i set it down to this is another keyword that um like uh it's another keyword that let kotlin know that we want to reverse it that we want to go from a higher number which is seven down to a lower number which is one and we can print that out in this case i'm just going to print out z and let me comment this out so it doesn't affect how it looks i'm going to print ln because i want it to be on separate lines and then boom it's going from seven to one it's going not in ascending order but descending order so we could reverse the order at which you know it outputs or at which it iterates and lastly we are not just restricted to just iterating by one we could iterate by two three four whatever you specify and we can do that by using the step keyword so let's do that real quick so wow x actually we already used x so let's use a well a is in 1 to 10 step by let's do 2 and then we can print out a and let me comb this out just like the above and if we run this we have an interval of one through ten and we're stepping by two so it will go one three five seven nine it won't go to eleven because that's not within the interval so it stops at nine cool we're not just restricted from going in ascending order we can also go in descending order as well while using step so let's do that let's do b it's in 10 down two one we're gonna step by three this time and then we're gonna print b and then we just comment this out and when i run this oh i always forget the ln disappointment but yeah ten seven four one so it just goes by three so it starts at one goes to four goes to seven and it goes to ten so we're not just restricted to going in a cent or yeah going in ascending order and we're not just restricted to just iterating by one so there's a lot of things you can do with ranges and um you know you're just not you're not restricted to just doing one certain thing there's many things you can do so that is ranges alright so let's dive into arrays an array is a data structure that holds multiple items of the same type or multiple types the data is stored in a contiguous memory location and the values are mutable which means they can be changed the size is fixed and can be accessed by using the index the index is the location of the item or the element within the array i'll place a picture so you guys can get a better understanding on how an array looks like so let's create our first array so i want to create a or create n array with names within it so i'm going to call it names and i'm going to make that equal to an array so to create an array in kotlin you use the array of with parentheses this is to let kotlin know that we're about to create an array and we're about to add elements within the parentheses to start defining the elements of an array so let's start writing down some names so the first one i want to use is joey second one i want to use is valentina the second one the third one i want to create is maria and the let me fix the a and the fourth one i'm gonna put is george now i want everybody to also have an age so we're gonna put another array called ages we're going to make that eager to an array now an array could specify a specific type just like a variable in this case above we like call it infer the types so in this case if there's only strings within this array we like calling inferred that there's only going to be or we let caller infer that it's going to only be a string but you could also define other types you could put a one in here you could put a a true one here column would just infer that this is a an array of string integer and boolean so it is possible but if we want to make it specific and then remove this we just add in some anchor tags and we place the data type of what we want to specify so in this case i'm using ages so i want to use int let's close that anchor tag open up parentheses and let's start defining some ages so for joey he's going to be 22 for valentina she's going to be 36. maria she's going to be 20 or 28 and for george not so curious but he's going to be uh let's say 45. all right now you can retrieve a value from an array by using an index so with arrays we start at zero up to the length minus one so in this case we have an array of size four as a plane go over my house but we have an array of size four is joey valentino marie and george but since that array starts at zero is going to be zero one two and three so to retrieve george we will have to use the index of three so let me print this out so we can make more sense we'll print out a name and we'll print out an age so let's put and put inland and then let's put name and use string templates since it's going to be retrieving a value from array we're going to have to use the string template for expressions and we're going to place uh square brackets so to retrieve a value you have to specify the name that you're holding the array put square brackets and then place the index so since we want to retrieve the last value of names or we want to retrieve george we'll use the index of three because like i said it is starting at zero to retrieve the initial value in the array and then it increments by one so that means the last value of the array for names will be three and that would be george and i want to retrieve the age of george and that is 45 so i'll print out age and we'll also use a stream template for expressions and use oh h h is is plural but we want to just retrieve one so we'll open up square brackets we'll place the index of three to retrieve the last value and then let's run that you guys will see that it says name is george and age is 45. now a sure way to make sure you retrieve the last value of this array of ages or any array is that you could take ages where you take the array name then put dot size which retrieves the size of the array and put minus one because like i said since it starts at zero and it ends at the size minus one this will guarantee i wish this will guarantee that you're retrieving the right last elements in the array so let me just do this for names as well put dot size and then put minus one minus one and if i run this again we are good to go but you also got to be careful with arrays because of the fact we could have an exception now what if we try to retrieve a value that is beyond the scope of this array so in this case what if we try to take names.size which in this case will return four but we don't have an item within this array that has an index of four so if we try to run this we will receive this array index out of bounds exception and you don't want this this can be very dangerous because this is a runtime exception you won't get this do a compilation it's only when you're trying to retrieve something so say for instance your application is running and you think it is perfectly fine but once that one thing triggers it will break the application you don't want that so you got to make sure that you pay attention to this type of stuff because it can break your application now we can also change a value like i said arrays are mutable in the sense that we can change the values within the array we can't add a size or we can't add another item to the array because of the fact that once you create an array it stays the same size but you can change the values within the array so say for instance we want to change the name george to you know joshua we can take names we can open up brackets we can grab the last um the last item or ls elements using you know the names dot size -1 we can make that equal to joshua and then if we run this this should be perfect joshua and his age is equal to 45. now you're probably asking yourself or if you guys don't understand that yet but it says val's names doesn't that mean that you can't change it not not with a race um that just means when it says vow it just means that we can't change uh what's already being held after we assign it so in this case we already assign names with the array of so as long as we're not changing it to you know a string individually or a number is fine but as long as we're changing just the array or the values within the array that's already being assigned to names then we're good to go and you guys see that it was mutable so we were able to change george to joshua so perfect so you guys can understand if you want to specify a specific type to array you can specify it with a anchor tag if you want to grab the last item within a array you could use the size minus one you guys got to be careful with a rate out of balance index when your application is running because it's a runtime error so you could just be running your application perfectly fine and then once a user tries to access something that they're not supposed to access it could break so you got to be careful for that all right so we're not just restricted to just you know one individual arrays we could have arrays within arrays we can have multi-dimensional arrays so let's create create that let's have one it's called numbers because we're just going to put an array of numbers we're going to call this array of open that up and then we're gonna have another array of that's gonna make another row of arrays now we're gonna make let's make two let's make a square so then we need to just start defining some numbers within here we'll just call this one and two and then we'll just call it this three and four so as you guys can see it's a square it's one two three and four now how do we retrieve three so it's the same concept as as before to retrieve the first value or the first element it'll be zero but in this case is multi-dimensional so the first row the first row will be zero this second row will be one but how do we retrieve the values within the columns now in this case we have the first row and the second rule but the columns is the same in this case it's the same logic so if we want to retrieve three we're in the second row right which is one because our rates start at zero so it'll be one and if you want to retrieve the first value we'll just have to use one and zero because to get the first element within this second row it'll be zero you're probably thinking to yourself man this really doesn't make sense so let's let's print this out so you guys get a better understanding so if we take numbers and we open up the first brackets and we want to retrieve a value in the second row but since since array starts at zero we got to subtract one so in this case if we try to get something from the second row it'll be one and if we want to retrieve the first value in the in this second row we have to use zero now if we run this we should expect to see three boom now if we want to retrieve a value in the first row what we want is we want to get the second element in the first row which is two we just gotta change this to zero because this is the first row and we want to retrieve the second value which got to make this second one a one and if we run this we shall see two now you can use for loops to easily go through these one by one but as you guys can see there's a lot of things that you could do with arrays you just got to be careful when you use them all right so let's talk about collections a collection is a group of a number of items although it could be empty the first collection we're going to go over is a list a list is a collection with the ability to access elements by using an index so let's write a list that will hold some characters so we're gonna use vowel make character list has the variable that's going to hold this list and we're going to make that equal to list of just like how we used array of we have list of and we can just open up the parentheses and we can start defining the elements that we want to put inside of this list so we can open up some single quotes and start placing some characters within this list and i've got to put a comma right there all right so boom so now if we want to access an item within this list we just have to use character actually let me just print this out so you guys can see print ln character list dot get so we can either do dog gets or as you guys saw we can use these square brackets just like an array uh but i'm gonna use dot get because that comes with the list and we can specify the index just like an array it starts at zero and then it increments by one so if you want to grab something in the middle uh in this case there's only three so we'll just use one and so this one signifies that we want to grab y and we can press run and voila why pops up and just to show you guys that just like an array square brackets work voila why again and also we can get the size of this list so we just use character list dot size and let's run that and in this case it's three so we should see three now if you do not know if a list contains a certain element that you're looking for you can do a check so this list comes with a dot contain method that will check if your list has the element that you're looking for so we can project a list dot contain and in this case we want to see if this list contains the character a and if we run this you should see false because that is not true it does not contain a what if we were to put x and we see x is the first element and we run that we should see true we can also check for the specific index of a character so let's just copy this and pasta that and let's use index of to check what is the index of this character z and if we run that we shall see that the character is two so there's a lot of useful things that a list comes with now the one thing we cannot do is we cannot add we can't we can't add anything so unless we can't add another character if we want to so in this case a list is not mutable you cannot write to this list so we have another instance of a list called mutable list and make some space called this mutable character list and we'll make this equal to now before we had list of but if we want a mutable list we'll use mutable list of and in this case you know since we know we're going to type char we can specify it just like an array we use these anchor tags and we place the data type we want to place within this mutable list and now we can start putting in some characters so in this case i'm going to use a b and c now before character listed and give us the option to add we can reverse the list there's a lot of things we can do with a list but in this case we couldn't take a value within it and change it but with the mutable list if my mouse can start working with the mutable list we can add and in this case i just want to add something to the end so i'm just going to put d and oh actually i forgot to put the single quotes make a mistake now if i print out this musical character list and i run it i shall see a b c and d cool stuff before i couldn't do that with a just a regular list we can just we only have read only access but with a mutable list we but we are able to write to this list i'm also able to add a whole list if i wanted to so let's do that so let's take this mutable calculus and use the add all to add a collection of data to it so we're going to add another list and we're going to just use a regular list of and what's f and then we're going to add g as well forgot to add it to his own character so we're going to add a separate list of e f and g and let's copy this printout and let's print it again so you guys can see that this is possible we can add single characters we can add a whole collection of characters if we want to so now it's a b c d e f and g we got the alphabets going on and we could also let's do this right before so you guys can see we can remove the character so let's remove f and let's run this boom a b c d e and then f is gone but then we'll just go to g now if you know the exact index of the thing you're trying to remove you can use remove at so i know i want to remove the last item within this list so i'm just going to use character or mutable mutable character list dot size minus one just to make sure that i'm actually retrieving the actual last element within the list now if i run this we're back to abc d and e so if you know for sure that you want to be able to read and also make changes to the list which is right you use a mutable list but if you know you just want read-only access and you just want to be able to retrieve values from the list you use a regular list so that is the first collection in the group of collections that we have in kotlin so the next collection we're going to talk about is a set a set is a generic unordered collection of elements and it does not support duplicate elements so let's start off by creating a set that holds the days of the week so let's make this a file let's call it this i should just call this days of the week and we're gonna make this equal to set of this is what you use to create a set just like list of to create a list just like array of the create array use set of to create a set and you open up the parentheses and we can start writing in the days of the week so monday tuesday and let's write in wednesday now we're going to create another set and this is going to be for the weekend so we'll create one called weekend make that equal to a set of but i want to specify the specific type in this case so i want this to be a string because we know we're going to be holding in strings so this one doesn't specify the type but we let kotlin infer that this set is going to be you know a string because we have strings within this set and we're going to make kotlin or we're going to specify a string to let cotton know that it's going to be holding just strings so let's make this you know i like to consider friday as the weekend and saturday and sunday and we are good to go now it preserves the iteration order of what we place into this set so if we print out what we have right now days of the week and it's pronounced oops missed the whole entryway all right let's call this weekend and let's run this boom so same order monday tuesday wednesday friday saturday and sunday all right cool now now the cool thing about a set is no matter what the order is let me copy this we're going to make this the same thing we're just going to call this days of the week 2 and we're going to check does this equal this if i even change whoops i change the order is this the same thing so let me just do that put monday in the middle i should need that so if i did if i make days of the week equal to days of the week too is this the same thing let's run that it's true even if the order is you know out of order i mean a set is an unordered collection so they still contain the same values within so they should be the same regardless all right so to create immutable sets in kotlin we'll use the mutable set of function so we'll make an example that used the months within the year to see this in action so let's make this equal to the mutable set of and open up the parentheses and we're going to place march whoops got to get out of double quotes april and then may now we can take this variable which is once and we can use the dot add if we want to we can start adding in stuff within this set so we're going to use um june we're gonna add june and we're going to add july well let's see what if we try to add july twice what if we made a mistake in this case it's not a mistake but let's just hypothetically think that this is a mistake and try to add july twice and we try to print this out what does a set do with this we talked about this a little bit in the beginning but the sets allow this it does not it just completely ignores the duplicate elements it only sees this one but once the set sees oh we're trying to add another element that we already have within this collection that's not good so we'll just ignore that so that's what a set does it doesn't allow for duplicates and and just like with the list we can also use months dot remove and we can place the string which is july and if we run this it removes july so it only sees that single instance of july even though it was added twice this one was never the second one was never added and we just removed the first instance of it and just like the example above we could specify a type so in this case i'm gonna put holiday months and obviously i'm not gonna put all the holiday ones because i don't know all of them but i'm just gonna put you know november and december because you know that's usually a big a big you know holiday season type thing so i'm gonna specify this as a string and i'm gonna put november and december i think i put that right probably did probably didn't and we can add to this so another holiday what's another holiday month i guess there's fourth of july for americans so we'll just put july and then let's print this out and if you run this it works just as expected november december july and then we could even do remove at just like we did before with the list if we don't know if we know the specific index we can just remove the last element in this case i don't know the last element so i'm just gonna put holiday months and i'm just gonna take the size and then i'm just gonna subtract one just so i'm sure that i'm taking the last element and if i run that again it should just say that oh it's saying that i removed july and so if i run this again with just holiday months it should just be november and december boom so yeah this if you want to unorder a collection you don't want duplicates sets are perfect no matter if you are trying to uh if you use two sets no matter what order they're in if you make them equal to each other if they have the same elements within it they're still the same set because a set is unordered alright the next and last collection we're going to go over is the map the map is essentially a dictionary it is a set of key value pairs each item in a map can be accessed by a unique key the map holds only one value for each key so now in this case we're legit just gonna make a map we're gonna have a map that holds countries so let's create a file we're going to call this map let's make it more specific to map of countries and we're going to make this equal to map of and let's remove that for now so just like before with sets a list arrays if we want to create a map we use map of if you see the correlation to make a list we use list of to make an array we use array of and to create a set we use set of so to create a map we use map of and within the parentheses we can start defining the key value pair relationship so before we set an enlist we didn't have to create a key but the special thing about map is that it has a key so in this case i want to create a key for each country so in this case i'm just going to use characters so for the us i'm going to make it have a key of x and to assign it to a value you use to and then we create the value which in this case i want to create us so the key value pair relationship the key is x and that is the key to the value which is us now the next key value i'm going to create is for canada so we're going to make a key call and that is to assign it to a value of canada and just a last key value pair that i want to use is z and that is going to be assigned to germany so it doesn't necessarily have to be a you know string you know a char it can be a number if you wanted to or it can be you know just a character and actually i should probably should just use cactus for this you know because if i'm only going to make it x y and z then i should probably just use cactus but yeah you're just not conformed to just using you know one type for the key and the value um so so now if i print this out let's print out the map in general first map of countries and then we're going to print out us retrieving in this case let's retrieve germany so let's use map and just like when array would just use the anchor another anchor the square brackets to place in the key so in this case we're going to retrieve z oh map of countries and then boom so if we run this we should see x y and z and the values assigned to it so it's going to say x is equal to us y is equal to canada and z is equal to germany and if we just try to retrieve z uh we'll just see germany and you're not just restricted that as well the map does have the a lot of you know predefined methods that come with it so we could use get and we can use you know the char so let's create y and well let's retrieve why and in this case it should just grab canada so cool stuff all right so just like with the list and the sets we do have a mutable version for the map that will allow us to write to the map so let's just create a mutable map of countries and let's call this legit mutable map of countries and we're going to make this equal to mutable map of and in this case i know i want to use char for the key type and i know i want to use string for the value type and yeah it's low case h so before we just use one data type to specify the element type but in this case we have the key and we have the value we have to specify that or we could just like call it infrared but in this case i want to you know specify myself so i just put chart and string for the key and the value all right so now we're just going to copy this from above because you know whoops my finger didn't slide exactly how i wanted it to let's copy that let's paste that in make a life easy and let's start writing some data into this so let's just print out the regular version so you guys can see uh you know the original and then let's start adding in so i want to start adding some more countries in so let's just use beautiful countries mutable map of countries let's open up some square brackets and we can specify the key here so let's specify uh let's make this a for the key and we're gonna make this equal to max for mexico and immutable map of countries we're going to specify b and we're going to make this equal to let's see what's a nice a nice low-key country let's call this nigeria and then immutable map of countries let's make this c and then we're going to make this equal to brazil so three should be good they have the key of a b and c and if we print this out we will see that we have a difference so let's run this boom so we have x y and z for us canada and germany and then we have a b and c for mexico nigeria and brazil looks good all right so now it's time to start to you know remove things so just so you guys can see how to remove things uh mutable map for countries let's just reuse remove and in this case we have to specify the key so the last thing we added was brazil and it has a key of c and let's also use replace just so we can see that in action so we'll take nigeria and we'll replace it with dominican republic so we'll use dr then we'll print this out mutable map of countries i always make this mistake for some reason mutable map of countries and we'll run this and separate this because this is bothering my eyes it's the it's still not doing it for me but let's run this and then we shall see that nigeria is not there anymore it's replaced with dr brazil was removed from you know this immutable map and you know this is the final result that we get by me manipulating uh this mutable map of countries so cool stuff so now you guys know how to specify you know specific data types for a map you can uh make the map mutable if you want to or you just use a regular map if you just want to retrieve from the map that you created originally so yeah that is the map out of the collections right so the cool thing about collin is that by default call-in variables are non-law references which mean that you cannot assign it to null so let's say we have you know a vowel called name and it's a string and we try to make that equal to know yeah kotlin doesn't like that it doesn't like that at all it can't be assigned to a non-null type and if you guys use java you know this was a big problem because sometimes you know you would get something that holds a note and then eventually get a no pointer exception so kotlin's big thing is all about no safety and this is why by default they make their variables non-no references so if you wanted to make anything no or any variable no you have to let kotlin know okay i really do want to make this variable no so to do that all you got to do is add a question mark boom this wiggy line of death disappears and this question mark just lets colin know okay um name can hold no um as long as as long as you know that you're sure i mean you told me um by this question mark that you know you kind of know but yeah for sure like this let's call it know that um it's okay um you can hold a no in this variable so yeah no safety now if we want to check if a variable is null before we use it we could do that and that's just you know a regular if statement would do we could just use a name is not equal to no then we just print out the name whoops a name and this i am bugging out right now okay my name is and then if it is not correct then we can just print out check that again if we run this it's going to ask us to check that again because for sure name is equal to no so we just avoided so many problems um but if we just put you know geo my name is geo you know and we're good to go so that's a way that you could check if it's known or not all right so in column we have other ways for checking if a value is not or not and there are fall back to that as well so one way is using the safe call operator and usually this is used with you know things that has a chaining effect so strings are an object and they have predefined functions called methods that come with it so we can use a safe call operator to check if the string is null or not and if it is no using the safe call operator it returns null so let's just use that in action so that makes more sense um so we'll just use ln we already have a string called name and oop that's outside of it called name and we'll use a question mark and then a dot and these are the predefined functions or they're called methods that come with a string so we're just going to use length and if we run this right we should just expect three oh look i've got oh let me just comment all of this out if we run it again we should expect three right now if we were to just put no and we run this it just returns no you're probably like okay that's a that's expected i mean this is not right but if we were to remove this safe call operator see only safe calls can be called on this because of the fact that it's no and we already made this notable colin already warned this beforehand like this is this is no you might want to use a safe no operator because if we use a dot length on a null property it would cause a lot of problems so we can use the safe call operator to just return null if this is no cool stuff the next no safety operator that we can use is the elvis operator the elvis operator is a fallback that would check if a value is null if a value is not no we keep the value else we place a fallback value into it so in this case we have name right and right now we have the value of no within it so we can use a elvis operator which is a oops which is a question mark and a colon and with this we can define a fallback value just in case this value is no so if it is no then it will equal the value of the fallback value so in this case i want to just use guest because if i don't know the name of the person i just want to call you a guest because you're a guest so if we run this we should expect to see guest now if i'm geo right and i run this you know my name you shouldn't put guess this should be geo because you know my name is not no and that is the case so the elvis operator is very useful if you know that it's going to be no but you also have a fallback value that you want to put just in case it's usually better to use this than the safe call operator in my opinion because it's to me i feel like it's better to have a fallback value instead of you know just placing null everywhere because the safeco operator just returns low so yeah that is the office operator you just use a question mark and a colon and a fallback value and attached it to a variable that may or may not be no alright so the next operator we have is the not no operator this is an operator that you know is two banks it converts the value of a variable or a value to a non-null type but if it is no it would just throw an exception so i really don't see much of a use case for this but uh maybe if you want an exception if you want to catch an exception just in case if it's no but um let's see this in action so let's have a vowel of y and this is going to be a string that accepts no so we're going to make this equal to no and we're going to have another vowel that holds you know that is x oops forgot to put on another line typing skills is trash we're gonna have another vowel that is x and it's gonna be equal to y bang bang bang bang so if we run this right there should be an exception and there it is the kotlin null pointer exception so it sees that y is no so if we put the bang bang it just throws the exception nice stuff if we were to place you know let's just use a string called value and we were to use a bang bang on it it just makes this y into a non-null type so let's just make this a string no errors now if we run this or actually no i didn't print it but no errors right but if i didn't have this bang bang on it it would be a problem because of the fact that it's a mismatch one is just a regular string which is a null null reference and the other is a a string that accepts no but if i use the bang bang or the two estimation points um it accept or it reverses the type of y it makes it a non-null reference only problem is that if this is null then it will throw an exception if i run this again it will do an exception so you have to use this wisely all right so in kotlin we have type checks and automatic cast so the first type check we're going to go over is the is and not is in java we had a thing called instance of or we have a thing called instance of which checks the type if the value is a of a certain type so it's the same in kotlin is is to check if an object or a value is of a certain type so to do a quick example we can first let's create a variable and we're going to call this thing name because i like names we're going to call this name joey we can use this name and we can check if it is of type string and if so nice name and then we'll just put name so if the value is a string then we're good to go obviously even beforehand if we try to use another value it's just going to straight up tell you that you know it's incompatible with a string and an int so you could use this with um you could use this with objects to check if an object is a string but we're not going over objects just yet i'm going to go over objects in the future but is this used to check if a type or a variable or a value is of a certain type so pretty convenient um we could also we also have a negated version of that um so it's legit just adding a not in front of this and if name is not of type string then we'll do something if not now we have else case and we'll just print out it is a string so boom so we could check if it is a string and if it is not a string in this case it is a string so we just catch that in the else so different ways to check if a value or a variable is of a certain type just like instance of in java
Info
Channel: TechWithGio
Views: 372
Rating: undefined out of 5
Keywords: Kotlin, Full, Beginner, Tutorial
Id: TE6e_6NGOd0
Channel Id: undefined
Length: 169min 48sec (10188 seconds)
Published: Mon Apr 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.