Kotlin Tutorial For Beginners 2023

Kotlin Tutorial For Beginners 2023
Spread the love

in this kotlin crash course I’m going to  teach you pretty much everything you  need to get up and running with Courtney  kotlin it’s a powerful and elegant  programming language for the jvm and if  you’re looking to switch from java to  kotlin then this is also the course for  you also kotlin now is the main  programming language for Android  applications so Android backend and with  multi-platform functionality as well  kotlin is a great choice this video is  about five hours long and you can find  the timestamps and the description of  this video in case you want to navigate  to a particular section before we start  this crash course literally just take  one second and smash the like button  also if you haven’t subscribed do so and  without further Ado let’s go ahead and  learn about kotlin  before we dive into kotlin I just want  to say a few things the first thing that  I want to say is that this course is  part of the massive course which I’m  currently recording and then planning  for kotlin developers and in general for  anyone really that wants to learn quote  there now the course is not ready yet so  what I want you to do is join the  waiting list and also on the website you  can basically enroll get a certificate  but also there’s a form that you can  find at the end of the course so that  you can give me some suggestions of what  are the things that you want to learn  about kotlin so enroll now on the  website so that you can stay up to date  with the latest recordings and future  updates but more important you’ll be  eligible for an early bird discount once  the entire course is out  let’s begin our journey in terms of  learning kotlin kotlin is a concise  cross-platform and yet fun  it takes all the good things of java and  it adds even more features to make it  somewhat a better language kotlin is  multi-platform allowing you to share  code for different platforms you can  write server-side code so if you’re  familiar with the jvm you can run your  kotlin code on jvm making it easy for  Java developers to switch to kotlin  also you can create multi-platform  libraries that works across several  platforms and is the official language  for Android development in this page  right here you can see that so if I  scroll down they’ve got a simple example  of how to get up and running with kotlin  but we’ll go through this later in this  course and here as I said kotlin  basically allows you to write code which  runs on different platforms so server  side Android iOS desktop and web which  is like amazing okay  so in this page here we can then  basically click on this button to get  started with kotlin  so also in this page you can basically  click on docs and you can go through  this page to learn about kotlin but  hopefully I’m going to break it down for  you in this course next let’s go ahead  and get up and running with kotlin  in order for us to get up and running  with kotlin we’ve got two options we can  use the IDE which I’ll show you in a  second so IntelliJ IDEA or Android  Studio or if you want to quickly see how  cotton is like you can use the kotlin  online editor so here let me just click  on it and in here we can basically write  some kotlin code so you can see that  this is the version  1.8.21 and in here you can switch the  platform so most of the times you’re  going to use jvm but you can see all  these different options  and in here we can also run the code  that you see in here so we’ll go through  this in a second but if I run this  you can see that we have hello world if  I cancel this and then say hello and  then Amigos code  and let’s just add this into a variable  so VAR  and then here I’m going to say brand and  then colon and then string equals to and  in here just say Amigos code  so we’ll go through the syntax in a  second but we can take this brand in  here and then just add it  in here so basically we are printing the  variable if I run this  you can see that we have Amigos code in  here  so this is basically the playground that  allows you to write and basically try  kotlin without you having to install  anything  next let’s install IntelliJ  and get up and running with kotlin for  real  cool for this course what we’re going to  use is the best IDE for the jvm this  includes Java and kotlin and by far  IntelliJ is the IDE for I would say  serious Engineers okay if you’re using  Eclipse then it’s time for you to switch  and give this IDE a try because it’s the  best for overall software development so  in here we can click on download and in  here you can see that we can download  IntelliJ IDEA for Windows Mac OS as well  as Linux and if you are on Intel or  apple silicon there’s also the  corresponding platform available for you  so we have ultimate as well as Community  Edition and the difference between these  two is that the ultimate edition gives  you profiling tools support for spring  HTTP client JavaScript typescript so  basically the whole front-end check bank  database tools remote development which  is currently in beta as I speak and also  collaborative development which is not  available within the IntelliJ IDEA  Community now luckily for you I do have  a code that you can use so that you can  get IntelliJ IDEA ultimate for three  months yes three months so if you are  taking this course on the website you  should find the instructions on how to  get the access code so that you can  enjoy this amazing IDE  cool now in order for us to download  this IDE we’re not going to do through  this button in here because the best way  for you to manage all of your ideas is  by using something called so if I go to  developer tools navigate to  where is it toolbox app so this right  here is basically a way for you to  manage all of your Ides you can upgrade  have the settings and also you can have  different versions running at the same  time and also previews and whatnot so  download this toolbox app in here is  absolutely for free and get the DMG or  the executable if you’re on Windows and  once you have it installed in here you  should see that there’s this box icon in  here so if I click on it  you see that this is the jetbrains  toolbox  now at this point you can install  whatever IDE you see here but the one  that we are interested is the IntelliJ  IDEA ultimate so the Community Edition  as well you can install it for free but  I’m going to give you the coupon code so  that you can try this IDE here for three  months and if I scroll down you can see  that we have stuff for python data sea  lion  dot net in here so you can basically  just install and then just try these  Ides and they are so awesome  cool so once you have two books go ahead  and click on install so for you you  should see installing here and install  IntelliJ IDEA ultimate  okie dokie so upon installation of  IntelliJ IDEA also I forgot to mention  that you can click on this clock in here  and basically you can create an account  and if you manage to get the coupon you  should actually create an account and  then log in and basically you should be  on your way to getting the three months  for free  so in here let me just click on IntelliJ  IDEA ultimate  and this is basically the latest version  as I speak for IntelliJ IDEA you might  see that maybe at some point when you  install IntelliJ you have the new UI and  then don’t you worry because now I’ll  show you also how to switch so this is  IntelliJ so here we can basically  customize IntelliJ  add some plugins and whatnot which we’re  not going to do at this point but what  we want to do is to create a brand new  project  and in here you can see that we can  choose have a look so we’ve got  different options in here so have a look  we’ve got  so if I show you we have Maven we have  spring we have Java effects quarkos ktor  which I will cover at some point as well  kotlin multi-platform view write AWS and  whatnot so what we want to do for us is  let’s just create in here a new project  so just say new project and then for the  name I’m just going to say kotlin  in here and I could also create a git  repository and this will allow me to  basically push all the examples so that  you have it for reference so I’m going  to take this  and then for the language you can choose  Java kotlin groovy JavaScript so what we  want is kotlin  and then the build system in here we’re  not going to do anything with Maven or  Gradle so just leave IntelliJ  and if you want you can add sample code  so just leave it as it is and there’s no  need for us to configure the advanced  settings so just leave everything as is  and one thing that I actually need to do  is I need to change the path so here I’m  going to put this under code and not  reviews  and now advanced settings should reflect  on that okay so choose whatever location  that you want to store your project  and also we have here jdk so I’m using  the 17 version but what you can do is if  you don’t have anything in here just  basically set your jdk so click on  download jdk  and you can see version 20 but at some  point you might see 21 22 23 so it  doesn’t really matter right so just  choose the latest if you want but in my  case I’m going to leave it at 17. but if  I want the latest version for example  just leave 20 and then you can choose  the vendor in here so either use Oracle  or eclipse in here so let’s just say  eclipse and for the architecture I’m  going to use this one because I’m on the  Mac I think it’s M2 Chip and select that  one if you’re on Intel just use this one  here so eclipse and then download  and you can see that now it has  installed and we’re good to go so as I  said the version doesn’t really matter  and this is pretty much it so let’s add  sample code and let’s create just give  it a second and that uh we have  successfully created a project using  kotlin and now just to make sure that  things are working  you see display button in here just  click on this play button or in here so  just run  and if this works it means that we have  everything working properly and you can  see that we have Hello World program  arguments and then exit code and then  zero which means that there were no  issues  awesome this is pretty much it catch me  on the next one  okay  as I said IntelliJ they have a brand new  version of their IDE and basically they  have changed the complete look and feel  so let’s go ahead and switch to that so  maybe by the time you watch this video  that will be the default but currently  it’s in beta mode so let’s just click on  this clock in here project IDE and  project settings  and then here we can click on settings  cool at this point under appearance and  behavior you should see new UI and then  beta so here I’m going to enable this  and then apply  and restart  perfect and now you can see that the UI  looks much much better  and we’ll go through some of these  things in a second but this is pretty  much it so I just want to make sure that  we actually switch to the new UI moving  forward so that we get the best  experience  this all for now catch me in the next  one  foreign  and some of the things that you see here  on the screen because moving forward  it’s very important that you understand  what is going on so if I open a project  in here so you see that we have kotlin  so this is our folder which is under  kotlin  this is some good stuff and don’t worry  if it’s green in here that’s because I  didn’t commit none of this  and here if I click on SRC Main and then  kotlin  you should see that so here I actually  have an extra toggle but you should see  main.kt which is this file right here so  if I put this smaller which is this file  in here okay  now this is where we type our kotlin  code  and if we want to run it we could just  click on this play button or this one or  we can right click on the main  and then run right so provided that it  has this main function then we’re able  to run it so if I change this from Main  to Mains in here you see that I’m no  longer able to run this because every  kotlin code needs the main  function so this is the main entry point  for the application okay so if I put  that back you can see that we have the  play button and we can basically run  this and whatever is inside the main  function will be executed also what I do  have for you is in here so line number  four if I press  command and then y you can see that you  have the keyboard shortcut so that you  can follow along so both for Mac as well  as Linux and windows so basically if I  press command Z you can see that command  Z and do delete line okay and this is  very helpful  so with kotlin code We have basically  functions so we’ll come back to  functions in a second but just bear in  mind that this is the main function and  it’s the main entry point of your  application  within your IDE you can do a bunch of  things which we’re not going to have  time to cover all the features about the  IDE but in a nutshell so this here is  the project and this is where you see  all of your files in here  and here this is all the git stuff  integration you can see the structure of  your file you can see AWS toolkit so for  you you might not see this unless you  have the plugin installed also you have  Services we have build if you want to  use the integrated terminal you can do  it so just click on it and this opens up  the terminal in here  and problems you can see all the  problems that you currently have within  your code and also git in here so you  have get in here and also you can commit  through here so this gives you the  history basically so if I click on it  you can see what we have so this is  currently the master branch which I’m  gonna have to change to main later on  but this is pretty much it so here if I  hide this  and most of the time what I’m going to  do is I’m going to have a separate file  for all the examples so you should see  here for example when you talk about  variables I’ll create a new file in here  called  variables.kt so KT is the extension and  then I’m going to commit all of these  files so that you have four reference  but most of the time I’m going to keep  my IDE like this full screen so that you  see everything nice big and bold also if  you want to access settings you can  click on this login here  and you can go to the project structure  go to settings plugins if you want to  change the theme in here so quite useful  if I click on theme you can choose  between light what is this what is this  don’t choose this please or you can  choose between the other ones right so  oh this one is quite ugly so please  don’t you dare choose these themes I  think this one is quite good the default  so just use that one and also if you  want to have access to the database tool  in here you have access which is really  nice but we’re not going to touch this  later when we touch upon spring boot so  then I might even show you how to use  this and finally you have code with me  in here and basically I can invite you  to have a Live code session but we’re  not going to do this as well  and this is pretty much an overview of  your IDE  a all right let’s explore this piece of  code which is the main function and also  I’ll show you how to use program  arguments so basically fun in here is a  reserved keyword for kotlin and then  main is the name of this function so  unlike Java so here let me just open  project  and then let me create a new and then  here I’m going to say Java class  and then here I’m going to say  main as well so we can have two Mains so  this one is main.java whereas this one  is main.kt so here I’m not going to add  this to git as of now so let me just  basically don’t ask again and then  cancel  and this is some Java code so if I say  in here so main you don’t have to type  this  but this in contrast is the exact same  thing so if I say South for a second  and if I open this main.kt hello world  this is what’s inside go back to  main.java paste it  and now if I put them side by side so  I’m just going to take  actually let’s just leave the kotlin on  top and then take this tab in here and  then put it underneath just like that  and then put this full screen and  and also what I’m going to do here is  you see line number four and five on  main.kt let’s get rid of it so I’m just  going to  select this line delete or just press  command and then y  basically this right here is the exact  same thing in Java so have a look so in  kotlin you say fun and then Main  and you pass some arguments and then you  say print line  and in Java is a little bit different  right so here have a look public class  Main  and then inside you have public static  void main string arcs and then you have  system.out.printline and then hello  world so you see that it’s way more  verbose the Java code when comparing to  kotlin in here which is quite simple so  it’s a fun Main and then args and then  print line  so let me just close this  in here  and basically inside of this function  so inside you can basically type some  commands so here print line basically  it’s a function it’s another function  that allows you to print to the console  yeah so this when you run this so let’s  just click on the Run button  or here as well so this will give you so  if I scroll up or just put this bigger  have a look hello world and then program  arguments  so this prints to the console and inside  you can have a string we’ll come back to  strings in a second but this is just a  series of characters  and also in here this is how you  basically combine a string with some  type of function or you basically  evaluate some variables so this is quite  neat as well whereas Java is a little  bit different  so in here we just say print hello world  and then we have program arguments so  now program arguments these are  arguments that you pass when you run the  program so what we can do is so in here  within IntelliJ click on these three  dots  and then click on configuration and then  edit  and inside we should see program  arguments  so here I’m going to say Foo space  bar  and then say apply  and here we can run  and you can see that now we have hello  world  program arguments Foo comma bar  so full comma bar what is doing this is  this right here  okay so I could say args  and then get the first element of arcs  by adding square brackets and then zero  and if I run the program we should only  have Foo okay so this right here is a  utility function that joins the array to  Strings and we will cover arrays in a  second  and this is pretty much it now obviously  we don’t need none of this as we learn  kotlin and also this right here so args  is optional so we can get rid of it  and this is our main function  obviously if we run it  we should see nothing being printed on  the console  cool so  now we have an understanding of the main  function which is pretty much the main  entry point for your kotlin program  again if I remove this name in here so  if I say  X for example or a z I cannot run this  okay so basically I should have one  function called Main in order to run  whatever is inside this file  cool this is all for now catch me on the  next one  when writing kotlin code you need to  understand the reserve keywords and  these are keywords which are part of the  language and cannot be used for anything  else other than writing kotlin code  so here you can see they’ve got a list  of hard keywords of keywords modify  keywords and the list in here it’s quite  extensive in here so field init param  set  modifier const data enum  and then special characters and then  operators  mathematical operators in here equal  assignment  in here for null safety you can see that  basically you’ve got these keywords and  they cannot be used to combine it do  anything else other than writing your  kotlin code okay so let me show you what  I mean so if we look into so let’s have  a look VAR so VAR in here  so declares a mutable  property or local variable  okay so if I go back to my IDE and  inside of my IDE let me first basically  rename this so main.kt  I’m going to rename so go to refractor  renamed and then I’m going to call it  reserved  and then keywords so for each lesson you  should see the corresponding file okay  so refactor nothing changes but the file  name and the contents inside are the  same so if I say for example VAR in here  and then brand equals two and then here  just add double quotes and say for  example Amigos code right so this right  here is a reserved keyword  and I cannot use it to combine it to for  example VAR so instead of the name of  the variable be name brand I cannot say  VAR in here  right so VAR is used to construct a  variable  now the name of this variable which is  brand cannot be any of the reserved  keywords I cannot say fun for example  because fun is a reserved keyword  however I can say main right so I can  give it a name so this is not a reserve  keyword I cannot say for example when  it’s a reserved keyword I cannot say if  I cannot say valve for example so these  are Reserve keywords so I could say name  so name is not Reserve keyword and if I  want to print the name in here  I can just say name  here I can run this  and we should see Amigos code  now we have this wiggly line in here and  we’ll come back to this later  but this is pretty much Reserve keywords  let me go ahead and create a brand new  file and their project in here and  inside kotlin create a new  and then here kotlin class  and I’m going to call this as comments  and then in here there’s no need for us  to have a class so later we will learn  about classes so just have a file  press enter  and now we have the comments.kt and you  will have access to this as well right  so the idea is for every single topic  that we cover we should have the file in  here for the corresponding topic now if  I want to have the main function I can  say fun  and then main right so basically you can  have as many main functions  provided that you will run each file  separately right so usually you would  not have too many main functions at  least one to run the entire program but  for us because we’re learning the  programming language I want to have the  main function in all of these files so  that I can run each file independently  cool so if I Collapse project  and you saw that we did type the main  function  also what we can do is you can say main  so just say Main and here you can see  that you have two options you can choose  Main  just like that nice and easy or if I  type main or just may and then here we  could choose the main function with  arguments  cool let’s just choose the main function  without arguments and here if I say  print so print allows us to print  anything to the console I’m going to say  hello  please  comment  and then me okay  so have a look if I run this  we should see hello please comment me  and what we can do is we can add a  comment in kotlin  by using the forward slash twice  and this basically doesn’t execute this  statement so if I run this  you can see that now there’s nothing on  the console so this is a single line  comment but also we can have so if I  duplicate this  and let me just say inside here  so let’s just change this to  multi and then line  so we can have multi-line comments by  basically adding forward slash and Then  star  and basically you see that IntelliJ did  autocompletion for me there but  basically now I can type anything inside  and this is a multi-line comment instead  of a single line comment okay  so this is a difference and comments  basically they allow you to comment your  code and they don’t get executed  foreign  let me just go back to this print line  in here so  it’s very important I understand exactly  what this is doing right so print line  in kotlin here this is used to print the  console so if I run this  you can see that we have hello please  comment me so this is from the previous  video  there we go so hello please comment me  let me just say hello in here for a  second  I’m just gonna say hello  and basically when you say print line in  here you can literally pass anything you  can pass a string which you’re going to  learn later you can pass numbers you can  pass objects you can pass characters  literally anything because there’s a  variation of this print line method and  that’s achieved with something called  overloading now you learn later  overloading when it comes to methods but  in here so if I say hello  and then here I’m going to say how are  and then you right it doesn’t have to be  all in camps so the beginning of each  letter but if I run this  what you should see is hello and then  how are you on a new line so Ln pretty  much just says print and then give a new  line which is equivalent as so if I say  print  so there’s also print in here  so if I run this we should see hello how  are you all in one line and this is  because here we just print we don’t give  a new line and then we print line and  the next print or print line will be on  a new line because of this and this  right here is the same as this so  basically forward slash and then n so  this gives a new line even though I’m  not saying print Ln I’m just saying  hello and then new line backslash n if I  run this  you can see that we have hello how are  you so throughout this course we’re  going to use print line quite a lot and  also what I want to show you is that  print so if I basically press command  and then navigate into it or command B  and you can see the keyboard shortcut  Down Below in here so command b or Ctrl  B on Windows and essentially what this  is calling is system dot out dot print  line and then pass it the message so  this right here is what this is Java  have a look Java Lang public final class  system cool so internally all it’s doing  it’s calling  system.out.printline that’s all for now  catch me on the next one  let’s quickly learn about variables so  variables is pretty much a placeholder  where you can pretty much store anything  so in here there’s a couple of ways of  storing variables so you could say VAR  you could say vowel in here and then  there’s other ways but for now let me  just stick with VAR and then I’ll show  you better ways of doing this and I’ll  explain the difference between VAR and  vowel very soon so if I want to store  for example a sequence of characters in  here I can basically say VAR and then  give the name of the variable  followed by so in here the data type and  we’ll cover data types in a second but  here let’s just say string  equals 2 and then double quotes and now  I can add a sequence of characters so  here I can say Amigos  or let’s just say Jamal for example  there we go so now this variable in here  called name has the contents of Jamal in  here if I want to store Jamal’s age for  example I can say VAR h  and then here colon and if I say string  equals two and then I want to store for  example 18  so this is not valid because with  strings you can only store sequence of  characters if you want to store numbers  you can basically say int  cool so basically a variable allows you  to store one and only one thing and  obviously if you want to store multiple  things inside of a variable we have  something called data structures so  these are arrays lists sets and all that  stuff which we will cover later but  those allows us to store multiple values  inside of a single variable  cool if we want to print so the variable  we could just say print  and then I can say name in here  let’s also print h  in here so name as well as age and let’s  run this  and you can see that we have Jamal and  then 18. if you want to print both on  the same line in here what you can do is  you can basically use  dollar sign  and then curly brackets twice and then  you can pass a value so you can say name  and this actually has to be within  double quotes for this to work just like  that  so name and then also dollar sign curly  bracket and then age  and what this allows you to do is to  format the string  so here I can say Name colon  and then I could say age and then colon  and here you can see how it works  cool so if I remove this and in fact let  me just put basically duplicate this  and then put age in here  and let me just comment this out so you  have it for reference so remember single  line comments or we could use multi-line  let me just move these above  just like that and if I run this  you can see that now we have name and  then Jamal age under one single line  and this is the concept of variables so  once you store the variables so think of  a variable as a placeholder to store  values you can use it then later  throughout your program in our case we  just use it to print but here we can do  way more than just printing but we’ll  cover strings and all the operations  that we can do with variables later  next let’s learn about data types  if you come from java background you  will know that in order for you to  create a variable you need to say int  and then for example a number equals two  and then basically 10 for example right  and this is the driver syntax now in  kotlin we don’t have this concept of  Primitives in here and pretty much  everything is an object so in order for  us to create for example a variable that  represents a number we can do it in  multiple ways but let me first show you  the exact same syntax that you would  have in kotlin to compare with Java so  here you need to say  VAR  and then number  and you can see that there’s a number  and then say colon and then here you  specify the data type so here this will  be an INT  and then equals to and then 19 for  example or 10 right so this is the exact  same thing but  in kotlin so kotlin this is the syntax  and this is the Java syntax okay so let  me just collapse this in here and this  is how you define a variable that stores  numbers if you want to store bigger  numbers you could say VAR and then here  I’m going to say L for long and colon  and then the data type in here long just  like this  and then equals two and then here I  could add you know a very long number  and I need to add the L at the end right  so the difference between an integer and  a long is the size basically okay so  this here stores bigger numbers so the  same with double and Float so if you  want to store decimal numbers you can  say VAR I’m going to name the variable  as D in here colon and then double  n equals two and then  3.14 or 1 2 or 1 3 right and you’ve seen  that I actually been adding semicolons  and this is because you know used to  Java but basically you don’t need  semicolons in here my bad and also if  you want to store bigger numbers that  have decimal you can use floats I’m  going to say f in here and this is float  equals to  and then  3.14 for example and then we have to say  F at the end  if you if you want to store booleans  true or false you can say VAR  and then here I’m going to say B and the  data type for this is Boolean equals to  true  or false okay so these are two possible  values  and also if you want to store strings  basically a sequence of characters you  can say VAR and then s for string and  then here just say string  and equals to  oops and then here you can say Amigos  code and finally if you want to store in  here so single characters you can say  VAR and then C so this is the name of  the variable and I’ll come back to  naming conventions and all of that good  stuff but here we say Char in here  equals two and then we can add a single  character and we need to add this within  the single quotes and then here we could  just say a for example for Amigos code  so this is how you basically store  single characters  and these are pretty much the basic  types that you will encounter there’s  other ones so there’s like a race which  I will leave it for later but in a  nutshell this is how you go about and  create so let me just say here end for  number  so these are the data types that you’re  going to use within kotlin and if you  have custom data types which you will  learn later so with classes then  obviously so actually let me just format  things then obviously this will not be  an integer but for example you might  have a person type right so then this  will be person but more on that later if  you have any questions drop me a message  otherwise catch me on the next one  okay  there’s one type of which I want to talk  to you and that is the any type so you  see that if you want to have a variable  you can say VAR and then here you can  say number for example  colon and then you can basically specify  whether it’s an integer whether it’s a  double or any other data type so here  let’s just say 10 for example so the  same which shrinks right so if I want to  have name for example I say Name colon  and then string n equals two and then  Jamila for example  so in kotlin there’s the any data type  and basically the any data type is the  type which basically all of these types  come from so integer string double and  long and pretty much any other data type  hence the name any which means that I  can change this time to any  just like this  and also this one just like that  and from this point onwards so let me  actually go back  so here for example if I try to put a  string inside I’m not allowed right but  if I say any in here I can literally put  anything I want so here I could say  string I can say number I can say a  decimal like that I can add a character  like that so just bear in mind that if  you see this it means that this variable  right here can basically hold any data  type and if I press command so just  press command on your keyboard and go  into it and you can see the keyboard  shortcut command b or Ctrl B on Windows  and here  any so public open class will cover a  public open class as well as package  later but here the root of the kotlin  class and every kotlin class has any as  a super class  so basically this is the root so here  let me just close this  and this is pretty much the any data  type and just before we wrap up this  video  you shouldn’t really use this right so  you shouldn’t have all of your data  types as any because it will cause lots  of confusion right so always be specific  on the data type if you’re using strings  just push strings if you’re using  integers then put integers right so  don’t use the any data type for every  single variable out there  catch me on the next one  so far you’ve seen that we have VAR and  then the data type long double float  Boolean string Char and whatnot  so doing this actually is not needed  because we can use a feature called type  inference which means that kotlin will  actually  determine the type for us without has  having to explicitly Define the type so  what we can do is we can get rid of the  type all together so here this can go  just like that  and even IntelliJ if I put my mouse in  here you can see that explicitly given  type is redundant  and what it means basically we don’t  need this so we can get rid of that  so let’s get rid of all of those so  float  as well as Boolean  string basically literally for  everything and for characters  and you can see that this is much neater  so don’t declare the types and obviously  there are times when you want to declare  the types but we’ll cover that later  most of the times just use type  inference which allows you to define the  variable without having to define the  type  all right  cool so in here you saw that we can  define a variable by saying for example  brand equals two and then here we can  say Amigos code  so this is one way of defining the  variable and we can also Define a  variable using the Val keyword so this  is Val keyword and here we could just  say let me just say B for now equals to  and then omega’s code  okay so let me actually say brand and  then vowel and brand and then VAR just  like that cool now what is the  difference between VAR and Val well with  VAR basically I can change the contents  of the variable once it’s been assigned  which means that I can go right so here  I can say brand VAR equals to something  else so here I could say code  then with  Amigos code for example right so this is  the difference whereas with Val so when  you define the variable Val you  basically cannot perform the exact same  thing so here if I duplicate this line  and  and bring it down just like that and  then change this from VAR to vowel so  vowel in here so I want to change the  contents of brand vowel from Amigos code  to something else I cannot and you can  see that it’s telling me file cannot be  reassigned change to Val instead so  basically this is how to create an  immutable variable meaning that once you  define it you cannot change it so in  Java this would be the equivalent of  so in here so in Java this is final and  then string and then brand equals to and  then Amigos code  so this is the equivalent in Java okay  and once you assign this you cannot  reassign it  and this is the difference between brand  and vowel now obviously which one should  you choose so technically you should  prefer immutability always because it’s  easy to reason about changes especially  when you are within a multi-threaded  environment but this is more on the  advanced stuff so for now I would say  use Val and if you need to reassign the  variable change it to VAR  okay so JavaScript they have the exact  same concept so JavaScript they’ve got  VAR and const so it’s the exact same  concept cool this is pretty much it and  let me just comment this  because otherwise we’ll have an issue  this pretty much it catch me on the next  one  okey dokey you’ve seen Val in here so  Val let’s just say name equals to  and then Jamal and you’ve seen the VAR I  equals two and then zero the difference  is that with Val this is read  only and this one is  mutable so you can change the value in  here right so if I try to change name  to equal something else I cannot but if  I do VAR or actually sorry I equals two  and then two for example I can do this  All right so let me just comment this  out in here and we also have something  called const which allows us to create  values that will never change  so the cost keyword is as follows you  say const and then you give it a name  now in here if I pretty much just say  const and then the value of pi for  example equals 2. so const and then I  need to specify whether vowel or VAR so  let’s just say vowel in here and the  value of pi let’s just say  3.14 let’s just keep it simple right  so basically in Java this is the same as  static and then final  this right here is the same as so final  basically this is the same as Final in  Java and this is static and then final  now obviously if you want to create a  constant you basically have to Define it  outside so basically it could not be  inside of a function  now you might be saying right so I could  actually Define the same thing without  vowel right so what is the difference  well the difference is that const vowel  so this value is known before code  execution before you even run your code  so we know this value in here whereas  Val so if I just say vowel then it’s  impossible right because I could have so  let’s just say in here if I say we’re  going to you’re going to learn this  later if I say func and then get and  then pi and then the value and we’re  going to have parenthesis and here’s a  colon and let’s just Define the type so  this will be double so let’s just say  double  and then equals to 3  0.14 okay don’t you worry too much about  this but this is me creating a function  that just Returns the value of pi which  is 3.14 now with a constant right here  so you see that we have a function and  if I was to say the value of pi in here  is get and then Pi this does not work  because I’m dependent on this code to  load the actual value so here if I put a  mouse in here you can see const Val  initializer should be a constant value  whereas if I say vowel and then I’m  going to say Pi in lowercase  equals to get and then pi value  so this is allowed and if I say const  this is not allowed right so this is the  same as the final keyword in Java and  this is static Final in Java  so this is how you create constants in  Python so it simply means that when you  say const this value must be known  before execution  so here if I say  3.14 I know the value right so the value  is here and it’s not been computed  from somewhere else  cool hopefully you understand the  difference between const and vowel and  VAR and if you have any questions feel  free to let me know otherwise catch me  on the next one  in this video let me walk you through  nulls with kotlin so here let’s say that  we have a variable called name and let’s  just say Jamila and in Java we can take  name  and then say equals to and then no  now in kotlin this is not possible so  kotlin type system is aimed at  eliminating the danger of null  references  so it means that if you want to reassign  a variable to something null this right  here cannot be no right and this will  avoid many issues and in Java this is  the issue so no pointer exception so  this guy in here  and it’s one which is very common to  beginners to understand why is it  happening but basically if you have your  variable that is null and you try to  perform any operation on a variable  which is no then you have the null  pointer exception whereas in kotlin this  is not possible which means that you can  safely say for example so here if I  print and we’ll cover strings in detail  later but I can say name  Dot and then here you can see that we  have a couple of methods so get compare  equals to string  in here drop last filter lowercase and  whatnot so let’s just say for example  uppercase  so uppercase so this will take this name  in here and transform all characters to  uppercase so if I run this main function  you can see that we have Jamila in  uppercase now if you for example want  the ability of a variable to be null  what you can do is the following so here  let’s just say VAR  and then brand and then let’s define the  type this will be a string and this can  be equal to and let’s just say Amigos  code  and now if I take brand and say equals  to no  so basically no in here so this is not  allowed and for us to do this we need to  add question mark in here so now this  variable in here can have the null value  which means basically pointing to  nothing so in this case the variable  name is pointing to the string reference  called Jamila whereas here it’s just  pointing to literally nothing  so if we print this value this will be  no  cool so if I take this actually so let’s  just put it null in here  and if we say print line  and here we’re going to pass brand Dot  and then uppercase just like that  now immediately you can see that  basically it’s telling me the value of  brand is always null right so if I try  to run this  I’m not allowed right so we get  compilation error so what we have to do  is just add a question mark so question  mark and now it says right so we know  that this can actually be null so in  order for us to you know invoke the  uppercase on the variable brand which  might be null we need to be sure so it’s  either going to uppercase or it’s going  to print no right so here if I run this  and you can see that we get no in here  and this is how kotlin protects us from  no pointer exceptions so now here if I  was to say Migos code  so we don’t get  if I run this we don’t get no but we get  Amigos code instead in uppercase and  we’re good to go now there are a couple  things that we can do with this syntax  in here but we’ll discuss this later  when we touch on if statements but for  now I just wanted to teach you the  concept of null safety with kotlin  okay  dokey let’s cover the string class in  kotlin which allows you to store a  sequence of characters and pretty much  if you want to store any text this is  what you should use so let’s learn so  here if I want to create a string for  example I just say vowel and I can say  for example name equals two and we can  say Ahmed for example so this is one way  of creating the string  or if you want also you can add string  just like this and as you saw that this  is redundant okay so let’s just remove  this now we have this variable called  name that stores this text  if I want to print the variable  or the string itself I just say print Ln  and then in here we can run  and you can see that we have Ahmed in  here so which strings we can perform a  bunch of things with it so here if I say  dot this gives me the list of all  possible methods available to us so you  can see uppercase so you’ve seen this  where you want to turn the entire string  to uppercase so let me just in here  duplicate this command D and then say  dot and then uppercase in here if you  want this to lowercase you just say  lower  and then case  so you say dot and then uppercase and  then here you invoke the function itself  we’ll cover functions later  but this is how you turn the string to  uppercase as well as lowercase also if  you want to know the number of  characters that this string has just say  dot and then length so length is  actually a property and not a method  available  within the string class and we’ll cover  also properties as well as functions and  methods cool let’s explore other methods  so if I say print Ln I can say name and  basically I can actually grab the first  character of the string by adding so I  can add the square brackets in here and  then I can specify an index so 0 means  this index in here one two three and  then four so if I duplicate this and  then say 4 in here this will give me  capital A and this will give me D in  here so if we run this let’s just run  this for a second  and you can see that we have capital a  lowercase D we’ve got five so Ahmed has  five letters  in here we turn Ahmed to all uppercase  and here we turn Ahmed to all lowercase  so you’ve got a bunch of these functions  in here and the best way of learning  this is basically explore the  documentation so if I say name  Dot and in here I can basically navigate  and then see whether there’s a method  that does what I want  but this is kind of it right so you have  a bunch of these that you can use so any  a sequence for each and whatnot so it’s  null or blank so it’s not empty so for  example here if I say is let’s say is  empty right here right so if I say it’s  empty so we know that this will give  this false because there’s  something within that string but if I  pass an empty string like so so empty  string and then I invoke dot is empty  run it this will give me true  okay so you’ve got a bunch of these  methods that you can use within this  string class and the way you’re going to  get used to these methods is just by  using this string class and it’s one  which you use quite a lot within any  programming language to be honest  okay  cool the other thing I want to cover is  let’s say that you want to in here let’s  just say that you have a variable so Val  and then you have H so H equals two and  then let’s just say 20. so here if you  want to print two strings together you  can say print in here or in fact let’s  just add it to so here VAR and then  let’s just say message equals two and if  you want to combine both the name  variable and H into one you could just  say name plus and then here you can  concatenate another string to give a  space and then plus and then h  so you could do this and if I print the  message  so this right here will gives us name  and then 20 Ahmed now don’t do this so  this is the old way of doing things and  the better way is for you to use the  string template so here just add double  quotes and add dollar sign  and then curly brackets and then here  you can specify name so the variable  itself should go inside here and also we  can have the H so dollar sign  oops like that Curly bracket and then H  so this is a better way of doing things  now even better what we can do is we can  remove the curly brackets so you can see  that the syntax now becomes way shorter  and easier to read and this should be  actually vowel not a VAR vowel and you  can see that we have the message and  here we’re combining both and the cool  thing here is that I can say name  right  and then here maybe I want to have a  comma for example and an H  just like that  if I run this  there we go you see that I have name so  this is a string and then Ahmed this is  the value and then comma Space Age and  then colon and then 20 in here  so this is how you combine strings and  this is the best way of doing so and  obviously this data type here is  actually string so here if I say colon  and then string it returns a string and  we can perform all the other methods  that you’ve seen  the other thing I want to cover is  multi-line strings with kotlin and that  is let’s say that we have a method in  here so let’s just say that we have a  variable so Val and an email  and then I can say equals and here just  add the quotes once  twice and then three times  and then press enter  so once you press enter this will be  autofilled for you so trim indent and  what we can do now inside here we can  basically have so hello  how  how R and then U okay so we could  basically have line one line two line  three line four so on and so forth  so if I print let’s just print the email  so email in here and then run this  have a look so you can see that we have  hello how are you in three different  lines right and the cool thing here is  that this actually keeps the indentation  for us so if I add you know two spaces  in between run it oops not debug it but  run it  you can see that we have hello how are  you  the Oracle thing is if you want to add  for example name so hello and the name  you want to pass a name for example into  this string so here you can say  percent and then s  so which means that you want to pass a  string and now I can say email Dot and  then format in here and I can pass the  arguments right so here I can say for  example hello  and then Anna  okay so this will be hello Anna how are  you so if I run this  have a look so we have hello Anna how  are you now obviously if you want to  pass for example a digit this percent d  and you can even format things and I  would encourage you to look into how you  can format your strings but this is  pretty much the multi-line string  feature within kotlin format right so  format doesn’t have to be with  multi-line so it could also be within a  normal string in here so here if I say  dot and you can see that we have format  and you can pass whatever formatting so  percent s for example and then that will  be replaced as well  awesome this is pretty much what I  wanted to cover about strings if you  have any questions drop me a message  otherwise catch me on the next one  in this video let’s learn about how to  compare strings in kotlin so in kotlin  we have an extra operator which is the  triple equals which we don’t have in  Java and it kind of works the exact same  way so let me break it down to you so  let’s say that we have a variable so  here must say Val name one equals two  and then Sally  so we have this variable in here called  name one and in here let’s have another  variable so I’m going to say vowel name  and then two equals to Jamila  now in here what we’re  now in here when we want to compare  these two to see whether they are the  same we have couple options so you know  that this right here so when we say Val  this is a read-only value in here so if  you want to reassign this you are not  allowed so if I say name two go name one  equals to something else not allowed so  if we want to compare these two values  we have to use D so if I say print Ln in  here the variable name so name one and  then we can say equals so double equals  and then in here we can say name and  then two  so if I run this in here  you should see that we have false  because in here  name one not equal to name two so the  contents of name one not equal to name  two now in Java this here so um it  wouldn’t work right so if you took my  Java course you would have known that  this right here is not used for Strings  and in kotlin also what we can do is we  can say name one dot and then equals  so one variable equals to another  variable so if I run this in here  you can see that both of them are  returning false now if I create a third  variable so Val and then here I’m going  to say name three  equals to and then Saleh  and let’s change now and here so let’s  ignore name two and let’s change this  from name two to name three and the same  in here  if we run this what do you think the  output if we run this what do you think  the output will be so here if I run this  you can see that both of them are  returning true okay  now this is actually correct and that’s  because when we say equals right so  double equals or equals here we are  comparing the contents I.E whatever we  have inside so if this was to be  lowercase s in here  and run this  you can see that now both of them are  returning false right  cool so this is nice and easy now what  about if I put this back and you know  that the equals in here and Dot equals  will return true so what about now if I  create the string using this so just say  string so this is another way of  creating strings  and inside  paste Saleh so just like that so Sally  just like this and say dot and then two  car array  so this is how we create a string using  the string construct so this is a  Constructor and you will learn more  about Constructors later  so now what do you think the output for  this will be  well the first one in here so let’s just  um use some some string template  so double quotes  and then I’m going to say double equals  and then dollar sign and in here  let’s basically print the result of this  like that and then end with double  quotes and we’ll do the same here so  double quotes here  and double quotes and then close this  with curly bracket and then dollar sign  curly bracket  and here we’re going to say  Dot and then equals  so I want you to see the output now what  do you think the output for this will be  so double equals in here versus Dot and  then equals  so if we run this let’s have a look what  the output will be  so in here we have true and then true  right so here nothing has changed and  from this you know that d double equals  as well as dot equals on strings they  are comparing the contents so in Java  this right here so equals so 2 equals is  comparing the memory location and if you  want to achieve the exact same thing in  kotlin what you do is the following so  here we pretty much say so let me just  duplicate this in here  and we say triple equals in here so now  this is actually comparing the  memory location right so inside of the  string pool which I’m about to show you  if I run this in here so let’s just run  this  which equal equals have a look this is  false right so in here what we’ve done  is so when I say string and then we pass  Sally so this is creating a brand new  object and it’s not reusing the contents  of sale which is stored within the  string pool  so if I was to go back in here so let’s  just get rid of that  in here as well  and this right now should give us true  before it give us false but if I run it  you can see that we have equals  right so they are the same in terms of  the value  in terms of the reference and here the  value as well  and this is pretty much how you compare  strings in kotlin now let me put this  back in here so that you have it for  reference but let me explain this in a  diagram  foreign  so what is happening is the following we  have a variable called name one and the  value of it is Saleh then we created  name two which is equals to jamida now  these when we create them the value for  them is stored within the string pool  then when we say name three and then we  say string so we basically use the  string Constructor this is creating a  brand new object called sale  so that’s why when we use the triple  equals it’s comparing the reference to  where they are in memory and obviously  you can see that name three and name one  they are in completely two different  locations in memory hence the triple  equals returns false but double equals  and Dot equals will return true because  the contents are the same so sale and  then Sally with capital s  and you saw that when we changed it back  so when we changed it from the normal  way of creating strings I.E in here so  this is basically how you integrate  strings all the time and let me have it  for reference in here so you can see and  let me just remove that in here  and also two car array or chart whatever  you want to say it and let me just  comment this  so if we were to create a string like  this right so variable name three equals  to sale what’s going to happen is  name three  we’ll basically check whether there is a  value for sale within this string pool  and if it does it just points to the  exact same location  so this is the video of string pull and  it’s a special area in memory Just For  Strings because strings are the most  used data type in kotlin and pretty much  in all programming languages and they  are a bit special in terms of how they  are stored so that it increases  performance and also that we don’t waste  memory okay so when we create strings  the normal way  it basically checks whether there is  something within the string pool if  there is it just points to it if not it  will just create in the swing pool and  then refer to it  but when you create using the string  Constructor  it basically creates a brand new object  so if you were to create name four for  example in here right so name four  and basically if you use the string so  here if you just say like that so Val  and then  basically name four equals to and then  Sally  so if you do this  name four will point to Saleh inside of  the string pool just like this right  however if you do this in here name four  equals to so this is what I’ve been  saying string  and then you put salad inside  and then you say two and then car or  char  Ray  just like that so let me just put this  on the side like this  so if you do this way then it’s not even  going to reuse the exact same solid in  here it’s going to create a brand new  object in here and then it’s just going  to point to it like so  and you can see that now there’s a bunch  of waste sadly here Sally here and Sally  here so when it comes to Strings  avoid creating strings like this and  always do it this way  if you have any questions on equality  and string pulls feel free to let me  know otherwise catch me on the next one  cool in this video let’s learn about the  arithmetic operators that ships with  pretty much any programming language and  kotlin also is no different so in here  you saw that if you have so here we can  have numbers so if I have 10 so number  10 equals 2 and then vowel and then  number  let’s just say number two equals two and  then two in here so let’s just name this  as number one and if you want to add  these two numbers you can print so let’s  just print and we can say number  and then one plus and then number two  so this is how you add numbers together  so if you want to subtract it’s the same  thing but you just say minus if you want  to divide you divide like so and if you  want to times these two numbers you just  add the star in here  so also there’s something called the mod  so let me just duplicate this instead  and this pretty much is the remainder  and this goes like this so 10 and then  mod so the percent sign here and then  the number two so ten more two it means  how many times two goes into ten  basically so it goes into ten five times  and the remainder is zero if I bring  this in display button  in here you can see that we have 10 plus  2 is 12 10 minus 2 is 8 and then 10  divided by two is five and then 10 times  2 is 20 and 10 mod 2 is 0. so if I was  to change this from three so three goes  into ten three times and the remainder  will be one if I run this  there we go you see that this time is  one and all the other operations  actually change as well  and this is pretty much it let me just  add some formatting in here so here we  could just actually let me just select  this right here  and then Ctrl G couple of times  and here I’m going to add double quotes  and then this will be  dollar sign and then curly brackets  and let’s go on the other side and then  add parenthesis or actually double  quotes  and we’re going to finish it like so  there we go and then here I’m going to  say add space and here I’m going to say  oh actually let’s just add this symbol  right so Plus  this is minus  this is division  this is subtraction oh actually  multiplication and this is the modulo so  here and if I run this  there we go you see that now we have  formatted the output  cool obviously you can format this  better but because my font is quite big  I’m going to leave it as is  and as always because I’m used to Java I  just need to remove the semicolons  because it’s not needed  this is pretty much the arithmetic  operators that kotlin comes with and  pretty much this is standard in every  language  you’ve seen all of these operators in  here so plus minus division  multiplication as well as the module let  me actually show you there’s a class  called math and basically the way you  use it is let me actually show you the  long way so here if I say kotlin  so this is a package basically and we  learn about packages later and then have  a look math  and here I can say dot and I’ve got  access to a bunch of things so here I’ve  got the value of pi if I want to know  the value of pi and you can see the data  type is double there’s absolute methods  a course  a sign probably remember this from GCSE  level seal cosine exponential floor  log Max so if you want to know the max  between two numbers this is how you do  it right  so in here round you can also round a  number  so you’ve got tan and I think that’s  pretty much it right so let’s say that  here I want to know the value of pi so I  could just say pi and I can print this  so let’s just print  just like this  and if I run this so here run this guy  and there you go so this is the value of  pi in here if I want to know the max  between two numbers so I just say Max so  this time I could just say Max instead  of the fully qualified name okay so here  we can also remove this  and we have we and we have to import  properly right so  from kotlin and math or dot pipe  basically so one thing to note is so  when you do this it actually it adds  this import statement at the very top  and we’ll cover this later but basically  this is just saying right so bring the  value of pi which lives in a different  package into this class in here and also  the same with Max right so Max in here  so we can say Max and if I press command  P it takes two numbers so  10 oh actually let’s just say I think we  have number  one and also number two right so between  these two numbers I want to know which  one is the max so let me just say print  and  just like that run this and you can see  that 10 is actually the max if you want  the Min so let me just remove this and  let me just remove this so oops not like  that but let me just get rid of that so  I have space so let’s say 10 and 3 I  think or two it doesn’t matter so if I  want Min I just say Min in here and here  it says that it doesn’t know Min that’s  because we need to import Min so you can  use IntelliJ to import the function so  just put your mouse in there or you can  duplicate this and say Min in here and  it should work if you don’t want to  import everything so here you can see  that I do have a bunch of imports I  could just say star so pretty much just  bring everything and I can get rid of  all of this right right here and you can  see that some people prefer this some  people don’t I personally disagree with  this so I like to import whatever I need  instead of bringing the entire world  cool so if I want to know the main so  here if I run this and we’ll cover  packages later so the minimum is three  if you want to round so on and so forth  so let’s just explore one more method so  print line and here I can say kotlin dot  and then math Dot and let’s have a look  square root S square root right here so  square root in here of basically let’s  have a double so 5.0  sorry s q  where root  sqrt so run this  and you can see that it’s two point  yeah right so not helpful and if you  want to I think round print line and  then kotlin Dot and then math and in  here let’s just use round so if I want  to round  so let’s just pass a double so  4.2 for example if I want to round this  you can see that this gives us four  right so if it’s 4.5 so 4.5 in here run  it this gives us four so it always  rounds down  so I’m gonna leave it here but basically  just bear in mind that this is how you  use the math class within kotlin and  this is actually not needed and I just  did this to show you the Imports but  we’ll cover more packages and imports  later  this pretty Madrid catch me on the next  one  since we are working with numbers and  let’s focus on the plus plus and minus  minus operators so plus plus  and minus minus let me just comment this  in here  so if we have so let’s just say print Ln  and then we have a number so let’s just  say  in here let’s just say  Val  number equals to and then let’s just say  10 in here if I want to increment this  number by one all I do is I could say  plus and then one right so if I run this  file in here  you can see that I do have 11 in here if  I want to increment by 2 I could say 2  so on and so forth right but there’s a  shorthand when you want to increment  just by one and I use it and that is  using the plus plus operator in here and  the space in here it’s not needed now  this here is complaining because when we  say number plus plus remember if it’s a  vowel it means that we cannot do this so  Val it means that we cannot take number  and say equals 2 and then something else  right so in our case we want to say 11  okay so if it’s Val we’re not allowed to  do this but what we want to do is change  this to VAR so hopefully now you  understand the differences and if I run  this  you should see that we have 10 but why  is that right so why is that  so in order for me to explain we have  two concepts we also can do number plus  plus B4 so plus plus B4 just like that  and if I comment this out this second  line for a second and then run this  you can see that now we have 11 which is  nice  so the difference is so in here plus  plus before the number  increments and Returns the value so  that’s why you see 11. so increments  first and then Returns the value whereas  with the second one  number plus plus in here this Returns  the number  and then increments when I mean Returns  the number when it prints out it has the  current value so it prints the current  value and only then it increments the  number which means that the next time  that I print number in here  this will be what have a guess  so if I bring this  this will be 11.  cool so hopefully you understand the  difference now if I was 2 so in here  remove this  have a guess what this will be  so let’s go step by step so print Ln  plus plus number so this will be 11  because it increments the number first  and then returns right  then in here what this will be  so the value now is 11. so this is  return 11 and then increment so this  will be 11.  and in here this will be what 12.  cool let’s just check whether we are  right  and there we go 11 11 and then 12. cool  so the other thing that we can do is so  we have plus plus but we also have minus  minus so here let me just  duplicate this twice if I say minus  minus and let’s just say minus minus  here and finally let’s print the number  in here  so  in here we have 12 we say minus minus  number this basically decrements and  Returns the number so this will be 11.  and in here the number is 11 so we’re  going to return the number and only then  decrement so this will be 11 and then  the final number should be 10.  so the original number so if we run this  we should see 11 11 and then 10 and this  is pretty much the plus and minus  operators  if you have any questions on this  please let me know otherwise catch me on  the next one  cool let me talk to you about the plus  operator because it’s very special in a  sense that when you are dealing with  numbers it has a completely Behavior to  when you are working with strings  so you know how to work with numbers now  strings and you’ve seen all of this now  if I say sum equals to 10 plus 10 so  these are two numbers  and some string  10 in here plus and then 10. what do you  think the value will be  so this one here what do you think this  will be what 10 plus 10 is 20 of course  but here 10 plus 10.  so what this will be  if we run this  there we go so here have a look sum on  numbers 20 some on strings  this is a thousand and ten but this is  wrong right so basically the plus sign  in here and this is basically a concept  of polymorphism which means that many  forms so the plus sign when  applied on numbers it performs addition  when applied on strings it just performs  concatenation so you just concatenate in  these two strings together and not  performing any arithmetic operation on  these two strings  so bear that in mind okay very important  concept  that many beginners fail to understand  but this is was special about the plus  operator and bear in mind that this only  applies to plus so here when dealing  with shrinks I cannot say minus right so  10 minus another string right so here I  cannot say times nor divide nor modular  right so this only applies to the plus  operator  this is pretty much it catch me on the  next one  cool now let me teach you that if you  have let’s say in here we have a number  so let’s just say  number and then equals two and then 10.  so this is a string  and I want to print in here let’s just  say that we want to print  in here 10 and then Plus  and then basically the number 10 right  and you saw that this does not work  right so when you have a number and you  want to work with strings you know these  are two different types and what we can  do is we can take number and then  convert that into a number so just say  number Dot and then say two and have a  look so we have two string two big  decimal  big integer to Boolean to byte to chart  array to double to end so on and so  forth so you could convert one type to  another so say to int and now I’ve  converted this string right here into an  integer so if we run this  there we go so now we have 20. so all I  want to show you here is that if you  have so if you have let’s say 10 in here  and you want to I don’t know maybe you  want to add it to number 10. so let’s  just say 10 in here right so this now is  the way around so this is a number and  we want to concatenate a number with a  string so if I want to turn this number  into a string we can say 10 Dot and then  2  and then here we can say string  so 10 and then two straight  so I think hold on so 10 Dot  and the IDE doesn’t really recognize  that I’m within here so I think you have  to basically add a space and then  basically say dot again  or Dot and then basically press Ctrl  space I think this is a bug but if I  have in here number  or let’s just say vowel number and then  two equals to 10 this time  so then here I think number  two Dot and basically you have no issues  okay so I think I just found a book with  IntelliJ so 10 dot it doesn’t work have  a look I don’t see the order completion  but if I press Ctrl space then it works  cool so here if you want to convert a  number to a decimal or to a string just  say two and then you can see to byte to  end to float to long to string there we  go and then I can say plus number and  now remember so string to string this  will be a thousand and ten  so if you want to convert any number to  a different data type just say dot two  so dot  so in here let’s just take a double for  example so Val let’s say d for double  equals two three point zero  and if I print D  and let’s also print D in here I can say  dot  and then here two have a look to integer  to string to flow to double to Big  decimal  so to duration as well and whatnot but  here let’s just change that to an  integer  so it will be three instead of 3.0 have  a look 3.0 and 3.  and just before we end this video  IntelliJ is actually telling me that I  can replace this in here  with so in here we can replace this so  let me just duplicate this line  and leave it here for you so we can  replace that with this I’ve just put my  mouse in there now the reason why I did  that so to string is just to show you  how to convert from one data time to  another this is pretty much it catch me  on the next one  in this section let’s learn about the  constructs that kotlin has when it comes  to conditionals and decision making  first let’s learn about the Boolean data  type which you’ve seen before but let me  actually go in detail now  so a Boolean is pretty much a data type  that allows you to hold true or false  values so here if I was to say for  example is and then  adult  equals 2 and then here I can say true  or I can say false  so this is a Boolean okay so is adult  this is false then we can say for  example  Val  is and then male for example equals true  right now here you can see that the  possible values are true or false and  we’ll come back how to use these values  in a second  but in a nutshell this is what a Boolean  is if I was to print these values it  will just print true or false so if I  also say print  and then is adult  is just going to print in here if I run  this  it’s going to print false  the value right here  awesome so this is false nice and easy  now in kotlin everything is an object  and this means that also booleans can be  null so here if I say for example Val  order and then completed  so maybe you want to represent an order  right so ordered complete equals to and  let’s say that we want to assign the  value of no  so no means that it has literally no  value assigned to it  now in order for us to say that this is  actually a Boolean right here we need to  use the data type so here  we say Boolean so this is a Boolean and  in order for us to assign this to null  we need to say that this actually needs  a question mark and here remember the  question mark  and this now is a Boolean whose value is  no so booleans can be false true  or no  I’ll come back to when we use these  values  and that’s basically with if statements  and I’ll show you the best way of using  them especially when they are null which  is really important  so here if I was to print  the order completed  will just get no  here we go so false null and let’s also  print the other one  which is let me just do it here  and this is is and then mail and let me  just pull this up  there we go if I run this  you can see that we have false true as  well as no so these are the three  possible values for booleans  cool now that you know the three  possible values for booleans now let me  quickly explain the naming convention  for booleans so with booleans you kind  of want to always ask a question  so instead of saying adult in here you  say is and then adult  is male  here is so is  and then order completed right here so  this is a better name okay  so you basically always try to ask a  question that will return true or false  for example did you eat  did you sleep right so you can also say  for example in here  has  and then slipped  right so it is slept doesn’t really make  sense but has slept right or maybe is  and then completed as well so all I want  you to bear in mind is whenever you work  with booleans so let me just put this  right here at the bottom so you always  want to ask a question for the variable  name that you are about to create and  even with functions as well right so  later you learn about functions so if  your function returns a Boolean value  then you always want to ask a question  so this makes much sense instead of just  saying sleep or slept or completed okay  so is has and basically try and always  ask a question when naming your booleans  this is pretty much it catch me on the  next one  cool let me now talk about logical  operators when dealing with booleans so  let’s say that in here we have two  booleans so Val and then is and then  female for example and here let’s just  say false  so if you want to combine these two  booleans in here so here I’m just going  to print but we could also store the  result into a variable so if you want to  combine two booleans you say is adult  and then you can say and so this is the  and operator  and this should be  is female so here I’m saying is adult  and is female  so when you use the and in here and this  is the conjunction or logical and  so every single expression in here has  to be true in order for the entire  expression so this entire expression to  evaluate to true so this has to be true  this also has to be true in order for  the output to be true if I run this  you can see that this is false okay and  that’s because of is female is false so  if I was to change this to true now  in here  and then run this  there we go you see that this is true  and we can actually combine so let me  just come back in here we can combine  multiple ants so you know just limited  to one and but if you want to have  another and and in here you can say true  as well  right  so let’s actually put this into a  variable in here so Val and then is and  then pilot for example  or is the driver  so easy driver equals two and then true  so now if I say is driver in here  and if I run this  you can see that this is still false and  that’s because of this is female so this  is the and we also have the  disjunction which is the or so here if I  say print line I can say  is in an adult  or so or  so these two pipes or is female  right and or it’s also known as a  disjunction  so this is the logical or so this  basically at least one of them has to be  true for this entire expression to  evaluate to true  so it’s adult in here so this is true  then it bails out because it doesn’t  really matter or okay so if I run this  so this will give this true in here true  so we’ve got two outputs so the last one  is this one and it’s true because this  is true then it doesn’t need to know  whether this is true now if we flip  things around so if we say is female  and then or  and then is adult  it’s female this equals to false then it  tries the other value right so or so is  this true no all right or let’s try if  this is true if this is true then it’s  going to be true so if we run this this  will be true  and if this here is false so if it’s  idle is false  the entire thing will be false right so  at least one of them has to be true and  again you can combine multiple oars  together in here to create a more  complex expression  and the last thing I want to show you is  the negation so negation works as photos  so you can take whatever Boolean you  have so if I say is and then adult  and you can say  is not adult so you basically you flip  the expression so you say exclamation  mark  and this now when you say is not  so this will basically flip the value if  the value is true this will be false if  the value is false this will be true so  if I show you so if I run this is adult  is true but now it’s going to be false  and similarly is female right so here is  female  so if I want to say if if if I want to  say is not female I just say print line  and then not  is and then female  so this basically flips the value for us  so if I run this  this will be true right so it’s not  female basically  all right so this is pretty much it now  obviously also you could use parenthesis  so here if you want to combine let’s say  line number five if you want to say is  adult and so here so and we can combine  here we can combine these two in here  with parenthesis  and basically this becomes now a sub  expression  and at least one of them has to be true  right for the entire thing to be true  which means that now have a look is  female and is driver this will evaluate  to false  false and true so false and true equals  to false but if I was to maybe negate  this so let me just run this so you see  that this is actually  false in here false but if I also say  is not a female right so this now  becomes true  and this is true as well and basically  now the whole expression is true if I  run this  this will be true but also what I could  do is if I don’t flip the value I could  say or in here so you can see how I’m  combining multiple logical operators in  here so it’s female so this is false or  is driver so this entire thing now will  be true because this driver is true  is adult true true and true equal to  true if I run this  there we go have a look true  awesome so this is pretty much how to  use the logical operators with kotlin if  you have any questions drop me a message  otherwise catch me on the next one  cool you’ve seen logical operators now  let me teach you about comparison  operators  so comparison operators allows us to  compare two or more values and the  result of the comparison is a Boolean so  if we have for example so Val  and then let’s just say and one for  number one and let’s just say that this  is 10 Val and two  equals to  and then 20. what we could do is I’m  just going to say in here Val  result  equals two if I want to check whether  number one is greater than number two I  can say N1 greater  then number two  so I could just say N2 in here  and if I print the result so print and  then the result  if I run this  you should see that this gives us false  this is a Boolean value so this is false  so this expression right here returns  true or false  so this is greater if you want less so  let’s just basically take this and not  even store it within a variable and just  put it here so that you have access to  it so if you want to check whether it’s  greater or equal you just say greater or  equal  if you want to say whether it’s less so  so less you basically flip the value so  less and the way that I remember this is  basically this sign right here has a  shape of an l  so this is less and here this is less or  equal  and also if you want to check whether  the numbers are equal you could just say  n 1 and then double equals  so these are the comparison operators so  if we run this  you should see that we have a bunch of  booleans right so here so the first one  was what the first one was N1 greater  than N2 false greater or equal false is  a less yes true less or equal true  and then finally is it equal to N2 and  obviously there are two different  numbers hence you’ve got false and this  is pretty much how you compare numbers  using the comparison operators  this is the machete catch me on the next  one  okie dokie you’ve learned about logical  operators you’ve also learned about  comparison and booleans now where they  come into play is with if statements so  if statements allows us to execute a  piece of code based of a condition so  let’s say that we want to execute so we  want to say if so this is the if  statement so if  N1  is greater than N2 so if N1 is greater  than N2  in here  we want to have a smiley face so print  and then here Ln  and let’s just have a smiley face just  like that  so if N1 is greater than N2 we want to  print a smiley face if I run this  it doesn’t print anything why because N1  in here is 10 10 is less than 20. hence  this right here is false  but if also change N1 to 30 right  we could run this  and now we have a smiley face so N1 now  is bigger than N2  now here obviously we could say greater  or equal we can also have and so we  could say and as well right so as you’ve  seen before and this right here so the  entire expression evaluates always to  true or false  right so if I was to say that you know n  1 is greater than N2 but also n 1  is less or equal  to 30 for example  well this will be true right because N1  and here is greater or equal than N2 30  and 1 is less or equal than 30. so if I  run this  you can see that we have a smiley face  but if I change this to 31 in here  so 31  run this  this doesn’t print anything in here  right that’s because  N1 in here it’s not less or equal than  30 it’s actually 31.  so this code will evaluate if this  entire thing is true you could also say  or if you want right so or remember  so or and now basically  one of them has to be true so if I run  this this time it prints  because the first  expression is true  cool so this is the if statement  also what I want to show you is that if  we have so print Ln and then some code  this will always print regardless  because it’s not within the if statement  okay so if we basically here let’s just  say and  and I want you to see that this is false  but some code will always be executed  because it’s outside of the if statement  okay so any code that you put inside  will evaluate if this expression is true  next let’s talk about the else statement  all right so you saw if statement but  what about if you want some code to be  executed when this is false  right so basically the default when this  expression in here doesn’t evaluate to  what you are asking for well for that we  have the else statement so else in here  so this will be executed when this  expression  doesn’t evaluate to what you are looking  for so here we want this to be true in  here so if this is true this will be  executed  otherwise this will always be executed  so let’s just take this duplicate bring  it down  and let’s just add a sad face for  example  there we go cool so if I run this in  here  we should see that we have a sad face  why because N1 in here yes is bigger  than N2 but it’s not less than 30 right  so 31 so if we change this to or let’s  just change this to an or  there we go so this time this should  print and the else will not be printed  so let’s just run this  and you can see that we have the happy  face and some code so some code is  always printed  and this right here is our if statement  so if else if this is true  then execute this and then skip this  carry on if this is false  if this is false  skip this so the inside and execute this  piece of code and then carry on  and this my friends is if else statement  now one thing to bear in mind is that so  else it’s not needed right so if you  don’t need the else statement by all  means get rid of it right but if you  want something to be executed in the  event that the if expression is false  then add D else statement right so this  is valid as we’ve seen before  right this is all for now catch me in  the next one  all right so you know about the if you  know about the else what about if you  want to perform way more conditions  before you reach the else Branch so  maybe you want to say if this is true  then if you want to try something else  something else something else until you  reach this condition well we have the  else  so else and then if  just like that  and in here we add the curly brackets as  usual  and with this in here we can have an  expression so let’s say that we want to  say that n  is or N1 equals to 100 for example right  so we could do that  and then perform something else in here  right if we want to check another  condition you could keep on repeating  this so else if  maybe 200  so on and so forth and you can see that  the general idea is that you have one if  as many else IFS  and then an optional else  in here so if else if else if and then  else and this is pretty much the  construct for if statements  now there’s something that I want to  talk to you and that is that the if it’s  not really a statement but it’s more an  expression let me show you why  foreign  let me teach you something with kotlin  and that is that if in here so the if  statement is not a statement but it’s an  expression so even though that I said if  statement is so that you can resemble  with other languages because Java they  call it as if statement JavaScript  golang you call him as if statements  right now the reason why they are called  Expressions is because they can return a  value so in here let’s say that instead  of me printing the value like so so  print Ln the happy face or else the sad  face what I can do is I can just remove  so actually let me just comment this for  now here so I want you to see something  so if I comment this in here so this  line  and here I can just basically add the  happy face and also here I can add the  sad face  so what is this doing well this is  basically returning so return the value  of  this string right here right so the  string or number or anything else so  which means that this actually is not  needed and by default now  this whole expression so this if  expression Returns the value so here I  can say Val  and then I can say value equals to  so you can see that now I’m storing the  value  happy face or sad face and I can print  it right here so I can just say print  Ln and then the value  right so hopefully now you can see how  this is an expression so if I run this  main method in here so right click and  then run  and you should see that now we have the  happy face hooray which is the same  thing that you saw in here right so here  uh the beauty of this actually is that  you can basically have any logic so any  code in here and whatever comes last is  what you return from this expression  okay so here even though  so let’s just remove that and say  true in here  and let’s just change this to false  right so even though if I run this we  should see true  right so true and then the happy face  you know here I’m printing  here I’m printing but then whatever  comes last is the return value of this  expression so this is pretty much why if  is an expression instead of a statement  because of this return value  awesome so hopefully you know exactly  what are the differences and let me show  you the else Branch here so if we change  this to an and in here  so basically is greater than N2 and also  less or equal to 30 now this should be  false and we get false and the sad face  and also this code which is outside of  the if expression always evaluates  basically this one in here  awesome this is pretty much it catch me  on the next one  in Java we have something called the  tenery operator which basically goes  like this  so if we want to assign a variable to  base of the condition where we have only  an if and then an else statement we  could say VAR and then result equals to  and then the condition so let’s just say  that this let’s just keep it  nice and simple so we say well if N1 is  greater or equal to N2 then we have a  value so in our case let’s just say the  happy face  otherwise so otherwise we have the sad  face just like this  so this is when you have scenarios with  one if and else and that’s it usually if  you can have more so if else if  but I don’t recommend it to do that so  in cases where your if  and else in here they are so simple and  you’re just returning one value there’s  not much logic inside literally it’s  just a value that you’re returning so  what you can do instead is  so obviously in kotlin these are  expressions right so the if is an  expression so what you do is let me just  comment this out and duplicate  and comment so in kotlin is like this  you say if  and then your expression  just like that  and then you remove the question mark  right here  and instead of the colon you just say  else  so this is the exact same thing as the  tenery in Java  right so let me just say R for result so  everything is smaller in here  oops are in here  so you can see how different this is  right so you just basically use the if  as you’ve seen before  if this is the case this is true this is  the value otherwise  sad face right so if also print in here  let me just say V for Value instead  actually  so if I was to print so print  Ln and then V  so let’s just run this  there we go we’ve got happy face if and  one in here so N1  is less  than N2 let’s just say 19.  and then run  we get the sad face  now obviously we can expand this right  here so maybe you want to have another  else if in here so what do you do well  we can let me just put this on a new  line but basically  we could so I’m going to duplicate this  and this could be Val actually vowel  and in Java there’s no Valdez VAR so if  you want to have else if inside of this  expression  what you do is the following so let me  just duplicate this and this I’m going  to call it V  and then one and let’s just put this on  a new line  and then here on a new line and what we  could do is we could say in between if  and then else in here the expression so  let’s just say that n is equal to 100  for example  N1  then we give the value so the value  would be  hooray and this should be else if I bet  so in here else if  and there we go so you can see how this  is clean and beautiful code  right and you can combine multiple of  these right here but I’ll just leave it  with one so if you want a second one for  example just duplicate this line and you  could add 200 for example array and then  200. something like that right and now  you can see that if you just returning  one value you could just use light  detonatory operator in Java but way more  improved  catch me on the next one  foreign  let’s look into the when expression with  kotlin so in Java we have the switch so  switch statement where we can switch  upon a value so we say value in here  and within that switch statement we have  cases and within cases then we have to  say break and within that then we can  basically return a value or we could  just perform something right now in  kotlin what we have is the when  statement so it can be used as a  statement or as an expression so in here  let’s say that we have Val and let’s  just represent gender in here and let’s  just say f for female okay now we can  say when  and the value so we can say gender in  here  so when and then here we can Define the  possible outcomes so here say F so  basically if the value is f  then have this Arrow so minus sign and  then the greater sign and then here we  can print  so let’s just print  female  if it’s a male so if it’s m in here  we can basically say print  and then mail  and we also have the default so we can  say else in here and within the else  statement we also need the error and  inside we can say print  and then unknown  known  and then gender for example just like  this  now if we run this  what we should get  is female right so because if this is  equal to f  with print line female  if M we print mail otherwise unknown so  if I was to say for example this  this is not a valid gender so it says  unknown gender  cool so this right here is the when  statement  this can also be used as an expression  meaning that we can return a value from  it so if you want to store the actual so  here let me just say Val  and then I’m going to give it a name so  G let’s just say G for gender and then I  can say equals to  and from now on this now is an  expression because this now returns so  it returns a value so we can get rid of  the print lines all together  just like that so in here we get rid of  all of that  and now the value of G  contains  female male or gender in here so if I  print so let’s just print  line and then G in here  and let’s just change this to input  and then I’m going to say input  and this will be gender  so much better  and now if we print gender  so you can see that we store that inside  of a variable  unknown gender if we pass in here  M and in here you can see that we have  male  and this is pretty much the gist of the  when statement so use when when you have  multiple branches and it’s very similar  to the switch statement in Java  cool and always remember so if it  returns a value it’s an expression  otherwise if you want to return a value  from it it’s just a statement  this is pretty much hit catch me on the  next one  cool let me go over a few other things  that you can do with the one expression  you saw that in here we have the if else  if else if and then else statement so  what you can do is you can actually  replace all of this with the when  expression so if you want to replace  this with one expression you do  basically when  and basically you add nothing inside  in here and you basically have your  conditions right so here this we take  this  and then come back paste it here  and we need parenthesis  and add the error and then you can  perform whatever you want so in our case  what did you do yeah so we just printed  this right here like so and you can see  that now we’ve switched that from  and if else to the when so similarly if  you want to have a second case you can  have the second case like so  so here it doesn’t really matter so n  and then equals to 200 or 100 for  example then you can do whatever so  print  and then Foo right and then at the end  we also have the else  and basically oops not like that but I  have the error and basically here we can  print  and then the sand face  so obviously you can repeat this as many  times you want and you can see that you  go from  if to when which is kinda neat right  then the other thing that we can also do  so let me just add a comment here  so if  else  to when expression  so the other thing is so we’ll talk  about ranges later but if we have for  example a value and let’s just say  h  and let’s say that H in here equals to  18. so what we can do is we can say  right so when  and then here this time we’re going to  pass h  and we’re going to say right so if age  is in and then some range right so if H  is in between let’s say  13 and then dot dot and 19 for example  in here if this is the case right we can  execute something so we can say print Ln  and then teenager  teenager right and we can keep on  combining these right so it will check  the first one and then if you want you  can negate this you can say not in  and then you can provide a range right  so zero for example to  all the way to 12 for example then you  can do something right so print line for  example  or if you want you can also have the  else  in here  and you can perform something within the  color brackets or if you just want to  print for example you could just do it  but you see that this when statement is  actually way more powerful than we  thought  cool so I’m going to leave it here for  the when statement and as we go through  our joining template of learning kotlin  you will see the when expression in  action but just remember that it’s very  powerful  this is pretty much it catch me on the  next one  cool early on you’ve learned about the  Boolean and what it is and you saw that  also that with booleans you have three  possible values so you can have a  Boolean so here if I say Val and then  let’s just say is and then adult  and then here I can say equals to false  true  or if I want this to be nullable in here  this is possible right now obviously  when it comes to performing these checks  whether the Boolean is equal to true or  not so more often so what you do is  you’d say if and then is adult in here  and you can see that this right here it  says that oops sorry so basically we  need to actually add equals equals to  true but I’ll explain this in a second  so if this is false in here this works  if this is true this also works but when  you have your Boolean as null in here it  is necessary for us to perform so if  your Boolean can be no in here so if we  say right so the data type of this is  and then Boolean right so if we say that  this in here can be no so what we need  to do is we need to perform the actual  checks so if it’s equals to true in here  and you know in Java for example this is  redundant where you just have if and  then your actual Boolean but in kotlin  if it’s now this is the way to do it if  equals equals true that means that the  value itself is not null and is true  otherwise so otherwise in here so if I  say else  so in here I can say print line and I  can say false or no so here let me just  say print line  and I’m going to say not null and true  so if I run this in here so let’s just  run this  you should see that we have in a second  force or no there we go false or no if I  change this to let’s say true  so this now is irrelevant so let’s just  run this  you should see that not null and true  right which is true  and as I said if you know that for a  fact that your Boolean will never be no  no need for that and also this check  here is redundant if adult equals equals  to true this can just be that okay but  really what I wanted to teach you is the  fact that when booleans are no  you have to perform this check  in this session let’s learn about arrays  and lists so far you’ve seen that if you  want to store a value for example a  string you can basically say VAR name or  vowel in here and basically you can  Define the type or not so this is the  string and this allows us to store a  sequence of characters  but the issue is that this variable  right here called name can only store  the evalued Jamila what about if you  want to store for example Jamila you  want to store for example jams  or maybe some other value  now obviously we can do this under a  string but this is not the right  approach  so in this section let’s learn how to  properly work with arrays and lists and  also later we’ll learn the constructs  that allows us to iterate through arrays  and lists  awesome catch me on the next one  foreign  let us first understand a race and then  we’ll look into lists which is a better  implementation of arrays so  if I want to store the value Jamila and  Jamis under a data type that allows us  to store multiple values we’re going to  use arrays so here what I’m going to do  is let me just comment this for now so  I’m going to comment this like so  and what we’re going to do is I’m going  to say Val  in here and then instead of name I’m  going to say names equals to  and at this point we can use the array  of so this right here allows us to  create an array  and when we say array of here we can  pass the values  so here I’m going to say Jamila  and let’s also say jammies for example  now this here is slightly different than  this string  because here we have the first value  and then here we have the second value  whereas here we only have one value  which is separated by a comma  and this is the construct of creating an  array  now we can also Define the type for our  array in here and this has to do with  generics which will kind of cover later  but for now let’s just keep things as is  but essentially in a nutshell if for  example so here let me just put this on  a new line just like that  so if I was to have for example here  number two  you can see that my array accepts number  two  now if I want to prevent this from  happening I can basically say array of  and then here I can Define the type so  have the less sign and greater sign and  then here we can specify the data type  that we only want this array to store so  in our case strings if you want integers  you can say int if you want doubles you  can say  double right so here if we say string  number two in here you can see that we  have a compilation error so we cannot  store number two in here  and if we only have strings you can see  that this becomes redundant and we can  get rid of it just like that  cool now if I was to print so let’s just  print  names  in here  right click run  you can see that we have this random  string in here and by default if you  want to print an array you have to do a  little bit more work  and then right after names you can say  dot  content to and then string  so this is a method function and we’ll  cover these later  but if I run this  you should see that now we have Jamila  and then Jammers in here  now one thing to bear in mind with  arrays is that  these are stored in what’s called  indexes  so this is the first index  so this right here is the zero index  one index and so on and so forth  so if I for example wanted to get hold  of the first index  I would say in here print  and then names  and then square brackets  zero so the zero index corresponds to  Jamila in here  then the one index in here  so one index corresponds to jammers  if I was to have for example  Sami  in here so this would be index number  two  so if I basically duplicate that  index number two  run  and you can see that we have Jamila  Jammers and then Sami what about if I  try to access so if I try to access  index number and then three for example  so index number three  so here if I run this  we should get an error and the error is  in here  array  out of bound so array index out of bound  exception  because basically there’s nothing at  index 3 which is empty  cool so let me just basically comment  this out  in here and these are arrays in a  nutshell  now obviously if you want to change for  example Jamila in here so if you want to  change the name of Jamila right so here  you can say names and then the index  which is zero and here I can say equals  2 and then a different name so let’s say  Samira for example so this now  will print Jamila and the names  zero Samira will replace the content of  the first index in here  and if I run this you can see that  initially it was Jamila but then we  change it to Samira cool also in here if  I hide this if you wanted to know for  example the size of how many elements  you have you can basically in here it’s  just right at the end we can say print  Ln and then names  Dot and then size  so this will give us the number of size  if I print we should have three inside  have a look three elements inside  and if I was to  remove  and if I was to add one more we would  get four so on and so forth  also one other thing that I want to show  you is if I want to check whether so  let’s just say whether a particular name  is within the array so we could for  example use if in here  and then we could have the string that  we’re looking for so here let’s just say  hello  and then we can say in names  so if this is the case I’m going to say  print  and then found for example  otherwise  not found  there we go not and then found  so if I was to run this  we should get not found have a look not  found but if I for example try and  search for jammers  in here so Jammers is Jammers in names  you can see that it’s found so this is  one way that we can use the in keyword  to check whether something exists within  the array and one last thing that I want  to emphasize with arrays is that once  you define  the array itself you are no longer able  to resize so the size of this array  right here it’s three and cannot be  expanded nor shrinked cool next let’s  continue to explore arrays  cool so you’ve seen how to work with the  Rays you’ve seen indexes you’ve seen  size also you’ve seen the in keyword or  the in operator  and also how to print the array to  string because otherwise you get a funny  character  also what I want to quickly show you is  that this is one of many ways of  creating a race  so if I scroll down in here and let’s  basically add  a comment like that so in here if I say  array  so you can see that we have a bunch of  constructs for array of so you’ve seen  this array of nulls so this will  initialize an array  of so let’s say that we want to store  strings for example and then we also  have to pass the size how many elements  that we want to store in here and this  right here so array of nulls so let me  just put this on a new line like that  so the array now in here  contains five spaces and all of them are  no so if I show you so if I say print Ln  array of nulls Dot and remember the  method content to string  so if we just run this  you can see that we have no no no no no  no no  now obviously you’ve seen that if I want  to change so array of nulls  and then here let’s just change the  position three for example or four  two and then hello for example and let’s  just bring this  right at the end  run it  you can see that we have hello right at  the end okay so basically this is how  you create an array full of nulls and  then basically act upon it  also if you want to create a raise so I  can say array and have a look you can  have Boolean arrays byte arrays you can  have double  so empty array so basically  I think it’s the same thing so empty  oops or actually array  in here and I think maybe this is the  same as the array of notes I think  that’s the same thing  and double array float int and I will  look so you’ve got all of these  constructs that allows you to create a  race now obviously also if you want to  create an array of numbers for example  you can say int  and then have a look array of  so this will basically be an array of  integers instead of string  and I think you have also double array  of  and all the data types but basically  this is pretty much what I wanted to  cover about arrays and you can do a lot  more things in terms of initializing the  array so for example if I wanted to so  this array of nulls if I want to fill  the array in here right so let’s say  that I want to fill the array with stars  for example you can say array of nulls  Dot and then you can use these methods  so have a look so you can fill you can  say plus element required nulls you can  reverse you can Shuffle you can slice  for reach which you’ll learn later  and then any as well so there’s a bunch  of these methods that we can use  including count as well  filtering flat map so this is more  advanced is empty  so in here you can basically say fill in  here and we want to fill the array with  stars for example  so if I run this  instead of nulls  it will have stars all the way and then  hello because  of this line in here  cool so we’ll leave here for a race and  the erase is a very important data  structure that you should know how to  use  and what we need to cover later is how  to iterate through arrays but this is  pretty much it next let’s cover lists  cool you’ve seen a race now let’s work  with lists  so the difference between arrays and  lists is that lists are expandable  whereas with arrays wants you define the  size you are no longer able to expand  its size  so if you want to create a list it goes  like this you can say Val and then I’m  going to say list in here  and then colon and we can Define the  type if we want so here we can say  string so this is a list of string  and here we can say list  equals to and then list  and it basically works the same as  arrays so you saw in here it was array  of so in here and here we have list and  then off  so you can say list of and then have the  size if you want to have an empty list  you could say empty list in here and  we’ll cover mutable list in a second so  if I say list of in here and I’m going  to add a new line and now I can add my  items so here let’s just say again  Jamila  and comma we can say Jammers for example  comma we can say Saleh  and in here let’s say Jammers and in  here let’s also say Peter for example  cool and let me uppercase Jamila in here  so now  let me actually also change this to  names so we have names that contains in  here four items now if I was to let’s  just again say  print line  and names  so right click run  and this time we actually get the list  printed instead of the funny sequence of  characters so you can see that we have  Jamila Jama Saleh and Peter  so if you want to in here access for  example the first element you can  basically say print  line and then names Dot and here we can  basically grab the index so I can say  like this so the same way that we’ve  done with the race we can do it so 0 is  the zero index we have Jamira  if you want to use the dot so there’s a  DOT get in here we can also use it but  you can see the IntelliJ says that we  should use the index operator  if you want to know the size in here  let’s just duplicate this you can say  names Dot and then size  so if I run this  you can see that we have four elements  so one two three and then four and from  this point onwards it kind of works the  exact same thing as a raise because  lists are built on top of arrays and if  I want to know whether Peter is there so  I can say print line  in here in the names dot contains  and here I can say Peter so if I run  this you can see that this will give  this true and if I basically say it  contains full in here run it we should  get false and you’ve probably seen that  contains has contains all as well so  basically you can pass a list of  elements and check whether they all  exist in the list  and um this is it now what about if you  want to add a new element so this is a  difference between a list and arrays so  lists are expandable whereas arrays are  not and also if you want to Omit this  all together you can right so you could  just do this so I’m just showing the  types actually  cool so to add a new element  so in here we need to say names Dot and  here we have an add method and basically  this will not work and that’s because  this list right here  so this list is a read only list of  given elements so if you want to add a  new item you need to use a different  construct and that’s what we’re going to  learn next but in the chill so obviously  there’s more methods that you can  explore in here so I can say print line  and then names Dot and if you want just  go through all of these methods and  basically just read the documentation on  what they do but for example we have  first if I want to find out the first  element run this  and we get Jamila she’s the first also  there’s a lust  so here  you can see that we get Peter  if you want the index so in here names  Dot  index of let’s just say Sally for  example  run it so Sally is 0 1 and then two so  we should get two  there we go  and this is pretty much how you work  with lists now bear in mind that this is  a read-only list which means that you  cannot add new elements let me actually  show you what you have to do next  cool so if you want to be able to add  new elements to lists you need to use in  here mutable list of  so this in here gives us the flexibility  so here we can say string and obviously  this is optional so let me just remove  it so in here now when we change this  from list of to mutable list  he just says by the name mutate so it  means that we can now add in here and  remove as well so very important add and  remove  so in here let’s just leave all these  operations as they were and so let’s  have a look in here so if I wanted to  for example remove Sally for example so  you can say in here  names Dot and then remove  and then in here we can remove first we  can remove last we can remove if as well  with a filter but let’s just say remove  and then Sally  now if I print lists so let me just  duplicate this and then print it right  here at the end  run it  and now you can see that we have Jamila  James Saleh and Peter then in here I  said names that remove Saleh and now  Saleh is gone  cool so what about if I want to add a  new element so I can just say names Dot  and then add so you can see that this  time  the add method works because this is a  mutable list so we have a couple of  options we can add so in here you can  see that we can pass the index or we  could just add it right at the end and  you can see we can also add a collection  so let’s just say add in here and let’s  just add for example Alex  now if I print names so let’s just  duplicate change the Line run this  and you can see that now we have Alex  and this is the beauty of mutable lists  right so basically it’s also built on  top of arrays but the beauty is that you  can add and delete elements from the  array now you might be asking right so  when should I use mutable list versus a  list or array so most of the times  you’re going to be using mutable lists  or read only lists and then the decision  that you have to make is right so do I  need to add and remove elements if so  just use mutable lists otherwise just  use read-only lists and from this point  onwards all the methods are the same so  here let me just say print Ln and I can  say names Dot and have a look  last first we can replace as well we can  remove we can set we can get by an index  remove if so this is to do with streams  and you can check whether it’s empty  right so is names for example is empty  right the array itself so if I run this  this will give this false so it’s not  empty because it has four elements  inside okay so if I was to create so in  here let’s just say print line  and then mutable  and then list of and then let me just  take this and put it into a variable in  here  empty and then list  and here we can just say any so remember  the any type which means that you can  store any data type you want so let’s  just run this  and you can see that this is empty and  if I invoke  Dot and then  empty in here or is empty  run it you can see that this time it’s  true whereas before  in here so this names right here is now  empty because it contains elements  and to be honest this is pretty much it  next let me talk to you about data types  and that is very important when it comes  to arrays and lists  catch me on the next one  cool let me teach you about the  destructuring Declaration when working  with lists arrays and pretty much any  collection so if we want to grab Jamila  Jamis and Sally for example under one  variable we would typically do this in  many languages so here we can say Val  and then I can say J for Jamila for  example equals 2 and the names  and you’ve seen the index right now  let’s basically get dramas so I’m going  to say ja and here dramas is at position  one and then Sally I’m gonna say it’s  just s and Sally’s at position two right  now this here is cumbersome and never do  this in languages such as kotlin and  JavaScript so what we can do is we can  turn the following so we can turn this  into this  so here let me just put a comment so  what we can do is we can say Val  and then open parenthesis  and I’m going to add a comment here  basically comment all of this so you  have reference in here just like that  and now I can grab the values in order  so here I can basically assign this to a  variable so I’m going to say J for  example or let’s just say one  to  two and then three  equals two and then names  so what this is doing is basically  destructuring the values for us  so have a look so one now will be Jamila  two will be Jammers three will be sari  cool so if I was to print for example  let’s just say print Ln and then one  and let’s actually print everything in  there under one line so here dollar sign  and then one dollar sign  and then two  and finally dollar sign and three  all right so if we run this  have a look Jamila Jamis and Sally so if  you omit in here so I think we can even  omit  so basically if you don’t want to pull  for example Sally but you want to pull  Peter for example right which is the  fourth  so I’m going to say four for example so  four  so now we are skipping right so we skip  Sally  and if I run this  you can see that we have Peter now let’s  actually check whether this is actually  variable so if I was to say underscore  yes so basically underscore we can’t  really reference to underscore because  this is how we skip a value so this is  kind of nice and the one that you should  really be aware so here this works with  lists of  array of so any collection really cool  this is pretty much it catch me on the  next one  whether you are working with mutable  lists arrays or regular lists never mix  types  so here never for example have a list  that contains strings and then numbers  and then doubles in here so basically  never do this okay  because if you start doing this it’s  going to be very difficult for you to  build applications and you’re going to  have to write a logical to accommodate  different data types so usually this is  a big no no  and hence sometimes you’ve seen in here  so if I say only strings so basically  when you say in here less sign and then  add the data type inside that you want  and then close so this has to do with  generics you can see that I’m no longer  able to add numbers in here right so  obviously if I remove this then the IDE  knows that I’m only working with strings  then it tells me right so this is not  needed and I’m safe  so if you do this you will encounter  lots of issues potential bugs and you  don’t want to mix data types when  working with lists and I can guarantee  you that you will never mix even if you  feel like oh but I I think I might have  to mix data types trust me you will not  mix so it’s the same thing with for  example a pencil case right so pencil  case you can only store pencils for  example now obviously you could have  different types of pencils but they are  pencils so you would install for example  a spoon within your pencil case or your  socks or literally any other data type  so socks spoon these are data types  within the pencil case so with  programming is the same thing and as you  write larger applications you’ll see  that what I’m saying does make sense  cool so I feel like I had to put  emphasis on this point so never mix data  types when working with any data  structure  all right this is my machete catch me on  the next one  foreign  session you saw how to work with arrays  and lists now let’s learn how to use  Loops effectively to Loop through them  and also show you some of the cool  things that we can do with loops so in  here I do have two  collections so the first one is a list  names and the other one is an array  of integers so here let’s say that we  want to Loop through them right so you  saw that if you want to access for  example so if I say print a Ln if I want  to get for example Mary in here  I could just say names and then the  index if I want to get Ali names and  then one if I want to get Alex names and  then two so this is the first index  which is this one  so basically this is the zero index one  index and then two index and if I say  three we get a raise out of bounds here  because there’s no  element at index three in here and you  can even see that  um basically IntelliJ is telling me  index is always out of bounds because it  knows cool but this is not a good  approach when we want to basically get  all the values so for that we can use  the loops so if we want to Loop through  the list of names we can use the for  construct so four goes like this you say  four  and then you have a temporary variable  so here I can just call it name  in and then the collection so in our  case names  so what this will do now so here we can  also add curly brackets and basically  now we are within this block where we  can do pretty much anything that we want  with each element so the first iteration  name will be Mary second iteration name  will be Ali third iteration name will be  Alex so if I was to say print Ln and  then name in here  let’s run this  there we go so if I put this bigger you  can see that we have Mary Ali and Alex  cool so this is the four construct so  let’s also print numbers so let me just  add a new line so let’s just say four  and then number  and then in here we can say in and then  numbers  so also if you are just executing one  line of code in here there’s no need  four curly brackets you could just say  print Ln and then number  now one thing that I want to tell you is  that so here so this is a variable name  so this could be n for example right so  this I usually basically just say if I  have something called numbers or  transactions I always have the singular  so transaction number name instead of  names right so I always have this  singular of the variable name  cool so if I run this  we should see that we have one two three  four five  basically five numbers that we have in  here right and the loop basically knows  when to start and when to stop and this  is pretty much the way that you loop  with kotlin now in here let’s say that  within this for Loop  we have each name in here so each name  Mary Ali and Alex and we want to  capitalize the first letter of each name  so M should be capital m capital A and  capital A for Alex so what we can do is  in here so let’s just basically say name  Dot and then we can say replace first  and then character  in here and basically this takes a  character so it’s a Lambda expression  and we’ll learn more about Lambda  Expressions later but here I can say I T  Dot and then uppercase  so basically  this right here will replace the first  character to uppercase and the result in  here I’m just going to call it n for now  I’m going to say Val and equals to and  then basically this now will be the  replaced so we’ll be marry  with capital like that so in here I can  now say print  and then n instead of the original name  that we get  cool so let’s just run this  and if I scroll up have a look marry Ali  and Alex  each of the first letters or the First  characters are now in uppercase  cool so this is a quick introduction on  how to use loops  let me teach you about indices because  sometimes what you want really is to get  the index instead of the actual value  and then you can perform other  operations so in here let’s change this  from an array of instead of numbers and  let’s just for example have characters  so we could actually I think we have car  array of so here let’s just change this  from a  B  and then C and let me just put this on a  new line like that  D and then e  cool so now this is C I’m going to  rename this so C in and then let’s just  say  letters in here and I’m going to say l  or you can say letter in and then  letters and then print the letter itself  right so if I run this in here  you can see that we have a b c d e now  if I want to grab the index instead what  we can do is let’s just put this inside  of curly brackets so that we have more  space so here I can say dot and then  indices  so what this will give me now so I’m  going to say index instead of letter  and if I print the index now if I run  this  have a look we get the index 0 1 2 3 and  4. so the index is basically the  position so zero index one index two  three and then four now if you want to  grab the values themselves so let’s just  do this let’s just print the index so  we’re going to add this within double  quotes and then here  I’m going to have the index so here  index  followed by  so in here I’m going to say comma  and I want to grab so  I’m going to have dollar sign and then  curly brackets and here I’m going to say  letters  followed by the index  so hopefully now you see how this is  playing together so as I’m looping I’m  going to go to letters this is my  character array  and the index will be passed inside here  so the first time is going to be 0 1 2 3  4. so if I run it  there we go you see that we have 0 a 1 B  and then two c three D four e cool so  the cool thing with this also is if you  want to print in Reverse right so if you  want to print in Reverse what we can do  is the  following so we can take this and let me  just say print Ln and then reverse  and I’m going to paste the exact same  code and now what I can do is I can say  dot so right after I say letters dot  indices Dot and then reversed  so this will now reverse and the index  will start from the end instead of the  start so here if I run this  so have a look so when we reverse now it  starts from 4 3 2 1 and then zero  awesome so this is pretty much how you  use the indices when it comes to arrays  now obviously this could be a list as  well so here if I say list of right so  list of you can see that this will still  work so it doesn’t really matter whether  it’s a character array or any other data  type or basically whether it’s a mutable  list it will just work so that’s the  beauty of kotlin cool so let me just  stick with character array in here and  this is pretty much it for this video  catch me on the next one  with loops we have a concept of range  and it goes like this so if I have a for  Loop and I want to print a particular  range meaning that let’s say from  numbers 1 to 10 for example we can say 4  I  in  and usually you see that here this is  where we pass the list or the array  but with ranges we can say right so we  can give it a range so this is an  integer so one between so dot dot  and then the end so here let’s just say  one to five  and this is IntelliJ telling us the  actual range now if I print this let me  just print it like this so print and  then I in here  so if I run this  you can see that we have numbers one all  the way to five so if you want maybe  um 10 to 100 for example you can do it  so if I just run it we should see all  the numbers from  10 all the way to 100. cool so let me  just  stick with small numbers and this is  nice what we can also do is we can print  in Reverse so if I say four  and then I in and in here five  and then dot dot and then one in here so  what I want to do first is I want to  show you that if I try to do this  and run this  and here we get nothing  and that’s because if you want to print  in reverse all we do is you don’t say  dot dot but instead you say down and  then two  so five down two and then whatever the  stop number that you want so if I run  this time  you can see that we do get five four  three two and then one  awesome so this is pretty much ranges  and one other cool thing that we can do  is so let me just duplicate this right  here so if you want to step so we can  actually use the step  and basically we can step right so the  default is one but we can step in twos  so one to five but we’re going to step  every second element so if I just so in  here let’s just have print line  and then I’m going to say  steps  so that we basically have a nice output  so also  print line and then here down  two  and here I’m just going to say print  line  and then range  all right cool so let me just comment  this one for now so we can have a better  output so if I run this  have a look so we have steps  but have a look we have one and then we  step from one to three so this is we’re  stepping in twos  so we have one and then we’re stepping  in twos one  three and then five so you can see this  is beautiful  also if you want to you can basically  step in here so step  and then  we can say step and then two as well and  coming down right so five  three and then one so if I run this  you can see five three and then one  and this is pretty much it  so let me just say steps  or step  and then down in here  cool so this is how you use for loops  and range as well as the down to as well  as step  two have control on how you iterate  if you have any questions drop me a  message otherwise catch me on the next  one  you saw that you can loop with lists and  arrays but also if you have a string in  here you can Loop through a string as  well so it’s the exact same thing and  let me show you something you can say  brand Dot  and then if you say f o  or  and then if you say four  IntelliJ Auto completes this 4S right so  here I’m going to say the letter in  brand and I’m going to print  l  and then obviously you could perform  anything you want right so you can have  if statements and whatnot but if I right  click and then run  you can see that we have Amigos code in  here and then here as well obviously if  you want to use the indices so dot and  then indices Dot and then reverse  basically you could use everything that  we’ve learned so far but I just thought  that I’ll let you know that you can also  Loop through a sequence of characters  foreign  another way that we can Loop is using  the for each construct and this is found  in many languages including Java  JavaScript and kotlin also has it of  course so if you want to Loop you’ve  seen that you can say four or you can  use IntelliJ order completion so names  and then four  here and then we get the construct and  then we can print right so here let’s  just say print line and then name okay  but we can also use the for each which  is a better syntax in my opinion and the  one that you will use quite a lot and if  you don’t need access to the indices  then this is a good approach so you  basically say so let me just put it  right above you say names dot so your  collection and then for each  so for each takes a consumer in here and  then inside we can add curly brackets  and here we have access to it so it is a  placeholder that corresponds to the  value for each element so now I can just  say print Ln and then I T  just like that so you can see that also  IntelliJ says that move Lambda argument  out of parenthesis and we don’t actually  need you know parentheses so we could  just do it like this which is much  better  have a look so names for each and then  curly brackets and then you basically  say print Ln  and obviously if you want to have multi  lines  just like that and you can see IntelliJ  is actually even telling you the data  time for it so this is quite cool  so this is the exact same thing as this  in here  so let me just comment this out so it  doesn’t execute  let’s right click run  and have a look we have Jamila Jama  Sally Peter so literally just use any  array and just use the for reach and you  can see that it’s quite neat  cool  there’s more to it when it comes to  functional programming which will cover  later but for now this is pretty much  everything about the for reach construct  cool another construct that allows us to  Loop is the wall and do while Loops so  let me show you and even then Java then  this is pretty much the exact same thing  and let’s say that we have a variable in  here I’m going to call it I and we’re  going to oh actually let’s just call it  number equals to 1 in here and let’s say  that we want to Loop through  while the number is less or equal than 5  and then print and also increment the  number so the way we do it is using the  while construct and here this is an  expression right so I’m going to say  while number  is less or equal to 5 in here so if this  is the case  right I want to print the number so I’m  going to say print line and then I’m  going to say number  and then here dollar sign and then the  number itself  and obviously here I need to increment  number so I’m going to say plus plus and  then number  cool so if I run this right click run  you can see that we have number one two  three four five so basically this is  from one to five but if you want the  reverse right so from five so five all  the way down to zero you say five  and then basically you reverse so while  number is greater  or equal to zero  and here we’re going to decrement so  minus minus and the number  run this and you can see that we have  five four three two one and then zero  cool so this is the while loop and  obviously if you have a given array you  can actually Loop through the array as  well so if we have a real list basically  so if we have for example in here let’s  just have a string let’s just have a  string so here I’m going to say brand  and then equals to and then Amigos code  so this will be Val and here  I’m going to have the index so VAR and  then index equals to  and then brand  Dot and then length minus one right  because the length is one two three four  five six seven eight nine ten right but  the index is from zero right so zero to  nine so this is me looping from top to  bottom right if you want the reverse you  just say zero so Zero from here and then  moving forward but I’ll show you in a  second so here if I now say that the  index so the index  so while index is greater or equal to  zero in here I’m going to say index  minus minus so decrement the index and  then here I’m going to print  brand  and then index so brand and then index  and  index can type today  and obviously I need to have curly  brackets  because it’s not just a variable but  it’s a more complex operation in here  right cool so if I run this  you can see that  basically  e d o basically Amigos code in Reverse  if you want from beginning to end you  just change this from this to zero and  let’s actually take this instead and  just reverse so just like that and then  obviously we have to change the sign so  this is now less or equal and then here  we increment the index plus plus and  then index run this  and now oops it shouldn’t be number but  it should be  um let’s just print like that right so  it should be that so if I just run it  there we go so you can see Amigos code  so this is pretty much how you use the  while loop and obviously I said this  could also be for example so we can  change this from  array  of  and here I’m just going to say uh  and then m  I so basically Amigos but I’m going to  leave it here and here instead of length  this is size  and in here so this is  brand instead  and we can remove the necessary string  template  and if I bring this we should have  Amy should be Amigos code but you get  the gist cool so this is how you use the  while loop and over C remember the  difference between vowel and VAR so the  reason why I’m saying VAR in here is  because here I’m reassigning the value  of index so if I say Val in here this  will not work  cool this is pretty much it catch me on  the next one  okey dokey you’ve seen the while loop  which basically works like this and here  if I say for example true  so this is the Boolean expression that  it takes right so while this is true if  I print in here  so if I say hello  so this will actually print forever okay  let me just try so you can see  so you can see that this is actually  printing forever and let me kill the  process by pressing the stop button and  there we go so  basically if I change this to false in  here and run this  this will never print nothing  now if you want  something to be done at least once  and then continued operation while your  condition is true what you can do is you  can say do  and then wow so now all of this goes so  the body in here has to go  and it goes like this right so do  something so here we want to print while  a condition so in our case before you  saw that while false and then inside of  the body nothing gets printed but now if  I run this so I’ve just indented things  so do this first while false so no  matter what this will always be printed  or executed at least once so if I run  this  you can see that we have hello once and  if I change this to true in here this  will run forever  so let me just stop this and you can see  that it’s running  and basically if you want to perform  something at least once and then have  your condition then use the while loop  cool this is image it catch me on the  next one  together  cool now let’s learn about the break and  continue keywords so let’s say that we  want to skip the number two while  looping so here let’s just say four and  then n in nums  and let’s say while we are looping if  the number equals to 2 we want to  continue I.E we want to skip so here  let’s just say print  and we’re going to print n for now and  if I run this you should see that we  have one two three four five nice and  easy so if I want to skip number two and  not print it I can say if  and n equals to  then inside here I can say continue  now what this does it basically if this  condition is meant continue was simply  skip whatever comes so whatever line  comes after line six and then go back to  the next iteration  so if I run this we should skip number  two so you can see that one and we went  to three instead  let’s say that you want to change this  so that if the number is even you want  to  skip it so you can say if n and then  modulus and then two  equals equals zero  so if there’s no remainder then we know  that the number is even so if we run it  you can see that now we are only  printing odd numbers so one three five  so if you want the reverse you could  just say not equal  and then run it  and you can see that now we are only  printing even numbers  so this is the continue keyword  now let’s say that so in here we also  have in you know six  seven eight and even more numbers and as  soon as the number so as soon as the  number is bigger than 5 we want to come  out of the loop all together so for that  we can use the break keyword so here  let’s just say if  and then n  greater than five we want to break  so break and obviously we can basically  have curly brackets in here and then  just say break or you could just leave  it like this  so it really depends on the formatting  style that you and your team decide  so now if the number is greater than six  we don’t even want to go back to the  next iteration but we want to  come out of this Loop all together in  here right so let me just say print and  then  some  other code  so if I run this  you can see that we only print two  four and then six and we continue to  some other code now obviously here I  said  six and I meant five my bad so this is  five not six but five so we should print  even numbers up to five right so two and  four and then as soon as the number is  bigger than five we skip  all of this by coming out of the loop so  let’s just run it once more  and there we go  and this is pretty much how you use the  break and continue now one thing to bear  in mind is so I want to quickly show you  that if I have  in here so nems dot forage  and if you think that you can use  the break and continue inside you are  wrong so if I say I T So if I T equals  to 2 for example  in here I want to continue so if I say  continue  so this is not possible have a look so  break and continue are only allowed  inside a loop so this is also Loop but  it is slightly different and we’ll  explore the functional programming style  later but also if I want to break out of  this Loop in here  gonna break out of this Loop I’m not  allowed and it’s the same thing with  Java  cool so let me just say  this is not allowed  and then we have a multi-line common in  here so  just like that  and then end it right here  okie dokie this is pretty much  everything for break and continue  do so for now catch me on the next one  foreign  let’s focus on functions and functions  basically it’s what makes a program in  general so if you have an application  and you want to perform a bunch of  business logic I.E all this all the  decision making all the steps required  in order for you to build a large piece  of your application  functions is the heart of pretty much  everything  now in kotlin this is a function in here  so you’ve seen that when we have  basically print line hello in here so  fun and then main so this is the  construct for creating a function  this is the name so this is a special  name because this is the entry point for  our application and then here this is  how we are invoking the function and  then within it we have the parenthesis  and Then followed by curly brackets and  inside this is the function body  now here you see that the function name  is called Main  and inside I have a sequence of  instructions so in my case print Ln  print Ln as well so if I put my mouse in  here  println is another function have a look  it’s a function so public inline fun the  name is println and then it receives a  message of type any and the return type  is unit which we’ll cover in a second as  well  so you’ve seen also that in here so I  can say hello but if I say hello Dot and  then uppercase  this is also another function right so  these are  predefined functions or built-in  functions that we can use so have a look  so this is public inline fun string and  then uppercase it returns a string  and basically it returns a copy of this  string converted to uppercase using  Unicode right so in programming we  basically use so also you’ve seen that  we can get for example indices right so  here if I say in this is so in this is  actually this is a variable in here but  have a look I think we used to be placed  before so replace first car right or  replace in here  and then lowercase  so all of these are functions  so if I run this in here so let me just  run this  and you can see that we have hello been  printed  right so this is a function called print  Ln and inside we have something called  an argument so these are the arguments  so what goes into a function and I’ll  show you basically arguments and  parameters in a second so in this  section let’s learn how to work with  functions  okay  let’s create our very first function so  in here literally outside the main  function let’s have our function that  pretty much will grit so let’s just say  fun so this is the construct for  creating a function  and here I’m going to say great  so we’re going to say grid and this  right here is the name of the function  and then just add parenthesis  and in here have curly brackets and this  is our function  now inside of this function called grid  we can perform  anything we want right so this is where  we put all the business Logic for our  application or for what we are trying to  do so in our case I’m simply going to  say print Ln  and then I’m going to say hello  so basically  let me just get rid of this print Ln in  here  this is our function now if I want to  invoke this function called Grid in here  inside of this other function called  main so this is where our application  runs I can just say Grit  so I just named the function in here so  just name the function right  and if I want to execute the function I  just add parenthesis  so this is actually invoking the  function  if I run this  we should see that we have hello  right so if I change this to hello  and then maybe add an emoji  and then say hey for example there we go  and run this  you can see that we have hello in here  if I comment this out in here  run  this doesn’t get executed so even though  we have oh actually sorry I didn’t want  to do this I wanted to comment out this  one in here so even though we have a  function in here it’s not been used  anywhere and even IntelliJ it’s actually  graying out the function name so as soon  as I  run this first  we get nothing but if I uncomment this  now the function becomes blue and we  know that it’s being used  so this in here is a function  now if I put a mouse in here on top of  this function you can see that this says  public and then fun and then great colon  and then unit so basically I can do this  I can say public and these are part of  the access modifiers and we learn these  later but let me just remove this  because it’s redundant so by default  these functions they are public unless  you want to say private in here which  means that the only thing that can  access this function is anything within  this file in here so let’s just get rid  of this and not complicate things at  this point and access modifiers not just  work with functions but also with  variables  cool so in here if I put a mouse in here  have a look public fun and then great  colon colon and then unit so in here if  I say colon  and then a unit  so this is a default return type which  is unit if you come from java this is  the same thing as saying in here  public and then void  and then greet  so greet in here so great is the name of  the function void it means that this  function doesn’t return any value so let  me just get rid of this and if I press  command and then you can see that I can  navigate into the unit definition so  let’s just go into it or you can press  command and B and also you can see the  keyboard shortcut Down Below in here so  in here public object unit the type with  only one value the unit object this type  corresponds to the void type in Java all  right so basically this is actually  redundant so if your function doesn’t  return any value then basically you can  omit this all together  okay so you don’t need this and it’s the  same with the main function as well so I  could actually say in here unit  it’s the same thing but this is  redundant  all right so this is your first function  with kotlin so fun is the keyword for  creating functions  then the name then here you add  parenthesis and inside parenthesis you  can have parameters So currently there’s  nothing right so I’ll show you  parameters also in a second and this is  pretty much it let’s carry on with  functions in the next video  all right now let’s say that our  function we want to have the ability of  passing an argument into the function so  when we invoke the function grid we want  to pass for example the name and then we  should be able to print hello and then  basically the Emoji and then the Emoji  followed by the name of the person so  how do we do that well with functions we  have something called parameters so in  here we can have a parameter and let’s  just say name for example  now when we say name we can’t just say  name because we need to specify the  actual type so what is the type for the  value which will be passed so here let’s  just say string  and now you can see that this grid  function here is complaining because we  need to pass the name so here let’s just  say Jamila for example so this is the  name  and here we not using it so if we run  this  so we still get hello  in here but really what I want is hello  and then Jamila so let’s just use it so  here we can basically add  dollar sign  and then name  cool so let’s just run this  and you can see that we have hello  Jamila now the beauty of this is that I  could invoke the exact same function in  here and I can say Alex for example  and here let’s just say Bob as well  if I run this  you can see that we have hello Jamila  Hello Alex hello Bob right so this is  the video of functions now this right  here is the parameter so a function can  have so function can have zero or more  parameters and then the values that go  inside so the values that go inside  these are the arguments  also just bear in mind that IntelliJ is  actually adding the hint for the name of  the parameter so here Name colon so  don’t type this basically right so this  is IntelliJ and I can get rid of it by  pressing option and then I can say do  not show hints for current method  and you can see that it basically gets  rid of it so let me just undo in here  and sometimes I kind of like this and  it’s really up to you  but basically these are parameters and  these are the arguments the different  values that we pass  so also if you want to pass for example  H so let’s just say comma  and then we can say h  and here the H is an integer  right now we can basically say  if  and then h  in here  is greater  or equal to let’s say 16  we’re going to say  print  and then name  is an adult  else  we’re going to say basically  the same thing but I’m going to say is  not an adult  and now here we have to pass the H so  let’s just  in here say comma and we can say 22 for  example  in here we can say also  16  and let’s say that Bob is 11.  cool so you can see that now we have two  arguments and two parameters if I run  this  there we go so hello Jamila Jamila is an  adult Hello Alex Alex is an adult hello  Bob Bob is not an adult  cool so you can see that basically this  function now we print we have some logic  in here and if it’s an adult or not we  basically perform some if else statement  and generally this is how we write our  business logic but in this video you’ve  learned about parameters and arguments  foreign  other thing that I want to cover when it  comes to the arguments is that in here  let’s say that you want to switch for  example first you want to pass the age  and then the name for example you can do  it so here what we do is we basically  get rid of  this for now and now I can say h  equals 2 and then 23.  comma  and then I can say name equals to and  then Jamila  so this is kind of the same thing as  IntelliJ is doing but basically here now  I’m being explicit on the actual  ordering that I’m passing these so  sometimes you might have you know a long  list of arguments and you want to be  able to Define so you want to say h  equals to 23 name equals to Jamila so  and so forth you can also do it so here  if I run this nothing will change so his  exact same thing hello Jamila Jamila is  an adult right and you could also do the  same for these ones this is what’s known  as named arguments  cool next let me talk to you about  default arguments  all right now let’s learn about default  values for arguments so sometimes you  might not necessarily want to pass a  value into a function or basically an  argument what kotlin allows us to do  very similar to golang and also  JavaScript is that we can have a default  value so let’s say that for example we  don’t want to pass age in here so what  we can do is we can say age  and then colon int equals to and then  the default value so here let’s just say  minus one  so we’re going to say -1 and now we can  have a bit of logic in here so we can  say right so if the age in here is  greater or equal to 16 we print is an  adult and also let’s just have another  if statement in here so let’s just say  if  and then H equals to -1  so we know that H is not provided so  obviously there’s better ways of doing  this but this just allows me to  illustrate the purpose of default  arguments so here I’m going to say print  Ln  H not provided for example and what I’m  going to do  is we could actually  you know exit out of this Method All  Together by using the return  keyword but let’s learn that later but  now it means that for example within  Alex in here I can remove the H  altogether  have a look  so this is good so now Alex age will be  -1 because it’s not provided so if we  run this let me just add a print line  but between  between each Grid in here so print line  as well or actually better what we can  do is so let’s just remove this print  line as well and we can do it right at  the end of the method so let’s just say  print line so this is much better cool  so if I run this now  what we should be able to see is have a  look hello Jamila and then Jamila is an  adult Hello Alex and an age not provided  Alex is not an adult so I’ll show you  something else that we can do so that we  don’t print this line in here and then  followed by Bob Bob is an adult so both  Ali so both Jamila and  so both Jamila and Bob they have age and  with Alex because we didn’t pass H the  default value is  -1 and obviously if you want here as  well so for the name you could have  equals two and then the default  um and then the default value but in our  case we want name to be mandatory cool  this is pretty much everything about  default arguments catch me on the next  one  foreign  what’s really cool about kotlin is that  you can patch functions as arguments so  functions within functions so in here  let’s say that we want to have another  function let’s just say function or fun  and I’m going to call it and I’m going  to call this as Foo in here and within  this full function I want to be able to  pass another function as argument so the  way we do it is we can say bar for  example this is  going to have the type as this in here  so this is literally a Lambda function  in here so it goes like bar and then  basically this is a Lambda function and  in here we need the body just like that  and now we can have some logic in here  so let’s just have a simple print I’m  going to say bar function in here  and  right after it I can invoke this  function which is bar so I’m going to  say bar in here and invoke it like so  which means that now if I want to use  this full function I can in here let’s  just add a comment  so that we basically organize things  properly and now within our main  function if I was to use the foo so Foo  in here I’m going to say Foo  and I’ve got a couple of options so here  I can basically invoke it like so  and then say bar  equals to and then curly brackets and  this now is the function so here I can  basically have now any logic I want so  here I can say for example another print  and then bar  as a function  cool so let’s run this and you should  see that  what we get is bar function and then bar  as a function  I can basically now take this Foo invoke  it again and this time  I can basically say for example  baz and then baz so this right here  is anything that I want right so let’s  just run this  and you can see that now this is bar  function and then bars Buzz so you can  see that this function has a different  implementation than this one when the  function is passed onto it  now you’ve seen defaults so if you don’t  want to pass a function what you do is  basically say equal and and you can have  an empty implementation like so right so  now here I don’t need to pass the  function in here  so this is kind of cool  and the other thing is so in here we can  basically so let me just duplicate this  in here so we can pass the function in  two ways we can have it as named  argument in here or so we can take this  in here  let’s just get rid of this  like so and we can pass it like this  have a look  so in fact we don’t need the parentheses  and  this is basically invoking the full  function and then passing the other  function inside so it’s the exact same  thing as we’re doing here so if I was to  run this in here  and um  let’s just say 2 for example  run it  and you can see that it works the exact  same way so in here bar function bar as  function and then bar function bar as  function two and then bar function and  then here this in here we didn’t pass  any function and it just gets a default  and we don’t do anything  so this is a really nice feature when  you can pass functions within functions  and bear in mind that this right here so  in here so this right here  will only work when the function so the  function in here so the function is the  last argument yeah so if it’s not then  you have issues so here if I was to say  for example let’s just say name and this  is a string for example right and let’s  have a default value of empty for  example so now you can see that  you are no longer able to do this right  so you can only do this when the last so  the last argument of it is the Lambda  itself so here let’s just switch places  and now you can see that this in here  does work  so if this is a bit complicated don’t  you worry because as you start to write  kotlin code and do more complex stuff  then all of this becomes second nature  and one last thing that I want to  mention is so here we basically have a  unit so in here have a look so name and  then bar so let’s just basically put  this on a new line like so so this is a  unit but this could return any data time  so if you want the string you just say  string if you want an integer then you  just say integer right and obviously  here then you have to return the default  value as well that comes with it but in  here you can see that this now fails  because we have to return a number for  example  there we go cool so let me just put it  back because it’s easy to understand the  way I’ve done it  but at this point if this is a bit  confusing don’t you worry about it  seriously cool this is pretty much it  catch me on the next one  let’s go back to this function called  grit still under the  functions.kotlin file and in here  remember  we saw that if the age is -1  basically it goes and prints all of this  but let’s say that I don’t want to  continue at this point and meaning that  I want to bail out so what we can do is  within the condition so here if and then  age right so what we can do is just say  return now when I say return it  basically everything that comes after  this line in here so outline 27  will not be executed so you saw that in  here  when we have  I think it was Alex  right so before it was printing that  Alex was not an adult in here right  because he goes in here so Alex H is  minus one which is a default but all I  want is basically to come out of this  method and not even bother to was coming  next so this is when you use the return  keyword  let’s just run this  and now if I scroll up you should see  that  we have hello Jamila Jamila is not an  adult and then Hello Alex  age not provided and then we don’t have  basically Alex is not an adult because  we exited the method all together right  so here we did exit method all together  and let’s also include a new line so we  can include the new line by saying  backslash and then n  and if I run this again so this time we  should have a new line in between so  here  I want to have a new line in between  here Alex Asia provided a new line and  then hello Bob Bob is not an adult  awesome this is pretty much it also what  I think maybe you can include the age in  here as well if you want so name and  then in here let’s just say h  and then Dash  and let’s just take this and put it  right here  and I think now we have better messaging  so if we run this you can see that now  we have Jamila 23  is an adult  Alex agent or provided Bob 11 is not an  adult  cool this is pretty much the return  keyword right so if I was two in here  let’s just write in here let’s just  return  so if we return at this point  nothing will be executed you can see  that IntelliJ is telling me hey what’s  this so if I run this  you can see that nothing get executed  apart from our functions within  functions  so this is pretty much it so what I’m  going to do actually is let’s just take  this right here  and I want to say  functions as arguments  there we go cool and you haven’t learned  about private so let’s just remove that  and here if you want to uncomment this  and run it just do it  all right so if I run this  the output should now be less verbose  have a look for nothing but all I want  is within the grid I want to get rid of  this because this is pointless  all right this is pretty much how you  exit out of a method whenever you want  using the return keyword  all right so so far you’ve seen that our  functions they really don’t return any  value right so you saw the in here the  unit  so the unit type and basically our  functions they don’t return anything so  let’s learn how to pretty much return a  value from a function so here let’s say  that we want to have the ability of  doubling a number so if we pass 2 the  output should be 4. so here let’s just  say fun and then double so this will be  the name of the function and here we’re  going to accept a number so I’m going to  say n for number and the type will be an  integer  and inside here you’ve seen that  basically all I do is this so print Ln  and then I can do n times and then 2  right so if I want to use this and let  me just comment this once more so if I  want to use this I can just say double  and here and then I can say 10. so this  will be 20 right so if I run this  there we go we have 20. now I basically  want to return the value instead of  printing okay so this right here doesn’t  return anything so it’s the same as the  void in Java but all I want is to return  a value so if you want to return a value  from functions you basically say return  so this is the returned keyword and  you’ve seen that it can be used to exit  also out of the function and also what  we need here is to say right so outside  so outside the last parenthesis so the  closing parenthesis just add colon  and then the return type so here I can  say int so this function returns an  integer  and have a look so now this works  beautifully so this now if I run it will  not print  right because this Returns the doubled  number which is 20. so here let’s just  store this into a variable vowel and  then I’m going to say d for double  equals to and if you want actually you  could just print  just like that if I run it  we should get 20.  so this is really cool  and yeah that’s pretty much it obviously  if you want to return all sort of  strings you can say string in here and  this will not fail because the data type  is different but you get the chest  awesome  this is how you return values from  functions  okay  if your functions return a single  expression the curly brackets can be  omitted and the body is specified after  the equal symbol so meaning that in here  so I can simplify this entire function  because here I’m just returning so I’m  getting a value and then I’m returning  so if this is the case then I can just  replace this function let me just  comment this out and we can rewrite it  as follows I can say fun  double  so everything is the same so n  and this will be integer and then I have  to specify a dividend type which is int  and here this is what’s different so now  instead of saying curly brackets  I just say equals  and then I can just basically have my  expression so here I’m going to say n  times and then 2.  so have a look how beautiful this is  right so function as single Expressions  right so this is something that Java  really doesn’t have and I love this so  you can see that nothing changes if I  run this  there we go have a look 20. we have the  exact same output  cool so this only works when your  function basically does one and only one  thing so for example if you were to have  this function here so let me just put it  back  and let me call it double and then two  for example so if you were to have print  line in here and need to a bunch of  other stuff before you can’t really go  from this to a single expression  function right so only if there’s one  value so basically one return statement  for example  cool this is pretty much it catch me on  the next one  so you saw functions the return types  and body and whatnot let me quickly  touch on the return types which is very  important so if a function in here has  the unit remember the unit this is  optional right so if for example this  was unit in here and if I remove this in  here  this actually is optional okay so void  right so if  it’s not unit  then so in here so we have an integer  this is always required so the return  data type  when it’s a single function expression  in here we can actually omit this so we  can get rid of this  so we can do just like this and this  works but only for single function  expressions  so I’m going to leave this in here as is  but bear in mind that if it’s a single  function expression you don’t need a  data type  and if it’s void right so if it’s void  so the unit you don’t need the type as  well but as soon as you have the curly  brackets  you will always require the return type  for your functions  this is pretty much it catch me on the  next one  foreign  working with functions if you want to  return multiple values from your  functions it’s possible by using the  pair data type as well as the triple so  for example with golang you are able to  return multiple values without even  having to use the pair or triple but in  kotlin if you want to do that we have to  use the pair data type so it goes as  follows so here let’s just have a  function so here I’m going to say fun  and then I’m going to name it as 2  and then values  and what we’re going to do is we’re  going to  say colon and the return time for this  will be a pair  just like that and here we have to have  the diamond and now what is the data  time so let’s just say we want to return  a string and also an integer just like  that  all right now within here we can pretty  much return so if you don’t have any  logic to do so let’s just say return and  here I’m going to return the  string so here I’m going to say Amigos  and we have to say 2  and then the integer type so here let’s  just say 20 for example  so this is how you return multiple  values from within your function if you  want to return three values you  basically do the same but here let me  just have this as three values and here  we’re going to name this as  triple so here just say triple and  basically the data type let’s also have  an integer or let’s have a character so  Char in here  and the triple is a little bit different  in a sense that  we say triple in here  and we pass the values so first  let me put this on your line then we  have the second  then we have the third so here I’m going  to say the character is Zed for example  just like that  and we need to comma here  and if I indent this cool put this on  your line  and you can see how this function is  looking like so fun tree values the  return type is  a triple it returns a string an integer  and a character and when we say return  it basically Returns the triple in here  so we can change these to single  function Expressions so this can go  and here and just say equal  and same as return and here right  so this is beautiful code actually so  have a look oops not like that but like  this so if you wanted to right so get  rid of that  and get rid of that and this and say  equals  and voila  cool so let me just put this one back so  you have it for reference  there we go and this is pretty much it  now one cool thing about  returning multiple values is that you  can destructure them so whenever you  want to use them you can destructure  them as follows so if I in here within  my main method if I want to consume them  for example let’s consume the two values  so I’m going to say two values right so  two values  and the values will be stored into a  variable so let’s just say Val  and now I can say V1 for Value 1 and  then V2  equals 2 and the two values  and the same with  triples right so here I’m going to say  three values  and here I’m going to name this as  let’s just say T1  t two  and then T3  and if you want to print them let’s just  print them so print  and here I’m going to print the one  and then also V2  let’s duplicate this  and also change this to T1  T2  and also t  and then three  with dollar sign and let’s basically  have a print line in here  so if I run this  you can see that we have Amigos 20  Amigos 20 and then Zed right so  basically the values here they are the  exact same thing so I could have changed  this to  code for example and then 90 or 0 it  doesn’t really matter run it  and there we go so this is actually  beautiful stuff so these are the values  so two values and then three values one  two and then three  and actually one thing that I forgot is  with pairs in here so you can say Amigos  and then two or if you also want you  could use so let’s just  duplicate this  and I’m going to say two values and then  two and you can just basically say pair  like that  and instead of using D2 so I prefer two  so it’s nicer but you can also do this  okay so either this or this awesome this  is pretty much it catch me on the next  one  foreign  let’s learn about classes and objects  which is pretty much the heart of  programming in many programming  languages where we kind of have classes  and these are the blueprints and then we  have the objects so these are the actual  things that we create from the blueprint  so classes is actually found in many  programming languages and kotlin also  has the exact same construct so if you  want to create a class you use the class  keyword in here and then you give it a  name so here let’s create a class called  smart device  and add curly brackets and this right  here is the smart device class now this  class right here it’s the blueprint so  blue  and then print  and basically it defines how smart  devices are created so you define the  properties so here Pro protease  and also behaviors  so here we are talking mainly about  functions right so these are the  functions  and these are the variables  so either with vowel or VAR  so we’ll come back to this in a second  but basically you have a class in here  where we can define properties and  behaviors in here so let’s focus on  these later but this is pretty much the  construct of how to create a class now  the way that we’re going to use this is  within our main function in here we can  say it right so now I want to say Val  and let’s say that we have  a smart device and we’re going to name  it as TV  equals to and then just say smart device  now in languages such as Java you have  to say new in here so this is not needed  in kotlin  and all you do is you just refer to the  class name just like that  if we want for example a phone in here  we say phone equals to and then smart  device now you can see that this right  here is the blueprint so the class is  the blueprint  and then the TV and the phone these are  the objects so we have two objects now  classes is something which is not new to  be honest so if you look into array for  example so array in here so array Mouse  in here have a look so if I open this  and just go into the array dot kotlin  file and scroll to the top have a look  public class and then array so this in  here has to do with generics which means  that you can pass either a string or an  integer or a double  but basically this is a class right and  you learn about Constructors as well in  a second but with kotlin we have many of  these classes and in fact if I open up  project  and in here I think we can locate so if  you click on the array.kotlin and then  click on this button select open file so  this should take you to where this class  is located and you can see right here so  if I Collapse this have a look  so all of these are classes you’ve seen  Boolean you’ve seen car or character  you’ve seen double  enum float int so int is also a class if  I click on it have a look public class  int  have a look number short throwable so  including string right here and all of  these are classes and if I Collapse this  you should see that there’s a bunch of  other classes right so have a look pair  for example you’ve seen the pair class  in here right so it has  um the first value and the second value  and in here you can find a list of a  bunch of classes  so what we want to do really in a  nutshell is for us to have our own  classes so that we can model and  represent the real world and with  classes we can pretty much build  anything awesome so now that you  basically know that a class is a  blueprint and when you instantiate in  here this right here is the object so TV  and phone are objects and that the  blueprint that was used to create these  was the Smart TV device  awesome this is pretty much it catch me  on the next one  awesome in here let’s say that within  our blueprint so this class called smart  device we want to have some properties  so maybe you want to have the brand you  want to have the color  you want to have the price so basically  these are the characteristics for a  smart device also the type whether it’s  a phone whether it’s a TV right so in  here we have all these properties and  then the objects themselves they’re able  to decide  which characteristics that they want to  take from the blueprint so in our case  let’s in here say that we have this  class called smart device and we want to  have the property in here we’re going to  say VAR and let’s say brand for example  so here we need to specify the data type  I’m going to say string  and here I’m going to basically have  empty string okay so brand with an n in  here  cool so let me actually get rid of this  in here and this as well and as we  progress you’ll see how this will work  out  awesome now we have this property in  here called brand the type is a string  you can pick literally any type you want  let’s also have another one so VAR and  then price for example  and this will be let’s just say double  for now in here and equals and we’re  going to say 0.0  so this is the initial price  cool now  you see that we have  two smart devices one it’s a TV and the  other one it’s a phone so maybe we want  to also represent the type now for the  type I’m going to show you later  something called the enum which is a  better way of representing types instead  of using strings so let’s just keep  these two properties for now so let’s  now in here when we have TV let’s just  say TV we can say TV DOT and then have a  look I can say brand  equals to  and here I can say for example Samsung  and I can also say TV DOT and then price  and let’s say that this TV in here  is I don’t know maybe one thousand just  like that  right so 1000 if you want currency and  whatnot you can also have it cool so  this is the TV and you can have brand  then model and you can basically model  this the way you want but let’s just  have brand and price  let’s do the same for the phone so here  I want to say phone Dot  brand equals to and in here let’s just  say apple  and now I’m going to define the price so  phone  Dot and then price equals to  in here we let’s just say iPhones are  quite expensive so  there we go so let’s just say  1200 just like that all right and if you  want you can basically say apple and  then iPhone I don’t know what’s the  latest model 13 14 like I don’t even  know but it doesn’t matter right so  apple and then iPhone let’s just see the  iPhone and then here you can say Samsung  Smart TV and then Q is it qled something  like that  cool so you can see that now we have two  objects in here and they are completely  different from each other now in the  real world this is actually  a thing right so this could be your TV  and this can be your phone we can see  that we use the same blueprint called  smart device because both of them are  smart devices  to create two separate objects and you  can see that in here this is how we  define the properties now one thing to  bear in mind is remember the difference  between the vowel and VAR so if I was to  change this from VAR to Val check what’s  going to happen so have a look the brand  for both objects in here I cannot do  this in here right and that’s because  the reassignment right so we cannot  reassign the actual value so we’ll come  back to the vowel and also data classes  later so this has to do with  immutability but for now let’s just use  the VAR that basically allows us to  assign the values in here after we  create the object like so  cool next let’s go ahead and talk about  Gettys and settings  all right so we have the TV as well as  phone if I was to print let’s just say  print Ln in here and let’s just have  quotes in here and let’s pretty much  just print the TV so I’m going to say  TV DOT and then brand followed by TV DOT  and then price okay so let’s take this  and then put it here and this will be  phone  so phone  dot brand as well as price if I run this  you should see that we have Samsung  Smart TV qled and then the price and an  Apple iPhone and this is the price  cool now we are able to get the brand in  here because of this so here I’m going  to say get  in here  equals to and then field  and the same for the price so let’s just  add in here get equals to and then field  now this is actually built into the  language and literally you actually  don’t need it but this is what’s  happening under the hood and you can see  that IntelliJ is even telling me that  this is redundant so redundant get  because by default this is included  hence when you say in here  tv.brand you are allowed to do this  right so if I was to change this to Foo  for example Foo in here so if I run this  you can see that now in here Foo  is replaced with the actual name right  so uh oh actually brand my bad right so  Foo is replaced with the actual brand  for both  in here this brand and this brand So  when you say in here so if I go back  get equals to the field is just going to  return this field in here  cool so this is how Getters work in  kotlin also we have Setter so here just  say set and then value and then inside  I’m going to say field so the field  so this field called brand is equal to  the actual value  so when we say in here price right or  either brand equals to the value so the  field equals to the value in here so  Apple iPhone so if I was to change the  value in here to bar this time  so you should see that if I run this  bar is replaced in both instances  so this is how basically Setters work  now obviously here if you want to  perform something so maybe you want to  have I don’t know you want to basically  uppercase everything so uppercase in  here or you want to replace  the first case with an uppercase you  never know right so this is how you do  it and maybe you want to perform some  validation before you set the value so  here you can also perform whatever logic  that you want to be honest so if I was  to run this you should see that now  Samsung TV is Mark you led all in  uppercase  so this is basically Setters now in here  so let’s just leave it as value  so value  and we can take this and basically put  it the same in here for the price but as  I said this right here it’s redundant we  can get rid of it  and in here as well and also here  awesome so I just feel like this is very  important for you to know what is  happening under the hood because Getters  and Setters is something that you find  in Java for example and in kotlin they  work a little bit different  cool if you have any questions drop me a  message otherwise catch me on the next  one  all right so you’ve seen properties in  here so these are the variables that  specify the attributes of the class  object now let’s look into the methods  so methods slash and then functions and  the functions are simply the behaviors  of what the object you create have the  capability to perform  so in our case in here so a smart device  might be able to switch on switch off  might be able to increase the brightness  decrease the brightness might also be  able to increase the volume record shows  and have a list of apps and all of that  stuff right so basically those are the  functionalities right so installing an  application and installing so all of  that is the functionality right so if  you for example take this same example  to a class called person right so the  attributes would be name skin color  languages and the behaviors would be eat  sleep play games and whatnot right so in  our case in here so for our smart device  let’s just have two methods and in here  we’re going to basically say  fun and basically the behaviors are  pretty much functions and here I’m going  to say turn  and then on  and in here we’re going to for now let’s  just print line  and then here I could use the brand  is switching  on  and let’s have another method in here  I’m going to say  turn off  in here turn off  is  let’s just say switching and then off  all right cool so you can change the  name here switching on or turn on it  doesn’t really matter so now  this right here is what the blueprint  allows objects to perform so in here  let’s just scroll up let’s use our phone  for example if I want to switch off the  phone I can just say phone  Dot and then turn and then off for  example  so if I run this  you can see that have a look Apple  iPhone is switching off  so if I was to do the same 4  let’s just say TV in here so TV right so  our TV  you should see that  we have  in here Samsung Smart TV uq LED so it’s  a long name is switching off and Apple  iPhone is switching off so you can see  that there are two completely  independent objects and when we say  brand in here it refers to the current  instance of the current class right so  this is pretty much how you define  behaviors  now what we could actually do is we  could mutate this Behavior so we could  have a variable in here so I’m going to  say VAR and in here I’m going to say  state  and the state in here will be a Boolean  and initially it will be off right so  let’s just say  false  or we could say is and then on for  example right so is on or is Switched so  let’s just say switched on so this is  false now when I turn on in here  I’m going to basically say is switched  on  equals to and then false or actually  sorry this is true my bad  and let’s take this as well is switched  on  and here so when we invoke turn off this  will be false  there we go cool and then in here let’s  just basically have another function in  here another Behavior so I’m going to  say fun  and in here I’m going to say get device  state so what we’re going to do is we’re  going to say print Ln and here I’m going  to say dollar sign  and then the brand  plus d is switched on I’m going to say  is on  and here dollar sign and then is  switched on  cool so hopefully you can see how this  is working so get device state  and if I scroll up in here  so you see that with our phone so we  turn it off  and here let’s just say phone  Dot and then get and then device state  so in here if I run this  you should see that we have Apple iPhone  is on  this is false right whereas so in here  if I turn on the TV  so  10 and then on  and then if I say TV DOT  get and then device state  run this  we should see that if I scroll up  have a look Samsung Smart TV is on  this is true  awesome so this is pretty much how you  define properties and behaviors within  your classes  if you have any questions drop me a  message otherwise catch me on the next  one  okie dokie now the last thing that I  want to show you is Constructors when  working with classes so you see that  here we have our TV  and the way we initialize the values we  say tv.brandtv.price  the same for our phone Fondo brand  phone.price  now it would be nice if when we actually  create the object itself  we were able to initialize these values  by passing them inside here  well we can achieve this with  Constructors so let’s scroll down to our  blueprint so class and this is the smart  device and in here what we can do is we  can say Constructor  and then within this Constructor we can  define the properties so in our case we  want brand as well as price and is  switched on so I’m going to put this on  a new line so I’m going to press enter  in here and let’s have the brand so just  say brand  and the type of it is string  comma  price the type of it is double  and finally is switched on so in fact  let’s just put everything under a new  line and I’m going to say is  switched and then on  and the type of it is a Boolean  cool so now in here you see that we have  the Constructor and now we can pass the  brand price and switched on so if I was  to in here  let’s just change the TV for now now in  here you see that this is complaining  because we need to pass the  value so here we just take this from  here  and here I’m gonna basically add a new  line and then paste that in there and  also the price which was a thousand in  here so a thousand  and whether it was on or off so what  we’re going to do  let’s just scroll down and let’s have a  default value equals to I think what did  we say we said false so false and you’ve  learned about this in here so this also  works within Constructors  and this is pretty much it so you can  see that now when we Define our TV we  don’t have to say  tv.brandtv.price so we could just use  the Constructor itself  which means that I can comment this out  in here  just like that and if I try to run this  so let’s just go back in here and I’m  going to add empty for now here so 0 0  and we’ll come back to this in a second  because otherwise it’s just complaining  about it so if I run this in here so  what I want to see  is that  have a look we have 0.0 is switching on  and then is on true but really what we  need is to say Samsung  basically the name of it right  and the reason why this is doing this is  because we are passing these values in  here Samsung so for the brand and for  the price right but we’re not assigning  them to anywhere so what we have to do  in this case is two things so we could  actually say right so the brand will be  this brand so we just reassign that and  then for the price the same here  and for the is switched on we can say is  and then switched on  and if you want all together you can get  rid of the type just like that so let’s  just get rid of the type so it’s not  needed  and just say equals  cool now  in here if I run this  now we basically assign the values can  you see Samsung Smart TV it’s now  populated and we’re good to go or we  have something even better so if I put  my mouse in here it says property is  explicitly assigned to parameter brand  so it can be declared directly in  Constructor so what we can do is we can  get rid of the brand so let’s just do  for the brand first  so we get rid of it  and literally it’s just getting rid of  it the same for the price  in here and for switched on so we can  move it to Constructor and basically if  I just undo this so you can see so what  we’re doing here is basically the  difference is that this in here becomes  a variable inside of the Constructor so  VAR VAR and then the VAR okay  so which means that now we can get rid  of this  and all the properties are within the  Constructor itself  so if I run this  you can see that this still works so  Samsung Smart TV and then this is the  price is switching on is on true so if I  hide this one other thing that I want to  show you is that you see that we have  Constructor so this actually it’s not  needed so let’s get rid of it  and it becomes much simpler  so look how beautiful this code is now  cool so that’s pretty much it you’ve  learned about Constructors you’ve  learned about the properties in here for  the objects and also the behaviors which  are functions  if you have any questions drop me a  message otherwise catch me on the next  one  oh  cool you saw Constructors and before you  saw that we did add some random values  in here for the brand and price for our  phone so  what we want to do is the ability of  having the ability of using the  Constructor in here like we did here but  also have the ability of not even  passing something inside right so we  want to have multiple Constructors if  possible so the way to do it in kotlin  is the following so you can see that  this is actually complaining but we have  a couple of things that we can do so if  we want to we can basically say that  basically these will have a default  value and the default value for these  can be no so if I say equals to null in  here this complaints because we need to  add the question mark so remember before  so this is how we handle nose so I can  say equals to and then no  now if I scroll up you can see that so  we are able to do this right so  basically pass nothing inside  so this is one technique  or so let’s just get rid of this  in here or what we can do is so  let’s just say basically like this what  we can do is have a secondary  Constructor and we can have as many as  we want so we can say Constructor in  here and basically if you want you can  basically have you know variables again  and maybe you want a new variable for  example or a new property Foo and you  define the type string for example  and what we have to do here actually  is be able to call the main Constructor  and pass these values in here so let’s  not do this for now and I’ll show you in  a second so here what we do is we say  colon in here and then say this  and this pretty much refers to the main  Constructor and you can see that now we  have to pass the price as well as the  switched on and the brand actually and  here this is the point where we can  basically I think if we say no this will  not work right because that cannot be  null so we just pass  empty Fields right so here this will be  0.0  so this is another way in here and for  example what’s cool about this so let’s  just scroll up you can see that here  this works so what’s cool about this  is that we can have so let’s say that we  want to have another Constructor  where we want to be able to only pass  the brand for example right so here I  can say VAR  and then brand  and let me put it on a new line in a  second so string so VAR brand just like  this  there we go and then we say this and we  pass the brand from this Constructor  into the main one like so and it looks  like we’re not allowed to have VAR  that’s fine on a secondary Constructor  that’s cool so we say brand and then  string and then we pass the brand inside  which means that now  in here I can basically remove the brand  from here and then just pass it within  the Constructor so get rid of that  and then here brand which is Apple  iPhone  and hopefully now you can see how this  is working out right so there we go so  this is pretty much how you can use  multiple Constructors and also if you  want these to be no so let’s just say  these can be no so just add question  mark in here for both price and brand  right and if you want also for the  switch done in here just like that  and with this it means that so here we  could just say no  and here we could just also say no  and obviously you see that we’re not  even using this Constructor so IntelliJ  is complaining about it but if I was to  create a new device so here let’s just  say device or smart device assign this  to a variable  so there we go Val and then I’m going to  name this as  laptop for example  there we go so you can see that we have  a couple of Constructors one that we  don’t pass anything another one that we  only pass the brand and another one that  we pass all the fields brand as well as  price  and this is pretty much it and you can  see also here we have default  right so defaults and uh yeah so if I  run this  everything should work as before have a  look Apple iPhone  as well as the Samsung Smart TV qled  cool this is pretty much everything on  multiple Constructors  catch me on the next one  now let’s look into how can we get the  string representation of any object so I  think before you saw that when we  printed the TV in here so if we print TV  like so  run  you should see that we get a smart TV  and then at and then this random number  which is the hash code  so this really doesn’t tells us anything  and more often what you want to do is  the ability of printing the actual  values I.E Smart TV the price and all  other attributes related to the object  itself so instead of you saying Dot and  then brand  and then Dot and then for example the  price  and whether is on I think right here so  instead of you doing all of this what we  want to do is just the ability of so in  here we want to  just print the object so just say print  and we should get the exact same  information  for that what we have to do is under our  blueprint so smart device so this class  which can be found here let’s go  straight to the bottom  and in here what we’re going to do is  let’s right click  and then we’re going to generate  to string  so two string is quite obvious on the  name so here we want to select all of  these fields so brand price and is  switched on basically all the properties  okay  and this will actually override so  override fun to string and this will  return the string representation for the  object  so here you can see that it says return  and then we have the brand price is  switched on and we’ll have all the  fields Associated from the generation  screen that we just selected so now it  means that if we run this so if we run  this  we no longer should see the random  string but instead you can see that we  have the class name  then brand equals Smart TV price and all  the other fields  cool so this is pretty much how you get  the string representation for your  objects  catch me on the next one  let us now learn how to perform a  quality when we have our own classes and  objects so in here we have this TV  and let’s just leave it as TV for the  name and let’s say that we want to have  the exact same TV and we’re going to  call it TV and then two  so TV1 and then TV2  now I want to know whether these two  televisions they are the same or these  two smart devices are the same so the  brand is the same the price is the same  so what we want to do is to check  whether they are the same so if I say  print line  and then TV One  equals  so double equals  and then  TV and then two in here  so if I run this  this will give this  false  so you might be saying hold on these are  the exact same televisions so what is  happening  well when we perform  this equality so equals and then equal  so 2 equals this is just saying whether  TV1  refers to the same place in memory as  TV2 which obviously they are different  right so TV1 is stored differently than  TV2  however if I was to change this so let’s  just get rid of that  and I say TV2 equals to TV and then one  in here  if I run this  this now should gives us true because  they refer to the same location right so  if you change TV1 TV2 will also change  now let me go back in here  and leave this as is now in quote link  things work a little bit different and  that is we have the triple equality so  in here equals equals and then equals so  three equals and basically this is used  to check the reference  so whether these two refer to the same  place in memory so if I run this this  will be false in here  and  if I change this to TV1  this will be true right  now when this actually comes into play  is so you saw that either this or triple  equals this is the same however if we  want to check whether the contents of  these two objects are the same right so  before we had so in here we had TV2  with the exact same contents here the  brand is the same the same for the price  so if we want to perform this equality  we use this  equals in here and what we have to do is  under our blueprint we have to override  the equals and hash code  so literally right after two string  right click and go to generate  and here equals in hash code  so again select all members in here  leave everything checked and then create  now what this is actually doing is  basically performing the equality based  on all Fields so first is actually  checking whether they refer to the same  place in memory and here have a look  triple equals so this refers to the same  place in memory  right  and then if this is the case then they  are the same object  then it checks whether they are of  different classes  and if they’re not then basically return  false otherwise it performs the casting  and then here then we are comparing all  Fields so brand price as well as the  Boolean  cool so if I now scroll up  and in here if we run this  this now should gives us true I’ve  looked so before without the equals and  two string this was giving us  false but now it’s behaving correctly  also if I was to say triple equals so in  here let’s just say double quotes on  both sides and here and what I want to  do is I want to say  equals in here and then dollar sign  curly bracket  and end curly bracket on this side in  here and let’s do the same for this so  this will be  triple equals and then dollar sign curly  bracket and then close curly bracket on  this side with double quotes cool and  here let’s just have triple equal  so if you run this now  you can see that  are they equal yes because we did  override the equals in hash code  and are they equal in memory so this is  actually comparing the memory location  okay let me just add a comment memory  location so run once more  and you can see that  this is true because all the fields are  the same  and in here this will return  false because they don’t refer to the  same memory location  awesome if I scroll up or down actually  so you see that we have the equals but  also  hash code now this in here has to do  when we are dealing with hash Maps so  let’s not focus about this and we’ll  look into this later  but for now this is pretty much it  so just to prove a point if I was 2 in  here  change for example  these two Samsung and then TV for  example now when we perform this right  here so are they equal this will be  false because brand  for TV2 is different than brand for TV1  and they don’t point to the same memory  so if I was to run this  this will be false and then false I will  look false  and also false  awesome and this is how you perform  equality on objects so I’m going to  revert this so you have it for reference  if you have any questions drop me a  message otherwise catch me on the next  one  as you can see kotlin it’s a powerful  and beautiful language if you enjoyed  this video literally just take one  second and smash the like button also if  you haven’t subscribed do so and as I  said this is part of the massive course  that I’m planning for kotlin so head  over to the website and stay up to date  with the latest recordings and also give  me some suggestions for what you want to  see on kotlin but you’ll see the list of  topics which I’ve already planned and in  case you have any other ideas I would  like to hear from you this is pretty  much it comment down below let me know  what other videos that you want to see  next and I’ll catch you on the next one  thank you  

admin

Leave a Reply

Your email address will not be published. Required fields are marked *