Learn Kotlin From Zero to Hero in 10 Hours

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello my friends and welcome to the complete kotlin developer course in this video we are going to make an overview what we are going to learn through the course so first we will start by the basics of kotlin how to set up the kotlin development environment we learn about expressions variables constants and control flow next we are going to move to the functions so we will learn about functions and nullability next steps we are going to the collections and lambdas so we will learn about the arrays and the list the map and the set and we learn about lambdas also we will go to the advanced things about advanced topic about oop object programming we learn about the classes objects properties methods and enum later we are going to learn about the genetics the genetics and the interfaces and we will move to the exceptions how to handle the error and how to prevent exceptions and how to make exception zones we will learn about the functional programming so we will learn about conventions and the operator overloading moreover we will move to the core routines learn about the kotlin core routines and these special cases and their scripting with kotlin and we will move later on into the databases so we'll learn about the firebase connection and integration learn about room database and preferences last we will learn about real projects so we will build real applications we'll learn till now we have finished the course about the e-commerce shop application and food recipe application and maps and tracking system application and much more we are making also more and more apps real projects in code in kotlin so we will use core routine we will use databases firebases uh firebase oop functional programming exception and all these concepts inside these applications so till now we have these apps uh and we are going to move and make new uh videos and new applications through the course like no other course over internet we have carefully created this course to maintain and to cover all the details that we will see in the kotlin so with lectures and new details and with real examples like this app we will make and ensure that you understand every concept and make real apps and publish them on play store hello my friends and welcome to the complete kotlin developer course kotlin is a programming language introduced by jetbrains in 2011 the official designer of the most intelligent ia java ide named intellij idea so we have worked with uh with it with the ide intelligent ide in the java developer course this is a strongly statically typed general purpose programming language that runs on amp java virtual machine in 2017 google announced scotland is an official language for android development why cutler why i should learn cutler kotlin is an open source programming language that combines object-oriented programming and functional features in into a unique platform so kotlin is getting high popularity among all levels of programmers and it is used for cross-platform mobile application that we are going to use later on uh like as in android studio android application application development also the cross platform which is also to be used in ios and the cross platform for the for the desktops and other things the web application development server side application desktop application development and data science based applications it's very and it is you widely used coding ide the primary tool that you will see in this course is to create custom projects in intellij idea from jetbrains okay so ide stands for integrated development environment or ide and is similar to other ides such as virtual visual studio and xcode if you have worked with one of these um like a just a a comparison and just an overview how ide write the code and work with the code you use an ide to write code in an editor compiler in the in an editor compile the code into a form that can be uh run on your computer see output from your program fix issues in the code and much more this is the the work the job of the compiler and ide source code a file contains your programming code the compiler source code goes through the compiler output compiled code with the class extension file name and the virtual machine and electronic gadget execute byte codes okay so this is a quick understanding for uh in kotlin and its history now let's go and install intelli j the first step to install it the intellij idea go to slash jetbrains.com okay and this is the most powerful java and kotlin and also android ide in integrated development environment so these are the specifications and you can scroll down and see what are the features okay we scroll down and let me there are two versions of intellij the intellij idea ultimate and the community edition you see that these are included these are included in both of the versions but these are not included in the in the community version it is included only inside the ultimate ultimate you should pay and it is limited but for our uh version for our course also you are going to use uh java kotlin groovy scala android maven get good debugger docker and we click on download for the community section and edition okay so after downloading we will start download so after completing the steps of installing intellij and follow the installation instruction on the intellij side uh please don't run the intellij id so this is the integer idea and you don't need you don't run it for the first time because we are going to install something called jdk so kotlin allows you to program several different platforms the two most prominent platforms are java virtual machine or jvm and android and instead of compiling to native java and machine code on each platform java programs are compiled into a format called bytecode and this is uh that is called and achieved by the virtual machine and the virtual machine is including the gdk so the easiest way easiest way to get the jdk from the platform is to visit the oracle site and slash technologies java se downloads or you can put simply on google download jdk okay you get this and click on jdk download okay you wait uh till appearing the download page and you select your operating system a specific package okay i will install this because it is x64 i have reviewed and download jdk okay it's about 150 megabytes click start and go the installation process of jv and jdk and after finishing installing jva jdk you will run the intellij idea and you will get this screen so our main purpose is to create a hello world um so once you have installed intellij idea and the jdk follow the normal process of starting the intellij by clicking on it and go ahead and choose create new project and a new screen will be appearing here one thing uh that you need to specify is a project sdk you can put if you have the latest version of java please always select the the latest version of the java and before since we have installed the java jdk so you can specify the program files java jdk you just ensure that you just ensure that you have the latest version of jdk and install it and select the path that your jdk have been installed for now i am not going to create java i'm going to create kotlin so here we go we have many many types of kotlin files that we can create and we can start by jvm id okay so clear create next click next the project name you need to specify the project name hello kotlin i will create the name and here is the project location i will put it on the desktop the project sdk as i told you if you don't under if you don't download and if you have done not downloaded the jdk please click on download jdk and it will parse the latest uh jdk okay it's very simple or you can specify the jdk from here okay use library and click on finish okay it's very simple and here we go it's configuring kotlin and wait for the progress to be installed okay and here it is a configuration notice and if we click on the file it is similar to that that we have seen in the java course so if you want to learn more about intellij please go back to our [Music] to our course java course or you can follow us here for the basic kotlin and basic intellij structures for the first thing we have the project panel this is the project panel i can select the project packages scratches open file but we will always use package of the project and we see here the hierarchy of the files the external libraries and but we are going to mainly work with src folder it is in blue okay this is the project panel you can click on this and you can hide it and show it this is the toolbar that we are going also to be used and to run a debug and get the configuration and here is the editor this is the editor okay i will create the first kotlin program by creating the file i will name it as hello click okay and you see guys it is dot kt which is the kotlin okay um always update the plugins to the latest version after this video i will update them but till now i have this file called hellocate.kt.com and i will i need to create don't don't uh be frustrated just follow me and you will you will be professional in kotlin so arrays of string we are going to go deep in every single thing and every single notice and concept in catlin don't worry hello master coding family okay you see guys we have created this function in inside the hello.kt file and there is a green uh flash and that it is indicating uh triangle run hello kt i click run and you see the console will be appeared here telling us hello master coding family so this congratulations you have make it uh you've now created your first kotlin project and your you run your first gotland kotlin like most other programming languages allows you to document your code using what are called comments these allow you to write any text directly alongside your code which is ignored by the compiler so the first way to write the code the comment is like so i inside this i can't write here hello this is the first comma so if i click run this line it will not be executed it is just a comment so you see guys this is the hello master codingham and this is the line only one line it will be executed and not this line so this is the first way to write the comment also the second way is a better way to write comment which is span multiple lines we can put a forward slash and put asterisk put like this and you can go hello hi what's up um and much more so in this way we are creating the comments for the multiple usage okay kotlin also allows you to nest comments this will be not useful but this way we have learned how to create the comments and how to make the comments inside the kirtland file at its simplest computer programming is all about manipulating data remember everything you see on your screen can be reduced to numbers that you send to the cpu sometimes you yourself represent and work with the data as various type of numbers but other times the data comes in more complex forms such as text images and collection in your kotlin code you can give each piece of data a name you can use to refer it later the name carries with its with its an associated type that denotes what sort of data the name refers to such as text number or data so you will learn more about uh some of the basic types in this lesson and you will encounter many other types throughout the rest of their course so let's start by constants look at this code we will start by val number and equal to 10. okay we don't use semicolon just put like this so this uses the val keyword to declare a constant called number which is one of the type integer then it sets the value of the constant to the number 10. thinking back to operators here's another one equal sign is an operator uh operator sign that we are going to here to take in the next videos but till now just learn how to create a constant variable we start by val number in and two points we specify the type of this value a variable and equal and we give it a value the type and can store integers so in is for storing stores integers okay the way you store decimal numbers is like this so val pi double equal to point fourteen fifteen nine okay so double stores decimals you see i'm using comments here guys in order to allow you understand better code and you write your own notes for understanding the code okay this is similar to the end constant constant except the name on the type are difference this time the constant is double and the type can store decimal with high precision there are also a type called the float so i can create float here then k float equal to six point test number we have an error and we need to specify f so when using f load we need to specify and tell the the compiler that this variable is a float type by specifying f letter at the end what does the what float and double differentiate there that stores short for floating numbers this float is a short for floating point the stored decimal with lower precision than double so always use double for higher precision and float for small and small numbers double you can put many numbers here but float small small pieces of uh of numbers after the decimal point so in fact double has about double the precision of float which is why it's called double in the first place so double decision double decision or and of the float point a float takes up less memory than double but generally memory used for numbers is not a huge issue and you will see double used in the most places even though we call an item created with eval a constant it's more correct to say the identifier market with val is what a const once you have declared a constant you cannot change its data for example consider the code now a number and equal to the 10 i can't put here that number number equal to an hour to 10 at 2 50 to 11. i can't put like this it give me an error what this error change to var it's telling me the to change the val to var so val is valued of all the constants and var is a variable okay so once you val is declared i can't make a change for it so because this is called constant so constants are useful for values that are not going to change for example for example if you if you were modeling an airplane and need to keep track of the total number of seats available you could use a constant or if you have a constant in mathematics like pi 3.14 15 9 you can use a value of constant and create as pi so in certain situation for example at the top level of your code at outside of any function you can add the constant keyword to val to mark it as compile time constant so i can put here cost okay at the top level of any method we need to specify it outside any method but till now we will later on we will talk about constant and the non-access modifiers and high in the oop section but till now you understand the variable the values and the constant of uh and the the constant data type in kotlin and we are ready to go through the variables in the next lesson thank you guys hello my friends and welcome to the complete kotlin developer course we have learned in the previous video how to create constants inside kotlin but often you want to change the data behind the name so for example if you were keeping track of your bank account balance with deposits and withdrawals you might use a variable rather than a constant if your programs data never changed then it would be a rather boring program but as you have seen it's not possible to change the data behind the const when you know you will need to change some data you should use a variable not val so variable nothing not use it when you change data changes not don't use that just you create var so these are the variables and throughout the course i will comment everything in order to ensure that you understand everything by comments and you check the source code by downloading it from the description in the resources folder and this documentation all of this documentation can found at master coding master coding app on play store so don't miss to download it to get all documents okay now variable let me remove this and put it outside and the variable we have var we start by var it is indicating a value so we put the variable name or the variable number that i am going to put the type of it equal to 99 okay i have created a variable called variable number i put it of type integer and specify the value it is equal to 99. only the first part of this test the statement is different you declare constants using val whereas that you declare variables using var once you have declared a variable you are free to change it to whatever you wish as long as the type remains the same so for example to change the variable declared above you could do this variable name variable sorry variable number equal to 100 i can change it and can do it exact but i can't use variable number number equal to hello you see guys there is an error let's see hover your mouse to see the the error type mismatch required and found the string so it's telling us that you it should be an integer of type integer so i can't change it again to 55. this is the the meaning of using variables and this is the power of using uh the variables now uh let me show you one trick always try to choose meaningful names for your variables and constants to ensure that you memorize them when creating a big softwares good names can act as documentation and make your code easy to read a good name specifically describes the role of variable or constant here are some examples of good names okay so i can create personage person each van it will indicate for every person for every coder that this variable will will give us a person age it's very simple or you can create a number of students this will indicate for us the number of student as a variable okay or the mark mark or grades grades for student okay these are the naming the good naming of uh and meaningful names or you can create v1 but v1 it may refers for the age of uh of the person but how you will remember that i advise you to put the age of person or person start always with small letter person h and you put the other word with the capital letter this is the pascal writings okay this is very good example about creating the variables you can create also variable of type string of type type string i will call name name person of type string equal to john you see guys the first part is just as [Music] as the constant is changed from var to val and the second part is the same as before okay so i can also create value of constant j you see j it will not indicate anything for me or for any for any coder later on it's just uh saying hello and it is constant while in the variable form we just defer it by var and also i can't change the name of person from from variable from john to anything i need to change it later on also i can create a variable of type float boiling point boiling point f type float equal to 3.45 and don't miss the f okay it's the same as constant but it differ in from var from val to var okay thank you guys see you next videos what's up guys and welcome again to the complete catalan developer course where in this video we are going to talk about the type conversion so sometimes you will have data in one format and need to convert it to another so the name way to attempt this it would be like so so var x of type and equal to 7 and i create i will create a new variable called y of type double equal to 3.40 okay now x equal to y this will give you an error because type mismatch required and and found double i am assigning x equal to y and y is double and x is int so i am putting a double inside in this says it will this will give an error okay if i put y equal to x this will not give an error because double is called i am putting a double here and inside this double uh box i am putting an integer an integer is smaller than double and it is contained in double so in this case it will be accepted okay so instead of simply assigning you need to explicitly say that you want to convert the type you would do like this integer when you need to put when you need to convert the type converting the type integer you use this formula integer equal decimal dot to end method okay we will learn about method later on but till now if you need to make a casting you need to to like type conversion it would be sometimes it is useful to check you the inferred type of the variable of a constant so and convert it so we will create x which is an integer equal y dot to end so we use this method okay i will commend this let's run and see i will print it out x print x and here we go guys we have eliminated the decimal part from the double that we have it in our y variable so we convert the double into end so no problem between converting from double to from into double and from n to double like we have seen in y equal to x and but there is a problem while converting from uh and double to integer okay so double cannot be contained in integer except if we use y dot and method we'll learn about this writings later on but till now just understand the relation between the primitive data and data types and we are going to to see later on the methods and other things just one another important note in kotlin that sometimes it's useful to check the inferred type of the variable or constant so if i create like 100 or 1000 line of codes and despite searching for the type of x i can put here i will buy you can do in intellij by clicking on the variable or the constant name and holding down the ctrl shift p shift p okay you can see how you can make and you at the invert type is integer so you get an message telling us that x is a integer okay this can be do by done by ctrl shift and p you see the expression and you get the integer the data type well primitives require less memory which means they are better for performance but they are also lacking some of features of integers so the good news is in kotlin you don't have to worry about whether you need to use a primitive type or an object type like in java so kotlin handles that complexity for you so all you have to do is to use an end you have been using double to represent the front fractional numbers kotlin offers a float type which has less range and precision than double but requires have as much storage so we have the four here the the six primitive data types uh of of numbers that are requiring the the and that we have seen in java so byte is the minimum values minus one to eight the maximum value that contain is one two seven storage size is one bit now the short is from this number to this number maximum values and the storage size is too big the end see guys the end is from this number this big number to this big number and its storage size is four bits the lung is bigger than end and it's the longest one from this number to this number and its storage size is eight bits the float data type is -3.4 exponential from this minimum number value to this maximum value precision is six digits after the decimal point storage size is one bit when the double is from this number to this number 15 digits storage size is two so let me go to the to the ide and we will learn now about the numbers we have seen the end and the val and a of type short equal to 12. i'll create some variables val then b of type byte equal to 120 we see that the range is of short is less than the by i'll create as s val i of type end equal to minus one thousand or what ten thousand i can create a val answer equal to a plus b plus or s plus b plus i okay uh just before talking about the answer the first thing that we have learned is how to create the variable in the variable uh section and we have introduced the numbers uh that they are short by end and long i will create also the long and create event about the float so long i will long equal to like this number i can create a variable of ty of type float equal to like coin f and val d of type double equal to point okay just in the float you need to specify that it is a float by putting f okay at the end of the number okay it's very important these are the numbers that primitive data types that are dealing with numbers that you can con create in kotlin okay now just a very uh interesting thing in kotlin that if you create a variable it is not it's not of known type i'm not specifying that it is a long flow type or end byte or short and you assign the s plus b plus i s plus i b plus i i am adding 12 plus 12 plus 120 plus minus 10 000. what do you expect from kotlin thanks for catlin for creating a special variable at the corresponding variable of these things so what i mean by that i will print now answer okay and let's run and see it will do the calculation and it's minus nine eight six eight it's not a short it's not a long sorry it's not a byte it's of type integer because if we go back to the to the documentation we see that this number is included inside the end range so automatically the kotlin will create this answer and it will create it of type in so if you hover the mouse over it val answer of type and you see guys val answer of type so we have learned about numbers in in in kotlin now next step we are going to talk about the characters and later on about the strings so for that i hope you stay safe and see you next videos to represent the character in kotlin card types are used so we have learned about the numbers and we are going to learn about the character so unlike java card types cannot be treated as numbers okay so we have learned in java if you will go to the complete java developer course you will see the character video and you and the character lesson and you see that characters are dealed according with an integer and as integers in the ascii table while characters are not dealed inside kotlin so in order to create a variable of type character we start by a variable or you can specify it as a constant val i will start by constant letter of type car okay you specify it you could make like this letter equal to k or you can directly put it like this i prefer using this okay now print print what is the letter that we are going to print you can print the letter like letter let's run and see you see guys it printed from the character okay so in this way we are creating and using the characters you can't specify here the unicode of the of uh of the letter like like this in java okay you only can put their characters like these now and only one cat okay in java you can do something like that like you can specify 65 which refers to a capital a but in kotlin it is a prohibited okay this is the character and it is not of double quotation it is of single quotation and you specify one one character and not to these are the characters you may uh like encounter this this data type but it is not very useful but we should learn about it because sometimes you may use this we have learned about the flow the numbers and we have introduced these six types the character variable and we are now going to learn about the boolean so the boolean data type has two possible values booleans have two possible values either true or false okay so i create a variable a value a constant value or a variable by the same i will call it as flag equal to true in this way i can create a value or i can specify its type by boolean okay now print in the same way i will use print flag okay let's run and see and here we go we have printed the bull so in this way we are go we have created the boolean type uh variable called or a constant called flag and we assign its value to true you can assign either true or false okay so in this way we we learned about booleans booleans are used in decision making statements this will be the discussed in later lessons about the loops and the conditional statements so don't worry we are going to talk about them in deep later on and we encounter this booleans much more inside our videos what's up guys and welcome again to the complete kotlin course in this video we're going to talk about the strings in kotlin so we have seen deeply the strings in java but if you are like a newbie or a beginner and you are learning directly um kotlin we are go you are coming to the you are coming to the right place so kotlin like any good programming language can work directly with characters and strings so in order to create a string a string has several characters like as a variable used for creating variables and and constants of style of type string that contains words or phrases or any other things so but strings are characters sorry our classes and we will learn about classes later on in the oop section of kotlin but till now we are good to know that i can create strings a constant string or variable string in the same way like this so string a type string equal to hello master coding channel also i can print this string by typing string a let's run and see here we go i have printed from the previous section hello master coding chat okay so in this way we are specifying the data types and we have learned about the strings but there it is as simple as this the right hand side of this expression is known as string literals we are creating a string literal this way string later on okay we will talk about strings later on when we talk when we learn about the oop and of course type inference applies here as well so if you remove the type in the above declaration the kotlin does not or does the right thing so if i remove the string or if i remove the boolean or character or double or anything java or sorry kotlin will do the right thing and understand this that this variable or this constant would be a string a boolean character and thing so this is called type inference okay but i should learn you the academic way should i should you start from zero and we will learn later on by removing this okay now i should talk about something in the strings called concatenation conca nation you can do much more than create simple strings sometimes you need to manipulate a string and one common way to do so is to combine it with other string in kotlin you can do this is a rather simple way by using the addition operator just as you can add numbers and you can add strings okay what does this mean i have created a string called or i will start uh by creating a new string string b of type string equal to high and second value of a constant called string c of type string equal friends okay these are two separate strings i can write like this print print string a or string b sorry string b plus string string c in this way we are concatenating the two string exactly like in java we can concatenate the and here you can concatenate the two strings so i will put here and n and print ln which is referring to print on new line this so you see guys that we have concatenated the two strings we have displayed high and friends inside this one single line by concatenating the the first thing and the second string you need to declare you can declare a variable here also like val string d of type string equal to string b plus string c okay you can do this and you can remove and let's run and see it's the same result you we have created a new string value of a value new constant and inside this constant we have getted the um the value of string b and the value of string c and we have concatenated there okay you and uh there is another important thing that we can talk about it in the strings it's called string templates so string templates you can also build up a string by using string thumb string templates which are which use a special kotlin syntax that lets you build a string in a way that it is easy to read let's create a value or the using the same string val string k of type string is equal to equal to hello i can put here a plus sign also this is also a called concatenation my name is so i am not talking about the concatenation you can concatenate the two strings in the same way now i am going to talk about the string template as i told you so i will create a vowel string and of type string i prefer writing like this to understand kotlin very well and to the the academic way the academic writings so i will create jack as string name and hello my name is i can put dollar sign and string you see guys i am putting the string here without specifying the name check like this because if i change jack i need to change here also jack so this is very useful thing in kotlin i need to print print string k i need to print k string okay so let's run and see and here we go hello my name is jack so kotlin will put a go and search for string and get its value and return it back so consider if you want to change it to a bb as a bus let's run and see and here we go it is changed automatically so this is string concatenation string template and the string class that we have talked about in this video my friends in this video we are going to talk about the per and triples in kotlin sometimes data comes in groups an example of this is a pair of x and y coordinates of a 2d grid similarly a set of coordinates of 3d grid is compromised and comprised of an x value y value and that value so for the per there is like a coordinates of x and y and while a triple think about it having three dimensional point which is x y and z in kotlin you can represent such related data in very simple way using a pair or triple other languages use a type named tuple to hold similar combinations of value like python okay so pair or triple are types that represent data composed of two or three values of any type if you want to have more than three values you can use kotlin calls a data class which is a which we will cover in the future uh tutorials okay so how to create like these variables in kotlin i will commend them sticking with pair for now as an example you can define a pair of 2d coordinates where each x's value is an integer like so so val we start by variable which is val or a variable var or you can make it as constant which is a value okay till now i will create a constant coordinates which is a value coordinates i will name it coordinates i can give it it's type it is of pear so you see guys this is the pear in kotlin and we need to make this shape this uh like this diamond diamond shape and we specify here that the data type that it would take so the x would be an integer and the y would be of type integer and we are going to cover this in later on in the generic section so how to make them them and why we are using these data types but till now you need to know that we can create a pair in this form so val which is a value a value of name coordinates it is of type data type pair having an integer as x and y as integer this is uh for just for uh demonstration guys but and uh for uh like making it simple okay equal to pair surpair off and we put here the values of the pair that we are going to make okay so to print them we can use printl and the coordinates and we put here coordinates okay you can convert a string concatenation to a template that we have seen before okay let's run and see and here we go guys the coordinates two and three the type of coordinates paired in the type of values within the pair is operated by commas and it's surrounded by these two angles and these are called the diamonds so inside the diamond we have the integer the data type the first the pair and the second part the second number inside the pair is of type integer and you can specify any data type here okay you see guys any data type like string and and many other objects that you can create later on the code for creating the pair is much the same with each value separated by commas and surrounded by parenthesis okay so we have created it and thanks for the type interface can if that can infer pair types too so i can create this by removing the pair okay and the two points so i can create directly a value echo a variable also of uh name coordinates and directly i can specify its data type by pair and two three this is called the type interface okay type interface but we like to learn the the basics and the academic way and later on we can like use the type interface okay so coordinates one this is two okay so i can create a variable like this and by using type interface that is related to kotlin and without specifying the pair again of data type it can directly get it and specify its data type you can make this even more concise with which kotlin loves to do by using the two operator okay so i can create here the same variable the or the same constant coordinates three coordinates three equal to two two three this will make the pair this is using using two operator okay so i can refer this i can make a pair two three by this that's the academic way this is the simplest and basics one and i can create the same variable or same constant or the same data variable with directly specifying using a pair using the type interface and i can create also in the same way two two three so i am using two operator this is four pairs in kotlin you could similarly create a pair of double values like this so i can create braval like coordinates f4 equal to pair and i specify directly 4.1 3.2.4 this is very very advanced and very smart kotlin um helping and feature that it can directly specify and create and tell itself that hey he is creating a pair of doubles okay in the same way and you can mix them by specifying an integer here and and a decimal here so also kotlin will know that you can you created here a decimal and here a integer okay and here's how to access the data inside the pair so i can create or the coordinates or the x coordinates let me check the x coordinates coordinates dot and i can you see guys there is first and second so it appears in this way i can access the coordinates which one i need the first one or the second one i need the first so let me run and see to check and here we go the x coordinates of the first variable is two and it is two let me check the second okay and here we go guys we have three here okay we have discussed in the previous video the pair in kotlin and we talked about it as x and y relationship and a point in a 2d space system now let's talk about triples it think about triples as a coordinate a point having a coordinate 3d coordinates x y and z so these are the three values of a triple and from here it is named triple containing three triple values how to create a triple so we start by the academic way the basics the basic so coordinate the coordinates 3d equal to i can this is equal triple a b c i specify here i need the data type as i told you and and i am making a triple of three integers and i create triple one two and three okay this is how you create how to create a triple inside cut so let me print it so print ln the 3d coordinates you can make the concatenation string concatenation or you can use the the temp string template by specifying the dollar sign and we have talked about it inside the string less and here we go the 3d coordinates are one two and three in the same way i can create val you can create var or val the same syntax but the coordinates of 3d or the dna 3d and the this is that second one equal directly a triple okay so you specify directly it is a triple four five and six okay so in this way i can create a coordinate at triple coordinate so i can print it again in the same way okay so this is the first time this is the second type how to put by specifying by using using um like type interface okay you can specify it okay this is how we can create a triple inside cut now how to use the data inside a triple i can create a variable here or a constant because we have created a constant here and the constant would be x or 3 equal to or x equal to coordinates 3d dot first i can create get the y separately coordinates second and val zed equal to coordinates dot third okay you see guys that we have get we have get the coordinates uh of 3d this is the first element the second element and store it in x and y and the third y at the third element stored in z okay so in this way we have get the data create three new constants and assign each part of the triple to them in ter okay so this is how to get the coordinates and data or the triple data in form of variables or constants or you can directly print anything like x or z of the names we can specify priority needs dot third okay and here we go the z coordinate is three see guys this is very simple and this is that trip about the pair and triple in cutler what's up guys and welcome again to the complete kotlin course in this video we are going to talk about three types any unit and nothing these words these keywords they are sometimes useful in cutling and we are not going to use them basically or or every time frequently but we should know about them because maybe you will encounter them inside your during your process of coding so the any type can be told thought of as the mother of all other types except nullable types every type in kotlin whether an integer or string is also considered as any so any see guys this in this card in kotlin this is uh covering or including the the street the end the string or anything except the nullable type so every every type in kotlin except now okay except no okay so this is like the object type in java which is the root of all types except the primitives for example it's perfectly valid kotlin to declare an int literally and string literally as this so using uh as any like so so i will create any number this is called any number and instead of using end i can specify any so it is 42 and i will create a new constant val or you can use var but i will use val any string i will name it as any string and i will give it the any type and here it directly understand that the data type of any string would be a string so any here refers to a string and any here refers to an integer okay this is very simple but we are not going to write like this because this is not a professional we use the type interface to directly use the val and the constant in declaration okay so this is how we use the any okay we should understand it now in order to use unit there is a type called special type called unit so unit is a special type which only ever represent one value the unit object which is deferring the one unit object a unit object okay this is very important it is similar to void it is equal to void similar to void in java except it makes working with genetics easier every function which does not explicitly return a type a string returns a unit so anything that does not return anything like data types strings or integer it returns unit for example i will create a function later on we will we will learn about the functions so but they'll now understand this add here and i will create a val [Music] a result equal to 2 plus 5 and print ln result this function simply adds two plus two or two plus five and prints the result but does not actually return anything not returning anything guys it's just printing so the return type unit is implied so the above function or this function would be the same as writing like this okay so unit refers as a void in java so unit is equal to unity to void in java so this function you can write this function without putting anything and this will just print the result and for demonstration for using the unit keyword or unit type it is not returning anything this type correspond to the void type in java okay this is how to use the unit now nothing type nothing is a type that is helpful for declaring that function not only does not return anything but also never completes so not a function is never completes what does this mean this can occur is if the function either causes the program to stop completely by throwing an exception or if if it simply goes uh on forever without ever finishing how to do that or what is the example or what does this means i will create a function here do and nothing for do nothing and if i specify nothing here this is the keyword it will make i will create a while loop true don't make anything inside this while loop okay you can cover while loops more with in the next chapter we will cover them in next chapter by the basic control flow but for now understand that this function will simply run forever without ever returning anything so welcome to the land of nothing it is just telling us that this will not make anything and this function will not make anything and it will not return anything so nothing it is a function that that it is return nothing do nothing okay so this is the data type this is the returning types or types in kotlin that should we that we should know any unit and nothing so we will not use them frequently but we should know about them when writing a computer program you need to be able to tell the computer what to do in different scenarios for example if we open the calculator we need you should the calculator app would need to do one thing if the user tapped the addition button and another thing if the user tapped the subtract subtraction button so 5 minus six is different from five plus six so these inputs and these are the operators and these will control the control flow the cont it will control the flow of the program so in computer programming terms this concept is known as control flow it is also named or it is so named because the flow of the program is controlled by various methods in this lesson we will start learning about decision making and repeat tasks in your program by using the syntax to control the flow so you will you will learn uh also about the booleans that we have learned that they represent true or false values and how you can use this to compare data so we will start by the comparison comparison operators compile result operators okay you've seen a few types now such as and double string here you will learn more about another type one that will let you compare values through the comparison operators when you perform a sub comparison such as looking for a greater of two numbers the answer is either true or false okay so kotlin has a new type called boolean and we have seen it in the previous uh previous lessons but just a demonstration about the bullions in kotlin so val yes variable f type boolean equal to true okay and val no variable i will name it as like this but evo boolean equal to false okay so i have created two variables the two constants of i will name it making them as variables okay so yes variable or no variable of type boolean equal to true and the other is false so booleans only have two type two values true or false okay and because scotland's type interface you can leave like this you can like directly put its value true or false a boolean can only be either true or false denoted by the keywords true or false in the code above here you use the keywords to set the state of each variable now we are going to introduce another thing and use the booleans inside the boolean operator so boolean operators so what are the boolean operators booleans are commonly used to compare values for example you may have two values and you want to know if they are equal if they are equal then true and if they are in so it is false so how to do that by using the equality operator val constant val does we will name it as does one equal to equal to this is the name of the variable equal to and here i can assign and check if one equal to two guys look at here one is it equal to two this is me this is the meaning of is it equal so boolean expression can can be simplified this is simplify boolean is equal to false so this is the direct answer of this boolean expression but the boolean expression here is telling and the checking equal equal sign this is for testing this is for testing equality so kotlin infers that does one equal to two variable is boolean as a boolean clearly one does not equal to two and therefore it would be false okay so this would return a false if we go and try to print does one equal to two and run the program it will return for us false so let me run it may took sometimes some time when running and it is equal to false so we have learned that the equality here the equality operator is testing for one is it equal to two it is false so it returns a boolean so these are the boolean operators these are boolean operators the two equal sign if i put one equal to it is very obvious it is wrong because i cannot assign this is assignment or not expression but if i put to operate to equal sign it is a boolean expression and it is an equality operator denoted by two equal signs okay now if you need to check if they are not equal so how to do that it is very a very good operator to check if it is not equal so we start by exclamation mark similarly you can find out if two values are not equal using the one the does not does not equal sign which is represented by exclamation mark and the equal sign okay now let's run and see and here we go guys we have it is true so 1 does not equal to 2 so it is a boolean of true and it returns true to the system okay this time the comparison is true because 1 is not equal to 2 and the prefix exclamation mark operator also called not operator this is the not operator toggles true to false and false to true for example if i need to change 1 equal equal to 2. this is false and i can put here the not equal the not operator not false this is the the answer of this would be false but not here not false it would be returning true let's try to run and see how it works and it's going here to display true so this is the not operator here we have learned about operators and the boolean operators and the two more operators let you determine if the value is greater than you can check like this greater than this 1 is it equal to 2 a greater than 2 greater than or equal to let's run and see it will return absolutely false okay because 1 is not equal to false 2 2 and 1 is not greater than 2. okay you can use also the less than this is the less than and you're less than or equal let me run and see and it will return true okay because it is less than two okay so this is less than sign operator this is the less than or equal sign this is greater than and this is greater than or equal sign and this is equal sign is it equal and this is not equal sign these are the six operators that we are using inside the boolean operators these are the comparison operators you can see check them comparison operators okay so it's uh very simple to understand this because they are logic well hello my friends and welcome again to the complete kotlin developer course we have started learning about the control the basic control flow and cut and we have learned about the comparison operators and we have learned about the six signs these are the equal equal checking for the equality does not equal the less than less than or equal greater than or greater than and equal okay so these are the six operators each of the examples above test just one condition thanks for george boole this is the event who invented bullions he had much more planned for it than these humble beginnings so he invented boolean logics and the boolean boolean the logic operators which lets you combine multiple conditions to form a result one way to combine conditions is by using and and or operators when you end together two ball booleans the result is another boolean if both input booleans are true then the result is true otherwise therefore the result is false how does this mean in kotlin in continent the operator of four boolean and is and and and operator is equal to double end okay and the full the or operator denoted by two vertical slashers okay so let's start by the end in kotlin the operator for the boolean as i told you it is denoted by two and signs so i will create val called and the constant equal to true and true so this in this case it will be true if either of the one of the values on the right was false then and would be false so true and true true false and false false true and false false false and true false let me run and see so this you can see here simply you can see here guys if you hover your mouth mouse boolean expression can be simplified simplify boolean expression and it is true now let's check false and false okay if we simplify the boolean expression it would be false so false and false equal to false true and false if we hover and simplify it would be false and again again false and true would be false again okay so true and true if we print it it will give us the same result so print and constant okay let me run and see and the check i will commend this from the previous exercise and it will give us true so if we change false here let me run again and see it will give us absolutely false okay so this is the and operator this is the logic operator of and okay another way to combine conditions is by using the or operator so i will move this to here and let me see so the logic operators the and and we have take an example about it and the or operator i will create a new constant here called or constant equal to when you or together two booleans the result is true if either of the input boolean is true only if both input booleans are false will result will result in false so in cut in kotlin the operator for boolean or is two vertical slashes like this so true and false as i told you when checking for the or equality the or operator and the logic if one of the two is false we can get it as one one the one of them is true is true true or false false the true true false or false it will give us false false and or true it will give us true and the true or true absolutely it will give us true so this is the usage of true in this case or will be true if both values on the right this is that the true or false if both were true then or would be true and we have sent seen this okay now in kotlin boolean logic is usually applied to multiple conditions maybe you want to define to determine if two conditions are true in this case you would use end and if you only care about whether one other condition is true then you would use or okay for example consider these codes i will make as exercises here val and the true equal to one less than five and 15 less than 100 what do you think about this one less than five it is first of all it is a constant called and true it will it have an and operator so it will con it will compare the condition the boolean this it will end with the boolean from the left side and the boolean from the right side so what is the result the boolean result of this on the left 1 less than 5 yes it is true so i can put it true 50 greater than greater than let me make it 10. 50 greater than 10 yes it is true so true and true it will give us true so let me allow kotlin to make this calculation for me simplify boolean expression here and it will give me false because i have made 100 again simplify it is true now if i put 100 here it is false here and true here true and false it will result in false okay now if i use the or true or false it will return for us true okay so this is the functionality this is the logic how uh how kotlin will work and how kotlin separate the conditions and compare them by using the logical operators well hello my friends and welcome again to the complete kotlin course we have learned about the basic control flow and we have said that we have comparison operators that include six operators equality not equal equals equal not equal less than less than or equal greater than or greater than d and we have learned about the logic operators the and operator and the or operator which values between the and give us the value between the result between the two booleans now we are going to talk about the string equality sometimes you want to determine if two strings are equal for example a children's game of naming an animal in a photo would need to determine if the player answered correctly in kotlin you can compare strings using the standard equality operator in java this is different we use dot is equal is equal method okay but in kotlin we can use the ordinary equal equal operator and standard equality operator in exactly the same way as you compare numbers for example if i have val i'm making an a quiz so i have stored x equal to let me say kotlin and the result val doug equals maybe y equals y equals x equal to x is it equal to java so let me print y equals x and it will return false you see here y equals x is a boolean that is in this case equal to false because kotlin does not equal to java simple so just as within the numbers it will compare x it will go through here it will put here val y equal x forget the equal sign here it will separate like this x it will go and get the value of x and it is equal to cut so here the kotlin is it equal to java is the same naming it's the same letters no it will return false so it will go and goes here to store it like equal to false okay this is the logic of kotlin and how it thinks about comparing the string equality okay this is how string quality works and also just as with as with numbers you can compare not just the equality but also to determine if the value is greater than or less than the another value okay so how it works i can create here val let me say zeb equal to php less than java let me run and see or you can simplify the boolean expression it is equal to false because it is not less than the syntax checks if one strings come becomes before another alphabetically so it is b less than j no j comes first before the p in this case the z equals to false because php comes or come or java comes before the php j comes before php now let's see if a less than b if we hover and simplify boolean expression and it is equal to true so this is the string equality we have learned about a basic control flow and we said that it is comparison operators logic operators and string equality later on we are going to talk about the advanced advanced control flow and but the way we are going to start with conditional statements and conditional expressions and later on the advanced control the first and most common way of controlling the flow of the program is through the use of an if expression which allows the program to do something only if a certain condition is true for example consider the following code if 5 greater than 2 go and print for me hey five is greater than two let's run and see and here we go hey five is greater than two if we put five less than two hey 5 is greater than 2 it's less than 2. let me see and here we go nothing happens and nothing is printed out so if five less than two and it is not the true so the if condition this between the two parentheses concerned and related to f are not executed so this is this is a simple if expression if the condition is true this is the condition so between the parentheses uh is the condition so these numbers are the condition then the expression will execute the code between the braces so if the condition between the parentheses is true then the expression between the braces would be executed okay if the condition is false between then the comp expression won't execute it will not be executed and the code between the braces is not executed so if this condition is not true like if five equal or five less than two this will not be executed the code between the two parentheses will not be execu braces are not be executed okay curly braces code will not be executed if the condition is not true the term if expression is used here instead of if statement since unlike many other programming programming languages a value is returned from the if expression the value return or the value returned is the value of the last expression in the if block so despite the conditional statement it will be conditional expressions so okay now let's continue with if expressions you are not required to use the return value or assign it to a variable you will see some more on returning a value next so you can extend an if expression to provide code to run case the in the case of conditions turn to be false how to do that this is known as else close so if 5 is greater than 2 go and go up make this otherwise if 5 is less than 2 what to do it will print oops 5 is less than two let me run and see and here we go kotlin goes and start reading from here if the condition 5 greater than 2 is true then go and print this and this is true because 5 is greater than 2 and it is executing all the code between these curly braces otherwise else would be executed if this if condition is not true let me see five is a less than two so five than 2 or greater than i will keep it f5 less than 2 [Music] condition one is true here com condition two is true let me run and see and it will return condition 2 is true if 5 less than 2 and this is not not true so this code till here would be skipped because the condition is not the true so everything related to the first condition which is defined by five less than two between the curly braces it will not be executed it will be skipped it will execute the else condition because this is a false what is the true this is the true so it will be executed condition two is true and is executed okay you see guys that the conditional expressions and how they are working together and how the logic of this would be like determined by understanding the conditional expression and making and make thinking of about uh the the flow of your program and you are going to use 99 of this control flow and all of your programs okay now one another thing before we finish the conditional expression lesson if i need to check if five equal to two here f is less than two the otherwise it will return a five greater than two but if i want to check if 5 equal to 2 then i need not to use else i need to use else if and i need to insert a condition so f greater than 2 so condition 2 is true and otherwise i will put else and print condition to 3 3 is true if 5 less than 2 print this else if 5 greater than 2 it will print this otherwise it will print 5 equal to 2 5 equal to 2. let me run and see and condition 2 would be executed because 5 is greater than 2. it is not less than and it is not equal now let's test with a real the constants so val x equal to 5 val y equal to 6. now let's check for real values of these so if x less than y print x less than y if the condition 2 is x greater than y y or x is greater than greater than y else x and y are equal okay let me run and see it prints for us that x is less than y because it goes and verifies x less than y five less than six yes it is true so go and execute this code between this curly braces related to the first condition and they drop these conditions okay now let me change it and here we go we have else if is executed so the condition 2 is executed because 15 is greater than y and it tell us x greater than y now let's change y also and give it an equality to x and here we go it okay it tests for x less than y 15 less than 15 no else if 15 greater than 15 no so skip this line now else they are equal if the first if x less than y no if x greater than y no therefore x equal to y these are the three conditions and you can put many conditions here and so on as your program and your application dictates so thank you guys i will see you in next video we'll continue with the conditional expressions and we are going to take a new example about finding the minimum and the maximum between the two numbers so if i have two numbers x and y and i need to get the minimum so val minimum of type integer and if a less than b then [Music] minimum equal x sorry x less than y so minimum equal to x otherwise i would put else and put the minimum minimum y equal to a minimum equal to one okay this is very simple i will create a val max of type int if x greater than y then max equal to x else max equal to y okay i'm finding the minimum and the maximum maximum between the two numbers by now you know how this works but it's a lot of code okay let's take a look at how we can improve this using the fact that if else expression returns a value as i told you we we said that they are conditional expressions and they are not conditional statements because expressions return a value and this value this returning value we are going to use it making use of conditional of returning value of expressions how to make use of them despite writing all of these codes to find the minimum and the maximum i ca and assign them to min and max variables i can put like this so i have created above the variable x and y and i will change the code val minimum and val maximum into a simple lines so val minimum equal to if a or x less than y then put it as x assign it to x otherwise assign it to y it's very simple guys it goes and mini because i have created minimum here despite writing all of these codes if x less than y put m a minimum equal i write it with a one line val minimum equal to this is the expression it returns the expression so this will return an expression so if x less than y it return x otherwise it will return y so minimum equal to x if x less than y and minimum equal to y when it is x greater than y so the returning value of the expression of this if x less than y it returns x this is the uh this is the difference between the expression and the statement uh in other languages like java and python and other things that where did not return the uh the value of the expression so we have learned how to make the minimum now let's create the val of max equal to f x greater than y it will return x else return y okay in the first example the condition uh is a x less than b or x less than y if it is this true the result assigning assigned back to the minimum will be the value of x if it is false the the result will be the value of y so minimum is set to if we go up seven less than five so minimum is set to seven and maximum in the same way would be set it to the 50. okay in this way we have learned how to make a very simple and and make very simple coding and despite writing all of this code we make use of returning value of expression and we have used it in one line to detect the minimum and the maximum okay it's very simple and uh i hope you've been it from this video and uh this course please rate us five stars on udemy and to support us making new videos loops are kotlin's way of executing code multiple times in this video we'll learn about one type of loop the while loop if you know another programming language you will find the concepts and maybe even the syntax to be familiar like java so if you want you can go to our the complete java developer course and see the loops syntax and learn more about them and you will see them familiar as cutler so the while loop while loop repeats a block of code while a condition is true you create a while loop like this while you write the while send a code and the keyword and here is the condition so here is the condition that you need to put and here is the loop code okay so this is the condition and this is the loop code okay this is the formula and the syntax of the while loop the loop checks the condition for every iteration and we are going to learn about the iteration in next couple of minutes so if the condition is true then the loop executes and moves on the another iteration if the condition is false the loop stops just like if expression while loops introduce a scoop so the simplest while loop takes this form while the condition is true so while true you can put like while true go and execute the loop code [Music] i will make an example to see you how to make var x equal to one okay i will create a variable called x and assign a value one to it now i will create a loop while equal to x less than 100 go and make x equal to the old x plus x plus one this code calculates a mathematical sequence up to the point where the value is greater than 100 and every time you execute it print ln the value of x okay now let's run and see here we go we have this output 3 7 15 31 63 and 1 7 2. okay now how these numbers have been calculated this is the run tab 3 7 15 and 31. how to run and how it will be executed if we look how the while loop has been increased and how the iteration works we find that x here represent the old x so this is th sorry the new x this is new x new and this is the old x okay let me rename them and see how to work with it so at first iteration x equal to 1 it enters here while x less than 100 yes the value this condition is true so go and execute this code inside the loop okay let me use the laser here [Music] the pen okay so we are going to execute this code this code many times until the condition starts to be true x the new x at the first iteration equal to x the old x plus the old x again one and plus one so this would make x equal to three so the printing would result at the first time with three now let me move to the second iteration goes up and the checks if the condition is still validated so 3 now is less than 100 and it's true so go and execute this code again the old the new x equal to in the second iteration is equal to the old x which is the three plus it is a substitution of x the value of x three plus one okay so three equal three plus one which results in seven okay so this is the second iteration now it prints out the result which is seven and goes up again to the third to the third iteration okay so i will use now the pen with another color to ensure that you understand everything it goes up now 7 less than 100 yes 7 is less than 100 so goes and execute this code again x the new value of x equal to 7 plus 7 into sorry a plus 7 plus 1 in 2 and seven plus one eight plus seven equal to fifteen okay so this is the value the third value that we have got now it prints out the results and goes up it checks if 15 less than 100 yes 15 is less than 100 so it again it again execute the new x equal to old x which is 15 plus 15 into 50 a plus one is equal to 16 plus 15 which is equal to 31. so this is the result of the force iteration now i will change the color to be like um this blue so 31 go it print out the result and goes up to validate the condition 31 is less than 100 yes so go and execute the code again so if the new x equal at the fourth at the fifth iteration equal to 31 plus 31 plus one okay so 31 plus 132 plus 31 it is equal to 36. it prints out the results 30 63 now it goes up again and the checks for 61 is less than 100 yes it is less than 100 so it repeats again x equals the new x equal to 63 plus one plus sixty three sixty plus sixty one hundred twenty plus seven equal one two seven it's it prints out the one to seven and goes up so i will change the color and goes up to here to the x to validate the condition x x is one two seven don't miss this now because we have the number here okay so 1 to 7 is less than 100 no so go and exit the loop and it will give us 3 7 15 31 63 and one two seven from this way the kotlin calculates and deals with the loops and this is how the loop works in kotlin and other programming languages but mainly in that hello my friends and we will continue with the loops we have introduced in the previous videos that while loop and we have learned about iteration and how while loop thing and how while loop will be executing its code according to the condition validation now we are going to learn about a new type it is called of loops it's called what do while loop or repeat repeat while loops a variant of y of the while loop is called the do while loop which is do while loop okay this is the while loop it differs from the while loop in that the condition is evaluated at the end of the loop rather than at the beginning okay so it differs from the while loop in that the condition is evaluated at the end of the loop rather than the beginning please write it down this note because it's very important to distinguish between and how to use and when to use the loop while loop and the do while loop okay you construct a do while loop like this do we start by do and here is the loop this is the loop code and the while while condition here is the condition condition okay so we start by the do and execute and evaluate the condition at the end of the loop not not at the beginning of the loop so here is the beginning of here is at the beginning of the while there is a condition validation and here in the do while the condition is evaluated at the end of the loop so it will execute the the code loop for the first time even if the condition is not the true this is very important trick guys okay so here is the condition how to do a quick example to understand it we will make by the do i will variable x i will use it i have created before so do loop code x equal to new x plus the same way plus x plus one i will do it again so while condition the validation of the condition sum equal to less than ten or x less than 10. okay in this example the outcome is the let me make it 100 to be making it the same as before okay i have i need to comment comment this and i need to print ln the value of x okay so let's start again and here we go in this in this example the outcome is the same as before however that is not always the k is not always the case you might get different result in different conditions okay so what are the conditions consider that if i will change the first one while x less than one or i will make it as 10 so if x less than 5 do this and the same thing i will make while x less than 5 or 10. so i will give x for the first while loop and see if it will print x and i will pass it the same x for the do y okay uh if it is it would be increased and give us 11 or not so let me run and see and here we go guys it is 21 so the first here is the difference okay please concentrate with me if you are using the y it will validate the condition at first and then allows you to enter the code inside the loop so here x which is 10 is not less than 5 so 10 less does not equal or less than 5 so this code will never be executed okay so it skips this loop it goes down to do and then validate so it execute the code it put here the new value of x which is equal to old value of x which is 5 and 5 plus 1 5 plus 1 is equal to 6 sorry it is equal to 10 okay so the value of x is equal to 10 not 5. 10 here and 10 here 10 plus 11 is equal to 21. now x is equal to 21. print it it allows us to see inside the console that it is 21. then it validates the condition if x which is 21 less than 10 no it is not true to go up and do it again okay this is the syntax this is the algorithm how the do while loop and the while loop execute and works inside cut okay sometimes you want to break out of a loop early you can do this using the break statement which immediately stops the execution of the loop and continues on the code after the loop for example if we go here i will delete the while do while loop if we go up to the first loop that we have created before and i will assign the x equal to 1 and while is it equal to is equal to true so go i always make this so how to stop this infinite loop if i run now it will go and in an infinite loop so you see guys there is an infinite loop and maybe make some memory leaks memory this you see guys these are the infinite loop it goes up and it starts let me run again it starts by printing numbers it calculated it before and then a minus one because it is it is reaching the memory leaks and memo infinite loop okay so how to provide this or how i prevent it or how to make a breaking out of the loop this is an infinite loop because always the conditions true i'm passing it i'm telling you that the condition is always true nothing will control or change the condition make it false to break out the loop there is another there is another um like way to make if x greater than or equal to uh 100 let me show you then what to do break statement so let's start by while true it will always execute this x equal and start again that we have seen in the previous video with executing this sequence and there is a condition here if x greater than or equal to 100 when reaches when x reaches 100 break go outside the loop so break it will give you a break outside the loop so this is the loop the break is responsible for getting you out from the while loop so it will execute this print like l n outside the loop i am outside the loop let me run and show you how this works it start by 3 7 15 31 63 it reaches 1 2 7 by the sequence the last sequence that we have seen in the previous videos and the checks if x yes 1 to 7 here 1 to 7 greater than or equal to 100 so go outside break this loop go outside and continue executing the other codes which is print i am outside the loop okay so this is how we break outside the loop you've seen how to write the same loop in different ways demonstrating that in computer programming there or there are often many ways to achieve the same result you should choose the method that's easy to read and conveys your in intent in the best way possible this is an approach you will internalize with enough time and the practice okay so the choose of the choice of between the while and the while and how to achieve things in different ways will be come with their practice and time and exercises hello my friends and welcome to exercise number one in this exercise we are asked to give the result of these constants we have four constants and we have these boolean expressions and we need to get the results so let's consider the first variable or the first constant it's named q1 and having true and true so true and true by the logic it is equal to true so as we said in the previous videos true and the true gives true true and false will give false false and the true will give false and false and false will give false so whenever you see the and expression and there is no true and the true here it will give false okay so this if we if we use the you can print out the you can print out the result like print ln but i will use here the kotlin we have if we hover the mouse simplify boolean expression and it is equal to true so it is the same result as we print the q1 okay now q2 what is the result of q2 false or false will give false because as we said in the previous videos true or true true true or false true false or true true false or false false this is the the only expression in or that will give you false and this is the only expression in true that will give you a true and end okay so if we hover simplify boolean expression and it will give false okay it's very simple these are the simple mathematics and simple logic mathematics now we have q3 we have this expression here and we have or you see guys this is the or and we have a second part here we need to simplify first the first part and the second part later on and we make the or between them so inside this if we take this and we put it here what it is equal to it is also divided into two parts the first part and the the second part and we have and expression between them so how to do that and how to think uh in logic true and we need to get this one does not equal to two so one does not equal to two is true okay so one does not equal to two so i am verifying it true and true and since we have uh calculated it in the previous question it's a q1 so true and the true will give true so it is very simple okay it's very simple so the first part in this question is equal to true so let me put it here this is equal to true and we have or between the two and we need now to calculate 4 greater than 3 and 100 less than 1. so i can give you now the result directly since true or anything will give us true so the q3 would exactly give us true despite it is true here or false okay but let me check the other equation to show to allow you to solve and think like a boolean expressions okay four greater than three and we have and sign here and one hundred less than one four greater than three yes it is true and 100 less than one no false and we have learned that true and false will give false because true should with and it should be true here to always give us true okay so it is false now we get true or false with will give us now it is true okay this is very simple guys it is about expression boolean expression and logic if we hover the mouse here and simplify boolean expression it will give us through exactly like what we have done okay now i want from you also to to make this expression and give me the result in the comment below okay so post the video and comment the question and the answer below so did you think it about a thing about it okay let me solve it we have here q1 q4 we have two expressions one and two and they are separated by and so let me start solving this and this we need to make the division first and make the evaluation is it equal a greater than 3 or no 10 over 2 it is equal to 5. so 5 greater than 3 yes it is equal it is greater than 3 so it is true at the first one we get this equal and the sign and sign and we need to verify this so 10 modulo 2 if we divide 10 over 2 it is equal to 5. if there is any remainder any thing remainder no because 5 times 2 will give us 10. so remainder is equal to 0. is it equal to zero yes it is equal to zero so it is returning true so true and true will give us true okay now let's use this simplify boolean expression you can click alt plus shift plus enter to get the simplified boolean and it is equal to true and good work guys we have solved the exercise number one and it is very important to start thinking about boolean expression and think about how kotlin yeah deals with the expressions the operators and how the logical things works in coding in the previous lessons you learned how to control the flow of execution using the decision-making powers of if expressions and the while loop in this lesson you will continue to learn how to control the flow of execution but in an advanced way you'll learn about another loop known as the for loop so let's discover what is the for loop and what is the usage of for loop loops may not sound very interesting but they are very common in computer programs for example you might have code to download an image from the cloud with a loop that could run that multiple times to download your entire photo library okay so you will also learn about the when expression which are particularly powerful in a kotlin and we will introduce it later on but before we dive into the for loop we will start by things called range okay before you dive into the for loop statement you need to know about the range data types which let you represent a sequence of countable integers okay so it's a method to represent please write them down to understand them and later on if you want to review your codes and studies you can get them directly from your notebook so represent a sequence of countable integers okay let's look at two types of range the first type it is called closed range i will create a constant called cr closed range 0 and look guys how to make the closed range zero two five okay so in this way we have created a closed range the two dots indicate that this range is closed which means that the range goes from 0 to 5 inclusive so from 0 to 5 inclusive let's print cr to see the result and run and here we go we have zero two five it is it is the range this is the range of uh the closed range vcr okay so that's the number zero one two three four and five are included inside c r the second type of ranges is called have have open range okay which would you represent like it like this val a half open i would say half open range h o r equal zero and look guys we are going to use until so it is a keyword here until five what are the difference between these two ranges the first one here you praised sorry here you replace on the second one the half opens range here your praise replace the two dots with until half open means the range goes from up from let me comment from zero to four inclusive okay so but not including 5 that these numbers are 0 1 2 3 and 4 and above the numbers are zero one two three four and five okay so this way we have learned about the closed range and the half open range okay this is the range in in kotlin there is another thing that you should learn one and half open ranges created with two dots and the until operators are always increasing they are increasing but somebody will ask me how to make the range decreasing so how to make it the making making sorry a decreasing range we start by in other words the second number must always be greater than this than or equal to the first to create a decreasing range you can always use down to which is inclusive so val decreasing range dr equal five look guys down two down to zero okay and this will include all numbers but in decreasing order so it is zero sorry five four three two one and z okay these are the ranges in kotlin this is how what are the difference of frame different ranges and they are commonly used in both for loops and one expression which means that you thought the rest of the lessons you will use ranges as well so we are going to use these ranges in the loops and the one expression so i hope you understand this that the closed range is from zero to five and inex inclusive so uh half open range is zero until five and five is not included and to make it decreasing range we use five down zero five four three two one and zero and zero is inclusive so zero inclusive and five inclusive thank you guys i hope you rate us five stars on udemy to support us making new tutorials and new courses in the previous lessons we have looked at the while loops now that you know about ranges it's time to look at another type of loop called the for loop we have said that the ranges have closed range zero two dots and five and so zero one two three four five are included while the half open range zero until five it is from zero to four zero one two three four and five is not included so only zero one two three four and we have said that to make a decreasing range is from five down to zero we uh we have used the down to keyword now in order to make a for loop for loop we construct the general for loop is like this so please write it down guys in order to understand it well and make a very a very uh brief notes so four and we specify here the constant so constant [Music] range okay this is the for condition for loop condition now the code here we can loop code okay so this is the loop code okay guys the loop begins with the four keyword you see this is the four keyword followed by a name given to the loop constant more on that shortly followed by in so the constant and followed by the range so we have here the range so remember guys four and we specify the constant in range constant in range this is the trick this is the key of the for loop okay so i will comment this and i will create a new example in order to understand the for loop i will create val count equal to 10. i need to count to 10. so var sum and make the sum for equal to zero i have created two uh one one constant and second is variable called sum and the count is uh is constant because it is having a value 10 and val while variable variable is sum and i need to add a summation to this variable okay how to do that using the four four open parenthesis i in one dot i put i specify here the constant constant the constant is i and in range so we specify the range as i told you from one to ten okay i can remove count from here in order to better understand things so i am specifying here the range and i need to make the sum equal to sum plus i okay what we have done here we specified that it is a for loop the constant i which is an integer that we have uh we have created here using the four so if we if we look at the java codes we see that we have created int i so we write in java for int i equal to zero i am specifying here the first condition and creating a new variable called i in kotlin it is directly created so for i this is the constant here in range i need at the range from 1 to 10. i need to get to get the summation all the sum of numbers from 1 to 10 sum of numbers let me write it here sum of sum of numbers um from 0 to 10. this is the program that we are creating using the for loop okay so the sum this is every time the code every time it is iterated the loop iterates from 1 to 10 the summation then holds the new summation this is some new this is the sum this is the new variable that we have created here is equal to the old result to the old summation to the old variable plus i i is in the range so i would be 1 at the first second 2 3 4 five seven and eight ten nine and ten so i would have one two three four five six seven eight and nine ten okay so let me run and then explain what are the results i will print every time the sum so every time the loop will will be executed it will print out the summation so back to here to the program okay and waiting for the cut and here we go guys we have these numbers we have one it starts by one one plus two is equal to three so if we go up here i equal to one now okay because it starts from i equal to one sum is equal to zero we have created it before so one is i and sum is equal to zero so the new sum now is equal to one print sum so it print out the one again it is in range okay going up yes it is in range i increased by one so when we specify for loop i this the increment would be by one i equal now 2 so 2 is in 1 and 10 yes so continue and equi execute this code the new sum this is the variable equal to the old sum which is one one plus i i equal to two so i plus two one plus two is equal to three print sum is equal to three going up three is is in uh 1 and 10 yes so some the new sum create the like update the sum and replace the sum with the old sum which is three and the i equal to three so three plus three is equal to six okay so sum is equal to six going up i is equal to four yes is equal to four four in one and ten yes so replace i with four and go and create a update the sum so remove the old variable and create the old variable which is 10 the old value plus sorry it is equal to 6 and plus 4 is equal to 10 then print the results and equal to 10. so in this way the algorithm of the the loop the for loop continues to execute all of these okay till 10 so we if we count them 1 2 3 4 5 6 7 8 9 and 10 10 results so we we can uh we like and like uh analysis or interpretation like we can we are getting 10 values from one one is included so we have learned this in the ranges 1 2 3 4 5 6 7 and 8 and 9 10 is included also so we have 10 times this code have been executed okay so this is the function this is the role of the loops and by the way there we have we can create like sum is equal to the old sum plus i we can replace it by plus equal sign okay this is the same as same as sum equal to sum plus i okay okay guys so we have this for loop and we have learned about it more in details sometimes you only want to loop a certain number of times and so you don't need to use the loop constants at all and the ranges like this example so in that case you can employ a repeat loop like so okay instead of using like this for loop i am going to use a repeat loop we start by repeat keyword so repeat keyword and how many times i need to repeat it you see guys that there is a notation telling us times in in so how many times you need to repeat this i need to repeat it 10 times and i specify the same codes as before sum equal to i you can uh or i need here to create is some sum equal to zero we have created it before and i need to create var last sum equal to zero sum equal to last sum and i need to create a variable here called a constant equal to temp equal to sum okay i'm storing the sum inside a temp first just equal to zero let me show you sum is equal to zero i'm starting it the first time at temp and getting sum equal to the sum the old sum plus the last sum and then last sum equals to temp okay print ln the sum i will comment this oops i we have zeros okay yeah the sum would be one here okay and here we go we have 1 2 3 5 8 13 21 34 55 and 89. okay so in this way we can create the same coding the same thing but we're using the repeat loop okay we specify the index here how many times you need to repeat this okay now we we will continue with the for loop and we said that we create a range from 1 to 10 and we are looping through this with this range okay now it is possible to only perform certain iterations in the range for example imagine you wanted to compute a sum similar to that of triangle numbers but only for odd numbers we need to make the odd numbers so we use here step step keyword and how many how would that the ratio would be jumping how many times you need to jump how many numbers or what is the way what is the pattern you need to jump i need to jump two numbers so this way has a step operator in the for loop statement the loop will only run through the values that the step falls on in this case rather than step through every value in the range it will step through every other value as such i will always be odd because the starting value is one okay so it takes one one it takes one one then it took two no it took three because it is stepping two so these are the numbers that it is included in the pattern and now we need to jump over these numbers so we start by one stepping two one two okay so three three step two one two so five so we have one five one three five seven nine because we are stepping two numbers not iterating through every number so this way we have inside this loop the iterations of i and the value of i would be 1 3 5 7 and nine okay so this is the way how to step two numbers or how to modify the stepping and the iteration okay this is normal normal loop here we have used step two loop and there is a countdown loop imagine that you need to count down the this the loop despite starting from 1 to 10 it is inversely starting from 10 to 1. how to do that you can even count down in a for loop using down two in this case if count is 10 then we'll the loop will iterate through the following numbers so if we use 1 in 10 in 10 down 2 okay down to 1. let me run and or what is the result that the loop will take as i think about it guys we have started we have specified by i is constant in and what is the range it is from 10 down to 1. okay guys you need to segment you need to partition there and divide the loop into many parts to understand it i in what is the range it is here from 10 to 1 so the loop will start from 10 to 1 10 9 8 7 6 5 4 3 2 1 1 is included and 10 is included but there is something here it's called the step two and step two starts from uh like modifying and getting the only and passing jumping two numbers by two numbers so it let me clarify this it is here i am writing 10 to one okay so it start by eight seven six five four three two one okay one is included and 10 is included because we are using uh down to 10 to one okay since we have used the step two we can remove this so this is the correct one down two okay let me write it down down to loop again if we use the step two here step two here means that we are going down decreasing order and we are jumping two numbers and from the beginning so 10 the other the next it will not be taken the other will be taken so 10 jumping one two eight one two one two one and two and then so then so the numbers that the loop will iterate or the values of i in this iteration and this loop would be 10 8 6 4 2 and only these numbers okay down step two down down to loop okay so we have covered the almost all the ways that iteration and the loop works in the normal mode and the stepping two by two and then we covered it inversely like going down to we specify that the numbers are in in decreasing order and we have learned that it is we can use step two and jumping two numbers by two also in decreasing number so this is the loop this is the for loop and we have learned that down to the step and how we work with in this video we're going to continue with the advanced control flow and we will learn about the nested loops so what are nested loops nested loops are simply a loop inside loop for specific targets and a specific goal what are these goals imagine that despite making a loop that prints uh like these numbers from 1 to 10 you need to print a table like like this from one two three and four i need to print this and again i need to print one two three and four i need to print this table and i will copy paste paste paste okay i need to print this table for specific reasons okay how to do that i will not create a loop here and then a loop here and then a loop here and then a loop here to loop through these numbers i will create one loop inside it as there is another loop that will take care about this conditions so first of all i need to clarify very important thing so let's analyze this example in the ordinary looping in the one loop in one iteration we are going using the i so i index will go through 1 it is increased 2 3 4 5 6 7 8 9 10. so we have used the index i so the index and this iteration have been increased one time every loop okay in here we have a different scenario if we need to print this table okay if we need to print this table we need to start and segment our table into two things the columns and the rows okay these are the rows and these are the columns there is a very important step that think about the rose as a ordinary iteration like this so for that i need to use i so i will loop through the rows okay think about this so i will loop through the rows i for rows how many rows i need i need to make four rows so i will be from zero 2 3 okay now how to print the column i need to make a loop and think about a nested loop another loop inside the loop so every row the same is repeated so i need to make a loop every time would be executed and make these lists as a row so it will continue one two three and four okay so it will loop through the uh four numbers okay so here i will create it as j so j is in blue i will make j here so j will be equal and loop through 0 to 3 because 3 is included so 0 1 2 and 3. every time i is increased j will be printing the same thing into the table okay so when we move from if when we move one row a column will be executed so a loop will be executed till the end so when the second loop ends we go to the first loop and make this this think about it as a big loop inside it there is another loop so this is the outer loop outer loop and this is the inner loop okay this is the inner loop now despite using these theoretical things let's go to the code and see how this works so we need to print this table inside the nested loops so i will create a for loop this is the outer loop and i will name the constant as rho so in 0 till 3 as i told you okay because i need the four rows i can start one to four okay but the index in a kotlin and other languages like java starts from 0. okay this is better so 0 1 2 and 3. these are the rows inside the rows loop i need to create a for loop another for loop and make a constant here called column so this will represent the column in range is also from zero to three okay okay guys it's very simple now i will create here i will print like stars first of all i will print stars and then i will show you what how to replace the stars and asterisk with the numbers let's run and see okay i need to print ln here let's run again and see and here we go guys we have a box of four rows and four columns printing the stars how this works it starts from the outer loop and still executing okay rho is equal to zero now four column it starts with this four when it ends from this four it will print a new line and again it will go up and check for the condition we go through this inside through this nested loop and we see that the inside the inner loop there is a for loop telling us there is a constant called column and it is range is between 0 and the 3. so what we will do it will print a star so it printed a star okay again it it will go and print another star three and four four stars are printed so we have finished from this for loop it will execute a print line and this is what happened we have printed one two three and four stars and we go to another line by println now we need to print again going up it checks rows now equal to one because it is increased by one this is kotlin how kotlin algorithm and we have learned it before in the loops that the inside the for loop it will increment by one unless you put here step two or step three or step whatever you want but if you don't specify the step the step would be one directly okay so row is now one one is between this range one yes between zero and three so continue now go and print again four stars and print a new line uh when you finish okay printing four stars and then printing a new line when finished i is now equal to two or row is equal to two it is between zero and three yes so go and print four four stars and print a new line so four stars and print new line again let's go uh it's going up so row is three s three three is between zero and four three yes it is inside it because a three is included inclusively included when using the range the two points this is the closed range that we have learned in the ranges lesson printing again the for loop which is the four stars here and printing a new line going up row is now equal to four four is not between zero and three so stop executing this for loop and and by a and and occasionally this is topped with a program and printing four rows and four columns this is how nested loops works in kotlin now we need to make this table uh like accurate and the same way that is printed so how to make this i will make a sum equal to zero so after we have learned about the nested loops and the how they think and how they work so i will create a variable called sum the sum let me check okay and there is no nothing above okay so for zero and three we will remove the print here and i will make sum equal plus equal to rho times call or to to column some or maybe i will not make this for at first time let me keep it for the next exercise i will print the multiplication table but till now i need to display this so i need to print the columns print column okay printing the column you can specify it with like this and let me run and see and here we go guys we have created it now if we need to change the the zeros and start from one we just make this range because we are printing the column variable so i need to change the column variable and here we go we have this box one two three four and four columns four rows okay so this is about the nested loops in the next video we are going to make an example about the multiplication with a multiplication table and then we will learn about the continue keyword well hello my friends and welcome to exercise number two in our kotlin course so we are asked to print out the multiplication table like this below we have numbers one two three four five and as column one two three four five and here is the result of multiplication two by two row times column equal to four if we get five times five we will have a result of 25 and so how to make this please pause the video and think about it and try to write it in your computer and test it and then then let me show you the solution so i am waiting you guys so you guess it you try it let's start with solving this exercise i am going to show you how to think and how to make your algorithm works fine the things that we succeed in approaching millions of people like in our applications like master coding application on play store or whether the udemy courses that complete java or the complete android developer course on our udemy pages and our youtube is that is that we teach algorithm we teach how you need to think how you think how to solve how to problem solving all of your exercises problems and many things so we are helping you and developing your thinking your algorithm your problem solution problem solving techniques with uh these courses so please guys take into consideration that these exercises are very important and to take notes are also very important thing how to solve this we start and we we look that that we have one row second row third row and it is repeated so the rows are repeated like one two three four five we have iteration so we have a row iteration and we look that there is a column iteration so we are working from one column one two three four and five okay guys so this will trigger for in in our brain that we need to use the for loop but there is one trick that we have to use the nested loops if we use the loop the for loop we can print only like this line or we can print only like this line one two uh three uh four five okay or we can print like this one two one two three four five okay this can be printed with using one loop and this will be printed using one loop but we need don't need like this we need a table so always when you have a table use a nested loop so we start for we make a row as in a constant and in 0 on 0 to 4 because we have 0 1 2 sorry rows 1 0 one two three and four okay again i need to print a for loop and i will call the constant column and it is in 0 also 2 4 okay now what we need to print inside every cell so we have here and access to the cells so each cell i need to print it out how to what are what we are going to print we are going to print the row times the column it will give me the result in this cell which is 25. so how to do that we start by creating a new variable called sum equal to zero okay so var equal to z sum equal to zero and now let's create a sum equal plus equal to the row times column okay let me clarify something new sum plus rho times it's the same writing the same syntax and i showed you in the previous video how to make the the the syntax works so plus equal is the same as sum equal to sum plus row cod we are making an update every time to the sum variable so the new sum is equal to the old sum plus row times column so sum is equal to zero let's me let me go to the first one two times 2 it is equal to 4 but sum is equal to 0 so 0 is equal to 4. sum is equal a plus column times rho equal to 0 plus 4 is equal to 4. 2 times this 3 is equal to 6 so 2 times co okay and so on okay guys we have introduced the sum as 0 okay so you can write if we hover it replace it by plus now we need to print ln the sum okay and when you finish from the loop from the column the first printing the first row go to the second row by making a new line okay okay guys so let me run and see and here we go guys oops i have used println let me run and see again okay let me pause so we have some errors how to do that how to check them i need to print the sum with the spaces and the variable sum sum okay and let's print out and see the results zeros zeros and here so if i print the sum is not equal to plus let me check it's equal to product okay so row and times column we have zeros here and zeros here this will be because we have started from one let me change it from one from zero to one and here i need to change it to five so i start despite starting from zeros i will start from one so let me run and see and here we go guys we have this table have been verified correctly so despite using this i will create a variable called the product sorry and it is now good product okay so we have created this table of uh multiplication 5 times 3 is equal to 50. okay so in this way we have sort of exercise number two which is creating the and printing out the multiplication table so we have created it and we learned that uh how to solve that product by changing the zeros to ones because we need to start from one times two times three times four and times five thank you guys see you next exercises sometimes you would like to skip a loop iteration for a particular case without breaking out the loop entirely you can do this with the continue statement which immediately ends the current iteration of the loop and starts the next iteration the continuous statement gives you a higher level of control letting you decide where and when you want to skip an iteration so we have seen in the previous video how to make the multiplication the multiplication table in the exercise number two and now we need to make the iteration iteration table but we need to skip these lines okay so how to do that we have said that it is a nested loop okay and it is going to make the product and give us and print it the result here we need for skipping the iteration i told you that we need the continue keyword so i have deleted the number two and number four rows which are even so i'm going to check if the row modulus 2 which is checking for the even and to parity if it is equal to zero use the keyword continue so this continue will make a check for the row is it equal to two which is checking for the parity model is two equal equal to zero so if we divide two over two is one so the remainder will be zero row modulus two would be equal equal to zero when the case equal to two when k is equal to four when k is equal to six eight and so on okay so this is how we check for the parity and for that we continue otherwise we are going to make this happens okay so let's run and see the program please rate us five stars on udemy to support us making new videos okay so here we come guys so when the first row is printed correctly when we came to the second row which is 2 so when the row modulus 2 equal equal to 0 we continue so we have skipped this iteration so this will not be executed it increased row but didn't increase the call so and increa and uh execute this in the row modulus 2 case so guys when two row equal to two or when the row is even we used continue continue well continue making the loop but skipping the current state so when the row modulus 2 equal equal to 0 when there is an even go and skip this whole thing and go up and row now equal to 3 3 modulus 2 is it equal to 0 no so execute this row equal to 4 rho equal to 4 4 the model is 2 equal equal to 0 yes continue so skip all of these and go up put a row equal to five and so on okay so when the row modulo two equal to zero the row is even in this case continue makes the for loop skip to the next row the break statement you saw in the last video used with the while loops also work with the loops for loops and takes you to the next statement after the for loop just like a break the continue keyword works with both for loops and while loops so you can use continue with uh for a for and the while loops okay here we have introduced and learned more about the uh the the the algorithm of the loops and how they works and we have introduced the new keyword called continue what's up guys and welcome again to the complete cut lane course in this video we will continue with the advanced control flow and we will learn on something interested called when expression you can also control flow via the when expression it executes different code depending on the value of the variable over the constant here's a one expression that acts on an integer val number equal to 10 okay we use when so you can make this when and we specify here the number i am making an execution on when on an integer or number so when it is zero make this print ln it is zero when other make this okay it's very simple let's run and see and here we go we have none zero so it works so when we use when keyword we are checking for the state of the past thing variable or uh and a variable or constant so think about it like as is which we have this number when we have 0 make this otherwise when we have anything rather any anything rather than zero go and print none zero okay so the purpose of when expression is to determine whether or not a number is zero in this case but it is not as simple as this i promise you to get more complex yes to handle a specific case you add the value followed by dash and this this diamond shape this uh flash okay and making this arrow so it is like an arrow so this arrow followed by in which indicates the code that will be executed if the condition is met so if i change the number is zero and try to run it it will give me the code needed and besides this arrow it is zero so when the condition zero is met and is satisfied it will execute this line of code okay and this is what happened now then you see how the else configuration can be used here also so if and else are the are the conditional statement and you can use them we're using else with when okay unlike other languages like such as java there is no need to indicate a break statement in each branch as the one expression will only use the first matching branch and then return in java we use the switch and in all cases we need to put a break statement unlike it kotlin use this smart and very fast thing that we can make by specifying only the condition and this arrow and what we are going to execute when the condition is met okay so it's very simple it's very uh logical it is uh it is like a switch that uh that i said to you if you have any uh prior example experience with java or other languages okay now let me make it some accomplish some adding some complexity to it so i will change the val number to uh name okay and i will name it as master okay so or udemy i am checking for the case of the name if it is equal to udemy for multiple choices for multiple like possibilities we can add them by separating the choices or the condition by the this apostle okay uh like uh youtube or not youtube like uh coursera like linda like plural and other print learning systems otherwise if they are not included the not learning systems okay let me check if we run and i have passed the name as udemy and it the result would be the learning system if i pass coursera it will give me the same thing because it is of the first matching states so if you have multiple states multiple choices of the same result i'm going to print all of these choices printing only learning system so i can use the apostrophe here separate the multiple choices the multiple cases by the apostrophe okay so in this case we have learned the the when expression and we see it is an advanced thing and it is uh controlling the flow of the program hey my friends and welcome again to the complete kotlin course so we have learned about the advanced control flow and we talked about the when expression and by the way we are talking about a one expression and we have said about it as an expression so expressions will be returning a value let's discover the returning values of one expression you can also give your when expressions more than one branch and due to the fact that when is an expression you can use it to return a value just like you can with if expression you can also ignore the value if you want to just use when as a statement as we have seen in the previous video a one expression will return a value from the first branch with a matching argument if you want to determine the name of the number you can assign a value with a one expression exactly like that we are going to do here okay so i will assign a number a number name equal to when expression so i am using a number here i'm passing a number and i will create a val number here equal to nine okay i am making a very a constant here and assigning it to a one expression and when when we have one expression it will return a value and i'm going to store this value of the returning value of the when expression inside number name constant okay so for that it may be sometimes complex but uh you should understand it i we're returning two three three five and nine okay and else print print ln unknown number okay this else else and print ln i need to print the number name okay so what we have done i have created a valen a number equal to nine and i have created a val number name which will be a returning value of the one expression so it will give two or three or five or nine or unknown number this these are the possibilities and the returning type of a when expression and they are going to be stored inside number name and then we are going to print the print the number name let's run and see and here we go guys we have nine so we have passed nine number and it checked it from here it will return it as a string so number name think about it now is equal to nine like this okay and we are printing it just as a normal variable or constant okay so this is the importance of the returning type of the when expression okay these are the com the common things that we have uh to do with that when expression and we can make it more and more complex and later on in the expressions and the exercises and the next videos we are going to make it more and more complex so be ready rate us five stars to continue making this course the biggest course on the earth talking about cutlin and we are going to solve the kotlin exercise hello my friends and welcome again to the exercises so in exercise number three we are asked to write a kotlin program that checks the position of a 3d point with respect to the three axes so we need to create a program that checks for a point on which plane it is on x y plane xz plane y is at the plane or at an ordinary point for that we need to create a 3d point how to create a 3d point we have three things to enters to enter them so we are going to use triple this is the trick so val i can name it as and pass the three variables x y and z because i need to check them okay so it is of type triple and i'm making the one i will pass 10 and i will pass the zero at the third variable so i create a tuple of x y z variables and i need to check if this triple is on which axis okay for that i will use a when expression when this one expression i can't pass here the the triple but it is directly using the variables so since x y z since i write it like this way i am declaring a global variable x y z and is putting them inside a triple now i can use x as a separate variable okay you see guys this is the power of a cut if x equal equal to y and y equal equal to z this will result in print ln x equal to y x equal to y equal to z okay now if z equal to zero equal equal to zero in this case go and print ln that it is on on the x y plane if y equal zero this will result in printing lm a message telling us that this point on the x z plane or axis okay if x equal equal to zero this will result in print ln on the y z plane okay else is this an ordinary point so print ln nothing special or ordinary point or the neri point okay this is the program that will check the relationship of this fat top triple 3d point related to the axis and in it is on x y plane because that z axis is zero so it is on x y plane let me check if it is 0 here and if we have this number it will result in y z i think yes it is on y z plane and so on so in this way we have checked the point and its relationship with to the axis okay so hello my friends and welcome again to the complete kotlin course in this video we'll start talking about functions so what are functions what are their usage and how to create them functions are a core part of many programming languages think about a function that it is letting you to define a block of code that perform a task then whenever your app needs to perform and execute that task you can run the function instead instead of having to copy and paste the same code everywhere in this lesson we will start learning about functions how to create your first function and how to make it complex and in later videos so imagine you have a an app that frequently needs to print your uh welcome screen your name your date your whenever you want let's start by printing the welcome message you can write a function to do that okay so in despite of printing every time despite printing every time print ln like welcome to our course despite of copying it every time i need to put it inside the code i remove them and include this inside a function so in order to start making a function we start by a keyword called fun fun is the function and i put here the name of the function print welcome okay and i pass here the two parentheses i write here two parentheses till now we don't have arguments to pass and keep it empty okay and we put curly braces i will move this line of code to here so our function will be like this fun the keyword the name of the function that is print welcome and the parentheses and the curly braces so the code inside the curly braces will be executed whenever i call this function the code is known as a function declaration you define a function using the funky word after that comes the name of a function followed by parentheses you will learn more about the need of these parentheses in next lessons so after the parentheses come an opening brace is followed by the code you want to run in the function so if we go up we have seen and defined a fun and cold main this is the main function in kotlin and we we write everything that you need to run inside it so i will cut it and go outside this function and paste this function okay so we have two separate functions this is for the main this is for running the program you see this green triangle and i need here to make the code so i need to print the welcome a message despite printing it like println i can put it inside print welcome function and now i need to call the function so how to call a function it's very simple i just put the name of the function that i have created so uh guys look at here print [Music] welcome it is now defined as a function here so we have a function symbol here in um in the ide and when calling the print welcome its color is changed its color is changed from not you not used to used so when it is yellowish it will be like indicating it is a used function so let's run and see waiting for the kotlin for compiling and here we go we have welcome to our course okay what happened here is we are calling the function print welcome so in this inside the main function so it goes here and execute all the codes that we are needed to put it inside the curly braces okay so despite putting this like here and run the program we can use the function to call the the block of code and be executed whenever we want this is a very simple example and this is not the mainly the the target and the purpose of the functions this is very simple with the purpose is for complex thing the code boilerplate we don't need as a professional coders to write the code every time every time and copy paste copy paste every time if you want if we ask it to write println many times and in many ways so i just call print welcome method and it will make the function and trigger the action to print inside the codes inside and execute the codes inside the curry braces in the previous video we have learned about functions and how to create a function that prints a welcome screen and we have called the the function in the main function and we're on the program and we have the following message has printed out to the screen welcome to our course now if we need to make a customized message like if the user logged in and we need to say hello and welcome welcome jack welcome ali welcome a pass welcome uh pamela to our course so how to think about it how to make a printing message for all of these maybe you are thinking about making an if statement and saying that if it is uh if the name equal to equal to uh jack like this go and print println like our print welcome jack to our course this will work but it is not a professional coding and it will make the complexity more and more and you are going to cover all the [Music] names of the persons in the entire world okay so it is not applicable the solution is making the function make this by a parameter by a parameterized function okay so how to make this you need sometimes to parameterize your function as we have said before so let's the function perform differently depending on the data past it via its parameters we said that these parameters is void and empty now but we can pass data and allow the function to work depending on these parameters for example as i told you if we need to make a welcome screen for the logged in user i pass here the a variable called the name user if you want and i will put put two points and i specify the data type that we are going to work with since we are going to display a name of the user so we are going to use as string now println welcome jack to our course welcome pamela to our course welcome mira to our course and so so i specify here the dollar sign of name user okay so this is a variable this now it is a variable a variable function it is a parameterized function it is depending on the name of the user passed to display this welcome screen you see guys above if we have we go to the main screen and the main function and we call the print welcome welcomes welcome function i need here no value passed for parameter name user there is an error so error and preventing me from running the program okay and it's telling me that there is an error no value passed for parameter name user i need to pass here a parameter of type string in order to allow the function to work so i can't name user passed it as an integer i need to pass like the name of the user as a string now everything works fine let's run and see and here we go guys you see this welcome screen now welcoming well jack to our course so welcome jack you see guys here the parameter name user is passed as a variable and displayed it to the message so we have allowing the function to perform the differently depending on the data past if i pass here like ali and run we have different behavior welcome ali not jack or not the constant thing okay so this is the powerful and the power of parameters inside the functions and we are going to work with these functions with with parameters many times and ma and covering all of our projects all of other videos and it's very important topic in programming languages and especially in cutler okay now let's make some complex complex parameters and allow it to take a multiple perimeter so this function would be more complex by passing multiple parameters so if i want to pass like rating i name it rating and of type end okay i'm making a rating here and passing it as an integer welcome to our course you rated uh rated your rate is and dollar sign rating okay so i am passing a new parameter called rating of type integer and i am going to display it within the welcome screen and welcome message so welcome jack to our course your rate is five for example and welcome ali to our course your rate is uh six okay and you see guys here there is an error so no value passed for parameter rating i have passed a parameter for the argument ali but i didn't pass any of the rating and you see guys in the ide there is this is passed and now you need to pass a rating of type integer i can't pass a string because it will give me an error so the rating would be like 8 over 10 okay so the rating is or 5 your rate is stars okay let me run and see and here we go guys you see we have multiple perimeters welcome welcome ali to our course your rate is five stars so you see this is the second perimeter and this is the first perimeter okay these are the parameters these are the arguments of the function so when we have sometimes it is helpful to use named arguments when calling a function to make it easier to understand the purpose of each argument okay so ali is named as name user so the ide intellij is naming this it will give you a naming like name user and rating to help you to detect and remember what what the type of the perimeter also what is the perimeter you are going to pass in order okay so this is the uh the perimeters in a function this is the concept of it and how to use the functions and the parameters and it is very very helpful we are going to make things more complex but we are going to simplify it with step-by-step teaching all of the functions you've seen so far have performed a simple task like printing a welcome screen functions can also return a value the color of the function can assign the return value to a variable or constant or use it directly in an expression we have seen this concept in expressions and we have used it in the if expressions and in the when expressions this means you can use a function to manipulate data you simply take in data through parameters manipulate it and then return it here's how we are going to demonstrate how to return a value from a function okay we are i'm going to remove this function this is for printing a welcome screen but i'm making a complex function that will make a multiplication so multiply numbers and this function will may take a number 1 or x it is equal to x end of type integer and the second perimeter would be y of type integer okay and we start by return we have a return function we and return keywords so when you need a function to return a value even a string number uh boolean or whatever you want you need to use the return keyword so i need to return x times y i need to perform this action okay but there is a type mismatch required unit found in okay there is a unit and we have seen that unit is not it is like void in java for that to prevent this i need to tell the compiler that hey i need to return an integer from this function you see guys that this the error has been removed i'm returning a result of five example five times five which is 25 and it is of type integer okay so i need to tell the compiler that this function called multiply number it will having this perimeter it will return integer okay so this is how we tell the function to return an end or a returning value i need to also to make a return keyword and put here what we are going to return the value of the returning thing would be and must be exactly matches that same type that you have declared so i am telling here a function multiply numbers having these parameters will return a type integer i can't return like a string hello like so this hello it is a string type mismatch required and because i tell the compiler to allow it to make at as an end i need from this function to return for me as an n and it is founding a string returning type a string so this is at the compiling time at the compiling time will make an error telling us that there is a mismatch in type so the solution is to make and pass the correct data type that matches with the returning type we have declared for the function okay now i need to call the method here as i told you to call the method i need to put to call it directly and i need to pass you see guys x and and y here i need to pass five as x and y as five okay now let's run and see nothing will happen because nothing will be printed on the screen 5 is passed and 5 wipe is passed and the calculation is performed but nothing will be displayed because we didn't make any print on the screen so for that i will store this inside a val equal to result and this result is calling the function multiply numbers now inside the main print ln the result okay let's run nc and you see guys it is printing our multiplication of x and y 5 times 5 25 it stored it inside a result now val result is equal in storing a result of this multiplication operation and it's printing it again on the screen so this is what we have talked about the expression storing the result of this function it is returning an end so it is a returning end it's returning and i have stored it in a val called result and this result is of type int then i have printed it here okay so it's very important to uh to think and under understand how functions work and how they compile and how they work with your codes hello my friends and welcome again to the complete kotlin course in this video we are going to continue with functions and we have seen before that functions can return a value and we can store this value inside a variable or a constant now there are many thoughts many questions that for if that you can ask about functions and they're working i will give you a hypothesis if i need to make a functions a function that prints the entered value so i can pass like i will create fun like display display number and i pass here value of type enter okay and what display print print lm welcome welcome or you entered you entered the and displayed the value okay you are asked to make a function that display the entered value as a nump so i can call the method display number and i pass six let's run and see and it's working fine you and third six now the question here or the thoughts if i pass like 6 it is of type string but it complies the user can enter a number in string okay so they would let in letters okay you entered six but there is an error preventing me from running and we have said there is a mismatch in the types mismatch in third type is a string but was expected it needs end here but the passing string will be or the passing value would be a string i need to create a function like display letters and it took value as perimeter of type string and print ln [Music] u entered value okay and i need to change display letter now it works okay u and third six but this is not the solution i can't make many functions and they're called the exact function that the user will enter i'm not with him during the runtime to tell him hey i need to use display letter function and now you are using display number there is a whole bunch of problems the solution is by making an overloading functions what does this mean what if you want more than one function with the same name this is called overloading and lets you define similar functions using a single name so i can create display number it's the same same naming okay you see guys no error here however the compiler must still be able to tell the difference between these functions within a given scope whenever you call a function it should always be clear which function you are calling this is usually achieved through a difference in the perimeter list a different number of perimeters and different parameters types okay so for example defining these two methods will result in if i call it display number okay now it called this now this method if i pass here six it calls this method you see guys the difference in the color of the naming see this is called this call the method that have an integer value if i pass six in letters as a string it will call the display number uh this function okay so this is this is called a method overloading or function overloading let's run and see and here you entered six if i call the same method and i enter six let's run and see and here we go guys this is the very the the very important thing about of method overloading and function overloading you entered six the compiler knows here that it is a string so it calls the second function the function the function display number with 6 as a number it knows that it is the display number that it accepts the integer type okay so method overloading depends on the type of the perimeter and the number of perimeters let me show that we have seen the type of perimeter like in or string it define and it distinguished between them so here we have uh known that it is the display function this is the function that we need there is the second the second thing that method overloading works on it and the detect it is function display name is the same name display number and i need to pass value one as a string sorry as an end i need to pass value to as an end also and here print ln u entered value 1 and value two okay you see guys that we have defined the three functions one for and with same name one having an end as perimeter the second is the string perimeter and the third having two perimeters of integer type and only one is called because we have defined this function and i'm calling this function above now i will call the display number here display number and we have for the ide we have three possibilities i can't pass an end i can't pass a string or i can pass two integers okay i will pass 14 and if i pass the second perimeter it is a very smart ide it removes value it continues with end but we have this is the possibility the bigger possibility to enter a second number so value one and value two is entered now let's run and see we have called the first method to end to be entering six and the second method is passing 14 and seven so you entered 14 and 7. okay this is the importance of overloading the methods above or here have the same name parameter types of number of and the number of perimeters but we have here different number of perimeters so as i told you method overloading depends on the the type of perimeter please write them down method overloading please write this node down overloading depends on one type of perimeters the second thing is the number of parameters okay this is how the method overloading works well hello my friends and welcome again to the complete cutting course we have learned about functions the returning type of functions and how to make parameterized functions and also we have learned about overloading now we are going to work with functions as variable this may come as a surprise but functions in kotlin are simply another data type you can assign them to variables and constants just as you can any other type of value such as an end or a string to see how it works consider a function called add so func add it took x as an integer for the first parameter and y as the second perimeter of type integer okay and it will return here it will return here an integer okay it will return x plus y okay it's very simple the returning type is an integer yes because y plus y and plus end will result in in so the returning type complies and exactly matches the declared and integer in the returning type of the function creation okay this function takes two perimeters and return the sum of their value you can assign this function first of all before we assign if we need to call this method add four and five okay so it will add that okay but we can assign this function to a variable using the method reference operator let me write it down method reference operator it is denoted by two points two points and two points four points you can make it look like so okay now how to use this here we can make a var variable and i call function i could name it as function and i assign it equal to two dot add function okay here the name of the variable is function and its type is inferred as int int you see guys this is the this is the function the variable and is infer that its returning type is inferred as in because this function and has two perimeters from f and and int we will return an end so the returning type of this function would be an int okay from the add function you assign it the function variable is of a function type that takes two parameters and return an end okay we have assigned we are telling that for this variable called function is of type function okay let me rename it as v or t that so the variable t now is a function type that takes two perimeter of type integer and return n n it may be sometimes be confusing for you and especially if you have an experience in java or on any other programming languages but please concentrate with me i have created a function containing uh two returning types sorry returning type of integer and two perimeters this would be stored inside a variable and it is transforming not storing it is transforming the function into a variable now i have created a function a variable t of type function add okay so this is the method reference operator two dots two dots okay and i am referring it now t is a variable con that takes two perimeters you see guys we need to pass x and y five and five okay this is how we can call this variable now you can use the function variable in just as the same way you had used the add function okay now to print it i need to just print t let's run and see oops print t that's returning type and where result okay i will remove this and t now is the returning type of the function if we have c that function add kotlin in and kotlin in and it return an end i need the fact that you can assign functions to variables come in handy because it means that you can pass function to another function how to do that this will make some confused some confused confusion to you but it is very simple okay i will create a method called here function print result and i pass t that variable that we have created of end and end and we need to make this as an integer to be passed an integer as a returning type and we have a as integer and b as an integer so so we have past three perimeters one two and three okay what does this function mean makes it will store inside val result equal to t of a and b okay i am calling this method t which is a function variable and i am passing this variable one and the second variable into inside inside the its perimeters and then i need to print result okay and the print result would be passed here i'm i will call it in the main function so print result and i pass here the data the i need to create it first the variable t so variable t is a function variable and now print result and i need to pass the reference so here i can't put add or i can't pass t okay four and two let me run and see and here we go we have the addition text place how let's make a recap we have created a function add i have assigned it to a variable i have created a function variable that the method reference operator is assigned to it so this variable now is called t as a function as having two parameters and return an integer and i have created another function that took this t this variable uh this uh function variable the second one the second the parameter is an integer and the third parameter also an integer now i create a val result t a o and b so this will add make addition of the third the second perimeter and the third parameter using the function variable called t so i'm using this variable this first perimeter and i have passed in the side the first perimeter a and b as its perimeters it is like uh think of think of about it as a nested perimeters so function inside the function so i'm having a variable think about it as a function a normal function having a variable a and variable variable b as parameters and returning type of it will be an integer because we have defined it here and we stored it inside the result variable and we have printed it we call inside the main function the we have created the varti this is the method reference operator telling that t will acts as add function and the print result passing the value function variable the a and b variables and takes place the addition operation and printing out so it's very simple it may be confusing but we are not going to use this in many many times and many many things or many ways that we can make directly make a function and call it without using a method reference operator but we should mentioned it to make it very simple thank you guys i hope you i hope you rate us five stars on udemy and support us in many fields buy comments and like hello my friends and welcome again to the complete cutling course in this video we'll start learning about something called nullability so all the variables and constants you've dealt with so far have had a concrete values when you had a string variable like uh var name equal to let me say jack you associated a value to this name and if we remove this we have ins instantiate it with empty name okay no name but it have a value with a no space and no characters but it have a value this is the difference between var name var equal to a var name equal to empty string and var equal to null so what is null and what is the usage of this sometimes it's useful to be able to represent the absence of a value imagine a scenario where you need to refer a person's identifying information like a name and you want to store his person's name age and occupation name and age are both things that must have a value everyone has them but not everyone is employee so var like age equal to 29 and var var occupation maybe it is um for you as it is student no occupation for me as a developer so i can developer okay so and the name everyone have a name so like jack 29 and this develop but what if i become an unemployee so maybe i have reached enlightenment and which to live out of rest of my days on top of amount this is when it would be useful to be able to refer to an absence of the value okay so this will indicate if i remove this it will indicate that i am i'm having an occupation okay i am having an occupation but it is not null so if i refer as a null it would remove all and it will indicate the absence of a value okay null is the name given to the absence of a value okay absence of eval and you are about to see how kotlin incorporates this concept directly into language in a rather elegant way some other programming languages simply use sinito some have the concept of null value but is merely a synonym of for zero it's just another's value like zeros but null is not making absence of a value kotlin introduced a whole new set of types called nullable types that handles the possibility of a value could be none okay if you are handling a non-null value they are then you are guaranteed to have a value and don't need to worry about the existence of a valid value similarly if you are using a nullable type then you know you must handle the null case it removes the ambiguity introduced in using other values so think of a nullable as a box it either contains a value or it does not let me show you how so as i told you think about noble as a box here and here another box it either contain a value or it does not when it does not contain a value it is said to be containing none so here no nothing if i put here null it contain a value the having a null okay so i can't put anything here in order to introduce it as a null so nothing it is void it's nothing contained inside this box so this is null nullable box containing no value if i store inside this box value 5 so it is not not [Music] ok it is not null box it's very simple a string or an end on the other hand does not have this box around it instead there is always a value such as like hello or one one remember non-null values types are guaranteed to have an actual actual file a value setting the value is very simple we can clarify this by making setting the occupation later on equal to developer okay so i have changed it by this but there is an error here telling change type of occupation to string and we need to make this step in order to make it uh nullable and allow to edit it later okay this is the assertion this is the assertion this is the concept of a changing printing out the value of a nullable or we have changing the value of a null okay so here it was a a nullable value nullable variable we introduced a string that's not this is the string and question mark we are going to learn more about it in the next videos and when we change the nullable we need to make this assertion and we have to introduce the new value so if i print occupation it will give me develop see guys how to change a value of a variable that it was initialized as a null this is how we do okay hello my friends and welcome again to the complete kotlin course we have started the nullability section and we have talked about nullability now let's check for in null it's all well and good that nullables exist but you may be wondering how you can look inside the box and manipulates the value it contains in some limited cases you just use a nullable as if it were a non-null type take a look that if we have create a variable called result and this result would be of containing an integer and having an exclamation mark here setting that it is and now a value of null and if we print ln result this will make things happen and we have print 30. correct now there is one another thing about this if we add plus one what we will do there is an error here if we zoom in operator call correspond to it to a dot qualified call result dot plus 1 which is not allowed on a nullable receiver result okay it does not work because you are trying to add an integer to a box and not a value inside the box but to the box itself that does not make sense if we move this we are setting that where the result is equal to a normal value normal variable of type in and 30 and if we print now it will result in 31 but if we are adding this exclamation mark here saying that for the nullable and it is preventing us from making this okay now let's learn about the not nullable assertion operator the error message gives an indication of the solution it tells you that the nullable is still inside the box you need to remove the value from its box it's like christmas okay so we are making a box here and we need to extract the value from it and add one to it how to do so if we make val verb or [Music] if i need to add one to it i may i make a variable a result plus 1 is equal is an integer in and here equals 30 equal to a result does not equal two times this is the operator not null or assertion operator plus one and now i need to print result plus one variable what we have done here is using this not null assertion operator okay the double exclamation mark after the variable name tells the compiler that you want to look inside the box and take out the value the result is a value of a non-null type this means that results plus one is a type of end and not int exclamation mark the use of word assertion and the exclamation marks probably convoys a sense of danger to you and it should okay so you should use not null assertion sparingly to see why consider what happens when you then nullable does not contain a value so if we have var result equal to null and i'm trying here to add one to it okay there is an error and if i need to print result directly so result your result is variable result okay let's run and see is equal to no but if i put here like this and i make does not equal to null plus one i'm adding to the result plus one there is an error here okay so let's run and see there is a null pointer exception telling us because the variable contain no value when you try to use it that's worse is that you get this exception at runtime rather than the compile time this is for the compiling time for the for use when using the ide intellij but if you have a pointer a or a result here like equal to null and we didn't assert it we didn't try to assert it we and check it for the nullability it will be making and closing runtime error okay so this will lead to null pointer exception how to make this by using the smart cast we are going to learn more about nobility in the next videos if you asked the user to enter his name and he didn't entered anything the value would be as a null as by by default but if you are going to display this result you will display his name as a null this will make some problems in order to prevent entering a nullable value or having a nullable value and under certain condition you can check whether a nullable has a value and if so you can use the variable as if it were not not i will create variable name of type string when you define a name of a variable or create a variable name of type string the default value is none without assigning any thing to it okay so the default value of any string would be not without defining anything to it so without the equal sign it will be not okay another var name not none string but it has a exclamation mark sorry a question to check for the nullability if the entered name and name like name or the un entered enter name is it does not equal to null i will create a variable here called var entered name equal to [Music] jack so i am checking for not null by using exclamation mark and equal sign checking does not equal to null go and make the name equal to entered name otherwise else make the name then nullable author which is a name not null equal to name and third name okay i'm checking for now for the not null and to prevent anything or any null pointer exception error you will immediately notice that there is no exclamation mark here when using the nullable name entered name using nullable checks in this way is an example of kotlin smart cast this is how the smartcast works in kotlin if the nullable contains a value the if expression then executes the first block of code this is the code within which kotlin will smartcast the name to the entered name or the entered name to the name to a regular non null string if the nullable does not contain a value then if expression execute the health condition okay so you can see how using smartcast is much safer than not null assertion and you should use them whenever a nullable might be none not null assertion is only not null assertion is only appropriate when a nullable is guaranteed to contain a value using smartcasts for nullables is only helpful if the nullable being checked is not or is not or cannot be changed after the null checks occur for example if the nullable is assigned to ivar that is not changed after the smartcast occurs and before usage or is assigned to a val so we need to to check that these are the last things that for checking for nullability okay this is for safety and safe casting of the variables and these should be at the last when you finish changing the variables or for using for the constants okay so this is how the checking for nullability may occurs in cutler we will continue with the nullability and we are going to introduce elvis operator there's another handy way to get a value from a nullable you can use it when you want to get a value out of a nullable no matter what and in the case of null you will see and use a default value so we are going to use elvis operator so what is alpha's operator the operator used on the here to extract the uh the the nullability nullable value is called elvis operator since it resembles a certain rockstar when you rotate it by 90 degrees clockwise so this if we have remo if we rotate it 90 degrees it will resemble an elvis star rockstar elvis okay now how to use it var nullable end and equal to 10 okay i'm creating a nullable end and now i need to extract the result and add them result equal to nullable and the elvis operator and zero okay this is how we use the elvis and what this means using the elvis operator means that the result will equal either the value inside the nullable end or zero if the nullable and contains none if this is nullable then it will use zero otherwise it will use result and equal to nullable end let me check this print result let me run nc and here we go we have printed result so it is equal to the nullable end because nullableint is not null if we put here zero or we didn't assemble it to anything of type and and and we assign 0 to it check it it will return also 0. let me check it is five let me run and it is zero because we have and variant is not nullable if we put here null okay if we assign cannot be assigned to an end if we don't assign anything to this null and remove this now let's run and see and now result equal to five so it checks if nullableint is null oh it is null so it will remove this side and took 5 as a result and assign 5 to the result if null and in case it is equal to 10 it is not nullable yes it is not null so go and put this result the same as the nullable end it will print for us okay so this is the importance and this is the usage of the elvis operator well hello my friends and welcome again to the complete kotlin course so far you've mostly seen data in the form of single elements although pairs and triples can have multiple pieces of data you've limited to combining only two or three items with them in this section we will start learning about collection types in coupling collections are flexible containers that let you store any number of values together we will see the arrays lists map sets lambdas and in details so we will start in this lesson with the arrays in kotlin arrays in kotlin correspond to basic to the basic array type available in java arrays are typed just like regular values and var variables and constants and store multiple values in a contiguous portion of memory before we create your first array take some time to consider in detail what an array and why you might want to use one so what is an array an array is an ordered collection of values of the same type this is very important it is a collection of values of the same type you can't make an array that contains strings and integers okay the elements in the array are zero indexed so the first the first element is ali in this array of strings this array of strings the first element in the array is indexed 0. so kotlin start with element 0 just like java which means the index of the first element is zero and the index of the second element bob is one and so on okay this is very important note knowing this you can work out on that that the last elements index is the number of values in the array less than one so we have six elements in this string array the index is six minus one which is five okay all values are of type string so you can't add non-string values like integers inside this array okay so when are val arrays useful arrays are useful when you want to store your items in particular order you may want the elements stored assorted or you may need to fetch element by index without iterating through the entire array let's go to our ide and let's see here how to make the array okay the easiest way to create an array is by using a function from the kotlin standard library called array of this is a concise way to provide array values we start by i will start it and store it inside event like odd numbers equal array off when you see your array off so you are creating an array okay since the array only contains integer kotlin infers the type of odd numbers to be an array of integer values this type is written as a re hint so if we click on on control and hover the mouse ctrl and hover the mouse let me show you how to this work kotlin if we hover the mouse array off you see guys this array and d so already this is in the generic that we are going to discuss it later in the generic so the type inside the angle brackets defines the type of values the array can store which the compiler will enforce when you add elements to the array this is called type safety and we will see them in the generics that to provide a compile time error compile time error and not run time error okay so it will indicate if you add here this will be used if we add here a string called ali or hello okay this will make an error at the compile time okay this is the type safety that we are going to talk about it inside the genetics so you see guys when we hover also on the odd numbers it is of type and it's telling it's telling kotlin that it is an array of integers the syntax of the array type is an example of a type argument or genetics that you will learn more about in the later sections it's also possible to create an array let me show you here how to print it we start by print odd numbers let me run and see and here we go we have printed the index so this is how we have some errors and you may encounter these errors these are not there we are printing the index so this is the index at is the index of this variable inside the memory okay so prints dot we can put get iterator set and so on so we need to make that this works so we are going to learn it in this way so it's possible to create an array with all of its values set to default values like val i need six sixes okay i can create use array and i specify here say let's see i need six elements of this okay so this will print or this will result in six six six six and six okay this you will learn more about the syntax in the in the section lambdas so this is one of the lambdas that we are going to use okay as with any type it's good to practice to declare arrays that aren't going to change as constant using that for example if we are going to create a constant array that contains like volts vowel letters vowel letters array of and we specify here a string or like a e i u o u okay these are constant because they are not going to be changed and vowels is an array of strings and its type cannot be changed okay but one important note that when using array off we are creating now non-primitive data like this so if we use array off and use one and then we insert one here one is represented as an object because if we see the array of type t this is t and it is of integer of object call of type integer and not a primitive data of integers when using array of and creating arrays with type such as array and the resulting array in a list object of types okay in particular if you are running on jvm java virtual machine the integer will be boxed inside integer class this is called boxing and in unboxing okay so uh using the primitive types over their boxed con counterparts will consume less memory and result in better performance unfortunately you cannot use primitive primitives with list okay we'll talk about the list in the next video so the kotlin standard library contains function other than array of that makes it possible to create arrays that correspond to arrays of primitive type for example if i want to make the same array here but for parameters so val even numbers equal and array off okay see you guys in array off we store 2 4 6 8 and so when running kotlin on jpm the even numbers is compiled to java array of enter this will result in compiled in interlay while the first one is compiled of array array list of type integer okay other standard library com functions include float array of you can see float array float array of is the same okay double boolean array off and so on so these are the primitive data arrays and you can make an array but this is of array of objects okay so using a layoff deals with elements are as objects while int array off deals with it as a primitive data and this is more faster and we are recommending using int array of despite array of when using primitives like integers car booleans and so on okay lists a type that is very similar conceptually to an array like java the list type in kotlin is an interface that has concrete realization in types such as arraylist linkedlists and others arrays are typically more efficient than list in terms of railroad performance but lists have the addition feature of being dynamically sized that is a raise of fixed size this is the very important note between the list and the difference between the list and arrays arrays has fixed size while the list has a dynamic size so you can grow up and shrink the list as needed as you will see in the mutable list later on so how to create the list we go to the intel j and we start creating our list let me go up okay so like arrays we start with a val we are storing and at a a variable storing a list inside a variable and we assign it so like the arrays the standard library has a function to create a list okay like uh planet list equal to list of like an array of see guys array of we have used it with arrays we start with list of and we specify here remove this and mercury earth and mars mars this is we have how to create a planet list using the list the type of inner planets is inferred to be list of string we have seen that here we need to put the angled breaks this diamond shape and we need to specify as a string but thanks for the latest updates of the kotlin that we can detect or the kotlin detect directly that it is a list of mercury so no need to put and insert here the type perimeter which is a string inside these diamond shaped angle brackets okay so it directly knows that it is a string list so this is an array list in java okay list of string you see guys it is detecting it as a list of a string now if for some reason you explicitly want to make arraylist to have the type arraylist there is a difference a different standard library function that you can use like arraylist off you see guys at a list off okay so we can make this also in an array list but we prefer making it list okay as or an empty list can be created by passing you know no arguments to the list because the compiler is not able to infer a type from this you need to use the type declaration to make the type explicitly empty list call val empty list equal we need to infer type because we are not detecting the compiler will not detect what is the type of the list he is going to do so i'm going to tell him that i need to make a list of strings okay equal a list of okay this is the function that we are going to use to create an empty list you could also put the type arguments on the function okay like list off can make like this remove this and we put list of here s3 okay so we are creating an empty list of strings that we are going to add and learn in the next videos how to add data inside the list and remove them okay since the list returns returned from list of is immutable we won't be able to do much with the empty list empty lists become more useful as starting point for for a list when they are mutable lists so what are mutable lists and how to make them we start once again the standard library in kotlin has a function to use is called mutable list of okay so i will remove this and make mutable mutable list one okay this is called mutable list of we see guys we are using the functions of the of the standard library i will start by using one here empty i may i'm making a mutable list of strings having two elements one and two okay you have made this mutable list number one a mutable list just in case or you are going to discover more and add more mutable elements inside this mutable list you can create an empty mutable list by passing no arguments to the function or you can make like this i can remove them and i need to make sorry a mutable list of and we pass the type of data that we are going to make the list so this mutable list one is going to be as a string list but it is empty okay so this is empty list but it is of type mutable list mutable lists okay so the mutable list have the uh capability of and dynamic addition insertion and deletion and iteration and so on okay but then now i will make it having two elements okay this is very uh simple we will learn in next video how to access elements how to add elements and so on to the lists being able to create arrays and lists is useless unless you know how to fetch values from them in this video we are going to learn the several ways to access the elements in both arrays and the lists and they are very similar so what it is applicable for unless is 99 applicable to the arrays using the properties and methods so accessing elements here one using properties and methods imagine you are creating a game of cards and you want to store players name in the list the list will need to change as players join or leave the game so you need to declare a mutable list that we have learned in the before okay like the mutable list okay so i will consider this mutable list as the list that we are going to access its elements and to work on it before we start the game of making it making the game for the players which we need to check if uh if there are enough players and for so we know we need to use the is empty and it's the same as we are going to check for the mutable list if con it contains con elements or no so i will start checking uh and using the properties and the methods for the list so mutable list one and we see dot we have many properties and methods we have add replace at all remove set size and so on we are going to use now learn about is empty this is empty method or function is it for checking if it is uh if the list is empty or not it returns the returning type of it is a boolean so boolean it will return a true or false so true if it is empty and false is for it is containing elements print if mutable list one is empty let's go up and run see the result if mutable list is empty or not and here we go we have false because it contains two elements one and two as strings okay this is the first method we're going to talk about it the second method that we are going to talk about it is the size and it is a sorry it is not a method it is a property the list is not empty but you need at least like two players to play the game so you are going to use that size and again if you need to make and get the size for like checking for certain reasons like a game we use dot size so let's go up and see how it works let me print print new line and the print line run again and here we go is checking for it is not immutable uh not empty the mutable list one and we have two elements you see guys mutable list dot size contains two elements and it is correct because this is the first element and this is the second l okay now we will continue with properties and method and we are going to learn about the first method so imagine that the list provide that you are creating we need to get the first element inside so print ln mutable list dot first so we have a method called first and i need to get the last element so mutable the list dot last element let's run and see you you see guys that the first one and is for empty the size is the second line and the first element inside the list is one and the last element in the list is two and this is correct because this is our the list that we are going and what that we are performing our actions on in on it okay so this is for the methods and properties now let's use the indexing to indexing the most convenient way to access elements in an array or list is by using the indexing syntax that we are going to talk about it in the next video and see how to iterate through the elements the most convenient way to access element in array or list is by using the indexing syntax the syntax lets you to access any value directly by using its index inside square bracket so we are working on a mutable list and it contains two elements one and two of type string okay now let's index and try to access the these elements i will create a print print ln i put here first element and i try to to put here the mutable data immutable list dot not dot we are not going to make the first or we are going to use the end the index so we are going first of all i will remove them not to be confused and later on i will add it i put to use indexing i will put the name of the list and inside a square bracket okay i put the antics as i told you in java and kotlin there is the numbering and the indexing start from zero so the first element inside the mutable list called one have an index zero so i need to access this element i specify the index here let's run and see if it is printing the last one and here we go it is the first one is printing and it's called one so the element one is accessed let me add some data to the three four if i need to access the third element 0 1 2 and 3. so this is the third element but it is an index two so i need not to put three i need to put the two okay so two means the third elements let's run and see if it changed from one to three and here we go we have three okay this is how we use the index to uh to access the elements because arrays and lists are and next you use the 0 to fetch the first object the indexing syntax is equivalent to calling get method on array or list and passing in the index as an argument another way of making this i need to get the element so i put here the mutable list one and despite putting here the index i can put dot and i call the method called get and i specify the index which is like one which is a second element inside the method which is the list which is do let's run and see and here we go this is the second element inside the array of of the order of the list and we are going to use tech so we have used uh the properties and the methods first and last to get the first element and the last element we have used the indexing by putting it inside angle brackets and we have accessed the element by using the get method so we specify also the index inside the brackets okay this is this is how we work and access with the elements now we are going to use the range for slicing you can use the slice method with the ranges to fetch more than single value from an array or list as i told you this for the list is applicable for the array so we can specify all of these data inside and all of these methods we can perform them inside the arrays but we are preferring to work with the list to be clarified okay so if i need to slice i will put here slicing val slice sliced equal to mutable data or mutable list dot slice you see guys this is the this is the method and it will return the indices in iterable and we will learn about iterable in the next videos so if i need to slice the range from 1 to 3 and then print ln sliced let's run and see and you see guys that we have sliced the elements from 1 2 3 included this is the range you remember the range so we are using and slicing putting them inside a variable called sliced and we have started from one two and three so we have three values and here we go two three four okay this is how we work with slicing and it is it's a good method but it is not we are not going to use it many and frequently okay now we are going to check for an element you can check if there is at least one occurrence of a specific element by using the in operator which returns it through if it finds the element and false otherwise you can also use this strategy to write a function that checks for a given a name inside the names array so we'll start by simple typing so if i want to check for the existence of an element inside array we start by i will print directly there the rule so i am checking for let me say uh jack it will print this and then just a normal message and as i told you we are going to use the n operator so print ln if guys please concentrate on this writing if jack in names array we are checking if jack the same typing the same letters capital j ak because it is say a case sensitive okay jack in names array we have used the n operator okay let me run and see and here we go checking for jack normal printing and the true because it found jack inside the mutable list now checking for um like johnny or john let me run and see if it is included inside the names array and here we go we have false okay so this is the checking of an element inside the array okay now if we are going to write this inside an element inside an um a function and we are checking for the existence of the element how to do that we start by function is found bypass a parameter name of type string and it will return a boolean return name in and you specify here if i need to is found to check the name inside that this array so i need to pass this array also so i'm going to make names array of type array the string okay names are a and i will name it this as array1 so this function is very important because we are checking the existence of a given name a given name inside an array so it took two parameters the array that we are going to search inside it and the element that we are going to search for so if i need to call inside the main function is found so is found here i need to pass two parameters the name of the s string and the array of strings that we are going to search for or inside it now i need to search for george inside array one okay type mismatch mutable less mutable mutable mutable list okay i have modified the mutable list type pyramid now i need to print out this to see if it is found as i told you you can directly put is found it will return a boolean and we will see if george exists inside the mutable list array number one and here we go george is not included now let me try ali ali let's run and see and here we go we have true so ali is existed inside the array one and so on so in this way we have created a function that checks that checks for the existence of an element inside an array and it's very important to learn the algorithm like passing the name that we are going to search for and the name array the array that we are going search inside it okay so this is the in operator and it's very you can make all kinds of changes to mutable lists such as adding and removing elements updating existing existing values and moving elements around into a different order in this video we will start working with the modifying of the list and we will start by appending elements okay so modifying the list would be [Music] one appending appending elements okay so if we need to add more names to the to this mutable list how to add it if we need to sign up if you if you make a system for a company and you need to add new users and new subscribers or new customers and you need to add a a sign up functionality for this these subscribers or customers how to do that you can by using the mutable list it's very easy you can make by calling array1 dot and we have many functions as i told you the previous videos and we use the add function so add function we have here two things like element and string and index of type and and element string so we can what does this mean you can either put a string so it will be added directly or we can add an index and the element inside it we will see the both functions don't worry i will add like what is the name like sarah okay i will add sarat the array now let's print the array one and let me see that if it is adding sarah or not and here we go guys sarah is ad have been added to the array one correctly but guys see this that we are attending elements appending is adding the elements to the la to the last index so at the end okay so let's search for the the the last of the the end of the list and we add it to the empty space think about an empty space here and we are adding new elements inside this for in this array or this mutable list and at the end so appending elements is done or takes takes place at the end of the list okay this is the very important note i i should mention to to allow you understand that concept and the algorithm of adding elements okay now there is another way to write this is if i need to add more elements to the array one as we have learned in the operators and the variables i can make array one plus equal so this plus equal means that the new element the new array is equal to the old array plus what i need to do i need to insert one element okay okay so plus equal to [Music] let me say okay now let's print ln array one to see if carla have been added to there and here we go guys you see carla have been added to the names array okay correctly and we are appending so sarah is added to the end of the list then followed by kal okay this is the importance of understanding the different writing of this in order not to be surprised or waste time searching for what people made or what people write their syntax okay now if i need multiple if i need to add multiple data how to do that okay uh how to insert elements how to delete elements or how to make some modifications to this okay this is the importance of the importance of understanding the data structures and how to deal with them so in the next videos we will start learning and inserting elements and and the deleting elements and many in the previous video we have started modifying the lists in kotlin and we have learned about appending elements and we have learned the two ways of writing the or appending elements at the end of the list but think about what i if if i need to add elements at specific position so in this video we are going to learn adding or inserting elements inside the specific position inside the array so inserting elements if i need not to uh add the elements at the end of the array or a list or a collection and as i told you these concepts and these algorithms are applicable for all collections inside cut okay so if i need to insert a new string a new name at the end at a specific position between muhammad and jack like this or jack and perdler so i need uh to put here one like lara what what what i can do so inserting elements in specific position at specific position for specific entries okay is done by the add position remember in the previous video i have showed you that add here have this this noting and this hint that it accepts an element that's a directly of type string as we have learned in the previous video so we have added zara directly and it is appending the element it's putting it at the end of the array while using the second one the second case that we can make is index and and element string so it accepts two parameters the first one of type end that represent the index and the second one is of type string it is representing the element okay what does this mean if i need to insert as i told you lara between jack and perla the index 0 1 2 and 3. if i need to insert it at index 3 so perla would be index four so i put here three and i need to specify lar the element okay now i need to print print and array one let's run and see and here we go guys ali muhammad jack lara and perla we have inserted lara between jack and pal okay guys this is very important thing that you should understand and you should take aware of it okay the first argument defines and where you want to add the element and the second one what is the element you are going to add to the list okay this is inserting element and specific index and this is the appending elements without specifying the index it would take place at the end of the list we will continue with modifying the lists and arrays and we have seen how to append the elements and how to insert elements in specific position in this video we will learn about removing elements from array or list okay so as i told you our array is a mutable list of ali muhammad jack and perl these are type strings now we have added sara and karla and we have added at the specific index now we need to remove so you will encounter like uh in the company that a subscriber or a customer we need to remove it from our database because he his license is valid or is not valid or it is expired or he need to cancel the subscription to our company or anything so we need to remove it from the array or the immutable list that we have stored it inside the inside a collection of an array or a list so how to remove this we make the coding simple by array one by calling one this is that variable this is the immutable list that we have and we click and control and space so we have these more these bunch of methods and we see here there is a method called remove so this method removed if we see there is an element string or an element string and the index is deprecated in the later version of cutlet okay so remove method removes a single instance of s of the specified element from this collection if it present returns true if the element has been successfully removed and false if it was present in there it was not presented in the collection okay now let's try to remove jack okay and again i will print the array one let's run and see after removing jack and here we go check is absent and have been successfully removed let's try to run and see as i told you it is successfully have been removed now this method does two things it removes the element android then returns a boolean indicating whether the removal was successful or not so that you can make sure that the sheeter has been removed like that like jack have been removed from the list how to print this result it's very simple we can copy the cut and we replace it to print the result so true it will indicate that jack has been removed false that jack is not that is that jack is not represented in the list or the array and has not been removed so true is indicating for us that jack it was in the list and we have removed it successfully okay now let's try to remove like fatty it will indicate false so because fatty is not enlisted inside the list or the array and it's not found in the elements that we have added so it will remove because it not be removed and returned false because it is not found inside the elements of the list okay it's very simple now to remove an element removing the element at specific index we can specify it like remove at so i will array 1 dot remove at this is the method that it is used and here we have index end so we specify the index end i need to remove the first element which is ali so i go down and remove at index 0. now if we print array 1 and click run we will see that the list this is i have been removing ali from the first okay so this is how we can make the removing at specific index so we remove an element by its value or we can remove an element by its index remove and remove add are used to remove the elements from the list or the array we will continue learning about modifying the lists and we have seen how to append the elements insert elements and remove elements from the array now how to update the elements if i need to update like perla i need to replace it with its sister like pamela so i need to update the elements without removing its current state so uh think about updating like pearl i need to give her account number or her subscription uh in your company to its family to its sister pamela okay so i need to replace perla with pamela it's at index zero one two and three okay and we have removed zero index zero so it perla would be at index and two okay so how to update the array the array or the list i specify array one and there is no no method like this to directly make it we specify the index of that compo the element that we need to update and it's very simple like specifying what it's the new value perla and we specify it by pa okay so we are going to replace pamela and repurla with pamela okay just indicate the array element the array name and the index between the two brackets if we print the array we see that perler has been replaced by family okay this is how we update the elements inside the array and the list but there is very important note be careful not to use a an index beyond the bounds of the list or your code will be crashed example if i'm going to replace array 1 and having index 12 there is no to a index 12 there is no 13 elements inside the mutable list okay we have added zara kara and lara three and we have removed jack and ali at zero so we have added to at three and remove two it will be z one two three four and one only five so twelve element is not existing the twelfth element is not existing or the 12th index is not existing let's run and see and here we go we have an exception so exception having the error index 12 out of bounds or for the length five okay because we have only five elements five elements we can't replace the element at twelve position with family because uh this index is not existing okay we have only five elements we don't have 13 elements to replace the index 12 as the element at index 12 with family this is the error this is the error origin and we need to pay attention for this while updating the elements okay one important note that i should mention this and here like updating the elements if you have an ordered list or an ordered array and you need to sort this element after like updating like removing like inserting there is a method called i will put it here like sorting elements okay so how to sort the elements it's just by putting array1 dot sort okay now let's see array one dot or print one okay still have this and you see guys our elements or our array is sorted correctly in alphabetical order c l m p s so it's very important to use like this method if you are going to sort the list or the array so in this videos we have learned how to modify the list how to insert append remove and update and we have learned about the sorting of the elements so in the next video we are going to learn about the iterating through a list and we will learn how to deal with data structure so data structures are very important uh concepts in every programmer need to advance his career and need to be professional he need to master uh star data structure okay so thank you guys see you next in the previous videos we have learned about how to make modifications to the list and also arrays how to append how to insert how to remove and how to update update elements also we have introduced the sorting now we are going to iterate through a list okay so iterating the or through a list while we are going to make an iteration the iteration means that we are going to search and loop through every element inside the list okay so how to loop through and how to iterate through a list we have introduced the four loops and we have seen that the four loops have their specific ranges and their uh limits and the algorithm how to increase and the increment of the iteration and the index so the loops work through the indexes and the iterating numbers but here we are going to introduce another like form and the shape of syntax for the for loop okay so i this is me this is known as for each loop inside java okay now i will start by since we are going to loop through the array one which is a list of strings we will consider every element inside this list as a user in and we specify the name of the array that we are going to loop through so i'm creating a variable called user this is if you see if it is included and it is designed as a string created as a string and automatically kotlin will create it in temporary memory and it will make it as a user inside array one what does this mean user this every element inside array one will be treated as user so think about a u element every element inside array one is temporary for like one second is called user okay and this is one second is very long for iterating through our array okay but till now think about it every element inside array1 in each loop is treated as user so what we need from this user is like print the user every time every element in array 1 will create a temporary value value eval called user and this user will be treated and used to be printed by print user okay let's run and see this modification but first i will print ln okay i will remove this and we start by running what we have done here is iterating through the a list and every time the loop iterates it will start and create by a user and treat it by printing the user name so carla lara muhammad these are the elements of the array one we are treating every element inside the array alone by making it and storing the element karla inside user okay this is how we work with the for loop and how we iterate through the for loop okay this is very important this is a very classical one which is which be used in java like for each loop and we are going to uh to iterate through every element inside the list the same concept and the same syntax here but without putting that in in java we use another syntax so every for each element inside array one go and store this element inside a user such that we need to use this user to be like uh to be used inside this um curry play braces and inside this curly braces for now we are just printing its name okay we are just printing the name this is how we iterate through the less or array list and they are very important and very simple a map in kotlin is an unordered collection of pairs where each pair is comprised of a key and a value so we here we have the keys to the left and the values to the right think about this map is a collection of the students these are the students and these are the marks or the grades that they have achieved in a given exam so if i need to store like ali have market 17 over 20 muhammad 20 to 20 over 20 jack 19 over 20 sammy 12 over 20. i will give the keys ali and the values the marks okay but what is that benefit from this data structure as shown in the in this diagram keys are unique the same keys cannot appear twice in a map this is a very important note in the case i can't put ali here again because keys are unique but values they can be like 19 20 20 20 and so on they can be duplicated but the keys must be unique all keys have to be of the same type and all values have to be of the same type the keys i can store all the keys of the same type these are the type of strings also the values should be of the same type which are the integers okay i can't store 17 and now i will write a string here either string or integers or any other type of object maps are useful when you want to look up values by means of an identifier for example the table of contents of like this this course names to their pages number so you can make an an index making it easy to skip to the chapter you want to watch okay so maps are very important how is this different from an array with an array you can only fetch a value by its index so in the array we have given if we go if we go back if we go back to the uh to the cutling course okay dot kt file with an array you can only fetch value by its index so we have an array here on mutable list and we can access and fetch the data by its index so we have updated the elements by using its index and arrays we are going to you to access elements by their index okay so with an array you can only fetch a value by its index which has to be an integer and all indices have to be sequential in a map the keys can be of any type that we have seen in the in the presentation and are generally is no particular order okay so let's create a map and see the advantage of using a map over like an array remember guys everything every concept every problem has its own data structures and the best that fits okay so don't always don't like make comparison between arrays maps and you need to understand when to use map when to use arrays when to use lists want to use the sets and so on okay so this is the trick about data structures okay now how to create map creating map the easiest way to create a map is by using the standard library map of function that is included inside the library of kotlin this function takes a ls list of kotlin per object separated by commas if i need to make the the map that i have created before let me show you this map ali 17 muhammad 20 jack 20 these are the keys and these are the values how to create them inside kathleen i start by uh var map equal and we use map off okay this is the method that we are going to use from our or from the kotlin library package now please please concentrate with me here we need to specify the keys and the values the first key we have is ali and 17 the value so i put the key first ali and i use to operator and its value so i am assigning ali to 17. i am telling that ali is a key and 17 is a value okay this is very important this is how we create the value now i need to enter another one so i separate that by a comma the second is muhammad and 2 20 the same as 4 jack 19 and sammy sammy 2 12. okay we have created a map here we have created a map here telling that a key is ali value 17 and so on exactly like this map okay the kotlin pair objects are created using the infects to function okay this is the infects to function note that map key value is the an interface remember map is an interface which you will learn more about it later on the concrete type that is created depends on which standard library function is called the map of function returns an immutable map of fixed size for that you need this is a mutable type we are having mutable or non immutable immutable file map which is fixed size fixed size if i need to get or to create a mutable map of variable or dynamic sized map we need to make a var students students map2 equal to mutable you see guys mutable map off and we start putting what you need from keys and values okay so when you print the students or this is the key and value ah we can't add anything to it while this the mutable map of we can add the keys and the values that we are going to add them later on okay so always use mutable maps of if you have elements to be added keys and values and map of if you have a fixed map okay for sake of demonstration i will use mutable map and the same thing here but for the student map2 so we are going for the next videos for accessing values and using index operators we are going to use a mutable map of to add and make the make operations on these elements okay now let's print this map student map let me remove this or i can comment it and let me start working with this print and here we go guys we have this result every key and its value after we have learned about maps and we have created a first map and printed it now as we have learned in the previous video we need to access these elements so if i provide the key it will give me the value this is the importance and the usage of the map as we have said that ma arrays you provide the index and it will give you the value or the element but here you provide the key and you will get the value and this is very important data structure which is a map that are used in many different types like the students marks that we have seen in the example or any other any other companies like any other projects and so on so as with arrays accessing values is very simple by providing that index so there are several ways to access mapped values okay so using the index operator also we can like a key provide the key and we get the value how to do that maps support using square brackets to access values unlike arrays you don't access a value by its index but rather by its key okay when talking about or when dealing with maps you are dealing with uh keys and while you are dealing with arrays you are dealing with indexes okay if i need to access the the first one the first index this is not index this is a key and value so i provide the key it will give me the value how to do that if i print try to print down the result off please concentrate with me guys i need to get 17 i need to get the value so i provide the key if i know the key which is ali so it's very important very simple so i will start by specify the map students map to and inside the angled brackets i specify the key which is the string okay so this is the string i provide ali and it will give me the value let's run nc and here we go we have 17 okay so this is the value that is associated with the key ali okay this is how we access the students and there the map and by providing the key and giving us a value okay now there is another property by getting like this so in addition to using the indexing this is the method of indexing okay i will make it [Music] accessing accessing maps by by mdx or by key now we are going to learn how to access map maps by a get function so in addition to using indexing you can also use the get function to access a value so print ln the student's name we specify the student's name map and get you see guys this is the method get and i will specify the key it's telling me that the key you should be providing is a string so if i provide an integer there is an error because we are not giving the correct type of key get like jack so jack is having 19. let's run and see okay and here we go we have 19. so we have accessing the elements of the map by the get method okay you specify either the index by this like the key and the angle the bracket or use the get function okay it's very simple maps share many of the same properties and method of other collection types for example both the arrays and maps have is empty and the size of members so if i like properties if i specify print i can access like this student's map dot size i can get it and it is returning an integer okay so the returning type of this property is an integer specifying their size and the student's map dot is empty i can check for this so is empty it will return a boolean returns it true if the map is empty okay contain contains no elements and false otherwise and size this word returns the number of key value pairs in the map these are some of properties of the maps okay we have learned about maps in the previous videos and we have introduced how the how to access the map elements and how to see the property of a map okay and use it now we are going to learn about how to add elements inside the map or how to add elements to the map for the mutable maps okay so as we have learned that map of ma function will provide us with a constant map while mutable map of function will give me or give us a dynamic map it will return as a dynamic map so we are going to use a dynamic map to see how to put data how to insert data inside this map okay students map2 this map is of type mutable map imagine you received more information and more uh like data for uh for uh the for any element okay so like ali 17 and the partial and final like 16. so how to do that okay and you want to add them to the map this is how to add it adding data okay so adding new data or adding a new data fields for keys okay so student map start by student math dot put what is the key i will give the key a data fields for the for map new data field or new data i will add for this map like ali muhammad jax sammy i need to add one another student like um jamil i will insert value here see guys if you are going to try to add and put elements you need to use put and here key and value okay here thanks for cutting and this thanks for the ide that will give you a hint that the first one would be a string and the second will be an integer like this so you are here like strings and integer okay there is even short way to add pairs like using subscripting like like this student maps if i need to add one another one and one another student like called rami i will give it the value here like 15 okay so in this way i have added new data i have added a key jameel and value 14 and i have added rami and value 15 with another syntax let's run and see i need to print students map and here we go guys you see these are the pre-made or pre-inserted data students and we have added successfully jameel and rami okay we have added two new students this way we need to to add and put the new values to the map by using the put method or by using this we have learned how to create immutable map in kotlin and how to access its element how to see its property and how to add new elements to this map now we are going to learn about how to update their values inside a map it appears that it it is very simple if you have learned the errors and how and the list and how to update and access the elements and how to deal with these data structures in kotlin in the same way if we need to update a value in a map it's very similar to array so if i need to update like jack i need to update its value from 19 to 40. so how to update it we start by calling them this we're writing the name of the map and here we have a key a string because because we have specified the keys as a string i need to update jack so it's very simple and very similar to arrays just you put equal sign and this is the ending this is the key when working with maps and you specify the key so i'm telling that i need to go to the students map search for the key jack and updates it's it's mark 240. okay it's very very simple let's print the whole student maps and see if jack was 19 and now it's 14. okay guys now jack is 14. this is very simple okay now if i need to remove the if i need to remove elements from the map removing pairs okay we need to remove all its information like it's ma it's key and its value so the first call to remove method will remove the keys will remove jack and it's sorry it's yes jack and it's associated value from the map which is the great so i need to remove but i put students map dot remove and here we specify the key string or you specify the key and its value or you specify only the key i will specify the key id i want to remove ali from the list now let's run and see after printing students map let's run and see and here we go guys we don't have ali in the map so we have successfully deleted ali from this map okay so this is how we remove elements and prepares from the map and how to update the value of an element inside the map we have learned in the previous videos how to iterate through a list or a map now we are going to learn about iterating through maps the for in loop works when you want to iterate over a map but since the items in a map are pairs you need to use a destructing declaration how to do that the same as arrays i specify here the the the type the data type that i'm going to create temporary i specify in operator and the name of the data structure that i'm going to iterate but in maps there is one single difference which is the key and value so since i'm iterating and getting every time a key and the value i need to store this key and the value in two different variables so i put here the name and i put here the grade these are two variables that i would create every iteration and the specif and make them temporarily assigned to the element of an map so what is the name of the map it's called student map2 every time or every element inside the student maps to will make every in each iteration will be assigned to the temporary created value variables called name and degree how let's see in the practice how this method works print ln and i will create here the name and i will put here also the great okay each time or each loop i will create its key and value and you see guys i have all the elements here inside the map containing key value key value key and value and key and value this is how we make this happen and how to iterate by through iterating the map it's also possible to iterate just over the keys so if i need for specific reason getting the keys only i put here like a name in student's map to print name okay print name but there is an error as i told you i need to specify the two two variables to create two variables when you're dealing with maps in student maps but since i need the keys i need to put here and tell kotlin that hey i'm using that i need the keys on okay i'm getting all the keys let's run and see and here we go i will put them print ln here we go guys we have get it ali muhammad jack and sami okay these are the keys and the same way i can get the values so for grades in studentsmath.values the same way but i put here values okay print ln and i put here the grades is for getting values this loop for getting keys and here we get keys and values let's run and see and here we go we have get it all decorates from this loop we have get it all the keys from this loop and we have get it the key and that's associated value by this loop so you will consider what to do with the map and why to use the specific loop and the specific iteration like key value keys or value well hello my friends and welcome again to the complete kotlin course in this video we are going to learn about the sets so a set is an unordered collection of unique values of the same type this is the definition of the set this can be extremely useful when you want to ensure that an item does not appear more than once in your collection and when the order of your items is not important so sets is an unordered collection of unique values okay the set is an unordered collection but of unique values it does not accept duplicates okay so how to create the set first of all creating set you can declare a set explicitly by using the standard library set of function for example val names equal to set off i need to create a set of what are the data that you are going to make i will make as a string so it took the element of a string directly the same muhammad and others like hadi and i'm creating a set of these okay now print ln the names okay let's run and see ali muhammad hadi and sammy it's very simple but there is a trick here guys let's try to add an element dublicate element like ali and let's run we have five elements but at the by printing it will print ali muhammad hadi and sam the second ali will not be printed because as i told you a set is an unordered collection of unique elements unique elements okay so you can it will delete this dublicates and skip it for uh for the duplication okay it's very simple you can get getting set from array or creating a set from array it's very simple sets can be created from an arrays i will create an array like uh grades array equal to array of and i will make eight and three five these are random numbers you can create a set from this array by passing the array into a standard library function set function and using the spread operator how to use spread operator or what is spread operator i will create a var grades set equal i need to create a set from this array mutable set of and here we need to concentrate the spread operator is the asterisk here and i pass the grades array so i'm making an a variable offset grid set it's immutable set off and passing this array transforming it into a set the array is spread into elements this is the spread operator the array is spread into elements when creating the set you don't have to explicitly declare the variable as immutable set of type in since the type is inferred from the argument passed into the function okay now if i have here an array like i would make a duplicate of 8 and i need to see the power of using this so i need to print grade set okay println let's run and see as i told you the set will not print the duplicates because it is having and unique elements so the var of great set is now a set of this array so it eliminate this eight it deleted it and passed it into inside the great set because it is not unique it is dublicated okay it's very we have learned about sets in cutler and how to create them and how to create a set from an array now we need to learn how to access the elements of the set so in order to do that you can use the contains method to check for the existence of the specific num element or you can use the n to check for the existence we will see both so print ln to see if inside the names name set dot contains this is then the method that we use to check inside the set called names if it contains like lara i will see if it contains lara so let's run and see and it returns false because in the set that we have created in the previous video we don't have lar now we will check for ali and we will see if it contains it and here we go guys we have true so we have this element called ali inside the set called names now we need we can check for the existence using the n operator okay print ln i can check for jack in names okay now let's see if jack is contained in the side this set and it is false okay let's try to check if uh hadi is inside names and here we go guys it is included so we can use in or contains method to check for the existence of the names or or the existence of elements inside a set you can also use the first method and the last method like this names dot last or first method which return one of the elements in the set however because sets are unordered you won't always know exactly which item we will get okay for example print this let's run and see and here we go we have ali but this is for four small elements or small set like four if you write like ali in the first it will not be kept at the first in the set because it is in random hash set okay it's storing them in in a random manner okay now we will try to add and remove the elements from the set so we will try to make application on adding and removing elements from asset this is done by the add function to add the elements to a set if the element already exists the method does not do anything okay so let's try like um we have created names dot add i need to add this is the names i will make it as a mutable set in order to be allow it to be flexible and add the elements and remove elements from set so you see guys this add unresolved reference add because it is not a mutable set i will call it mutable set of so i now the error here have been disappeared so i can now add the elements that i want larry nora names dot add car and so we have added two elements for the for to the set okay let me show you names print the names to see the set now how it looks and you see we have appending them into lara and car okay now how to remove in the same way i can make names dot remove i need to specify the element okay so i can specify here one if i put one because it is different from a string it will give me an error so i need to remove ali how is that done is by removing putting here the if the element exists inside the list is the set this will return true and it will remove it from the system or the from the set if it does not it will do nothing and will remove and will return false and here we go we have removed ali from the from the set so this is how we add and remove elements from the set and make operations on in the previous lessons you told about functions but kotlin has another object you can use to break up code into reusable chunks these objects and other objects are called lambdas so what are lambdas these have many uses and become particularly useful when dealing with collections such as an array and map a lambda expression is simply a function with no name so lambdas simply a function a lambda expression is simply a function with no name you can assign it to a variable and pass it around like any other value this lesson will deal with we will learn about lambdas and we will continue through this section to learn about hernandez and the basics of lambdas so lambdas are also known as anonymous function and derive their name from the lambda calculus of alonso church in which all functions are anonymous lambdas are also synonyms with closures and go with by that name in many other programming languages so variables and constants constants used within the body of ananda are said to have to have been captured by the land okay you can you may ask if lambdas are functions without names then how do you use them to use lambda you first have to assign it to a variable or constant including as an argument to another function so how to declare it i will start by var multiply lambda is of type and we start by this taking two perimeters these are the four parameters they are of type in and this will give us and so this is the lambda expression we specify what data type you are going to use and what are the parameter parameters that you are going to pass for the multiply lambda function and what is the returning time and how it will return and what are the relation of these two parameters for each other we will see don't worry we will clarify everything now multiply lambdas to take two in values and returns an end okay took two values of type and and return a value of type and as a returning value notice that this is exactly the same as a variable declaration a variable declaration for a function okay it is the same as fun multiply lambda multiply function it's taking two parameter a of end and b of in returning type in and we have here the body return a times b okay this is the function multiply function we are making this the same as lambda but we are specifying here the minimum the code minimum codes so remember lambdas are functions anonymous functions with no name okay so we are not making this we are making a var let multiply lambda we specify the data type and what is the solution or the result of this and returning value of this function okay now you assign a lambda to a variable like so how to use the lamp we start by multiply lambda the same as calling function equal to we assign a as end b as integer and here we put int a times b okay a times b on a new line in order to see here that lambda expression on the next line this is similar similar to a function declaration but there is subtle difference there's the same perimeter list but the arrow this is the arrow symbol indicates the returning type the body of the lambda begins after the return type so the body of the lambda is in the on the new line a new line so it will begin after the return type the body of the lambda begins after the return type and the lambda expression returns the value of the last expression in the body so if i have like a plus b here or a a minus b it will return the last line inside the body of the lambda so these will be executed but at last the value will be returned a times b okay so this is very simple with your lambda variable defined you can use it just as it were a function so how to make this multiple i will create a val lambda result equal to multiply lambda i will pass like 7 times 10 and see the result print lambda result lambda result okay now let's run and see and it's very clear that it is 70 and we are working fine so we have created our first lambda and we have specified that the first perimeter is 7 times 10 it will result well hello my friends and welcome again to the complete kotlin course in this video we'll learn about the shorthand syntax of lambdas we have introduced lambdas in the previous video and we have said that a lambda expression is simply a function with no name okay so compared to functions lambdas are designed to be light weight there are many ways to shorten their syntax first you can use scotland's type inference to in shorten the syntax by removing the type information i will transform this into this multiply lambda into a shorthand lambdas how to do that i will make short and lambdas without specifying the type we can directly write multiply lambda equal to a b will give us this a times b without specifying the end we can directly write a b is a times b and we have agreed that a times b the lambda expression would be on the new line so the new line will determine the lambda and we have said that the returning type of this function or this lambda would be the last line inside this between two curly braces okay first or remember you already declared multiply lambda as a lambda taking into a two integers and returning and in so i can remove this and keep on the shorthand length okay so you can let kotlin infer these types for you now we are going to introduce the it keyword okay so have learned about the multiply lambda the multiply lambda which is in lambda and we have written it into a shorthand lamp now in lambdas we will need to uh we can use a further operator called it so for a lambda that has only one perimeter you can shorten it even further using the it keyword okay it keyboard i will create var double lambda equal to this is for one one perimeter a is one and and two times a the returning type or the returning value of this lambda would be 2 times a we are doubling it since there is only one perimeter and the lambda type is now specified the lambda can be shortened by double lambda can shorten shorten lambda by specifying double lambda equal to two times it you see guys this is the keyword called it you can also use it in the new declaration like var square of end will give us end equal to it times it okay it times it see guys this is the square and we you can use it for the only with lambdas with only one perimeter okay this is the it keyword okay now lambdas versus as arguments you can make them as an arguments so you can make a function and make it the result looks like an a repeated one or a recursive by declaring a function that make a lambda as argument don't worry we are going to see this in the next hello my friends and welcome again to the complete kotlin course in this video we will learn about the custom sorting with lambdas lambdas come in handy when you start looking deeper at collections we have learned how to sort an array in the previous video so if i need to sort an array i will create an array called names equal array of like 1 5 2 4 and 8. and i need two numbers here [Music] and i need to sort this array dot sort okay it will sort the array by specifying a custom lambdas passed by by to comp to compare by a method which returns a comparator for a sorted with so if i if i need to make a lambda that it is specifying its custom condition how to do that i will create a valve a lambda names by like names by length equal here i need to specify the array so i will create a new array called names equal to like aaa and an n m if i need to make another one and this is array of sorry if i need to make new like p or rr and cc look guys here if i call the names dot sort or sorted here we are sorting the arrays according to the alphabetical order let's print this and you will see why i'm doing this so if we look we look here we have this print names let me check again okay i'm printing the index the address here we need to use arrays dot to string okay and let me run and see here we have sorted the array according to the alphabetical order a c n and r okay please concentrate with me guys this is the order of the for the compatible interface this will be comparing objects of the string according to the alphabetical order now if i need to sort this array these elements of an array in a different concept in a different criteria so i need to to sort the names according to the length of the name so from from the biggest to the uh to the from the longest which is cc n a and then r how to do that i told you that lambdas will help you making this with a kotlin and we start by the array name sorted with this is the method that we are going to use and compare we are comparing by compare by method okay this is very important guys and we are goodbye going to compare it dot length okay we are specifying we are passing the compare by and this is the lambda expression and we are comparing by the length so it we see the it operator in the previous video and we are making a lambda expression here and telling that we need to sort that this array called names into a according to the length of the elements so the longest one would be the first which is cccc and an nn a a a and rr okay this is how we make this comparison let's print and see arrays again arrays dot to string and names by length okay or we can specify directly this print and okay and here we go guys the first ordering is the uh ordinary this is the original and natural ordering for any string or any object like comparing it with alphabetical orders order or if we have an object of type and passed to the arrays and we need to sort this array it will and create sort it by increasing order now if we need to modify the criteria of making and sorting things and sorting objects of the array we could use the lambdas so lambdas are very important in terms of using them with the collections like sorting and modifying the natural things like here we have sorted the array by the length of its element and nuts and not in the alphabetical order in kotlin collections implement some very handy features often associated with functional programming in this video we are going to learn about iterating over collections with lambdas these features come in the shape of functions that can apply to a collection to perform an operation on it operations include things like transforming each element or filtering out certain elements these functions make us or make use of lambdas the first of these functions let you loop over elements in a collection and perform an operation like this that we are going to do it val for example val values equal list of and numbers okay values dot for each you see guys this for each and without using the four each we are going for each with the parenthesis we are going to use it with the curly braces and print ln this will print for each one variable this is the variable it so it will print it and it will print its square so how to do that this is very simple like specifying here what we are going to do it times it we are multiple we have seen the the square here i need to remove it and this okay we have seen this example it times it in the previous video and where we have talked about the square and it here just for telling that every time you pass me a perimeter i will display it display it means i will display every element i am passing so it refers to that perimeter so value of the perimeter is now the value of perimeter is then timed times by or time by uh multiplied by the same value it so for example one is passed here one is multiplied by one times one which one times one is equal one and it return one as a constant of value which is here referring to a string in green and the variable the result here would be in a dollar sign this dollar sign in orange sign so this result would be the variable okay so again if i pass three three times three it it will be passed forever the perimeter will be passed for every it and replace it so it refers to the parameter past three this is the constant variable it would be uh or three here would be three times three nine so it would be nine and this would be passed as after calculation two three dot is equal to nine it's squared is equal to 9. okay let me try to run this and see the results and here we go guys 1 1 2 4 3 9 4 16 5 25 this loop through each item in the collection printing the value and it's squared this is iterating over over a collection using the lambdas another function allows you to filter out certain elements like the filter so you understand this very well okay and it's very simple now if i need to iterate to allow to filter other certain elements okay how to do so so if i need to filter a list if it is above 10 so i will create var like values equal to list of i will remove list of and like 6 8 10 17 5 i use var value larger than n equal to values dot filter this filter would be passed like it dot or greater than 10. okay now if i print larger than 10. here we go we have larger than 10 is 17. if i put equal it will give me 10 and 70. so this way we work with lambdas in collections okay so collections are very powerful when using lambdas and lambdas are created for the purpose of using them with collections okay and you can make map you can make a user input map you can map not null and many things with the lambdas and it's very a very big thing to use a very big concept using lambdas with collections okay don't worry we are going to practice uh to practice exercise exercises and we are going to make uh building in many types and many things later on many data structures using the lambdas so don't worry we'll clarify everything if there is anything be being dope hello my friends and welcome again to the complete kotlin course we will start the object oriented programming in cutler so you will get acquainted with the classes which are the which are name types so classes are one of the cornerstones of the object-oriented programming a style of programming where the types have both data and behavior in classes data takes the form of properties and behavior is implemented using functions called methods okay so a class will have data takes data takes think about a class is a template of creating the objects later on and the object have or the class data takes the form of properties okay and the behavior is implemented using functions called methods or using methods okay so data takes the form of properties and the behavior is in form of methods so we have a method and a property forms of inside the class okay so a class is consist of behavior and properties data and behavior properties or methods and methods creating the class how to create a class consider the following class definition in kotlin i will make this i will remove here in order to create a new class in kotlin we go to the source we open the project select the project go to source here and we select onto new kotlin class or file and we select the name of this class i will name it as person so this person have been created here you see guys we have a class here and our main one is here that we have created in the previous videos i will close it and now the person here is the class that we are going to work with a class will begin with a class keyword okay the name is person is the same as the file okay it's very simple now i will modify this and later on i will clarify first name of type string last name of type string also and here i will make val full name is equal to get function equal to first name last may what i am doing what is this writing that's simple enough the class keyword is followed by the name of the class as i told you inside the parenthesis after the class name is the primary constructor so this is the this is called the primary constructor when you see a method like a method here and having the same name and aiming as the name of the file of kotlin so consider this as a constructor okay this is the main constructor or the primary constructor of a class and the for a person you are indicating that there are two mutable string properties i am telling this i need i need for the constructor to have two uh two variables a string of type string the first name and the last name and then you will see how to create other constructors later on in the advanced classes but everything in curly braces is a member of that class so curly braces here and this is the member of the class you create an instance of a class by using the class name and passing in arguments to the constructor how to do that this is the class name and if i go to the cutling course again in the our main function and i need to create a new object creating an object here an object from the class from person class how to do that i will create that uh like uh and for sylvester stallone the sylvester stallone equals is a person he is a person having the first name as johnny or john sylvester study and i will pass another like think about it as a perimeter another um another uh stallone story a string is for his last name okay the class instances are the objects of object oriented programming not to be confused with kotlin object keyword okay so person have another property named full name with custom custom getter that uses the other property in its definition how to get this we see that the person accept a first name and last name but in the inside the class here we have another like algorithm like a full name is getting is a getter method it will return the first name a parameter space last name as a string so get it will get the full name van what it is containing inside the full name is the first name and the last name separated by space and they are returned as a string let's see how this look and how this worked all together by println sylvester stallone though and you see the properties here that they are appeared in the list first name level name and the last name i need to get the full name okay let's run and see and here we go guys this this are these are the first name and the last name this is the full name of the person that we have created called silv style okay in kotlin an instance of a class is a mutable object classes are reference types and a variable of a class type does not store an actual instance but a reference to a location in memory that stores the instance if you were to create a simple person object or class instance with only a name like this so if we have a var from this type we consider making in far one with a reference to its value so i'm making a reference type of simple simple person named jack and the reference here is for the var so how to do that in kotlin it's very simple in kotlin if we go to here and make a another kotlin class called [Music] like simple person and i will scroll up and here i make i need to pass its constructor parameters val name and of type string and i need from this to make a var var1 equal to simple person constructor this is the where we have need we need to get it you can make a product printing for the for here for the class but i will not make it here now i will create an object from this class like var va person1 equals simple person and i will pass like check so it's the same as here we have a reference var and reference referencing for a jack object okay so this person having a simple person and an instance of the simple person class but there is something that it is special for the referencing going here we have reference in var1 but if we have a reference var2 having the same reference it will give us the same object the same value so we refer this bar to have different reference and uh sorry the var1 and var2 are different but the reference are the same so it will give the same result as check so the main important point i need to you to understand that when dealing with the reference you have the same reference so you will get the same result despite they are of different objects so far one is different from var2 they are the same type but different type of different location in memory but the reference of their this would be the same so var1 is a jack and var2 is jack how to do that in kotlin it's very simple so i'm making a var var2 or person2 is equal to person one is equal to person one this is the referencing i am telling that person two reference is having the same reference as person one so go and get person one which is get a jack so i will print if i have uh the printing so i will try to print let me show you person two let me show you if it will print a reference yes it this is the reference they are having the same reference see guys they are of the same reference okay so this is how to deal with their references this is the type reference in kotlin and there is a very important note that i should mention this before we we end the reference type when you create a reference type such as a class the system stores the actual instance in a region of memory known as a heap references to the class instances are stored in a region of memory called stack so reference is stored in the stack or and the actual instances of regional memory known as heap okay unless the reference is part of a class instance in which case in which case the reference is stored on the heap with the rest of the class instance both the heap and the stack have essential role in execution of any problem problem and the program okay so this is a very important thing that i should mention before ending this and moving towards the more and more advanced things okay so thank you hello my friends and welcome again to the complete kotlin course in this video we'll make a real a class exercise that will contain uh classes contain collection and we we will practice the operations and the execution of functions inside the classes so welcome again and in this exercise we are asked to make a grade class and student class in which a student have a first name last name a collection of grades objects and a credit while the class or grade will contain the letter that it's specifying the name of uh of the subject the points and the credit of this this subject okay so first of all i will create a class called great okay i will remove this simple persons and i will create here source new java class sorry not java we are working with kotlin yes so i will create great custom code grade custom file and this class will took inside it in a parameter as a parameter for its constructor letter of string it will take that points as double and then credits as double okay this is the grade class okay it's very simple class okay now i will create a cup a new cutlin class it's named as student so val first name string type string val string called last name this is here sorry this is the constructor last name of type string valve grades of mutable list this is the mutable list of grades okay this is a mutable grade mutable list of grade class that we have created before okay so i'm making a list of grade of all of grades in his subjects okay equal to mutable list of this is how we create it as a parameter inside the constructor of class student for credits double equal to zero point zero okay okay it's very simple now i need to put here the function function like record grade grade grade and grades i am putting this function will add that grade the past the grade to the collection uh called grades of type mutable list of grades okay so i'm making a collection called a list a list of all grades of this student so when i need to add a new record a new grade for any student i will call the function record grade okay grades dot add and grade okay and credits plus equal to create dot credits okay i'm adding also the credits which is equal initial to zero i'm adding the credits that what the student has been marked okay okay guys it's very simple now if i need to use these two classes i will go to the function main function if you if you want to make it runnable but for for purpose of demonstration i make it in a separate file called kotlin course and this will be in a function and runnable function because we are going to test it and display the result so val i will create a student one equal to student this is a student so i need to pass here the perimeter so if you look here the first name of type string i need also to pass a string for for the last name and i need to pass a mutable list of a list of and credits okay so student like uh jack sparrow i will create a subject uh to make it as a grade so in science science subject equal a grade so it he market a letter um he market b in science he achieved 9 over 10 or sorry it is it's achieving 15 over 20 he is clever yes and credits is equal to okay here it is a double zero and point z and now val in mathematics math subject is equal to grade a market letter a is very clever in mathematics and it's 19.0 and here the credit is four okay now student1 dot you see guys here there is grades credits first last name and record great because now because we have created an object an instance from student one student class and we can access all of its components like first name last name grades or credits and also we have access to the function record grade i need to add this grade by calling record grade and i will call science a subject and also student one will record grade math subject okay note that record grade function can mutate the array grades by adding more values to the end like any mutable list grades can be added to even though the grades reference itself is mutable this is independent of the fact that student one is marked as an immutable val reference similarly the credits double value can be changed in record grade because it is defined it's defined as immutable var within the student class so this is the example or this exercise like clarify all things about creating the classes and making a fun a collection and making a function that deals with this object and add with the immutable list to that this to this collection a great so this is how to deal with that functions how to deal with classes and how to deal with collections it's very simple and it's complicated in the same time so please if you underst if you understand everything every concept in in its separate way it's very simple but if it is complicated because you didn't understand every concept so please go back to the course and watch again the classes the functions and the collection to ensure that you understand all these exercises if you understand this exercise and it's very simple so you are clear to go through the course and watch uh the next uh cutlin introduces a new keyword that is unavailable in the other languages to which it is often compared such as java and swift it reduces the object keyword like java and swift kotlin supports object oriented programming as you saw in the previous videos in a class video you use classes to define custom type and in instances of the class are called objects kotlin use object to denote a custom type for which is only a single instance can be created so object is for a single instance while objects are called are for using the custom types and references types the name true choice of the for the object keyword can sometimes lead to a confusion with class instances okay so since they are called objects as you will see in this video and later on we will use object to create anonymous object for which multiple instances are created each time the anonymous object is used another potential source of confusion don't worry object keyword lets you easily implement a common pattern in software development it is a singleton pattern we will uh learn that these patterns later on in the patterns design patterns uh course so don't worry we will clarify everything later on the singleton the single term pattern is one of them of the more straightforward of the design pattern used in software engineering the most object-oriented languages the pattern is used you want to restrict the class to have a single it single instance during any given run of an application so when you're creating a global state or global object or global interfere instance you are making a singleton pattern now let's go to the source folder here and i will create a new kotlin class file and i will name the file x okay here and here we go now i will add here inside the class x an object x okay despite using class i will make an object object x var x equal to z okay now choose tools then kotlin then show kotlin bytecode open a kotlin bytecode pen and in this panel i will show you we have a decompile and these are the these are the codes now i will click on the compile the decompiled java for the simple object will open a new editor win window and it's telling us it is of a public class egg final class x having a static index and instance x not null we have get x and set x these are the getters and the setters and this is the empty constructor okay and this is a static so comparing the java and the kotlin version of the code which is here you will see that the boilerplate singleton setup code has been significantly reduced by using the object keyword so despite making these in java or in kotlin we can use object keyword x var x equal to zero okay it's very simple so where we use singleton an example use case of or is in gilton is an in memory repository for a set of data consider an app that needs a registry for students who are defined with them with a data class like student that we have seen before using object you can create a registry that maintains the list of student in immutable list how to do that okay we need to add all the student in one multimutable as the list how to do that i will remove this i will close this window and inside the source i will create a new kotlin class i will name it as student and i will put here data later on data class we will see it later on does this mean data but then now concentrate with me i'm making an ordinary class that we have created before val id of the student and val first name of type string and val of last name okay val last name also type string okay so var full name equal to last name separated by first name comma okay so i'm creating this data structure this data class is called further student now if i need to create a class instance of the student i will go to the main and i will make like a val a student one equal to student and i will pass the parameters that i need so one like id is equal to ali first name uh val student 2 equal to student id 2 first name is jack and so on using the object keyword you can create a registry that maintains the list of the student in a mutable list lets you add and remove student from the registry and lets you print out the full name of all the students in the registry how to do that i will create a new custom course okay kotlin class sorry and i will create it as a student student registry okay this is the student registry and i will move remove that class and make it as an object okay so this object would be a singleton and what is the benefit of using singleton is ensuring that we have one instance of this object at runtime so what we will do i will make a mutable list as i told you all students it's called all students equal mutable list of end of type students containing all the students okay now i will create a three functions one for adding student add the student it took student a student a variable type student all students dot ad student i'm if i pa i am adding to all students this mutable list this data data collection it i'm adding the past parameter called student to this collection all student by calling ad student okay now the same way i will create a function called remove student student of type student and i will all student dot remove that remove we have seen these functions in the collection framework in the previous videos okay now i need to remove the student this is the student now i will create a function that lists all students so print all students it is avoid it will not take any perimeter all students dot for each and you see here i'm using lambdas that we have seen also in the previous videos and print ln it operator dot full name okay i have created an object called student registry it took it will it having it's having or it has a val all student it's a mutable list it contains three functions one for adding student one for remove and one for printing all the students okay you called methods defined on the object using the object name with dot syntax exactly like uh like any object in the java or anything so student student registry dot you see guys this methods print add and removes and all student this is the val this is immutable list okay so you can access them and student and i will add like student one and then i will make a registry for student 2 okay add student student 2 ok now will make student registry dot print all students if i go here it will give me a print out for all the for each student name or each element inside the collection all student okay now let's run and see i have here the first that's the first name or the last name and the first name last name and the first name of all the students that i have in my database so think about object is and database that you can store it in this uh example i i'm talking about this example student registry where i have uh stored all the students information about in the all student mutable list okay so it's very simple i have uh i have added two students i have created two students references and data types and then i have added to the object this is an object which is a new type in kotlin i have introduced a singleton one which is student registry and i have adding them add student remove student and print all student functions and then i have printing all the elements okay it's very simple this is how we use the object in kotlin in the previous video we have learned about creating a student registry object and the student data class and we have used them inside a runnable main function and we have said that it is a class the object student registry and it has a singleton pattern or a singleton class that ensure that we have one single all students and a student here is a class an ordinary class of data class type and having accepting the id first name and last name but what about not passing the id of the student it making it a an auto increment so we need a static one start a static variable called id but in kotlin there is no static keyword so the static keyword is used in these all other languages like java to denote a class member that is common to all instances of the class so in this case the student have an id that should be aesthetic and shared among all the instances of the class and it is not specific to each instance static members remove the need to dublicate items that are common to all instances but removing this code duplication is useful so so how it does kotlin allow you to define static members you do so by creating a companion object inside the class so what are the companion objects this is similar to the static variable and objects and you can create a companion object by prepending companion to an object defined in the class so we go here to the class student and i make here this val valve and i will make here a companion start by companion object and we define here var equal current id that i am going to make it a static for all the the object and shared among all instances of this class and here i will create function new student and i will put here the first name as string and the last name as a string and it will return and it will return a science scientist or the students list student student list okay so there is one thing that i should mention that when using this function and making a companion you need to make remove the data and make it private constructor private construct okay so making this private constructor will ensure that you understand every concept in making in you a new uh id a new instance and make all these ids are shared among the instances of students okay so every time you create a student there is an id that it is would be added directly like this student list i will make it later on and current id i will add it every time by one so return scientist for this students list student list current id first name and last name okay this is how to make the class a private constructor and modifying the constructor and the data that you should share among all instances by making a companion keyword so the far current id will be increased every time you create a new student a new instance of the student this is the purpose of making the uh the the the use and using the student so now if you create a new a new um a student no need to make here the id so i i can pass here no id okay and you can see here i have changed this from primary okay okay you can remove this and everything will be fine in the previous videos we were introduced to properties as the data members of kotlin classes and objects we are going to go deep with the properties of the class and we are going to make an examples to understand the getters setters accessors and data types later so i will create a new coupling class i will name it as car and it is f type class and inside this class this constructor later on we will talk about constructors so don't worry we are going to talk about uh properties in this video later on the methods and uh later about the constructors so val it will make a make of type string [Music] and val color of type string okay i'm creating a just a basic card class having two property supplied in primary constructor and they store different string values for each instance of car properties can also be set up with custom accessors also known as getters and setters the properties supplied in the class primary constructor use default implementation of the accessor storing the data in the backing field what does this mean later on we'll talk about the custom accessors and initializers don't worry so the constructor properties here as you may have guessed from the examples in the previous video you already familiar with many of the features of properties to review the ma to review this imagine you are building an address book uh the common unit you will need is that a contact like a card here that make and con if i change i i will create a new class here new cutling class i will create contact and here i have to pass also the var full name type string and var email address of type string okay now you can use this class over and over again to build any array of context each contacts each with a different value the properties you want to store are individuals full name and email address okay these are the properties that you are going to store these are the properties of the contact list or contact class you provide a data type for each but opt not to assign a default value because you plan a plan to assign the value upon initialization after all all values will be different for each instance of contact you create an object by passing values as argument into the class primary constructor that we have created in the previous video so properties or properties here i create a val called contact one equal to contact i would put the name of the instance that i am going to create the full name you see guys there is a full name of type string and email address of type string called the jack jack sparrow and the email oops there is an a bug here inside the intellij but don't worry i will contact the company and i will tell them about this so contact again var i'm just retyping them string far email address type string okay now if i want to create a new contact as i told you i will create in your object val or a new type reference reference contact 1 equal [Music] to contact and i pass here the name jack sparrow and and the email address is jack sparrow jack s at jimmy expert okay so we have created a new contact and we have added the full name in the email address now as with any function called not using default values using named arguments in the primary constructor is optional you can access the individual properties using the dot notation now if we need to we have created this instance of contact one we need to access this properties so access the properties val name equal to contact1 dot full name and val email equal to contact1 dot email address okay see guys this is the dot notation if i put here alt plus a control plus space i can access all the methods and the and the properties for the uh for the instance contact one okay for that i will access the email address now i will print that name and then i will print and the email value okay emailie and nami to distinguish them okay guys now let's run and see and here we go we have the name and the email have been successfully uh like getting them from their contact one okay we are extracting the full name and the email address by the dot notation you can assign values to properties as long they are defined as variable since these are defined as variables if we go to the contact and we have var here so here is the far far so we can change them this contact one is jack sparrow if i want to change it without changing the email address so i simply make contact one dot email or name full name equal to harry potter okay now if i print then name me it will be harry potter it is changed to harry potter it is contact one the name okay we are printing the val i will store it in you or i can directly print contact1 so contact contact1.fullname again harry potter since val namey is val and it's not the change i can put it as var you can make it like this okay so you can change the value of the of the contact name and the properties you can change them by making the dot notation and you specify here the equal sign this is the equality sign that is assigning you in a new variable new value okay if you would like to prevent a value from changing you can define a property as a constant instead using val okay it is very important so you can make here that car sorry like a contact without using a ver var it will maybe as val okay these are about making the properties and using properties inside that class we have learned in the previous video how to create a contact class and how to make an instance from this our entire preference okay so and we have told you that uh the contact has a default constructor here that accepts a full name and email address think about if we are going to add a con a sharing and a common variable or a default value for any contact we need to make so if you can make a reasonable assumption about what the value of property should be when the type is initialized you can give the property a default value how to how to do that in kotlin it does not make sense to create the default name or email address for a contact but imagine there is a new property type r to indicate the kind of the of this contact so i will make it var type and it is of type string i will put them on here and i can give it a default value as a friend okay so this would indicate that that default value for any contact that i'm going to create is a friend you can modify it later on like making it a close friend like college friend like a war co-worker or friend of work or anything that you need to do but a default value for this contact is a friend but by assigning a value in the definition of type r type here type r type of relation you give this property a default value any contact created automatically would be a friend unless you change the value of type to something like work or family as i told you okay so if i go here there is no error so no need to pass a second third parameter okay i will create or before i will create print contact one i will print it it will give me the address i think okay the address to string let me see okay not making this contact dot i have access of type or full name and email address and type r is not specified here i just specify the full name and the email address so if i print the type r let's run and see and we get the friend type okay so this is the default value i if i need to to change this default value type r equal to close friend or like friend family now print contact one dot type r let me see if it is chained and here we go we have the family okay these are the default values in the creation of instances of the properties can also be initialized outside of primary constructor using literals and values passed into the primary constructor using a property initializer so we in this video are going to talk about the property sorry property initializer okay consider the contact class here we have class that has full name i will change this i will remove this first name and the var another var sec the last name string okay and here we go we have a valve full name that it is equal to the concatenation of the first name was the last name okay in person in this class contact the full name property is initialized using the values that are passed into the primary constructor okay so if i create a new contact here jag and here i may sparrow if i put here print contact one dot full name so if i print the full name it will concatenate the jack and the sparrow the first name and the last name you can set the value of properties with their declaration and also in the initialize initialization block which is init block okay let me consider a new class called address and in this class i will make the initial initialization block init block so i will create first of all address one type string var var address 2 string equal to null and var city you see this is an error because it is not initialized so if property must be initialized this is what i'm talking about property must be initialized or abstract okay we will see how to solve this error and why we are doing this i will create also new city and a var state of type string also we need to initialize these two properties state and address okay this is very important now in order to initialize this property initialization how to do that it is very simple init we use the keyword init address one is equal to by default to empty space empty string okay and the state here is also equal to empty string okay in this way i am initializing the properties so in address the address 2 and the city property address 2 and city property are well defined and initialized in their declaration the address one and the state properties are initialized inside the init okay this keyword this is the new keyword that we have introduced in this lesson so since all four properties of address are given values inside the class definition you can create an address instance using an empty constructor call so how to do that we go to the kotlin course i will remove these and i will create the val address equal to address and this is the perimeter this is the constructor of this and we are using the default access and default initializers okay so if we print address dot address one or address two let me run and see address two is equal to null because we we have initialized it with no okay now i will see address one elvis one let me run and see and it is an empty space so or empty string so this is the initialization property of property in each block you can init the initialize that variables not by using these you can use by doing this okay you can make initialization and giving the value default values or initialize initializing values of variables by init many properties work just fine with the default accessor implementation in which dot notation returns the value directly and an assignment statement just set the value like this address we have an access to if we ctrl f press ctrl and space we can get and access the default values or the initialized values okay so anything we can access it by the dot notation but properties can also be defined with custom getter and setter methods if a custom setter is provided then the property must be declared as far we will see as this in the next couple of minutes but let me talk about the custom getters so the custom getters here custom think about making a new class a tv class i will create a new kotlin tv kotlin class and this tv class will have as i call its default constructor var double and we have our width double okay now if i need to calculate that diagonal and see how that the measurement of a tv would be done the measurement of atv is the perfect use case for a custom accessor the industry definition of size screen size of tv is not the screen width or height it's diagonal so we need to calculate the diagonal and return it as a diagonal so how to do that we start by val diagonal int and we go to get and we start by getting the and calculating that there so result is equal to math dot square root we are using the library of math here and the square root we are calculating the square root height times height so height square plus width square the result of this conv addition will be square rooted like radical radical height square plus width square will give us the result then we will return result dot around to end we need it in inch and it is without the um the float because we don't need the point and the floating and the decimal part we need the end part so i use the round to end see guys this custom get what we'll do what does this will do going through this code one step at a time you ju you use an int type for your diagonal property okay although height and width are each a double tv sizes are usually advertised as nice round numbers such as 50 inch rather than 49.52 okay so because that i we have used the round to end okay instead of the usual assignment operator equal to assignee value as you would for the normal property you use the get okay so without making the calculations and then we use the dot notation we can use the get with a custom declaration here okay once you have the width and the height you can use the pythagoras theorem to calculate the length of the diagonal and we have make it the math square root okay now since you have provided a custom getter no value is stored in for diagonal it is simply returned based on a calculation from outside of the class a property with a custom getter can be accessed just like any other property how to do that i remove this how to access the tv so i will create tv instance of the tv tv high height is equal directly to like 53.93 let me say with nine five point eight seven okay this is the width i can remove it now val size equal to tv dot height width and diagonal i need the diagonal okay it is around 110. you have a 110 inch tv let's say you decide you don't like a standard movie aspect ratio and you would instead prefer a square screen you cut off some of the screen width to make it equivalent to the height so for that i can make like tv dot width equal to tv dot height now val diagonal equal to tv dot diagonal this will give you a 76 inch okay now you only have a 76 inch square the computed property automatically provide the new value based on the new width okay so this is how we can get the custom the custom dialogue so a diagonal diagonal is a vial property and it is returning of type end or its type n and we have get method that will do this take this mathematical calculations and return a value for this diagonal and store it inside the diagonal so i can access this diagonal by directly calling the dot notation dot diagonal okay print they are gonna let me see the diagonal it is around 76. okay and here we go this is the 76. okay this is how we can access and create a custom getter inside kotlin in the previous lesson we have learned about custom getters in this video we are going to talk about the custom setters so the property you wrote in the previous lesson is called read only property it has a block of code to compute the value of the property okay block of code that compute a value of property the custom getter this is the custom gather it's also possible to create a read write property okay so this is read only property this will be write or read write property it's also possible to create read write property with two blocks of code a custom getter and a custom setter this setter works differently than that you might expect a property that has no place to store a value the setter usually set one or more related other property indicate indirectly so how to do that first of all we have the diagonal here we have the custom get that we have done in the previous video and we start here by the for the custom setters here custom getter and here custom set start by set value and val ratio by ratio width equal to 16 let me say val ratio height equal to 9. val ratio diagonal equal math dot square root of ratio width or ratio height times ratio height plus ratio width and times ratio width okay now height equal to value dot 2 double we are converting it to double now times the ratio height over ratio diagram ratio high okay the ratio diagram and the width equal to height times ratio width over ratio height okay this is very simple how to make a set for the for the custom setters now i change it to var math okay this is very simple we have changed it to okay very simple how to create a custom center here's what happened in the code you've changed the diagonal to var instead of val since you are giving it a setter to change the value okay so we if we have ctrl z if i change controls that there is an arrow telling me if i alt plus enter it's telling me to change to var so if i go up i i have changed the val to far so why because since you are giving it a setter to change the value also you use the same code as before to compute the value of indicator and for the setter you usually have to make some kind of assumption in this case you provide a reasonable default value for the screen ratio in k in this case 16 times 9. this is a default value why we have done this was a default value the formulas to calculate the height and width given a diagonal and a ratio are a bit deep you could work them out with a bit of time but you've done the dirty work for you and but we have done the dirty work for you and provide them here okay the important parts to focus on this formula for the height and width are the value perimeter which a value parameter to the custom setter lets you use whatever value you pass in the end during the assignment and since the value is an end you refer you first convert it double using two double you see guys we have converted it from end to double okay once you have done the calculations you assign the the height and the width property of the object okay now in addition to setting the height and width directly you can set them indirectly by setting the diagonal property let's go here and without using this we can set directly tv diagonal tv i need diagonal equal to 70. i need to calculate the the height so tv dot width and tv dot height here i provide the diagonal i need a tv with 70 inches and now we discover the width and the height of the tv it is let me print them print that and print that and now let's run and see its width will be 61 and 34 is its height okay so you give me the diagonal of the tv and i can give you the width and the height of this tv this is the custom getters and this is the custom setters that we have learned about them in this if you have a property that might take some time to calculate you don't want to slow things down until you actually need the property say hello to lazy properties these could be useful for such things as downloading a user's profile picture or making a serious calculations look at this example of the of the class called circle i will create a new kotlin class called circle and i go up class and i will start by getting the radius so far radius double equal to zero point zero i will initialize it as the use the default the values now pi here is double and i would use a new keywords by lazy okay so don't worry we are going to clarify everything so i will get the circumference of the square but till now i will create a val pi and by lazy i will calculate this by lazy like 4.0 times math dot 8 10 1.0 slash 5.0 minus math dot 810 1.0 over three 2.39 point zero times four point zero okay now val circumference friends double get equal to pi times radius times 2. i'm getting the circumference by calculating the pi i am creating and calculating the pi not using its 3.14 directly i'm calculating the pi every time i use the i need to get the set confidence i need to calculate pi it is not directly using it like 3.14 i'm going to calculate it here double by lazy and making this calculation and it makes to take some time to get here the uh the returning value times radius that it is the uh passed by the perimeter and times two here you are not trusting the value of pi available to you from the standard library like three point fourteen sixteen you want to calculate it by yourself you can create a new circle instance in in the kotlin course here by creating circle val circle or circle equal to circle and i passed the radius it is about 5.0 okay got a circle pi is not b run the calculation of pi waits potentially to see waits patiently until you need it only when you ask for the circumference property is pi calculated and assigned a value so without using the circle and without passing this parameter without creating a new instance of the circle pi would not be calculated it is awaiting your orders okay now if i print a circle dot circumference it will give me here let me run and see it will give me 31.14 41 and it is working fine but since you've got eagle eyes you've noticed that the delegated property pi uses by lazy okay pattern to calculate its value the trailing parenthesis are the lambdas these are the lambdas okay so again we have seen lambdas here the trailing parenthesis are the lambda that initializes the value of pi but since pi is marked as by lazy this calculation is postponed until the first time you access the property for comparison circumference is not delegated property and therefore is calculated every time it is accessed you expect the circumference value to change if the radius changes dot pi as a lazy property is only calculated the first time that's great because you want to calculate the same thing over and over again when you first initialize the circle instance like here the pi property effectively has no value true when then when some part of your code requests the property of pi its value being will be calculated the value the value will be only changes once so you can use the val on the property okay this is the lazy property uh lazy property okay so it is useful when dealing with non-synchronous or one say you have a things that you need to be calculated or done and they are heavy and you are not making and slowing down your uh your compute your program for doing that so use lazy to do the calculations and to be fasting your uh your program and taking its complexity hello my friends and welcome again to the complete coupling course we have learned about the classes methods properties and much more concepts but classes concepts include inheritance overriding polymorphism and composition which makes them suited for their spark this purpose these extra features require special consideration for construction classes hierarchies and understanding the class life cycle in memory this chapter will introduce you to the finer points of classes in kotlin and help you understand how you create more complex classes introducing inheritance as an overview we we can make a class that allow other classes to inherit properties and method from it how does thus means or how this works in cutler okay i will create like a class a general class and allow other classes to inherit from it so for example we have done this in the previous lessons i will create a grade class this class will make would be making as data class grade and having val letter car then points as double and the val credits double okay we have created this in the previous lessons okay i will show you in this example the importance of uh of inheritance and how to convert these hierarchies into inheritance hierarchy person i have created a new class called person and having var first name of type string and var last name type string and function i will call the function full name or making a function full name equal to call inside it having function that will concatenate the first name with the second and the last name okay this is the class person now i will create a new class kotlin class called the student student this student will have var first name name of type string var last name time string okay and having a var are making it as global grades mutable list of grades i'm making a grades a collection mutable list of the grade and it is of mutable list of i will pass great okay now this function function record grade grade of grade now grades dot add green okay i'm this function will record the grade inside the mutable list so every time you pass a new grade for the student it will be recorded inside the mutable list called grades okay this is the person and this is the grade class now it's not difficult to see that there is an incredible amount of redundancy between the person and the student think about it there is a first name here if i put them here like this if i put here also the student and the person student has a first name and the last name and the person has last name and first name every time i i will i need to create a person i need to pass his first name and last name and every time i need to create a student i need to pass first name and last name okay maybe you have noticed that the student is a person yes student is a person so this simple case demonstrates the idea behind behind the class inheritance no need if the if student is a person so no need to allow the student to define the first name and the last name every time i create a student because he is a person so student is a person so this is a relationship will clarify the basics of the inheritance the simple case demonstration the idea behind or demonstrate the idea behind class inheritance much like in the real world when you can't think where you can think of a student as a person you can represent the same relationship in code by replacing the original person and the student class implementation as this so since i have the student is a person i can remove this redundancy code so no need to write every time these codes i can make a person i put open here so the keyword open open class person having the first name and the last name and this concatenation function okay and inside student class the student first name directly without var without clarifying without creating var every time i put first name here the same as last name also here and i will keep the var for the grades because these would be inherited from his father his super class person so the class that allowed the other classes to inherit from it is called super classes and the inherited the inheriting classes is called are called the subclasses okay so how to allow that the the inheritance to take place open we make the father of all the classes the super class we will make open and student inside the student the subclass of the person would be like this we remove every vowel or var that we have created for the for the common properties okay first name and last name here and we keep on the variables that they are not inherited from the super class and then we put here two points it is inheriting from person the first name and the last name so in this way we are telling that the student is a class having two three parameters two of them are inherited from the its father it's superclass person which are the first name and the last name so we have removed here the var and numerically okay it's very simple guys now the function here i need to put it as an open because we are adding it to a class student that allows the person to use metal in this modified example the person class now includes the open keyword and the student class now inherits from the student by making this person and inheriting from it the first name and the last name okay you must still add through inheritance student automatically get the property and properties and methods declared in person class in the code it would be accurate to say that the student is a person so a student is a person person is a super class and the student is the sub class with much less duplication of code you can now create student objects that have all the properties and method of a person now let's see how to make this person and student so val i will make it as jason equal to person it's a new person having a first name as jason this last name is statin van another student like [Music] jennifer equal to person jennifer and the last name lopez in order to get the full name function because it is a person and i can get the full name so jason thoughtful name i will print it here okay it's the it's normal class because i'm making i'm using making use of this i am just concatenating the two first name and the last name but if i make the make use of the student object how to make this i will make val new grade grade history equal to grade having letter a on history points is 19 over 20 and the credits is equal to three three credits okay three points pointy points 19.0 i will add for jason the jason jason i can't access only in only the first name and the last name okay i can't add grade to the history because the person is the father and he he don't have any methods except the full name okay i need to make use of record grade inside the student so jennifer i will transform it into a student and everything will work fine because the student here the student will took the first name and the last name and the var grades would be a null till now okay jason will not able to record the grades while jennifer if i put jennifer dot i press ctrl and space it creates last name record great you see guys and the full name so we have a record grade function in student yes it is here but if we look closer control and space we have full name function so full name function full name is came as coming from the person class that the student inherits from it this is the importance of inheritance because you don't need to write for every student for every employee for every teacher for anything who inherits from or he is a person we are not going to write a function that in every time for the student tell him that to get the full name so that this is the redundancy of code we don't need to rewrite rewrite everything and copy and paste the code we need to make a clean architecture having a inheritance and having an a class a super class that allowed the subclasses to inherit from it the sharing of the functions the most commonly used functions the common function and the properties between these subclasses okay so the student here it does not have a full name function it contains a record func a record grade only so grade history i i can add it to the to jennifer because jennifer is a student and is a person by the way so full full name jennifer full name i can get it but i need to use record grade and i will add the grade of history okay now uh jennifer is a student but i can't make this for jason because jason is a person person does not inherit from student student inherit from the person a class that inherits from another class is known as subclass or derived class and the class from which it inherits is known as superclass or bayes class okay kotlin class can inherit from only one other class a concept known as a single inheritance only also in java is a single inheritance while in c plus plus it is multiple the student-person relationship demonstrates a computer science concept known as polymorphism we said that a student is a person okay so in a brief polymorphism is a programming language's ability to to treat an object differently based on content context so how does this means or what does this mean student is a person okay i can treat a person as a person but i can treat a student not a student also i can treat a student as a person what does this mean how to do that we said that jason is a person okay if we look on the properties that jason accepts and the methods that have acquired have last name first name and full name okay these are for the super class but if we put jennifer jennifer is a student if we go to the student we have a first name and last egg we have grades and we have record degree we have this function record the grade only if we go back we see that jennifer is a student and we press control and space we have grades first name and last name record grade and full name so full name function is not in is not found inside the student class but it is inherited from the full name so we can treat jennifer because it is a student we can treat it as a person so i can write here full name treating it me treating it as a person means we can use the properties and the function of a student and a person inside a student okay so if i print here print ln println we run it we see jennifer lopez okay so we have used the full name function that it is not written inside the student but because the student inherits from the person and the person is the super class of the student we can get the full name function to be inherited and called and be applied for the data that it is for the student okay so we are treating now a student as a person sub classes receive all properties and methods defined in their super class plus any additional properties and methods the subclass defines for itself we have seen that a person is a super class having a full name and property's first name and last name and the class student in hertz from the person so additional to full name and the first name and last name properties that inherit from the person student defines a new mutable list grades and a new function record grade in that sense subclasses are additive for example if you you've already already seen that the student class can add additional properties and methods for handling the student grades these properties and methods won't would not be available for to any person class instances but they would be available to students of classes okay so besides creating their own methods sub classes can override the methods defined in their super class so if i need to get this use this method inside the student class it directly we create a new student jennifer and uses the full name but we can override this method assume that we need to add the maiden name for the full name function okay i need the first name the last name and i need to add like hello i need to say hello for the or my name is first name so if i need it to modify in the student class the full name to say like the student name name is like this but i can't put here in the person because the person will apply for all sub classes so i need to override this method inside the student class so i write here the override the keyword function and called record or this is the full name full name and it took it will it took them the first name and the last name okay super i will use the super keyword i will i will modify it later on super plus super dot so the super keyword will refer for the function that it is inside the super class i need to call the same function as full name okay full name i have modified open function because if i remove this it will give me an error telling me that it is out if i press alt plus enter change return type to string i am getting a string and override alt plus enter make first person.full name open if we go there we see that open here if the function is not open i can't override it okay so here i need to make a return plus hello hello student okay this is the function over writing now let's run and see the same the same thing jason will printing the full name because json is person it will ordinarily print his first name and the last name while in a student because jennifer is a student it will print something different let's run and see and discuss what we have jason stanton is a purse okay why and and we have this first name and the last name why jennifer lopez is a student so we have added directly to this custom hello student message so this is the all method overriding we have added to the function that we have inherited from the superclass person a constant string hello student with these dots okay this is the method over writing in certain situations you may want to prevent class from being instantiated but still be able to be inherited from this will let you define properties and behavior common to all subclasses you can only create instances of the super cl subclass classes and not the base parent class so you can make an instance of subclasses but not making an instance of a super class such parent classes are called abstract classes classes declared with abstract keyword are open by default and can be inherited from in abstract classes you can also declare abstract methods and market with abstract that have no body the abstract method must be overridden writing in the sub classes this is the beautiful thing in in inheritance and abstraction in kotlin and java now abstract class how to define an abstract class i will create i remove this person grades these codes you can download the source codes from the udemy course resources folder abstract yes i will create a new kotlin core class here i will name it as memo and this mammal class would be abstract i can't create an object or an instance of the class as a mammal because abstract sorry because there is maybe um a bird anything that having four legs two legs but they will making from an inheriting from the mammal okay so i can't create an animal directly an instance directly as a mammal because it is an abstract class and the logic will tell me that i can't make or i can't find in the nature something called mammal i can find an animal that belongs to the class mammal okay so i will make birth date string i will define also a method called abstract function consume food okay this is an abstract function that should be defined and override a written inside all of subclasses of the mammal so everyone having a subcla superclass as a mammal i should define the function and override the function consume food and see how to override it later on so i can create now a new kotlin class called human and human by the way is mammal so mammal here is or human here it will take birth date birth date from directly as i told you in inheritance when i need to uh to get the the the properties i should declare them here without making var or val and i will define that this this class is inheriting from mammal so we have seen this in the previous video and i am getting the birth date from the mammal here till now we have everything working fine but there is a new error we have seen this and we have done this in the previous videos about inheritance and polymorphism but what this error means if i click in this red bulb implement numbers or make human abstract implement members and i see here that i should implement members and this members is a function consume food let's put a click click and here we go it have been making and overriding a function called consume food i will remove this and i can make here overwrite function consume food i this function will print uh consuming consuming 1000 calories okay per day for the humans okay if i create new kotlin class i will name it as like birds it is not mammal okay so i will remove it i will create a new kotlin class like cats this cat will take birth date as a string from its super class and its extending mammal passing birth date and this error is the same because i have to implement and overwrite the function alt plus enter implement members consume food and click ok i will remove this and i will run here consuming calories per day okay so human and cats are mammals okay extending from the abstract class mammal and they should override the function called consume fruit okay now let's go and at the core a kotlin course class that is fung where the main function included abstract i will create a val human equal to human and i will give the birth date then 10th of 10th of june 2000 and i will create a cat object equal cats i will give the birth date of it five zero five this is 2020. okay these are the classes that are inheriting from the mammoth but there is one thing that i should remember you that i can't make a val mammal equal equal to mammal and i can't pass like this like zero four zero nine two 2000 there is an error because here cannot create an instance of an abstract class this is the error i can't create a mammal instance of the mammal class okay this is the importance of using the abstract classes you can create an instance of mammal subclasses human and cats but not the mammal class itself abstract classes are closely related to interfaces that we are going to see in the next sealed classes are useful when you want to make sure that the values of a given type can only may come from a particular limited set of subtypes they allow you to define a strict hierarchy of types the sealed classes themselves themselves are abstract and cannot be instantiated see all the classes act very much like enum classes that we are going to introduce later on which but but also allow subtypes which can have multiple instances and have state consider a cld class called shape here create a new kotlin class called shape and this class here i will name it as sealed name and giving it as a keyword as cl and the sealed class shape it has a class circle having val radius and and inheritance from shape and another class called square val side length and shape okay okay you have used the the keyword sealed to mark shape as a sealed class both circles and squares are shapes but a circle has a radius and a square has a side length unlike enum classes you can create multiple instances of each type within sealed class let's go to the kotlin course dot kt this is the class that we are going to test and we have tested all the codes that we have created so i will create val circle1 equal to shape dot circle so i'm like making a nested cla nostal nested classes i'm making class and then a shape for a limited number of classes so shape or shape will contain a circle or square so you can't make a shape only if it is a circle or a square circle of radius 4. and i will create another val square of square two equal to shape dot square and having a radius our side length equal to 2 okay and functions defined on shape can distinguish between the different subtyping using the one expression okay so if i need to make a function here and use this seal the classes function size shape shape and the returning type of it is end return when shape if it is a circle is shape dot circle return shape dot radius get radius then is shape dot square return shape dot side length okay now sir if i need to test this circle circle one third size i can put this inside here function size and i will put here two okay sorry the shape circle it will return for me a circle because i have passed a circle and it will consider if it is a shape circle it will calculate the shape dot radius okay so the size here may have two possibilities either a radius or a side length if the shape pass is a circle it will know directly that it is a radius if the shape is passed square it will be known as a side direct while the open keyword determines what you can and cannot override in class hierarchies visibility modifiers determine what can and cannot be seen both inside and outside of classes the four visibility modifiers available in kotlin are the first one is public visible from everywhere within subclasses other files and other project models if no visibility modifier is specified is it is default to public so this is the default modifier if you don't put any modifier it will be a public the second one is private visible only with the same class for classes and only within the same file for top level functions and other non-class defined definitions so private we will see them in the example three protected visible only within subclasses for class hierarchies and for internal internal visible only within the same model for example an intellij mod okay so generally you want to limit the visibility or scope of your for your classes and variables as much as possible this will keep the responsibility for of your classes clear and prevent you from changing the state of class when you really shouldn't be okay now let me create a new kotlin class here and it is called privilege and this class is of type data class it will take in its default primary constructor val [Applause] id of type end and val name the string okay this is the class of privilege okay i will create another class here but let me first remove these from the previous exercises i will create a new kotlin class named user and the c class user it is an open class it will take val username of type string private val id of type string and protected var h of type and okay this is very simple just creating the classes now i will create another class and define the function and see the relation of the for the access modifiers or access the scoops the visibility modifiers that we have talked about there this class now will be named as privilege privileged user and it would take the username this thing and it will be extending the user okay so this is the subclass of the username of the user class id of string age of and it will be extending the user class having username id and h okay now i will define the functions here to see how the functions and how it will work if i create add privilege function take privilege object as per meter i need to create privileges here it's of private you see guys this is the first modifier private val privileges equal to mutable list of and we will see of type privilege okay and i will add when you call the function add privilege it will took the past perimeter and add it to the list that we have created here remember the aim of this exercise to understand the modifiers and how they work okay so the private the privileges here is a list defined as private we will see how to access this and privilege from here okay again i will create a function has privilege and authoritarian boolean return privilege dot map at dot id i'm getting method id that contains id checking the map and function about taking turning and string this string will return username and age okay look guys if we see the id it will is id is private because if we go up the user if we see that private here is the id and the protected is the h i can use here because privileged user is subclass of superclass user or a privileged user is a user and privileged user class inherits from user user but there is one thing that you should be pay attention in the super class the id property is marked private okay so you can only be referenced inside the user class this private will give you an access inside this class but not inside the sub classes okay while protected the age protected property or the age property is protected so the sub-class privileged user can see it you see guys we can access it if i put id here it will give me an error because cannot access cannot access id it is invisible private and super type in privileged user you can make make id public okay i can make id public so i can access it okay the error has been gone if we go to the user we see that the uh private is have has disappeared if i add private the error will be found so if i put the age it will be good if i put username it will be good because here there is a public keyword okay so this is how we access the how we use them modifiers okay if i go to them to the main and try to test this code val privileged user equal to privileged user having user name like jack id and age 30 okay if i create also val privilege equal privilege one in like no access privileged user dot add privilege i'm just adding them okay and now if i try to print the privileged user dot about let's run and see it will give me the name and the age and not the id you see guys this is the importance of making uh and understanding the modifiers because if we go back to the the about function it will give me the user and the h but if i replace it as i told you with the previous exercises id will be not accessible because in the super class id is defined as private so private is only for the class itself protected for the it's for the class and its sibling its subclasses public is for all this is the modi you have learned about two kotlin custom types classes and objects there are another custom type that's quite full it's called interfaces unlike the other custom types interfaces aren't anything you instantiate directly instead you define a blueprint of behavior that concrete types conform to with an interface you define a common set of properties and behaviors that concrete types go to implement and implement okay the primary inter difference between interfaces and other custom types is that the interface themselves cannot contain state they are containing behaviors only methods only functions in this lesson we will learn about interfaces and how they are used inside kotlin so you define an interface much as you do any other custom type by going here to the source create kotlin class and we see here that this green one with i is called interface i will make an interface called vehicle and this vehicle this is the keyword interface okay and in this vehicle interface um remember there is a keyword interface followed by the name of the interface followed by curly braces with another the members of the interface at the inside the big difference you will notice that the interface does not have to contain any implementation we cannot implement anything here we cannot define a value here or we cannot write a function complete function here like accelerate that and we put a body of of the function it is not allowed so what why we use it i will tell you every vehicle in the world will have two things like accelerate and stop so when you have a very big common between things like vehicles like airplane cars boats and whatever you want there is there are common function between these objects which are accelerate and stop and you need to mandatory implement them so we see in the abstract classes when you define an abstract function all the subclasses or of the abstract class will will be mandatory implementing them but here interface any class implement the vehicle interface it should implement all of its methods okay that means you this would not be impli instantiated this means that you cannot instantiate a vehicle class directly so if we go to the main i can't write here vehicle of type leak i can't because interface if we see on the error look interface error interface vehicle does not have constructors and interface is not having and constructors and cannot be instantiated instead you use interfaces to enforce methods and the properties these methods on the other types what you have defined here is something like the idea of vehicle it's some time that can accelerate and stops okay now let's go deep and learn about the interface syntax an interface can be implemented by a class or object and when another type implements an interface it requires to define the methods and properties defined in the interface see guys remember these two methods accelerate and stops okay so i will create a class here in your class concrete class i will name it as bicycle okay this bicycle is a vehicle so how to implement the interfaces it's just putting two points week okay it's very simple there is an error here appearing class bicycle is not abstract and does not implement abstract number public accelerate implement the members these are the members that i should implement accelerate and stop we need to put okay click ok so we have implemented these two functions accelerate and stop i told you to save this naming because everything extends or implements sorry using implement for interfaces and extends for the classes okay this is the inheritance any class that implement vehicle interface should include inside its body the accelerate and the stop method if i define the third method here it will enforces this class to implement the other functions okay this is very important part now let's say here speed valve speed equal to zero when you accelerate speed would be equal to eight kilometers per hour per per hour speed equal to z okay just two functions uh adjusting the speed of the bicycle okay this is very important now there is something in strength interesting in interfaces since stop in all vehicles will make it like zero but the accelerate it may differ between the vehicles and the the vehicles itself so the acceleration of a plane will be different from the bicycle so but the stop but the stop of the plane is the same as stop of the bicycle in terms of the final results i can define here default method implementation how this works i can put here for the stop like a print like a slow down to zero [Applause] so if i go back here and then i alt plus enter implement members accelerate okay i have implemented the accelerate only okay where does stop have left you have defined an implementation for stop inside the interface but left accelerate undefined and it types implementing vehicle such as a bicycle must include an implementation of xli when enter an interface and defines a default implementation this is the default implementation you can still override the implementation in the type that conforms to the interface i can now override okay so override function stop okay you see guys this is the function override we are making as this and we are making use of stop and here the bicycle both overrides both of the functions the accelerate and the stop and it also uses super in the stop to call the default implementation as we have seen in the super keyword it will do the same as its super class or implementation bicycle will do this the same now in the stop as its implementation it's just print down the slow down to zero speed in order to add things and allow it to be more reliable reliable speed equal to zero and accelerate speed is equal to eight okay always do add the codes after the super you are not allowed to add here like speed equal to zero it will give you an error okay just put everything after the super dot stuff okay in programming centralizing your code is one of the biggest ways to save yourself from headaches and prevent bugs that way when you are executing the same thing in multiple places there is only one place where those executions are actually happening and the only one place where you they could possibly break a really helpful feature of kotlin for this is called genetics the general concept of genetic programming is that you don't necessarily need to know exactly what type an object is or an object associated with primary object you are working with in order to perform actions with or around it this allows you to combine and simplify functionality in real real powerful ways okay so anatomy of standard library genetics type when getting standard started with generics it helps to look at the major generic types that you that are included in kotlin standard library this way you can see how the language itself uses this functionality and get some ideas about how you might be able to use itself let's take an example of the list if we go here if i type equal to list if i click on ctrl and hover the mouse on it and click the left click i can go to the source code that the list is writing this okay so this is inside the collection i scroll down to the list of see this method that we have used them list off list of and mutable list of an arrays mutable list of and we see that there is this diamond these diamond clan immunication or these typing like t is the type and these angle this bracket and angle brackets um they are surrounding t and here is again the t and here is the again t so what this means okay take this part a bit this is declaration if we go scroll up if we are looking for the list itself okay so let me check for a list of okay this is the function list of okay we are going to work with it so if we go this is the declaration of all collections in the uh in the library and this is the core the class that we are going to test the code and learn about it look guys if i'm going to make var list i need to specify if i want to the type of this string and list list of and i put here like element one element one element two element three and so on okay so look guys if we took a closer look here if we call the function list off there is before public function list of there is between them this angle bracket with the t okay cutling collection list of and returning and the type of it is t this is this is the symbol or the syntax that they are telling us that the list of is accepting is a genetic function if we go through here list of string we go up and the returning type of this would be a list of t so t represent anything any object any type of collection any type of objects that can be used in the collection of list so this if we hover here the string class represent character telling us in the character and we are telling that this would be replacing the t here so t now is a string this function now deals with the t as a string in the case of the our list we will continue with the code list of we are getting list of and elements t and i told you that since the t is now declared as a string so these are being accepted the function t a function list off accepts all the elements of this d t represents string so list of uh now is string in the case of the list the first t indicates that it is go and this is going to be a function does not or does something with genetic type okay and the second t indicates that the list you are calling this function on must be a list containing only objects of the same of that generic type okay this is the list and this is the t that we are going that we have talked about okay the third the third d that the returning value this is the d the second t and the 30 return value of both the same type d which is a string here okay this is generics it may be confusing don't worry on the exercises we are going to see how it works and how to be clean you are wondering why we are going to use generics and what are the importance of genetics let me try to make this list and you'll see why the importance of the genetics and we'll start by the list and we'll take examples about the map and the sets later on but till now i will make an immutable list of and i put one and two okay kotlin now understand that this is immutable list of integers if i try to add list dot add jack here okay print the list or print list list let's run nc it does not allow you because there is an error and this is not on run time error it is on compiling error on compile time error when typing there is an error this is telling us catlin type mismatch infer type is string but in was expected so you are passing and in the string but there is accepting as integer so if i put integer here it will works fine but if i put a string it will make an error okay since kotlin's type inference system only sees the two end objects passed into mutable list of it assumes that the generic type of the mutable list being created is and okay as we have seen in the previous video when we talk about the list and the mutable list and the list of we have seen that it is a passing and uses genetics in form this is the t d so when passing 1 and 2 d is now an integer so the list will make sure that only accepts t equal to integer and here when we passed a string it refuses to add this okay this is great if you want to prevent someone from accidentally adding an object of uh of the wrong type to your list this is called type safety so the genetics the main purpose of genetics is making the type safe so you are preventing anyone from entering the age like from entering the age or entering the anything that you are only accepting integers only accepting the the strains or only accepting specific objects to the list or any collection okay so if you want to be able to add an object of any type to the mutable list it's very simple where there is a type that we have talked about it called annie so if i create here that a change val list a mutable list i will make a mutable list here mutable list you see the e here it's telling me that i need the e which is the element i put the keyword annie so any in kotlin allows you to add anything for this mutable list one two as integer okay and now there is a confusion because when we create a mutable list i passed any now any will uh will override the integer will will uh like pass the integer yes the root of the kotlin class higher okay every kotlin class let me show you and it has an ani as a super class okay every kotlin class has any as a super class as object class inside java is the main object it is a super class of all the classes you create and the same way annie is the father of all data types in cutler so one two is and in we are integers okay but this will not define the mutable list as an integer so the t here or the genetics is passed as any it accepts all type of of data so if we go here we see that jack now be can be passed as a string to this integer list let me run and see and here we go guys 1 2 and jack so we are accepting integers and we are accepting strings okay this is how we use any with mutable lists and how to pass the type safety of genetics and why we use genetics because of its type safety and it's very important guys when making big problem big programs you are mandatory and you are you should add this type safety because people or users may encounter accidentals accidentally errors or bugs in your system
Info
Channel: Master Coding
Views: 47,471
Rating: undefined out of 5
Keywords: kotlin, kotlin tutorial, kotlin programming, learn kotlin, kotlin multiplatform, what is kotlin, kotlin vs java, kotlin android, kotlin programming language, kotlin for beginners, kotlin course, kotlin course for beginners, kotlin tutorial for beginners, android development, kotlin full course, android, kotlin crash course, Tutorial for Beginners, kotlin tutorials for beginners, full kotlin course, Kotlin Full Course, learn kotlin from zero, learn kotlin from scratch
Id: cxm9AHNDMPI
Channel Id: undefined
Length: 624min 42sec (37482 seconds)
Published: Sun May 01 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.