foreign and in this video we're going to talk about core Java in this particular course which is a lengthy video you're going to learn everything from the basics of core Java to the end of core Java now this is not the first time I'm uploading the video on Java if you can see from last few years you will find multiple Java playlist and multiple Java full-fledged video but we all know right the quality of videos are going up every day plus the number of features being added to a language is going up and that's the reason I'm going to re-record the entire course now this is a full-fledged course and that's why it's so lengthy but you cannot finish the entire course in one day even if it says 12 hours or 13 hours it will take time now let's talk about Java now why Java is so famous now if you see the programming index the top five programming language you will always find Java there maybe at the first position or at the fourth position you will find Java why Java is so famous is because Java has multiple features which helps it to be used in the Enterprise Market I mean not just Enterprise Market it can be used for mobile development web development and of course Enterprise application and most of the big companies if you go there they are using Java as their preferred language now in Java we have a technology called jvm and it's not just Java we have multiple languages running on jvm so even if you are working on some other language let's say kotlin Scala groovy ultimately you're working on Java technology and that's why if you learn core Java it is much easier for you to jump to different languages and I've also observed irrespective of which entry you are going for it can be for development or testing and that doesn't matter which language your working on that maybe python C sharp C plus plus they might ask you Java questions because because most of the interview questions are based on Java because they feel that if you know Java that means you know other languages as well or you can learn other languages easily as for my experience or did I forgot to introduce myself uh my name is Naveen Reddy I'm a corporate trainer software developer and currently focusing one on blockchain but Java is my first love so I'm working on Java from the last 12 years I've made multiple courses on Java on YouTube and different platforms and yeah I can teach Java any day anytime yeah sometimes I have to watch my own videos to realize the concept but the thing is I love Java and with this experience I'm making the entire course I was talking about my experience right in fact I have learned multiple languages in this all 12 years maybe I have worked on 12 different languages you can name it maybe C plus plus C python Java JavaScript uh go language so I love most of the languages some languages are learned just for the projects but then when I was learning those languages I was I was feeling hey it's it's so easy it's because I have learned Java yes people say Java is difficult just to start with it is difficult maybe if you want to learn Java as your first language it's kind of difficult because if you're coming from other languages let's say python or JavaScript and if you want to print one statement you can actually see that in one line uh that's not the case with Java right you have to write five lines of code but yes once you make a big application the number of lines will remain almost same okay so Java is difficult yes just to start with but the thing is Java is one of the most readable language what I mean by that is if you are looking at someone else code you can read the Java code line by line uh that's not true with other languages okay so Java is great in that terms Java is maintainable and that maybe that's why they most of the industries are preferring to use Java now Java has multiple features we'll talk about those features later but just to name them we got Voda which is right once one anywhere which supports multiple multi-threads it has a collection API concept you can handle the exceptions and we are going to learn everything in this particular course now just to go for the history of java Java was made in 1995 by sun microsystem and it was led by the team of James Gosling later on Oracle bought Sun so now the trademark of java belongs to Reichel yes in between Oracle did some messed up things with Java but yeah they are back on track and the beauty is Java is getting updated every six months not the major updates but they are working hard to make Java as the most powerful language so I hope you were excited for the entire course we are going to start from the basic as I mentioned and you will learn a lot of Defense stuff but as I mentioned it's not about completing the entire course in one day you can use a timeline to jump between these sections and also with this we also have a code there so in the description you will find the link GitHub link where you can access all the codes and maybe it will help it will be helpful for you to write this code so let's start the journey I know you were excited to write your first Java code but the thing is to write the Java code you need a place to write the code right so basically you need a editor of course notepad works right we all love notepad apart from notepad we have multiple softwares using which you can write your Java code maybe you you can use wordpad or you can use notepad plus plus you can use edit plus when it comes to programming there are multiple editor options available specifically for Java the one of the most famous IDE now IDE stands for integrated development environment so basically we have editors where you can only type the code or you can use a IDE where you can type the code we can where you can run the code where you can compile the code where you can debug the code and you can do everything in an IDE now which is good now for beginners using editor makes sense but then if you are working on a real project you can use a IDE now it is depend upon you which idea you want to use in this course we are going to use vs code now there are multiple options available we have Eclipse IntelliJ IDEA we have netbeans in fact I have worked on a lot on netbeans I just start off my career I have worked on Eclipse as well for some few years and then I've worked on intelligent idea of one or two project my personal favorite is intelligent idea the only thing is the ultimate version which is awesome is a paid version the community version is good but it has some limited features now why we are using vs code is because it's a very lightweight editor or an IDE so you can do everything in vs code and it's also lightweight okay apart from that you also need a compiler right so of course that when you write a code of course you have to compile your code so that your machine can understand that and in terms of java we have to download jdk which stands for Java development kit now how do you get these two softwares it's very simple just go to your browser and search for jdk download and also you can search for vs code so these are two softwares we're going to use let's start with JK first in jdk we have two versions We have a propriety jdk of what I call and we have a open source version in fact there are multiple implementation for open source you can choose any of them uh we have collector we have different implementation you can just export them but I'm going to use Oracle jdk here yes at one point Oracle made a Java paid but now it's free again so we can use Oracle jdk here and you can just click on the Java downloads and you can see it will give you multiple options the thing is at the point of this recording Java 19 already launched Java 20 is going to release soon and with every new version in fact Java is following this six months uh updates now so every six months you will get new updates right so with every new update you will get some features but the basic language will remain same so irrespective if you are watching this when we have Java 25 don't worry Java is Backward Compatible and the basic language features will be same of course W minor 2x in the new updates new features but overall it will be same in case if there is something new happening which is changing the basic concept uh we are going to re-record that video in the in this series okay now which one it wasn't to choose here should we go for Java 19 or Java 17 the thing is with every six months you get new features right but not every version is LTS LTS stands for long term support so there are some versions which get long-term support now Java 17 is one of them so instead of downloading Java 19 we are going to go for Java 17 because it's LTS so if I click on Java 17 here and you can depend upon which OS you are using I'm using Windows here or for the kojo we are going to use Mac OS but then the setup will change but the underlying software will remain same the code will remain same right so irrespective of which OS you are working will not make any difference for for the course but for this video I'm using Windows here and if you're using Linux you can choose Linux distribution or Linux package here if you're using Mac OS you can download Based on is it a mac Apple chip or it is a Intel chip and you can go for Windows as well if you're using Windows and for this machine I'm using Windows so you can click on this download here and it will take some time for the downloading this setup is actually very simple you just have to change one thing which I will show you in some time and straightforward in Mac uh in Windows you have to set the path okay we'll see how to do that next is vs code and you can just click on this download here now first of all vs code is a very popular IDE now if you're coming from some of the languages you might have already used vs code so it's quite easy okay now depending upon which OS you are using you can download one I'm using Windows here I will click on download and yeah it's starting soon and you can see it is downloading here now let's start with the jdk first when you click on jdk uh it will take some time the thing is in my machine I already have this two softwares so the first screen might might look different for both of us you can see from it says reinstall I will say yes so here you can see it says next in fact for you as well you have to just click on next next everything is smooth and depend upon your machine speed it will change the speed might change okay that's jdk install but how do you verify if you have jdk okay we'll do that in some time but let's do the setup for vs code again straightforward setup uh say I accept you don't have a choice here you can say next next um nothing to specify just body fight will be there so just keep it and install so by the time it is happening what I will do is I will open my command prompt and how do you check if you have Java installed so go just type Java hyphen hyphen version and it will give you the Java version if it is coming that means Java our JK is installed in fact you have to check one more thing which is Java C version for the compiler and you can see it is getting giving the version here but in case if you get error that the Java C command not found even if you have installed it that means the path is not set how do you verify the path here it's very simple actually or you can just go to search and search for environment variables or if you type environment it will give you the first font just click on that and here if you can see we have an option of environment variables just click on this environment variables and you can see we have user variables path here you have to make sure that you have a path variable or you can edit times in system variables if you want this to be applied throughout the system different users then you can use system variables or you can use a user variable here I will go for system variable make sure that you have path in case if you don't have a path click on new and set the variable name as path and set the value now what is the value here now the value is your bin folder so what you can do is you can go to your C drive go to program files search for Java here you can see we have Java and open this jdk click on bin now this is a path because inside this bin you have Java C so copy this and you can paste it here in the environment variables value now since I already have pathway table I will not create new if you don't have a path variable create click on ok but I will not do that because I already have a path variable just double click it it will show all the path variables for my machine I already have it here if I don't have it find the empty place or click on new paste and you're done and click on OK for me it's only this I will just remove this and click on ok ok now once you do this setup you have to make sure that you restart your CMD and now try for Java C version and it works okay so that's how you install jdk and make it work now let's open vs code now once your vs code is installed you can see it's done for me I will say launch so that's how vs code looks like now this is a place where you have most of the settings in fact I have already used vs code in my machine with different extensions and that's why you can see them here but for you I don't uh you will not be having this extension you will be having only first four uh now how do we check if everything is working just click on Terminal here new terminal and try to do Java C here as well and it works so your jdk and vs code is all set now you can start the code now once we have our setup done it's time to write our first code that's right so finally we are going to run our first Java code here now for that we have to open our editor so of course in the last video we have discussed that we are going to use vs code so let's open vs code here so I will say code okay so our vs code is all set to write our first code you can see it will give you a welcome screen when you do it and then it will ask you the preference do you want a light color dark color dark high contrast or light high contrast I would say go for dark because it's good for development it will launch train your eyes at least so let's close this get started and this is how your vs code will look like okay simple stuff right now what we'll do is first of all uh where you will get your folder so you will see your folders on the left hand side so this is where you will get your project all the files which you want to work with so you'll be having that here now of course in the upcoming tutorials we'll be using a different IDE and I want you to get used to different IDs when you work with so on the left hand side we can see we have an Explorer here and then we if you want to search something we have it here if you want to add some extensions which we are going to do later uh we'll add certain extensions to work with at this point we don't need any extension so just keep it blank I mean no need no need to install anything here in my machine I already have something on some other accounts but on this account I don't think so anyway and then if you want to run and debug debug you will find the option here now why do we run of course we know what to why to run right but why do we debug the application yeah we'll see that later at this point let's open our files of Explorer here and this is where I want to create my first project now how do we create a project now project will be a very broad term at this point what we are going to do is we will create some files and of course when you merge these files and libraries you create a project at this point we do are not working with any external libraries at this point we are just going for a simple files okay hello world right so for that what we'll do is we'll click on open folder because all these files will be part of one particular folder so we'll click on open folder and it will give you certain options where you want to save this so I will save this in on my desktop itself okay in fact on my desktop I want to create one more folder so I will say open folder again inside desktop I would I want to create a folder here we'll say this is I will have a short name so it's a course create and open so you can see we got our course folder here what I will do is I will zoom in inside my vs code so that you can see everything properly and again the recommendation would be to watch this on a bigger screen so that you know you can actually understand each and every line because a mobile sometime you will be seeing a font size and a small font okay I don't want to welcome I don't want to see this page every time I open my vs code so I will just uncheck this and click on close here and let's create our first file and this is where your going to write our first code right so we have already seen how do we install jdk how do we check the version on the terminal can you do that in this so vs code itself has a terminal here so you don't have to open the external terminal of course if you are using Windows you will be having an option of command prompt but let's click on Terminal here and you can see we have an option of terminal click on this one okay this is where you can enter your commands whatever commands you have the same command will write here so we'll say Java version and you can see I have Java 17 in this machine and Java 18 is already released but then Java 17 is the LTS version so when you say LTS it means long term support right so we'll use that and we'll also check if my Java C is working you can see javasa is also there now Java C basically stands for Java compiler as we discussed before so we need both we need Java to run the application we need Java C to compile the code right okay cool so we got the terminal as well I will just minimize this a bit okay so now let's create our first file so what we are going to do is we'll click on this particular file here and we'll name this as Hello dot Java now this is something you have to remember whenever you create a Java project you have to make sure that you give an extension or Java files basically you have to give an extension with DOT Java now different languages have different extensions and when you talk about C we say dot C when you say C plus plus language we say dot CPP when you say JavaScript we use dot JS in the same way in Java we say dot Java simple stuff right so you got our file here now vs code will give you some suggestions will ask you to install some extensions for Java and we'll click on recommendation okay it is asking us tension pack for Java which will help us it will give you some options and extra stuff again this is just an advantage which you get from vs code again not required if you don't want to install this extension I think I should go for it or we also have intelli code for AI assistance but yeah we'll stick to this extension again in your machine you don't have to install this there's no compulsion you can still write your Java code but maybe it will give you some suggestions and stuff in between let's see let's close this and let's write our code so extension will be installing behind this thing you don't have to worry about it so basically how do we write our Java code it's very simple actually uh first of all the question is what you want to do in this code do you want to create a game or do you want to build a rocket which will go to mass they will not be doing those extra stuff what I will do now is I will create a very simple file which will print hello world I know that's how you start a programming language so it is irrespective of which language you're learning the first thing you do is you print hello world so here I want to print hello world now how do we do that see uh if you in case if you are coming from other languages let's say python right so what you do is if you want to print hello world in Python you can write only one line and maybe Java is not that famous for Simplicity at the start of course when you build a bigger project Java is much more structured compared to the languages but then to start with it's not that easy so just bear with me okay so what we are going to do now is if you want to just experiment with Java if you want to play some if you want to test some code or stuff we have an option which was introduced in Java 9. so basically you can write your code in one line you can get the output example so even before we jump to hello world let me show you something so so we have we have a concept of J shell in Java so it was introduced in Java 9 as I mentioned so you can open your terminal and here let me just increase the size a bit so what I will do is I will click on J shell or I will type J shell and it will give you a j shell prompt okay now J in this case is Java so you can it make it should make sense now okay so what you can do in Json is if you want to experiment something example why do we write code why do we build applications so to solve problems right now it can be a very simple problem as well example if you want to add two numbers it's very simple you can say two plus three and if I say enter you can say we got five right so we got the answer which is five what if you want to do a subtraction you can do that so 9 minus 6 and you can see we got three so this is working but what if you want to print something so in this case what you can do is whenever in Java if you want to do something we use something called Methods at this point you don't have to remember methods on what the definition is just remove that there is a concept called Methods using which you can do something the only thing you have to remember is if you want to do something you need methods in the same way if you want to do something here I want to print something right so we have to use a method called print it is a inbuilt method in Java so you don't have to worry about it just use it it will work so whenever you call methods you have to pass the values so I want to say hey print but then it will say okay I will print but what you want me to print so you can mention that in this blanket so in this round brackets you can mention I want to print hello world now if you want to just print a number let's start with that first so let me just print a number which is six and uh in Java there's one more thing you have to put a semicolon at the end it should work in this but then remember this at the end put a semicolon that's how you mentioned hey something is getting ended so we're going to semicolon there and now what you can do is uh let's try to run this code now adults right adults are very important in your life in your program in life basically you still got there it says cannot find a simple call point it's not working okay cool no problem so let me just clear the screen now clearcom is not working ignore that you will say so let me open JSL once again and let's because I just want to keep it on top so what I will do now is if I want to print something and I want to print a value 6 at the end I want a semicolon as well now Java says if you want to call print print belongs to something right so it when I say it is available inside Java so the statement you have to always mention is system dot out dot println see there will be a point where you I will talk about what is system what is out at this point it's very difficult to understand so just bear with me this is a statement which you have to re which you have to use if you want to print something on the console okay so if I want to print something here just say system.out.print and whatever value you want to pass say enter you can say we got 6. but what if you want to print hello world I don't want to print six so if you want to repeat the same statement of course you can type or you can click on the up Arrow it will give you the option now here if you want to print hello world you have to put that in double quotes so whenever you want to do something with text see numbers are easy to use you can simply say six but if you want to work with a text you have to put that in double quotes the way I've done it here so in this double quotes you have to mention hello world right that's how you start a programming programming Journey right I will say enter and you can say we got hello world what if I want to print something else which is very simple you can say double quotes not been ready the Disco and if I say enter you can see we got the text so whatever you want to print just put that in double quotes and it should work and you can see we are getting the output as well so cool so we are able to do this with the help of J shell but will it work in this if I try to copy this in fact why to copy when we can type it so let's type system dot out dot print Allen and here we'll say hello world and let's give a semicolon at the end it was working on J shell but when you work with Java files because this is how you build a project right Json is not to build projects it's just for the experiment if you are learning a language which is good but if you want to make it work you can do if you want to build a big application say dot Java file and write your code there so let's compile this code now how do we compile okay so there are two steps you have to follow first when you write a code you have to compile the code and then you can run the code so to compile the code you can say Java C in the terminal and let me just increase the font a bit so you have to say Java C and then you have to mention the file name so first you have to be make sure that you are in a correct folder which is course you can see that this is a folder we have and then you have to mention Java C and then the file name the file name in this case is hello.java click on enter oh we got an error okay so it says something is missing you know something else was expected and you are giving this is not working so this is not how you write applications or code in Java file we have to do something else but at this point we have understood right how do you work with JSL so you can if you want to do something with JSL just write a code it will work if you want to add two numbers it will work you can't do that here we have to do something extra so let's solve this problem so we are not able to run this code right this is not working we want to get Hello World on the screen it was working with J shell because J shell says it's just a password statement I will work but then when you build an application when you run the application how exactly it works behind the scene once we understand that we can write this code so in your machine you will be having something called j v m Now by default when you say Java is platform independent is because of jvm because in your machine you will be having jvm irrespective of which OS you use it can be Windows Linux based Os or Mac or whatever famous OS we have it will have jvm installed and of course if you have jvm you can run Java application and if you don't have jvm you can't run job application okay so we got jvm here right now what jvm says hey I will work but then there's one question when you say jvm will work but jvm works on which environment so underlying we have your OS so this is your OSS and then your OS is working on a hardware right so you'll be having a hardware it can be a laptop it can be a desktopic in your phone doesn't matter you have a hardware on top of that we have an OS on top of that we have something called jvm now jvm says okay my job is to execute a Java code okay so whatever Java code you create JV will execute that but then jvm will not accept the code directly see what happens is Java goes in multiple steps in Java application when you as a programmer so let's say you are a programmer here and when you write a code you will not directly run write a code which will run on jvm okay and we don't even do that right we don't even write a code which actually works on the hardware and we don't even know the code which works directly on OS Java says if you want to make it work you have to run your code on jvm but then JV will not understand what you are talking about and one more one more thing we have talked about right Java is platform independent so what does platform Independence means it simply means that you can run your application on any machine okay so when you write a Java code it will run on any machine irrespective of the hardware or the OS but the only thing it requires is that machine need to have jvm so irrespective of which platform talking about if it has jvm your application becomes platform independent but jvm itself is platform dependent okay so example you can't run jvm on iOS so we can't say Java is platform independent when jvm itself is is dependent so yes your application is independent jvm is not so jvm need to be built for a particular OS and let's say if it is Windows in my it's so in my machine it is Mac but let's say if it is Windows we have jvm available on Windows so your jvm says hey I will run your code but you have to submit me not the Java code but something called a byte code so you have to convert your code into a unreadable format for the normal human bin and this is called a byte code because jvm only understands byte code but can you write byte code of course not you only understands English type of languages right so whatever we have returned the code we have a returnsystem.out.print it's English like language right so what you create is a Java code so this is your Java file or Java code and then you have to convert this Java code so this is Java code which is mode of readable this is not something which you can read so basically you have to convert this into a Java code or byte code so for that we have something in between here which is called a compiler now since this is a Java compiler we can call it as Java C which is Java compiler okay so basically you will write this code so this code will be getting compiled into a byte code and that byte code goes on the jvm it will run okay makes sense right so that means when you write something it will go into byte code and then byte code will run on jvm now the only thing is see your project can be a big project right here I'm writing only one file it can be 10 lines of code it can it can be 10 files it can be 100 files it can be thousand files out of all these files there will be only one file with jvm Will execute okay JV will start with the first file and then of course that file will depend upon the files and it will execute so jvm says you need to tell me what that file is okay so if you even if you have thousand files which you want to run in your project you have to give the first file so in this case what you will do is you will specify hey that's my first file so when you run the code you will specify the first file and also that file need to have something called a main method okay so as I mentioned before right we have print in the same way we have main so your file the first file need to have a main method so when I say first file it is not like you have to follow a sequence create this file then that's that file out of all the files in your project the execution will start from the first file which you specify okay and that file need to have main not just main it is looking for a specific signature okay so when is the signature it means the entire method signature the method name what are the values you're passing and all the stuff and of course all this term you will you will get used to in some of these sessions don't worry so first you have to make sure that you provide a main and Main has a particular signature again what each word means that we'll see later so the signature is this it is public static void main so you have to remember always pass this method I know there are four words but then this is the thing you have to remember and Main will accept some arguments some parameters so you have to pass string a now again we'll discuss this what this string is what is public static void we'll discuss that in some time but main is looking for the signature so your code will not work without the signature okay so remember this so that makes sense so what are the steps you have to follow so the first step is as a programmer you will be creating a Java file okay now once your Java is ready you will put that into compiler then compiler will compile it then that code becomes a byte file a byte code basically then you will take this byte code you will run that on jvm now once you once your jvm says okay I'm running the code jvm will look for this vertical syntax it should be there in your code and then you will get the output right simple stuff okay now there's a space in between here if you can see this is a space I have left left that space intentionally because we'll add certain things there so at this point just remember there's a space here there's a box remaining here we'll we'll add that box in in some time now once we have understood all those things it's time to expand this code now if you remember we have to write everything in a main right so this section should be a part of remember what jvm will ask you for JV will ask you for main so we have to put that into main so what your Syntax for main it's very simple it is public static void main we have seen that of course this words will Define that words in the upcoming session and once you understand all these terms you are Java programmer right and in this will say string a and that's it this is a syntax to follow now there is one more thing uh in Java now Java is object oriented now what is object oriented means we'll discuss that later but in simple terms object oriented means everything should be an object and to create object we need to class okay again we'll discuss all those things in the upcoming session but a class is missing here in fact if you compile this code this is what your Java say will tell you JavaScript says hey where is your class your class is missing okay let's do that so let's put that a class here and we'll give a class name at this point we'll keep the class name and file them same and we'll say Open Bracket and close and we'll give our indentation so basically we have to follow this thing always give a tab so every time you open a curly bag give a tab it looks good okay there's no compulsion it looks good and we love to build stuff which also runs and looks good okay this looks good right ignore this accent these suggestions uh these are the exercises coming from your vs code but you know that part in fact is it because it's happening of this extension let me just remove this so let's not use that extension because it will give you a suggestion it's good for development but not for teaching purpose so it adds extra text there okay cool so this is how you write your code okay we got a class a file name and then public static void Main in bracket you have to mention the parameters again we'll discuss that later and the only thing important statement here is system.out.print Hello World okay now let's see if this works let's create the screen and compile you can see there is no error but the moment I compiled if you can see on the left hand side we got this extra file we got hello.class now this is your byte code remember we have talked about a byte code here this is a file which got created automatically so when you create a Java file when you send it for the compilation it will create the byte code and the extension for the byte code is Dot class file the extension for Java code is dot Java file and now it is compiled done right now it's time to run your code so remember when I mentioned to run you have to use jvm and for that you have to use a command which is Java and you have to mention the class name not a file name this time just a class name the class name is hello and say enter you can say we got hello world so yeah that's how it that's how it works so simple stuff that's why we can able to complete our code and we are able to run this code now if you have done till now give a round of applause or a part on your back because you deserve it okay so this makes sense right so there's a gap here right so let's remove this Gap so what we have here so basically you know when you when you run your code basically think about you're cooking something in a kitchen right so where do you cook of course in a kitchen right why not in a hall or in a bedroom it's because in the kitchen to cook something you need extra things right example if you make a Biryani I don't know if you know the recipe even I don't know the recipe but if you make a Biryani you have to add extra ingredients right now those things are kept in the shelf so in the kitchen you will not just have that resources which are there in the platform but also you'll be having extra stuff now the same way if you want to run your Java application you need extra libraries and you also need a runtime now runtime is something which where you can run things right now you can run something in a jvm so that is part of run but then what if you need some extra libraries you have to Unix you need a environment as well so Java provides you one more thing so we have one more layer here which is above your OS which is called JRE so when you want to learn something you use JRE to run it now JRE stands for Java runtime environment and what is your jvm jvm stands for Java virtual machine okay so basically we have two things we have a jvm and we have a JRE now jvm is a part of JRE now what else you'll be having inside your jvm you will be having some libraries so jvm with libraries is a part of JRE so whenever you've done something it runs inside the JRE and jvm is actually responsible to run it okay and by default when you when you want to run your application on any platform it need to have JRE which will have jvm now not every machine will have Java C Java C is only for development purpose right so we can imagine this in this section uh so basically we have an outer box which is your jdk which is used by developers inside which you will be having something called JRE and in this you'll be having jvm so as a developer you'll be installing jdk jdk will have JD Jr you will have jvm but to run something on any other machine let's say if you have met the application you want to run this on your friends machine your friends machine will only have JRE and jvm jdk is not required and that's why we say Java is Vora which is write once and run anywhere right so you just write on your machine you can run it on any other machine provided you have jrd and jvm installed so that's how your Java behind the scene I hope it makes sense now okay so we were able to run this code and we understood something what is happening behind the scene right so basically we know now that as a programmer as a as a Java developer you write our Java code which will be having dot Java extension and that gets compiled by Java C and then when you when you compile the code what you get in output is a byte code which will be having dot class file and then you can run the dot class file in your JV I mean basically it's a part of JRE which will have jvm as well as a libraries lot of classes and then everything is a part of a JRE that JRE you will be installing on OS right so basically you need to have a hardware of course you need a machine to run right that machine will be having an OS and then OS will be having JRE which is which will have jvm but as a developer you will need one more tool which is jdk now when you install a new jdk you get the updated JRE and jvm with it okay so the version of jdk which we are using is is 17 which is LTS which is long-term support and in every new version you get some minor extra features right so even if you are watching this after 10 years or 80 years or five years you will be having new version of java of course next five years you will get 10 new versions and every new version will have minor updates but the base Thing Remains same right so whatever you're learning is actually from java one itself the system.pinterland is same in Java one so don't worry about the new updates the code concept will remain same okay cool now once you have this thing what next we can do now think about this world of course first of all why we build software we build software so that we can solve a problem now what problem we are trying to solve uh so basically we are trying to solve the real world problems uh with the help of virtual world example you wanted to buy something so you've got Amazon which delivers everything at home so you got Amazon is online service you want to to book a cab you got Uber so for every problem we have a solution in the virtual world right so banking everything is happening online now when you say you are replacing the real world with a virtual world solution so basically we need to work with one thing the most important thing now is actually data so basically we want to work with data so how will you work with data so basically you need data as well as processing on it so whatever whenever we build a project or when you build application what we are doing is we are doing the processing on data now this data can be coming from user or something so maybe you will accept the data from the user and then you will do some work with it maybe you want to store this data now where exactly do we store data so we store data in something called database so we have a database here in which you will be storing your data so whatever data you're getting from the user or from after processing so let's say we got a user here user sends us data we will apply a processing on that so we will do some work in between and then then at the end we might store data in database now this database is actually a permanent storage or a persistent storage which you say now when you say it is permanent that doesn't mean you can't change it of course you can you can replace the values as well but then the data stays there okay so if you just store it and if even if you shut down your machine the data will be stored example when you copy movies or games on your laptop it stays there right it will get deleted when you say delete but the data stays there so this makes sense right but when I say you will receive data from the user and then you will be doing some processing on that where you will store the data for temporary purpose of course database is there for permanent storage but during processing you have to store your data somewhere and that somewhere is variable so this is where you store your data now what is variable so imagine variable as a box and in this box you will store your data now what data you want to store your data can be numeric it can be text it can be image whatever you want to store so you can store that data in this variable now I'm not talking specific to Java in general user you want to store data you will store that in a box and that box will have a type to it it can be of type numbers it can be of types text or image or video so here we are trying to store data right so let's I want to store the number which is five so this box will have a number five and it will also have a name to it maybe I can give a name let's say a number it doesn't matter what name you give of course there are some restrictions on names but num works you can use any name which you like maybe you can create one more box here and you want to store the number itself but let's say a decimal value something like 6.5 you can do that okay and maybe you can have a different value to a different name to it maybe you can say marks or grades or maybe you want to have one more box where you want to store your name so I can do that I can store my name as Naveen and it will have a variable name so maybe I can say this is user so user is Naveen so basically what you do is you create a box and inside the inside the Box will be having values now this box are actually called variables so we can name this box as a variable okay and so what we got till now we got thought that a box will be called a variable and you will be having a name to it you will be having a value so name and value will be there apart from that you know Java is actually called strongly typed language now what it means is whatever variable you want to create so if you want to store the data of course Java says hey we'll accept the data but tell tell me also what kind of box you want a different type of data will require different blocks okay that makes sense so I will say hey Java I want to store a number so I want to create a box for me which will accept numbers so we can say okay this makes sense number will go there we can also tweet this as a number but this is a text right so in Java we call them as a string right so text will be having a string so this box type is string what about this box of course we say this is number this is also a number but in number as well we have a category inside it so one of the categories I will mention is this we can if you want to store numbers we can store that in something called in now int means integer okay so we can store data in the integer format but we cannot store 6.5 in it because 6.5 is a real number so int will accept numbers but white is called int why not natural numbers or whole numbers is because maybe you want to store negative values as well so remember your maths concept so we have negative numbers to positive values so everything will be considered as int so we can store minus 5 plus 5 that's your choice okay so this makes sense what I will do now is if I go back to the code of course we want to print hello world but we have done that now I want to store value so that I can do some processing on it now what kind of person think I want to do maybe I can I want to add two numbers maybe two plus two or five plus six simple stuff right but before that we'll see how do we store data even if you don't store data you can just go inside this system.print whatever option you want to perform you want to say three plus five that will work so you can just go back here and say compile and run so remember this thing every time you make a change just make sure that you compile it first so the first step is comparison and then running and you can see we got the output 8 just ignore this percentage symbol as of now I'll show this one more thing I wanted to mention whenever you print something let's say I want to print this number here as well I want to perform different options so you can write multiple statements for different operation you want to perform so you can say eight plus seven so we are doing addition here and the addition here let's clear the screen okay I have to first compile okay that's what compile run and you can see we got the value now the only thing you can see is the result are bit that together right we want to give a space of maybe I can train 15 on new line so what you can do is instead of using print you can actually use something called print Ln now print Ln means print new line so it will print and it will go on new lines so we can do that here as well so just say Ln Ln at the End Print Ln which is print new line and now compile run and you can see we got the value we got 5 and 15. so from now we'll not be using print we'll be using Ln println okay so again println means it will print it will go on new line so we got print Ln now what else we can do here so I don't want to perform the operation here remove that I want to store that value first so how do we store it of course we need box right so I can say num1 I can as I mentioned I can use any variable name so let's say num1 or to start with let's say num just to keep it simple and then we can give a value to it let's say value is three so remember you have to mention the variable name and then we have to mention the value for it and at the end of course you can put a semicolon so we can see we got num equal to 3 and we got semicolon so in Java after every statement you have to give a semicolon you might be thinking hey why don't we have a semicolon here it's because that is a block so we don't put a semicolon out at the end of a block the reason is it signifies that's the end here if you don't mention semicolon how do we Define that that's the angle so it's good to put semicolon at the end and anyway Java will give you either if you don't do that the next thing you have to do here is we have to also mention the type what type of num is this so we can say int now we have to say first you have to specify the type and then you mention the variable name maybe if you have learned some other languages where you put the type afterwards in Java we first put the value or put the type and then we put the name then we Ascend the value now this equal to here is called assignment operator because you are assigning a value so what it will do is it will take the value from the right hand side it will assign the budget to the left hand side okay remember this equal to will take the value from the right hand side it will assign the value the left hand side okay so we got a variable which is of type int okay so this is num which is a variable and then we can print it now let's only print the value that's it nothing much I will say save now once you click on save you have to compile the code and run the code and you can see we got three Okay cool so now can I create more variables we can actually so let me just rename the value variable from num to num1 and here as well I will print num1 okay and then I will create one more variable which is num2 is equal to so every time you want to get a variable just mention the type which is int or whatever type of variable you're creating at this point we are working with numbers so we'll say end num2 equal to we're going to assign the value which is 5 so we got 3 and 5 here what you can do is you can actually perform the operation here itself so against the num1 plus num2 that works so instead of adding values we can also add the variables okay so now we are what we're doing is we are saying num1 plus num2 and the addition will be getting printed let's try so I can just go back here and say compile run and you can see we got eight you know what instead of adding it inside this it's not a good practice what you can do is we can create another variable called result and that's right variables are free to use of course behind this engine is consuming a lot of memory but it's working right it's free as of now but later on we'll focus on how can you make your code more efficient so that it will use less space right at this point it works so we can say now one plus so whatever option you want to perform you can do that here and instead of printing and this is doing addition here we can simply print result so what you're doing now is you're adding the value and then you are storing that in a result and then you are printing it it should work let's try compile run and you can see we got eight so this is working so that's how you can you can create variables so remember this thing every time you want to create a variable you have to you first need a name for it at this point is num1 you have to also mention the type of it at this point this is int and you have to assign the value for it in the upcoming videos we'll talk about how can you create a variable which will be having a name with a different type and different value so now we know what variables are and how do we create them so basically in the last video we have talked about how to create a variable with the help of the data type and the value as well now what are the other options we have so we have worked with eight right do we have other options so basically we have multiple data types to store your data so let's talk about the first thing which is uh data type in fact there are two categories of data type which we can normally work with one is called primitive and the other one will be will keep it question mark okay at this point we are not just discussing about the second one the first one is primitive so basically in Java we have lot of inbuilt data types available which you can use and they are primitive now primitive simply means it is simple basic to use to work with so what about data you normally have normally we work with numbers we work with Point values we work with characters we work with true or false now for that we have this that category so we can divide primitive into four categories here uh the first way one the first one is of type integers uh the second one we have is float floating point value basically so we can say float and the third one is characters and the fourth one is your bullion of course when you divide this we have more categories to it but in general we have four we have integer float character and Boolean now when you say you want to store a range a negative range to positive range numbers normal numbers or natural numbers you can say in fact we can we can call them as integer itself because we are starting with a negative value so that goes into integer whenever you have Point values which that goes into float value whenever you go with characters like a b or any Regional characters you have all across the world that goes into character and then when it comes to true and false we go with Boolean now inside integer itself we have multiple subtypes to it uh we have a concept of byte we have short we have int and we have long uh for float we have another option which is of double and of course we have float and then carries characters are characters so in characters we can have different type of characters in Boolean uh the only values we can have is true and false okay uh so example if I go back to my code and if I want to create a different video table this time let's say float so I can just come back here I can remove this section and here I can say this we are creating a float value now how do we get a float value base so we can simply say float and then we can have a value let's say seven or we would name to it right so name we can have is marks of course we don't put in marks but let's go with marks and then we can go with a value now floating Point values would be having a DOT right so we can say 6.5 something like this uh some float marks equal to 6.5 that makes sense right okay cool in the same way we have also mentioned we have subtypes to integer we can have a byte format but why do we need all these extra formats for that let's understand uh some basic concept now basically when you talk about integers itself so if you talk about integer we have some sections to it so integer will have different part so interesting will have different sizes example if you want to store a normal number so in general we always go with int and int has its own value okay so when you talk about in the size of int is is four bytes so we can say 32 bits and then it has a range to it and you can find the range on the screen uh we also have a format which is let's say if you want to store a bigger value a very big value so in this case I'm I mean of course the value goes beyond that range we have to go for the long format now long supports eight bytes now of course when you have more budget it will occupy more values and it will have a range so of course you can find the range on the screen now what if you want to store a smaller number so of course int provides you that range but then what if you want to save memory you don't want to go for end range in that case we can have a short value now short is only two bytes so if you want to have a limited range you can find the range on the screen and then we have the next range which is of byte now byte is only one byte okay so eight bits and whatever you want to store you can store in that of course it has its own range now how do we calculate this range so we do with the help of it goes from negative values right so it is minus two raised to 8 2 plus 2 raised to eight minus one okay so which is 2 s to 8 is what it is okay starting with negative value right so just two is to 7 and 2 is to 7 and the range so how do we calculate version 7 is 128 so it starts from minus 128 to plus 127 so 128 minus 1 127 that's how you can calculate the range for all the other numbers and you can of course you have seen that on the screen so this is how we can use integer so which one to use so if you want to get a variable which is a which will have a value let's say five or six or maximum 127 you can go for byte now how do you get that variable it's very simple you use a type which is byte you specify a variable name let's say num and you assign a value which is eight or any other value which you want of course that should be in a Range and that's how you can store it now what if you want to have a float value now float actually gives you two different values or two different types now it depends upon which programming language you have worked with before example if you have ever worked with CVA by default we have a value which is float so by default we have float there now float only takes four bytes and it has its own values you can store and then we also have option of double which takes eight bytes now what you think which will be default value think about it will it be float or double by default in Java so default is actually double not float okay now why double is because see float are good when you have limited values when you have limited Precision because after your point value example if you have value which is 126.5678 or whatever values you have so float will have a limited Precision set here but on the other hand double will have a longer one right so double can have more values so whenever you perform scientific calculations or any kind of calculation if you wanted to make it fast and it should support maximum Precision you can use double there right and by default Java supports double so if you want to create a variable which is with double so you can simply say double and you can create a variable name num is equal to you can assign a value which is 5.6 so whatever value you want to assign but how about float now if you try that with float it will it may not work example if you say float and if you say a variable name which is num and equal to so when you try to assign 5.6 it will give you error it's because by default the moment you say Point values it will be considered as as double and if you want to say hey I don't want to work with double I want to work with float so explicitly you have to mention hey okay this is actually double value so you can this is actually a float value so you can put a f there right so 5.6 F that's how you define it's a float value for double you don't have to mention that now how about character so when you when you work with character and in other languages character will have a less bits but in Java it goes for two bytes so so in other languages like C you have only one byte it's because it normally occupies the integer range it normally occupies the character range ASCII values but then in Java we thought hey can we just have all the characters in the world not just English all the languages so Java actually follows something called Unicode not ASCII by default it will support Unicode and it has two bytes which is 16 bits so it has a bigger range right so this is a range it normally works with so of course you can work with any type of values you have and any type of characters basically you have and if you want to define a character variable it's very simple you say Cad and then you specify the variable name let's say character or c equal to whatever character you want to assign let's say I want to assign a character which is K now in this K in this case if you want to work with a character can we use double Quotes no double quotes is for string right for character we have to use single quote and you have to put your character to single quote and remember it will only accept a single character and next we have of type Boolean so if you want to work with Boolean Napoleon will only accept true or false okay no other there's no subtypes here only true or false now one thing you have to remember now in other languages it may represent it with the help of one and zero in Java we don't do that so in Java it does not it doesn't work with zero and one it is true and false this Knight like true is one and false is zero it's only true and false it is normally used for conditions if you want to check for the condition based on that you will do something that's what we can use two and false I mean think about computers when you say computer can think it's because of this true or false okay and how do we Define a Boolean variable so it's very simple you use a keyword called Bull and then you mention a variable name let's say B is equal to you either assign true or false okay you don't have to put a semi a single quota double quote because they are itself keywords so these are the options we have now if you want to see this in a code it's very simple you can just go back here and say okay byte is done let's create some variables with different type so ain't we have already seen so it's a number one this is how you assign integer value what if you want to go with a byte so you can say byte B I will say b y equal to now can I assign a value which is 129 you can say it will give you if I do that if I go back to my compiler compile you can see it will give you error it says possible lossy conversion from into byte so this is int which is 129 it will if you try to save that in a byte it will not work but what if you say 127 that's the last value you can store compile okay this is a problem with result let me just remove this section and compile no issue okay so this is how you save byte value what if you want to store shots you can say short sh equal to and you can assign value which is let's say five five eight any even number doesn't matter and then what if you want to work with long so long L is equal to now when you work with long remember this thing after every number for long you have to put a l at the end to specify that's a long number the thing we have done with float we have to put F in the same way long needs l and what if you can do here with float so you can say float f is equal to 5.8 okay let's try with 5.8 and let's see what happens if you try to compile this compile okay you can see it is giving you it says again possible logic conversion from double to float because by default 5.8 is double if you want to mention hey that's not double that's float you can put a f at the end that should work you can work with double as well Double D is equal to 5.8 same value but this time it is double and then what else we can work with we can work with character as well so we can say care C is equal to in single code you can mention the character k then we can go for Boolean which is Boolean in this case and you can say B is equal to True simple stuff right uh let's try to compile this of course we are not printing anything so we don't expect anything on the console compile or there's an error it says cannot find symbol bull okay my bad it is Boolean the only problem is when you work with multiple language at the same time you get confused somewhere so that that's Boolean notable so in the notes as well I need to change it so I will say this is Boolean cool and now let's compile this once again compile okay there's no problem and let's run this of course it will run but then you will not see any output right no output because we're not printing anything so yeah that's about this we have different type of data types we can work with and all these are inbuilt apart from this we also we also have some more data types which we'll see later they are not type of primitive but this is primitive data types and this is working okay awesome so now we know what what variables are what data types are what a size or range of each data type here and you don't have to buy hard them you will get used to it the more practice you do with this variables you will get used to when to use what but what if I remove that single code there let's try compile and you can see we got another we can't even put double quotes there it will give you as well and you can see you can't even put double code so you have to put single code because that's how that's what character represents and the character can be numbered as well so you can put a character here which is eight which is the character itself right digital also character no other okay so when you talk about data types and then values what kind of values we can have so example if you talk about this 9 or this eight these things are actually called laterals because they're literally the values right so what we can do is we can play with some literals here let's work with integers first and we'll come back to this so we are basically working with literals now okay now what kind of relatives we can have for example we can have direct values we have nine here and we can have any big value provided that goes into the range so whatever range we have it is a part of the range it will work so we have all these are decimal values right so it will have a base 10 what if you want to work with base two something like a binary format can we do this let's try it so I will say 0b 1 0 1. so basically if you want to put a binary number like 101 which is five so you can put B and then you can put a zero let me just drive this works I will say system dot dot println and then let me print num1 let's see if this works put your semicolon at the end compile there is no issue so let's run this code and let's see what's the output and you can see we got five so yeah that's the binary format Works what if you want to work with hexadecimal numbers in this case you can use x here and let's put a number seven e so e also supported right so let me just try compile run and you can see we've got 126 I don't know if that's the correct answer but you can verify there's one more thing which will introduce later in Java which is what if you have let's say this number and trust me I always get confused with number of zeros in any amount even if I mention a password or a pin or account number and whenever you have multiple zeros I always get confused so how do you count to the number of zeros here of course you can do it one by one but in Java we got this feature where you can put a underscore between numbers if you want to differentiate them it was easy to count right so you can see we are getting zeros and then we got underscore even this works let's try it out compiled one and you can see we got the output of course in the output it will give you a normal number but as a programmer it helps you for the number of zeros you are entering that so these are basically individuals right let's try with float this time so let's say this float and we are saying num1 and then I want to assign a very different value can I assign let's not let's go with double just so that I can avoid that F at the end and here I want to assign let's say 56. so basically when you try to assign a integer value to float it will work or double it will work because integer will get converted into double automatically when you save it okay uh okay that makes sense can I have different value let's say I want to store a very big number so I can actually use Epsilon e in between I can say 10 e 12 e raised to 10 and let's see if that works compile run and you can see we got the value as well here and it will adjust automatically based on its own implementation so it is assigning 1 0 here so there's basically 12 into 10 raised to 10 or 12 into 10 into 10 to 10 and then it will I just it's according to its own way and we have one we have already discussed about the Boolean it is only true and false there is no one and zero there uh don't try to assign one it will give you error let's try compile okay you can see we got there it says int cannot be converted to Boolean remove this okay how about character let's try uh so when I say character let's just see and I want to assign a character here which is uh say in single code let's say a now when you try to print the character uh this will work compile and run you can see we got a now basically character can be also treated as integer that's right we can actually perform operations I can say C is equal to C plus one so what I'm doing is I'm incrementing C I'm enhancing the value of it if it is a normal value value it's a 5 it will become six okay what if you want to do an increment of course we'll discuss this later what increment means but you can say plus plus to increment the value and if you try that with character compile it you can see that it is getting compiled and if I try to run this code we got a or because we are printing it there my bad I will just have to put that here so first we have to increment the value then we can print it so C plus plus implements changing the value okay it's not a language I'm talking about the operation on a c variable and you can see we got B so when you increment the value of a it becomes C it becomes B cool so basically all these things are called literals in fact there's a concept of string literals as well where you example when you talked about hello world that's a string that's string literal in this video we'll talk about type conversion and type casting so in the previous video we have talked about variables how to create a variable and then one thing is for sure in fact two things every variable needs a name so we have talked about it and every variable needs a type as well so if you want to get a variable for numbers you will say ain't float double or long in fact we have sub tabs as as well for INT and then if you want to store a character we can use care if you want to store a Boolean value we have Boolean option and then we also have a string which we'll talk about that later at this point we have a lot of different types now there is one confusion what if you want to change the type of a variable can we really do that so so unfortunately we can't we can't change the type of a variable but then can you assign a value to other type example let's say we have a variable which is of type byte so I'm using a byte variable which is B and it will have a value now of course we have talked about the range of byte as well you can have maximum 127 plus I mean plus 2 127 this is the max value you can accommodate here but can you assign a integer value that's a question so what if you have a variable of type int a and the value of a is 256. now what will happen if you try to assign this a value which is 256 into B something like this what if I say B is equal to a so what I'm doing now is I'm assigning a value of integer to byte now unfortunately this will not work but what will work the reverse will work what if you have a variable which is a and then you are trying to assign a value which is B this will work so what happens is the type of a is integer right it has a bigger range and byte has a small range so the compiler says hey you're trying to assign a integer value to byte and at this point you will lose you might lose some data so basically we are trying to convert it and it is narrowing it right so we are narrowing the size of it on the other hand if you talk about this operation here we are widening it because this the range of byte is small and integer is a bigger one so of course you can put a small items in a bigger box but if you put a bigger item in a small box it will not work right okay cool so that makes sense right but then what if you really want to do it you want to convert a integer value into byte see at this point if the value of a is not 256 what if the value of a is let's say 12. now the value of a is 12 which can be accommodated in b as well so at this point even your compiler will say hey I don't know what your value of a is but I will not allow you to store the value of a in B because a is integer and B is byte so in this case you can say hey compiler give me one second here what you can do is you can say B is equal to you can convert a you can do a conversion by saying you can give a bracket and you can mention the type in which type you want to convert you can say byte and you can mention the variable name a so what you're doing is you're converting integer value into the byte format okay now this constant is called casting so basically this is actually a conversion but explicitly you're doing it so whenever you do explicit conversion it is called casting so when I say explicit there should be implicit as well right and that is correct we have some conversions which are implicit example if you talk about this particular operation here this is a implicit conversion basically you are trying to store a byte value into a integer variable so it becomes integer when you assign it right so this is a conversion okay so when you do it explicitly that is casting when you're disopening if it is happening automatically that is conversion now this makes sense right now is it means you can do it with anything not exactly there are certain types example you can't store a character value into a Boolean value okay because Boolean only supposed to end false and character supports all the characters so you can't do those conversions but anything in the range of integer float double actually works example uh we have talked about integer and byte if it is long as well that completely works now what if you have a scenario where you want to store maybe of type a float value now let's say you have an integer variable here which is int let's say x is equal to and you're trying to assign a float value now float value is let's say 5.6 of course I can create a float variable I can assign this value to that or maybe this is double because we don't have F so let's say this this is double and then you're trying to assign that to integer now what will happen is in this case if even this will not work so what you have to do is you have to apply you have to convert it so what I will do now is let me just create a float variable here so I will say float f is equal to 5.6 and remember you have to put F at the end now if you try to put f it will not work so what you will do is you have to explicitly convert this value into integer so what you're doing is you're assigning a float value to an integer so you're converting it now what you will lose here so at this point you will lose your point values so whatever value you have in after that point you will lose an example the value of five point which is 5.6 so here when you convert that it becomes 5 so in the X the value you will be getting is only five cool uh so it makes sense right so for float it will cut the value but coming back to byte now what will happen when you talk about byte here what do you think we don't have Point values so if you have a bigger number example let's say this is not one this is not 12 let's say we have a bigger number here and the bigger number is let's say 257. now what will happen when you assign 257 because the range ends at 127 you can't go beyond that right so in this case it will use a concept of modulus okay or modular operator so what modular will do is it will divide this number so whenever I try to assign a integer value to byte which which we are doing here so when you are trying to assign a integer value to a byte it will convert that into modulo which is it will divide the number a which is 257 and it will find the percentage okay so 257 percentage which is a modular symbol and it will divide it with a range okay which is 2 256. because a complete range of byte is 256 right from minus 128 to plus 127 and then the remainder you will get here is one cool so the answer here when you try to do it with the 257 it will be one I will show you that in the code so coming back to the code let's try all this operation which we talked about so what we'll do is we'll start with a integer value we can say or maybe we can start with byte so we can say byte B is equal to and maximum you can have is 127 let's take that value and let's try to print it here so we can say system Dot dot out Dot println and here let's print the value of B so when you save that and when you go back here when you try to run this so we say Java C and Hello dot Java comparison done let's run this code I can say Java space I have to run with the class file which is hello and you can see we got 127. now what will happen if you try to do that with 257 it will not work first of all it will say either you can see we got data it says incompatible types because it is integer type now 257 okay so you can say 127 here now next what I'm going to do is I want to create a integer variable here which is int int n is equal to and I can assign a value maybe let's say a because we have used that so we'll say a and then the value we are going to assign is of B so can you assign a byte value to integer because this is this is where you're doing converting and if you try to print the value of a let's try that in fact there's one more thing which is which came recently in Java so basically you know we always do two steps right first you compile the code and then you run the code right so you compile that with the help of java C and when you compile it you see you say hello.java and when you run it you say simply say Java and hello you have to do two steps right so when you're into learning phase why to do two steps you can actually do a single step again not recommend it for a big project or not even for a small project don't you don't even try that in your company for learning purpose we are doing it so basically we got a shortcut we can say Java space Hello dot Java directly maybe it is getting influenced by the other languages where you simply mention the command and a file name no need to mention compile and run Subway step let's try this and you can say it works so you can see we got 127 it is perfectly working and just to show you that things are happening behind the scene uh the value is getting changed you can see we got 125. so from now from whenever you have single file will use this but remember this is just a shortcut ultimately behind the scene we need to do this okay I mean when you build a project first you have to compile the code then run on the code this is a shortcut and one more thing this will work only in the recent version of java I guess from java 14. if you have an older version of java this might not work so if it's not working in your machine that means you have an older version upgrade your jdk version Okay cool so now what I will do here is we can assign the value right but what if you have a integer value let's say 8K and now you are trying to assign or maybe let's let's get a byte value byte K is equal to a and let's comment this section instead of saying B I want to assign a value which is 257. now even if you have a smaller value let's say 12 even this will not work because ultimately the value the I mean the type of a is integer right you can't assign a integer to a byte format and if you run this code you can say it will give you error it will say incompatible types so we got one way which is you can just cast it with byte so we are doing a typecasting here so when you run this code now it will work you can say we got 12. now we got 12 because it is a in a Range of bite what happens when you have a bigger value which is 257 now this is out of range right so you can come back here run and you can see we got one as I mentioned before it will perform a modulus operation which is actually finding the remainder so it will divide this number by the byte range which is 256 it will find the remainder which is one I hope this makes sense right so we were able to do the byte conversion in conversion this is working let's try with float now so let's say we have a float value which is f and the value of float is 5.6 F at the end because when you look at a float variable you have to put at F at the end and then we can create a integer value we can say int let's say this time we'll go for T doesn't matter what variable name you use and then if you try to put F there and if you try to print the value of T let's see what happens you can see we got error again the same thing it is it says there might be loss of the value from this conversion but then I want to do it I want to convert that so we can put a casting again and we can say float here and if you try to run this code so you're trying to convert that into integer right what's wrong with me and let's try now and you can see we got five so we lost that point six there now this concept of casting is actually very important when you start working on the oops concept object and the further concept this casting will be required there as well at this point we are doing casting on primitive values but later on we'll also work with objects okay now we have talked about casting so what is conversion so conversion is automatic or we can say conversion is automatic conversion and casting is explicit conversion so when you specify something it is explicit cool now there's one more concept which is type promotion so what are what what do you mean by type promotion so let's say we have a byte value and the value for byte let's say b or maybe let's say a and the value is 10 here okay and then we got a byte which is B is equal to 20. and then when you perform the operation maybe maybe this should be 129 let's let's say this is 30. now when you perform an operation on this let's say a into B now whenever you do a into B it is what 300 right so when you say 300 here it is going out of byte now can you save that into integer can I say int result is equal to a into B now what is happening here you're performing an operation on buy so when you know Java says hey we will promote you so this value will be promoted into a integer value okay and you can store that into integer so this is called type promotions let's run this and you can see we got an error because we are printing T here let's print the result and run and you can see we got 300 so I hope it makes sense what is Type conversion and casting in this video let's talk about operators that's right you can perform operations on different values in fact we have done that before but let's expand the examples and more operators so basically we can add two numbers we can subtract two numbers we can divide two numbers we can do we can multiply two numbers and also we can perform an operation so that you will get a remainder of an operation okay so when you divide a number and if you only want a remainder we can do that as well so let me show you those things with a code here so what I will do now is first of all let's create two variables and of course we can name any variable name but let's go with integer num1 and the value it will have let's say seven and then let's have one more variable here which is let's say five uh num2 is 5 and then I can perform some operation and I can save that operation in a result so let's say if you want to add two numbers we can do that we can save the output in result and then to perform the operation we can use a plus operator that's right we can actually use a plus operator to add two numbers and then once you have done that we can simply come back here we can say system dot out Dot println and here we can print the result and just to see if it is working and as we discussed before this is not a good way of running a code you have to first compile the code then run but we got a shortcut I can simply use Java space file name which with DOT Java say enter and we got 12. so when you say 7 plus 5 we got 12. cool now what I will do is uh so we can add two numbers what about subtraction let's try that so I will say minus symbol and run you can say we got two so just talking so you can add two numbers we can subtract two numbers how about multiplication we can do that so we can say star num1 into num2 come back here one you can see we got 35. so 7 into 5 is 35 that work that is working how about division uh let's try that so the moment I said division we have to use a slash okay so we don't have a division in fact that's how we do right we say underscore or something obviously slash so yeah we got uh Slash let's try and you can see we got one that's right one is a question here right so when you divide seven by five you get two things one is a question which is one and then you will also get a remainder now in this case when you use slash you will only get a quotient not a reminder so how will you get a remainder in that case you have to use a modulus operator so we can use when you say percentage symbol like this it is a modulus operator that what it will give you is only a remainder okay so if I run this code you can see we got two to the remainder here right now we can use any complex numbers as well whatever number you want to perform with and I would suggest you to try certain examples so that you will get the get used to these operations now all this operation actually makes sense right these are very simple stuff to understand can we use some other operators as well let's try what I will do now is let me just remove this result for time bin or maybe I can just comment this part I also don't need this num2 let me only focus on the first variable which is num1 now let's say I want to uh add this number one by two okay so basically the value of number is seven I want to increment the value by 2. so what we can do is we can say num1 is equal to so basically we have to use num1 which is number one in this case and we'll add a value to it which is two and then whatever new value you will generate after this will be assigned to num1 okay that looks cool right uh let's try this I will say num1 print num1 and let's run this code you can see we got nine so this is working so seven became nine so just walking right now when you know that you are adding the number by itself okay uh what you can do is you can just use a shortcut now what that shortcut says is let me just comment this section here and let's write it fresh here so I will say now one see you want to increment the value of num one by two right you want to add Itself by two so you can say I want to add myself but how much value so I have to say equal to 2. so what we are saying is we want to increment num1 by 2. even this should work let's try or we got an error because I forgot the semicolon and let me just clear this run and you can see we got nine so this is working cool now it's not just tradition we can do that for any other operation maybe you want to subtract a number by two so we can say subtract equal to two so it will reduce the value by two you can see God we got five we can multiply with some other number maybe I want to multiply the same number with another number and assign it so we got 56 7 into 8 to 56 we can divide as well and so you can use slash and all the operators which you have awesome so this is shorthand now going back to the addition operator and then let's say if you want to add it by two we can simply mention the value 2 right we can also add it by one so in this case if you say add it by 1 the value of num1 is 7 and then you are adding it by one you can see we got eight now in fact there's a shortcut for this as well what you can do is instead of using num plus equal to 1 you can also say num1 plus plus even this is possible let's remove the space in between so even this is possible you can say num1 plus plus which is incrementing so what we are doing here is something called increment and if you're on this chord you can say we got eight so this operation let me just make it one so all these operations are similar okay so why I'm saying similar why not same I will discuss that in some time but this is similar and maybe if you have done that before as well when we talked about characters you know we remember C plus plus we've done that so yeah we are incrementing it as well now not just incrementing we can do decrement as well so instead of saying num plus plus we can also say num minus minus so what we are doing now is decrement so when you say plus plus that is increment let me comment this part and here I will say num minus minus now why you have to comment this but I want to run this so we can say num minus minus let's run this code if say this works and you can see we got six so that's how you can perform this addition subtraction multiplication division and increment decrement in fact we have also talked about modulus which is for finding the remainder okay now that was about increment decrement right we have talked about that now instead of using plus plus we can actually use something called let me just write it in comment so instead of saying num1 plus plus we can also say plus plus num1 you know just to simplify this example instead of saying num1 everywhere let's say num so that maybe you will not get you will get confused by that one extra symbol there which is one uh just ignore all this operation just focus on this part which is numb and numb also remove this also remove this let's see if this is working okay this is working so basically we can also say instead of saying num plus plus we can also say plus plus num now what's the difference between these two even this also work let me just try that and show you you can see we got eight we are incrementing the value even this will give you eight but what's the difference what's the difference between writing this afterwards and writing before the variable so basically we have a different names to it so whenever you perform operation after the variable it is for increment or decrement so when you do it post we say it is post increment and when you do pre before the variable it is called pre-increment okay so when we are doing it afterwards we say post increment when we say before it is doing pre increment now you might be thinking hey ultimately the value is same right so even if you pull even if you do this or this both will increment the value then why we have to defend Syntax for it why not just simply one the answer is it behaves differently when you try to assign a value when you try to fetch the value let me repeat these two operations will behave differently when you try to fetch the value okay let me tell you what I'm talking about let me show you here let's come back here and let's say I have a different variable which is ain't and then I'm storing that in result and what I'm storing is num or maybe I will say plus plus num okay now think about this and maybe you can pause the video or whatever you want to do just think about the what will be the value of result okay I will say result here to print and maybe you have done your thinking let's run this and you can see we got eight so what is happening here is the initial value of num is 7 and then you are saying plus plus num that means you are incrementing it that incremented value will be assigned to result okay and then you got result which is eight okay so initially I've also mentioned that this two will be incrementing so we can also say num plus plus even this should be doing the same thing right let's try oh there's a Twist here so plus plus num and num plus plus are not same I mean it works same with their individual statement but when you try to fetch the value from them when they are performing the operation they behave differently so let's understand what is called pre-increment and what is called post increment so whenever we use assignment operator with this operation what it will do is it will check hey we have an option which is num plus plus but that's a post increment so first it will fetch the value of num let me repeat it will first fetch the value and then increment in fact it looks like one statement but we have two statements here right in fact we have this statement here num one equal to num plus one so what it will do is it will first fetch the value of num which is 7 in this case and then it will increment the value of num and it will make it eight okay but then what is assigned result is 7 itself and then you are printing that seven here okay but what will happen when you say let me just write it here so we'll say end result is equal to plus plus num in this case what will happen is it will first increment and then fetch the value okay so it will first increment itself and then it will fetch the value and that is what is going to result I hope this makes sense right so this is how your code works when you say S Plus num and num plus plus in fact we'll do some more operations with this once we get started and it will make much more sense later so in the operations we have talked about other thematic operators till this point so whatever operation we have done it is for the arithmetic purpose right addition suppression multiplication division but what if you want to compare two values what if you want to have compound conditions so we are talking about operators right so we have talked about arithmetic operators where we can do calculations we can add two numbers we can subtract two numbers in fact we have also seen how do we increment a value and decrement right now it's time to enhance and go towards something called relational operators in Real World what we do is we basically have uh we compare We compare things right so it can be comparing two values and if you can check with the help of less than uh you can check with the help of greater than you can also compare with two values right if they are equal or not so we use something called equal to there right uh what if you want to negate something what if you want to find a negative eight example uh if you have a True Value maybe you want to make it false so in that case we also have to move from True to false right so basically we want to implement this with the help of operators in programming language now specific to Java if you talk about we have different operators we can use less than to compare with the help of less than or less than uh the first value should be less than second value if that is true of course you will get True Value we can also check for greater than we can also check for equal to now the only difference is when you talk about equal to in programming specifically for Java equal to means assignment operator example let's say if you have a value which is 5 and you want to assign that to a particular variable let's say x so you say x equal to 5. so this equal to symbol here actually becomes your assignment operator but what we want here is we want to compare two values right so that's why we use double equal to so in programming in Java we use double equal to come to compare two values what if you want to check if two values are not equal in that case we can say exclamation and equal to okay that's that's what you say it is not equal to so this is comparing for equal and this is comparing for not equal and this is of course less than this is greater than and you can also combine example if you remember in mathematics we use the symbol Which is less than equal to in the same way of course we don't have this key on the keyboard so we have to use less than uh so we have to use less than equal to for this particular combination uh we can also say greater than equal to right so we can use all this combination which comes up as a part of a relational operator now basically when you compare this to what your example let's say if I want to compare if 7 is less than 5 so this will return u a value which is true or false so it will be either true or false of course we got a static value here which is seven and five so the output for this will be of course false but if you say 5 is less than 6 yes that's true so that's how you define uh that's how you compare two values and that's how you get the output the point to remember here is the output which you're going to receive is a Boolean value okay now how do we Implement that in a code so let's go for that so let's say uh we have this our we have our code here and in this what I'm going to do is I have two values let's say integer I can say x equal to 6 and then I got Y is equal to 5 and now I want to compare these two values the way you can do that is of course when you compare these two values you will get a Boolean value so I can say Boolean uh against the result is equal to now how do you store a value and how do you compare so first of all storing is easy you can just assign the value but how will you compare in that case you can say x is less than y and of course that's available on keyboard so you can directly use it and what I want to do is I just want to print uh the output here so I can say system Dot out dot println just for printing stuff and then I can just say result now the moment you say result it will print the output and of course you have to compile this code and the way you can do that is by saying Java C demo.java enter okay there's no issue with the compiling because the syntax looks good and now I will just run this I will say Java demo enter and you can you can see we got false now why we got false is because the value for x is 6 and the value for y is 5. so the first value is lessons is not less than second value and that's why we got false greater than simple here it actually makes sense because that's a oh remember every time you make a change first you have to compile the code and then you can run the code and you can see we got through this time so basically that's how you can compare two values and you can print it but what if these two values are equal so let's say we got 6 and 6 and now if we try to run this code of course compile and run we you can see we got false okay but then they are equal right so maybe I want to check if two values I mean if the first value should be either greater or it should be equal so we can put that uh equal to symbol here as already discussed and now I can just compile and run and you can say we got true so this is working we can also check for less than even that works because it's equal right and anyway we have equal so it should work so we got true uh we can also check if two values are not equal example let's say I just want to make sure that X and Y are not same and the way you can do that here is by saying not equal to so when we say not it simply means it will uh when you say exclamation it simply means dot so not equal to Y and with this if I try to compile and run you can say we got true so even this is working but what if you don't want to check for less than and greater than you want to check if the two values are exactly equal so in this case normally in mathematics we use equal to but that will not work here so you have to use double equal to so you are comparing x with Y and they should be equal clear the screen compile and run and you can see we got false the reason we got false is because we have both the different values so if I make it same now and if I run this code compile and run and you can see we got true so basically uh we we have different operators and this will be used a lot in the upcoming sessions uh so it doesn't matter what type of programming you do see when you say your computer has a brain you know we have to decide based on some values of course I have just checked it with integer you can try it with float as well in fact maybe I can try with double so I can simply say double here and I can say this is 8.8 and I can say double which is let's say six maybe nine point eight and I can just check if they are less than equal to and of course I want you to try different values there and compile No it should run and you can see we got true but if you check with the greater than symbol this should give you false and we got false so basically whenever you have a primitive value uh this actually works uh maybe once we start with string we also try to compare the string values and let's see how that turns out to be but this is your this is how you do your traditional and traditional operator checking with the help of relational operators we got less than greater than equal to symbol which is double equal to not equal to that's one yeah so this makes sense right but what if you have two conditions example let's say I got this values and now I have int a equal to 8 and int b equal to 6. so I want to check if I mean I want this x to be less than y and I also want this a to be less than B I want to check both the operation and the thing is when we say I want to check both the things should be true then only it should print true how will you do that how will you combine two different uh operations or two different checkings so we are done with the arithmetic operators and relational operators right then we had one question what if you have two relation operators and which will result in true or false right uh and then you want to combine them so you want one true or false out of two or three true or false so what I'm saying is let's say I have two variables here which is x y and also we have one more commission which is a and b and then I want to check if I mean I want X to be less than y and I want a to be greater than b uh this might result in two and true and false or it might result in true and false what I want is the output of I I want is I want output of this two combined can be either true or false but I want the combined output irrespective if they are a true or false individually so how do I combine this now to combine these two different conditions we can use a logical operator in between example uh this logical operator can be and or I mean and the second option is R so it can be and and all now what exactly it means see if you want that your output to be true so let's say if you are out if you want your output to be true so you can use if you can say Hey you know I want both of this to be true then only I want true or you can say if any of this can be true I want it to be true now this is very important in terms of computation So when you say your computer is smart it's because of a computer can decide based on multiple conditions so to do that we use something called a logical operators so what we do is uh again this concept is coming from mathematics is not a programming concept it is coming from mathematics where you can use something called a and operator we can use odd operator or we can use a not operator which is which is the exclamation so the and operator in programming we represent that with the help of and and the odd operator so we use Ampersand for and and we use a pipe for odd so we can use Ampersand which is and we can use Pi property which is or or we can use the exclamation mark for not okay so how do we decide what could be the output of two operations so let's say the first operation gives you true the second operation also gives you true so maybe you can come we can do that with the help of these two conditions if both are true then what should be the output so the output will be true again I'm talking about in terms of and operation if one is true one is false it is false if one is false I mean the first one is false second is true again it is false and if you want if you have both of them as false again it will give you false now when you want such type of output when both if both are true then only should be true then we can use something called and and operation here but what if you have a different set of my set of things in your mind uh let me just divide this and the second set would be let's say if the first one is true and second one is also true you will get true if the second one is true so the first one is true second one is false it will give you true if the first one is false second is true it will give you true and if both are false it will give you false if you have a condition or if you have a requirement where if any of them is true then it should be true so example if you can see if any of one so of course both are two that's great but if even if you have one true that's true even if you have one true that's true even if you have one true that's true right but if if you have both false then it will be false now if you compared with this and when you have both true it will give you true if any of them is false you can see if there's a false you will get a false here so this is your and and odd so that's how you can combine we have one more here which is not now not basically used to reverse the thing example if you have true and if you want the output to be false and if you want if you have false and if the output you want is true then you will apply a not operation now apart from this we have one more which we have not talked about which is xor again we'll talk about that in some time but then we have three basic operations here we have and or and not so basically how you combine this if you want to do that in programming of course you can also use and operator or pipe or you can use not directly the other option you have is something called a short circuit so instead of using and you can use double and instead of using or you can use double or and not is as it is we don't have to do Nots there and now what's the use of this so first of all let's talk about and so let's say if you have X rest X is greater than y and if you say and here and you can say a is less than b so we have these two conditions here and I want both them to be true and that's where you want the output to be true so example if you have a Boolean value which is R here so you can say R is equal to X is greater than y and a is greater than b if in this case if both are true the output is going to be true right but if you what if you use R here so if I remove this and if I put up R here if any of them is true it will give you true as simple as that okay now why do we call it a short circuit the reason is let's talk about R in R see the point is if even if you have one true the output would be true right so if the first condition itself is true it will not even check for the second one Beauty right I mean you're saving your time that's why it's called short circuit you are saving the time if you are taking a shortcut and you will say hey then what happens when you have and here so let's say let me put and now in this case even if so let's say first one is true of course it will check with the second one but what if the first one is false if the first one is false it will not even check with the second one because the output is going to be false right so that is why it is called short circuit okay so from this point we are always going to use short circuits and an odd instead of using a single Ampersand so it can be you can say double Ampersand for and double pipe for or and of course not will remain not so example let's say if you already have a Boolean value which is R so I can say R and if if you want to save that value in s so whatever value of R you will get you will save that in s example if R is t s will be t but what if you put a Ampersand here so but what if you put a exclamation here so in this case if this is true if R is true it will give you false it will give you a reverse value and this is very beneficial when you start working with the uh project where you have future you have a data and if you want to do something related to the data So based on the data you will change some you will do something you will change the condition all the stuff okay this thing looks cool here but how will you implement this in the actual code like that's what we're going to see now so if you can see uh I of course I have to go with the same variables I can say int uh X is equal to let's say seven and in fact you know uh let's let's do it on your line and I will say int Y is equal to 5. so we got these two values and then apart from this I want two more variables let's say A and B so I will say int a is equal to 5 again and end B is equal to nine I'm just getting random numbers here and what I want to do is I want to save the data in a Boolean variable so I will say Boolean result and let's see what the value you're getting so first I will declare it here and I will also print the value for this okay so basically we are trying to print result here but then I want to perform some operation on this so I can say if x is greater than y okay I'm just checking for one thing and now you know we have done this multiple times I will just compile this code and run and you can say we got true because the value which of X which is 7 is greater than the value of y which is 5. but then I want to put one more condition here and I want both of them to be true if I want to get the output as true so what you can do is you can use Ampersand double Ampersand here and you can say a is greater than b now if you can see the first condition is true okay this will give you true but the second one the second operation here will give you false now what do you think is the output think about this and let's see now the output which I'm expecting here is false let's try and yeah we got false is because the first one is false so the first one is true second one is false so in this case you both need to be true example if I make this as less than of course you should not check change your condition you should change your values but just we are learning into we're doing we are learning right so it it's fine it's fine now let's compile this code and run and you can see we got through since both are true it's true but what if you use a pipe here now pipe simply means R right so even if you are so let's say that both the conditions are true now and both these operations are true run and you can see we got true but what if let me make the first one as false right so you can say we have 7 is greater than 5 so this will be false but the second one is true right but still if you compile this and run and you can see we got true right so this is how you use and and all when you have multiple uh relation of course you can add more you can also add one more pipe here by saying the value of a should be greater than one that's your choice how many pipes you want to add compile and run and you can see it works uh so this is how you use and and or what about not operator here so example let's say I will only say a is greater than b now what do you think what the output is so if I compile this code and run and you can see we got false that makes sense right because the value of a is smaller than value of B but what happens just before printing result I'm saying not so whatever result of whatever value of result is we are just we are just doing the opposite of it if this is true it will give you false if this Falls it will give you true and if I compile and run you can see we got true it's because this was false the Oliver once was and now when I say exclamation result it will give you true so as I mentioned before it's very important once we go forward towards conditions I mean if else for loops so this multiple conditions or multiple operations which will result in true or false will be very help very helpful in this video we'll talk about conditional statements now basically what happens you know when you say your computer can think of course we have a lot of different Technologies now we have AIML right which which can help your computer to think right uh we are saying AI will change the world but if you go to very basic of AI or any computer I can say AI is an advancement in terms of computer which can think but if you can go to the base of it we have one important thing which is the flow is depend upon true or false example let's say let's say when you execute a code so what happens is whenever you execute a code the flow goes in this way let's say there's a flow going on and suddenly there's a point here so that let's say at this point so this is a flow of the code and at this point you decide okay if the condition I mean if based on something if it is true I will go this way if it is false I will go this way or maybe based on some other condition I will go this way so what happens you know your flow of your execution actually changes based on what you decide at that point and even if you think about Tesla cars which is uh maybe I need driverless cars but which moves and based on some conditions the speed will increase the based on some condition the speed will decrease based on some condition uh the the car will take a halt but if there's a object in front of a car at that point the car will stop right so this is a point where his decide hey it's time to increase this speed of a car so maybe you can say based on some condition let's increase the speed of a car uh decrease the speed of a card or stop the car so based on some condition you decide the another flow now how will you do that in programming and that's where we have this option of conditional statements so what you do is you write statement statement statement and then you decide I will execute this bunch of statement based on some conditions so here we'll be having a condition if the condition is true and of course we have talked about Boolean values we have seen relational operators logical operators if you get true you will execute this particular block here I mean this block if it is false based on something I will execute this set of lines right so should we execute this lines or this lines will be based on your condition now the way you can do that in programming with the help of if else now this is similar to most of the languages if you are coming from other any other language maybe you have seen this if not don't worry I will talk about if else in this particular video to understand that let's head towards our code here and as you can see uh we have we have a demo code and basically by using the same file here I want to basically do something based on a condition so let's say I have a value of x here which is 8 okay a simple value maybe I want to print hello or buy based on some conditions so so I will simply say system.out.print and then I will print X here okay in fact I want to I don't I want to point hello and then I also want to print buys I can just reuse the code which I've written here with a proper indentation yeah and then I want to say bye now of course if you execute this particular code here if I say demo dot Java and run you can say we got hello and buy both what I want to do is I don't want to execute both the statements I want to execute hello based on some condition if that condition is false I don't want to execute hello I want to execute the buy statement so at this point I want to decide now based on what so for this example I want to test based on the value of x so I can say if the value of x is let's say greater than 10 maybe I'm just taking an example here if the value of x is greater than 10 in that case I want to print hello so and then if it is false I want to execute the Buy okay so maybe at this point let me just comment this section let's say I just want to print hello only when the value of x is more than 10 okay and to start with let me set the value of x as 18 which is greater than 10. so what I want is at this point so we can draw them this we can say okay the value of x is 18. so if you go for the condition so if is a condition here or it's a condition statement it will check the point here which you mentioned or with the condition which you mentioned if it is true it will execute this statement or it will not execute the statement now of course that's true so if you go back here and compile and run you can say we got hello so this is basically the Syntax for if so how it is syntax we have to use a keyword called if and then in the bracket in the round bracket we have to mention the condition see ultimately if is not concerned what type of operation you are doing here if is just concerned is it true or false so even if you give a true here if he's basically happy okay so if don't want a operation that if says you give me true or false I will execute example if I say false here and if I compile run you can say it will it is not printing hello why it is not printing address because we are saying false so when if C is false here it says okay I don't have to execute the block whatever is written under if okay so there's a space here if you can see again that is optional even if you remove that that perfectly works because Java is not dependent on any indentation uh by any chance if you're coming from python in Python we do that we give a tab to specify that it's an it's a part of the eve Block in Java we don't do that okay I could have simply used a meme from Avengers you know it says we don't do that here okay anyway so we you can see we got false and we are printing hello that perfectly works I mean of course it is not working because we got false but if you make it true it will surely execute compile run and you can see we got hello right now uh what if I just want to go back to my condition the value of x should be greater than 10 and for this as well of course we want the value to be true and it should print hello and it's working but what if the value of x is not 18 it's 8 which is false right 8 is not greater than 10 so it will give you false and that's where you will not see hello and you can see we don't have hello okay so that's how you can use if of course you can put multiple conditions here if you remember we have talked about logical operators so we can say uh the value of x should be greater than 10 and I want one more operator and so I want to condition basically and I want X to be less than 20. so I want the value to be in between 11 to 20. I want 20 to be included so I will say equal to so the range now okay we can also comment something in programming with the help of double slash uh which will not be read by the programming or the compiler so the value accepted value for a x now is from 11 to 20. uh because we are saying greater than 10 which is the value above 10 which is 11 and less than equal to 20 which is including 20 okay and now I'm saying and here and let's make it 18 again so if you can see it will surely work and we got hello right so we can basically put multiple conditions but what if the value of x is 8 or maybe I can say 28 which is not less than 20. so this is true X is less than x is greater than 10 that is true but X is less equal to less than equal to 20 no that's not true so this will result in true and this will result in false so what's what do you think the uh end result would be so true and false will give you false remember we have talked about that in some table in fact that table used is called as a truth table okay so now what I would do is if it is not printing hello I want to print by so I can just remove this and if I compile this code and if I have this code you can see we got by hello I mean the hello is not there because the condition is false but what if I make it 18 now of course that's true so what I want to do is I just want to print hello I don't want to print buy because if you run this code and if you see the output we got hello and buy both we don't want a buy in this case so what you can do is if you want either this or this you can use uh one more thing here one more qubit which is called else so normally if else works together you can you can also write if alone or you can say if else okay so there's a compulsion to have if when you when you want to use else okay else cannot be alone okay that works and now if I uh compile this code and run you can see we got hello but what if you make it 28 at this point we it will only go for compiled one you can see we got by so that's how basically we can use if else so we have if we check for the condition and then we can also write else in fact in the next video in fact let's try to do one more example here so let's say I have the value of x as let's say 5 and I have a value which is Y which is which is seven okay so we got two values and maybe these two values are coming from the user so what I want to do is I want to print the maximum of these two so if x is greater than y I want to print the value of x if Y is greater than x I want to print the value of y so how do we do it so basically I want to print the value of X and Y so I can say system dot out Dot println and I want to print the value of x and let me just reuse the code I want to print the value of y but if I just try to print this without com without checking any condition it will print both what I want to print is I want to print the greatest number from 5 and 7. so in this case of course this is a very simple examples and I'm just I'm on purpose taking simple example is because at this point it's important for you to understand the concept not the complex operation right so what I can do is I want to print either I want to execute either this statement or this statement so whenever you have this condition where you want to execute a one statement or some block of statements always go for if else so I can say if and I can check if x is greater than y that's what you are checking right if this is true I want to execute this particular statement else I want to execute this statement that simple right so that's how we can use if else and if I compile and run you can say we got seven but if you would change the value let's say if I make the value of X as 8. of course the output should be eight and that's what we got now one thing to remember here whenever you have one statement under if or else you don't need the curly brackets and you can say we don't have it but what if you have multiple statement example let's say here uh the way we are printing X let's say I also want to print uh thank you okay in double quotes I will say thank you now you can see we have two statements inside if so I want to execute this two when the when this condition is true the moment you have two more than one statement you have to make sure that you put the curly bracket so you open the packets and you closed it and this thing was very very important whenever you have if or in future when we work with loops and some Advanced concept as well at that point if you have multiple statements you have to put curly brackets if it is only one statement you can skip it okay so yeah that's it from this video I hope you enjoyed it before that uh let me just go and you can see we got the output so that's it from this video in the next video we'll try to expand this logic more because in this video we were able to compare two numbers what if you have three numbers that will be challenging right and if you want to you can just try with three values without watching the next video and let's see how you do it see you in the next video so we were able to compare two values now it's time to compare three values so let's say we have int x equal to 8 into y equal to 7 and int let's say Z is equal to let's say five or six let's see and I want to print the greatest of these three now do you what do you think what could be the solution here see one thing we can do is we can compare all the values but at one point you can compare only two values right example we can say x is greater than y we can't simply say x is greater than x y and J okay that will be invite syntax let me just show you that if I try to compile this code you can see we got the error so what could be the solution of course we can't do this but we can do one more thing here can we say when when we are saying let's compare for X so at one point you can compare if x is greater than y and J both and we have done that right so in one if condition we can say x is greater than y and x is greater than Z okay but then don't you think these are two different conditions and if you want to combine them and I want both of them to be true in this case we can use and and we have seen this right so basically we are doing an and operation here for this condition and this condition so if this returns true and this returns true it will execute X in fact you know just to simplify this I just I have put the curly package in the last video let me just remove that just to keep it simple and now you can see we got this condition so if this is true it will execute X that means X is greatest that makes sense right but what if in fact let me just run this code if this works properly uh clear first and then compile and run you can see we got 8 because 8 is greatest but what if the value of Z is nine and if you compile this code now and run it says seven oh that's weird that means the code is not complete see at this point if if is false I mean if by any chance if this is false that means we now we have to go for Y and Z we have to compare them how will I do that this was simple how will you compare the gain because in else we don't put a condition right but what if we can so what if we can say else and again we can put an if and in this if we can say Y is greater than x and or not this and the logical and and then Y is greater than Z so basically what we're doing is in the first case we are checking for x if x is greatest the second case we are checking for y if Y is greatest at this point let me just make y the greatest which is 17 so of course it should print 17 run you can see we got 17 but what if okay first of all let's do a draw it on here so what happens is it starts from this condition if the value of x is greater than y if that is true it will check for the next one and as you can see here itself it is getting false so the moment you get false here it will not execute the block what it will do is it will go next it will say okay we got else here it will execute the else block but an else is not a simple else it is else if one more condition here which is checking for y if Y is greatest it will print y but what if Y is not greatest it is z here but still if you compile this code you will get nothing you know why is because we are not printing Z anywhere so what you can do is you can you can do one more else here for Z so if this two are false of course you will execute the else block I can just simply copy this and paste it here in Aisha of saying copy paste I can I can say code reuse that makes much more sense and I can say Z and now if I can compile and run you can see we got nine now this makes sense right so if both these conditions are false it will execute the S block okay and that works now you can just change the values in your machine first of all copy the code and whenever you change the values it will surely work but there's one more thing if you can think about this when you execute this one and if this gives you false don't you think the x is small now compared to other two values so in that case when you check for y why we have to even compare a y with X and if x is smaller right so now we just have to compare y with C we are just saving one in one of one operation here and it's very important when you say one one operation you are saving time you are saving resources so try to try to be a efficient programmer not just a programmer and that's what you do you check what is required which is not required and remove all the unnecessary code and once we have done this compile the code and run you can see we got nine and you can change the values it will surely work so what we have done in this video is we have talked about if else if else and that's how it works now once we have talked about conditional statement in fact in that we have talked about if and then we have also seen how do we use if else and then we have worked with if else if now for certain use case we have a shortcut for it and it is called a ternary operator now if you want to use if else on the same line or if you have a shorter condition and if you can write out that on one line why not so what I'm saying is let's say if you want to check if a given number is even or odd how do we do it it's very simple what you do is you uh first of all you take a number let's say another number is 4 and if I want to check it this is even odd so using IF else how you do it is we say if and we check for the condition if n mod 2 is equal to equal to 0 that means it's an even number right so what you do is you come back here and you can you don't need curly brackets because I want to add only one statement and I want to store that in a variable now basically we can use a Boolean type here or we can use a string as well well so let's say I'm using a string okay and based on this string or maybe let's let's keep it simple so what I will do is I will not print even or what I want to do is I want to set the value which is in X or maybe I will put that in result and I want to put the result by default it will be 0 but based on this if the number is even or odd I want to set the value for result if the value if the value is even I want to set the value for result as 10 if the value is let's say odd I want to set the value for result as 20 okay so for even it is 10 for odd is 20. how do we do it so in the if condition you can do you can say result is equal to 10. so if that's a even number user result equal to 10 and if in the else part you can set result as 20. so what we are doing here is based on some condition I'm assigning a value now this is one way and of course at the end I can print the value of this as well I can say I want to point result nothing fancy came simple stuff and then I want to compile this on once I will say Java C demo dot Java and Java demo and you can see the output is 10 why the output system is because the phone here is a even number what if you make it five now we are making it odd right so I can just go back here compiled one you can see we got 20. so this is working now what you can do is you can convert this code into something called a ternary operator now how it how it works let me just comment this section see ultimately what we are doing is we are saving the value in result variable in both the thing we have the same thing right if it is if is it true then we will set the value for result if it is false for the else part also as a result so can we do this can we say result is equal to the result can be 10 or it can be 20 right but how will you do it how will you check for the condition so what you can do is you can directly run a condition here so you can see if n mod 2 is equal to equal to 0 if this is true then we have to do something or or will do something so we can use something called a ternary operator here now how it looks like so turning up it looks like this let me just write that in comments so we have to use a question mark and a colon so what is this question mark and colon so whatever condition you have it might return a true or false so you can put a question mark you can ask hey is it true or false if it is true then execute the part which is after the question mark which is 10. if it is false you have to execute or you have to take the value which is after the colon okay I will just give a space here so that it will be much more readable right so what we are doing is we are saying for this condition if this is true in that case we turn 10 if this is false in that case return 20. so you can see out of all these four lines of code we are doing that in one line with the help of tunnelio operator of course it is not suitable for every evils it's not like you will say hey from now I'm not going to use zfl that's not that's not the case this is suitable for some examples this Theory it makes sense because we have a very short assignment and ultimately you are sending value to result so this makes sense I can just compile and run you can say we got 20 because the value is odd but if you make the value as even let's say 4 again and if I compile and run you can see we got 10. so this is how you can basically use a ternary operator now if you remember what we have used if else if you can also do that here I would request you to try that put one more condition here and try it out for multiple use cases in this video we'll talk about switch statement now to understand switch statement we have to go for an example so if you go back to your code what we have here is let's say we have a integer value and the value of n is 1. now the value of n could be from 1 to 7 and might be you have guessed it so when I say one two seven it might be the number of days and you are right so basically what I want here is if the value of n is 1 I want to print Monday if the value of n is 2 I want to print Tuesday so based on the value of n which can be from 1 to 7 I want to print the day of the week how do we do it so of course you can just simply print the values right you can say hey I want to print Sunday and you can just uh copy paste it or maybe copy pasting is not the right word you can say uh code reuse so just ignore this xci just just suggestion given by the vs code we can say this is Monday and we can just paste it multiple times so this is Tuesday this is Wednesday Thursday Friday and Sunday you know what I want to keep Sunday as a last day because that's what we say right weekend the Mondays the videos will start from Monday okay so I want to print this based on the value of n at this point if you just run this it will print it will print all the values I don't want to print all the values I just want to print uh one day based on the value of n so of course you can do one thing you can just check with the help of efl so you can check if the value of n is 1 you can just print Monday if the value of n is let's say you can say else a fear if the value of n is 2 in that case I can print uh Tuesday right we can do that at least let's do between Monday and Tuesday because that's what you're putting into effects and if you run this code with that changes you can see this printing Monday it is not printing Tuesday because Tuesday is in if else and you can apply that in all the conditions here and all the statements you can simply put if else and you can check for n is equal to 3 and then list goes on right this is one way there is another way using which you can do this in fact you know what I can do I can just simply complete this uh code and here it should be 3 4 5 6. you know for seven it actually makes sense to not even mention the condition because if none of it is matching of course it will be Sunday right uh so if there's no office that Sunday okay so basically this is what you can do here uh but don't you think there should be a good way or better way of doing this so instead of doing if else everywhere and at the end of else we have a good way which is called a switch now what switch will do example when we talk about switch it will switch between the ports right uh example in networking concept as well we use something called switch so here what you can do is we can use a switch now based on a value it will execute a particular case and that's why most of the time we say it's a switch case statement okay just follow me so what I can do is instead of using this if else I can simply rewrite everything in a switch block I can say switch switch and in this switch you can pass the value which is n in this case and then you can write everything in this block okay how do we mention that in this blog I have written Capital switch my bad what you can do is you can put all this thing inside this block the only changes instead of saying if we we don't for the condition we don't fully check for the condition we just match we try to match the case it can be something like this we can say case one colon so whatever value you have passed if this is matching with the case example if n is matching with a 1 it will execute this statement same applies here in fact you know I just want to give a tab to everything else and then here I can just say case to for this and let me just complete this thing for all okay and then of course the number has to be changed so we can say this is three this is 4 this is 5 this is 6 and this is seven okay so we are trying to match with all the cases what I will do is I will start with seven and let's see what happens the moment you make it seven we are basically trying to match the cases so what happens is when you execute this it will go to the switch statement and switch says okay I got the budget which is seven and now with this 7 I'll try to match with all the cases here so it will check if the value which is passed which is 7 is it matching with one no it will not execute the block is it matching with two no is it matching with three no this goes on it goes on it matches with seven the moment it matches it will execute this statement which is print Sunday and it will do that so if you run this code example if I compile this and run you can see we got Sunday simple stuff right and this one looks much better than the efls because we are checking for the condition multiple time here we are matching the case Okay so this looks better right and it also works better compared to e-fills but those of you there's only one issue here the issue is what if the value of n is 2. now what do you think the output would be think about this and maybe in your mind you already have an answer so what I will do is I will just compile this code and run this code oh that's weird we were expecting Tuesday but we got all the values the reason is when the moment your switch case matches with a case example it will check do is it matching with one no it will not execute this block is it matching with two yes the moment it matches the case it will execute the block for sure and it will just try to complete the entire switch statement it will execute all the cases it will not try to match again because it's already matched oh that's an issue right so what I want is I can simply ask my switch hey you know once you match with one of the case just come out of the switch block and the way you can do that is by using a special keyword called break now break is very important because this will be used multiple times for different concept what we are doing with break here is it says simply come out of the block now once you match with any of the case just come out and of course for that you have to put break in all the cases I will just copy this I will try to reuse the code here and paste it everywhere okay now what we are doing is the moment it matches with the case it will jump out let's try this now compile run you can see we got Tuesday it is not printing all the other blocks because the moment you print Tuesday it will simply jump out because we are saying break that looks cool right now now there's one more important thing see for the last case we are still checking right or maybe we can have one more case what if the value is 8 is not in one coming in any of the case here in that case what you can do is you can do something called default case if none of the cases are matching you can do default and you can print enter a valid number I mean you can print any statement you want I'm just saying enter the valid valid number and you can see their value here is 8 so it should print enter the value valid number and that's what you got enter the valued project number so this is how basically you can use this switch statement now with the recent versions of java you can actually avoid using break there's a different syntax we can use but again that will see in a separate video of advanced Java concept where New Concept has been introduced in new versions right but at this point this perfectly works and in most of the companies they are still into Java 8 so let's stick with the syntax so try it out with different examples and you will be good to go one of the most important thing about programming is you can do certain things repeatedly of course we have talked about different things now in normal software development basically you work with data and that's why we have variables so basically you can store your values in normal application you decide based on some values and that's why we have seen if else we have seen internally operator we have talked about switch statement but then there are certain use cases where you might want to repeat the same thing multiple times in fact there are a lot of memes available which says you know in our school time when when teachers used to say write is 100 times what if you can just do that with the help of software write the same sentence multiple times not manually but automatically it should reiterate it should iterate itself and the way you can do that in programming is with the help of loops Let me Give an example here so let's say I have a code here and I want to print something let's say I want to print here hi and I want to print this High multiple times not once but multiple times example if I run this code now it will only print hi once if I want to print that multiple times I can just copy this and I can just paste it multiple times here let's say four times and if I type it four times of course it will print the output in four times we don't want four times what we want is we want to write this once and it should call itself multiple times the way you can do that is by removing this part and by doing something maybe I can just say uh I can just write in comments I can say repeat this statement four times maybe it's something like that of course this will not work because comments are normally ignored by the compiler but how do you do that how do you mention Java hey I want to run this multiple times and that's where the concept of Loops comes in a picture so we have a concept like Loops which will reiterate the same thing and there are multiple Loops available in Java with different languages we have the same thing or we can say similar thing so we have something called a while loop we have do while loop and then we have a for Loop now mostly we use a for Loop but then we'll start with while and do while and they have their own use cases it's not like just because we have for Loop will not be using while and do while they have their own use cases now just to give you a hint maybe you want to repeat certain things multiple times but then what will be the condition maybe you want to print that 100 times that means there is a finite number five times ten times eight times fifteen times twenty times 25 times 100 times or maybe you want to do that do something based on some condition example uh do something till I press the enter button do something till the system is alive okay something like this so maybe you want to do something finite times or maybe you only do something based on some condition uh and for that we have different types of Loops available so what is loop basically if you want to do the same thing multiple times you use Loop and there are multiple Loops available while loop do while loop and for Loop and there are multiple versions of for Loop available but we'll see the simpler version which we call normal for Loop and yeah that's what we have will explode all these Loops we have talked about the need for Loops right and so we have decided that if you want to repeat something Loops can be a good thing and we also talked about there are different types of Loops right we have while loop do while loop for Loop in this video let's try to understand the first Loop which is while loop so we'll go over this go for the same statement here if you can see we are Printing High and again as I mentioned before ignore this x this is a suggestion given by your vs code uh this is what we are typing just high and what I want to do is I want to print this High multiple times so what we can do is we uh we can say Hey you know Java I want to print this multiple times Java will say okay the first thing you do is you put this in a block of course it's not compulsory when you have one statement but just to understand just to get started we are putting that in a block now and then of course we have to follow into indentation as well and we are doing that now what we can do is we can say Hey you know Java I want to run this five times so Java will say okay if you want to repeat you have to use a keyword called while now in English as well we normally use while to say repeat something or while it is true do that so here we can say while it is what in this bracket you have to mention is it true or false now basically that's how Wild works so if you say true it basically it will just keep printing this High because we are saying true it says okay while I'm true I will keep printing it so that's why if you just run this code by just by mentioning the while you can see we are getting high I just keep printing okay it's it's not stopping if you can see if I'm trying to scroll down it is continuously happening and maybe it will uh fill my Ram it will crash okay I don't think so but let me stop this execution otherwise there will be an issue so what we can do is I don't want to continuous this for a long time I want to do that for a particular condition maybe I want to say I want a counter which will start from one and it will stop at five oh when I say counter it means I need a variable where I can store the value so what we can do is here I can just come up and I can say int I is equal to let's say one now most of the time when you use Loops you will be using a variable name I it's not compensative we can use any variable name but it makes sense right because normally when you go inside the while you are executing something you are changing the values of the eye you basically increment it right so it makes sense to call it as a iterator which will iterate so I iterator makes sense and that's why most of the time you will say i j but as I mentioned you can use any variable of your choice so we are using I here which is I is equal to 1 and then what we can do is every time you run this loop after Printing High you can just increment the value of I oh but then how do we increment and we have seen that right we have a concept of I plus plus so I plus plus will simply increment the value of I okay that makes sense and oh how do I check for the condition see it makes sense now because I is 1 and every time you run this Loop the in the I value will increase right so if you run this while for let's say five times it will increment the value of I let me show you what I will do is I will print hi as well as I will print uh maybe you can you can print both the things I say on the same line you just have to give a space after high and I can give a plus symbol so when you want to concatenate two different things High and a number we can use a plus operator yeah plus Works in that way it works for addition as well as it works to concatenate two things and here I'm writing I now let's see what's the output is again I have not changed the condition so it will print infinite times but I just want to see what happens if I compile this code and run you can see the value of I is incrementing continuously and you can see it so fast depend upon your machine speed as well and let me know when you try on your machine how fast it is so what I want to do is I want to stop this somewhere so we have to apply a condition how do I stop it so we have to say if I is less than equal to 4 so what it will do is it will check for the condition remember we have talked about this in relational operators it will continuously check for the condition so what how it runs is initially it will set the value of i as 1 and then in the while loop it will check if the value of I which is 1 is less than 4 yes it will continue it will go inside the loop it will print high it will also print the number which is 1 and then it will increment the value of I what next it will go back to while loop because and again it will check for the condition because that's a while right it will continuously happening it will again check for the condition where it says okay I value now it's 2 so 2 is less than 4 yes and it will continue at at the end when the I value becomes 4 it will check 4 is less than equal to 4 yes it will print High and 4 as well so we got four iterations and then it will increment the value of I which is it will make it five it will go back here and it will check is 5 less than equal to 4 false and that's where it will jump out of the loop so remember this thing the moment your while gets a false here it will not execute this block cool right now after saying all those things let's try to compile and run and you can see we got high one high two High three and high four that's how while loop works now with this I have a question for everyone here and the question is what if at the end I'm just saying as out and I'm saying buy and with this by I'm printing the value of I what do you think what will be the output of I okay try to guess it I'm just running this code to show you the output compile run you can see we got all the values here but with by you got five you know why is because we got false here when the value of I is 5 and that's why you got the value here now since I is declared outside the loop the value will be maintained okay now one more thing here I want to run this in a debug mode let me show you how it works in debug mode so what I can do is I can go to run and I can say start debugging okay so there are some options okay for debugging you have to basically you know uh you have to set the checkpoints what I will do is I will just set the checkpoint here so basically debug is an awesome feature in programming where you can set the checkpoint this is where you want to iterate one by one so normally when you run the code it will directly give all the output right what if you want to see the steps so it makes sense let me just clear this and again I will go into run and get into the debug mode okay so you can see uh we are in debug mode now and here on the left hand side we got variables we got local a and I we have two variables you know why we got a is because of this a variable here but it is at this point we want to ignore that we want to focus only on I variable and the value of I at this point is one you can see that I is one but now if I say step into you can see we got system.winder it will print the high and I the value of I which is one uh okay I want to go step into now it will increment the value of I you can see we got the output as well we got i1 and then when I say step over it will go for the next step and you can see before executing it the value of I is 1 but once I execute that statement the value of I now becomes 2 okay so that it went back to while loop and this will keep happening for I value 2 I value 3 I value 4 as well the moment your value of I become 5 here you can see the value of I is 5 now it is checking and it falls it it will jump out of the loop it will print by with the value 5. so that's how your while loop Works in fact what you can also do is we can run a nested Loop here now how an estate Loop looks looks like let's say I want to print i1 and then I want to do something else maybe I want to print hello three times after every high so let's say I'm Printing High here and then I want to print hello as well and I don't want to print the value of I anymore or maybe let's print the value of I let's see what happens if you keep the value of I here and then incrementing it here as well that makes sense but now what I want to do is I want to after printing this High I want to print hello five or three times now in that case I can simply say hello and I can write the same statement multiple times right that's how the loop works right so I will say hello hello and you can see we got hello three times but don't you think we are doing the same thing multiple times so it makes sense to put that in a loop so I can simply say while oh but what should be the condition can I say I is less than equal to 3 no right because see I want this hello three times for every high so high we are getting four times and for every High I want Hello three times so the same value will not work in that case what you have to do is you need to work with a different variable and I will create one more variable here which is called J and the value of J initially will be 1 and then every time you execute this while loop where it prints High it will jump inside here it will set the value of J as 1 it will set the value it will check for the condition where J should be less than equal to 3 and then every time you do that let me open the curly brackets and we can put this hello statement in here so we don't have to put this thing now I can just simply say hello and of course I want to print J plus plus as well and remember this thing we are saying J plus plus in the inner while loop and then we are saying the I plus plus in the outer loop okay if you want to understand this with an analogy uh just go with an example of days in a week so we have multiple days in a week right so we have Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sunday I've already mentioned you know we we basically want Sunday twice a week but anyway so if we talk about these days every day we have 24 hours right so if you say this is 1 pm on Monday again the Monday gets over the clock will start at zero right then zero one two three four five six and then it continues right as the date changes the clock repeats in the same way once your eye is incrementing it's like a days Monday Tuesday the inner loop you can imagine that's as a number of hours it will keep incrementing but after three it will go back to one because it will complete the inner loop it will go back to Outer Loop and again this is again the value of J will become one let me show you how it works let me just compile this code compile and run you can see we got high uh we are still printing one here you know what I will do I will also print J so that you can see the value of J as incrementing it and let me give a space here as well let me say clear compile run so you can see we got high one and then it is going for the inner loop now so once you execute this High it will go for the inner loop where we are saying J is less than 3 so basically it will print this statement three times because the value of J is less than equal to 3 so it will iterate three times and then again you have to go for the outer loop so the the moment this is false it will execute I plus plus where it will make I2 it will go back to the upper loop again it will say okay now the value of I is 2 is 2 less than equal to 4 yes it will go inside it will print the value of high which is high 2. again it will make the value of J as 1 and that's why it is incrementing again cool right so this is basically called a nested while loop so that's it from this video where we have talked about uh how to create a while loop how to use a nested Loops using uh while loop now once we have seen while loop it's time to move towards a do while loop in fact before we proceed first of all I want to remove this breakpoint because we don't want to debug anymore and I want to remove the inner loop because we want to focus only on the Outer Loop now using this Loop we were Printing High four times and then we are printing buy as well let me also remove buy for now and with this I just want to run this code once again to see what happens compile run and you can see we got the same output right okay now what I want to do is uh when you talk about while what happens is when you talk about this line which is line number seven it will assign the value of i as one wait go for the next line which is line number nine here it will check for the condition if it is true it will execute this block that means if this condition is true then it will execute otherwise if this condition is false it will not execute the block that actually makes sense right that's that's how the loop should work but there are some certain situation where you want to execute this block even if your condition is false at least once I mean you want to earn this at least once maybe something like uh you want to send a message to your friend and if the service is not available from your friends and at least you should be able to send a message once at least okay so that you can get the reply the user is not available or something like that so what I want to do is I want to execute this block at least once even if the condition is false now when a second condition is false what it means is example is the value of I is 5. now when you make the value of i as 5 and if you run this code you can see will not get any output is because the condition is false so what we can do is can we just cut this part from here and put it after the block we can do this and we can just also give a semicolon here because that's how you end this statement normally when you write a while at the start the curly brackets becomes a statement but then if you write while after the block we have to end it first by the semicolon and we have to add one more which is do so we have to say do while now what it does is it will execute the block at least once even if your condition is false now if you can see in this situation first of all let me just go with one again and let's see what happens if you're on this code compile run you can see we got the normal output and this the same output we got when we had a normal while loop as well but here if you make this condition false I mean if you say I is 5 now which is a false con which will give you false here but if you run this code or compile this one you can see at least you will get the output once this is important so if you want to get the output even once for even if your condition is false you will be using two while so yeah that's the main difference between while and do while otherwise it works the same way it executes uh check for the condition again execute check for the condition and list and the flow go on now once we have talked about do while loop and while loop let's talk about the next type of loop which is a for Loop now if you go back to while loop so let me just remove do here and let me put this back here and okay let me remove the semicolon as well there is one more thing if you talk about this while loop if you can see we have three things here the first one is we have initialization we have a condition and then we have a increment so basically we are doing three things on three different lines and if we talk about any finite Loop so when I say finite Loop it means it is starting somewhere it is ending somewhere and we know when it is going to end there are some Loops where you're not sure when it was going going to end example when the user says enter it will end maybe the network goes off it will end so based on some condition it will end maybe you are trying to read a file so in a file you let's say you have a thousand characters and we don't know how many characters we have so when you open the file and when you try to read each character so the the loop will end when the characters end in the in the file so we are not sure that's why I'm saying you it can be uh not a infinite Loop but somewhere we cannot determine when the loop will end so at this point we can determine right but anyway so we have three things here we have a counter we are initializing it we are checking for the condition and then we are incrementing it so what if we can just write all these things in one line what I'm saying is can we just pick up this thing from here cut and paste it here itself so on the same line we are initializing the variable and on the same line I'm give I can give a semicolon I can say I plus plus and of course we don't have to do that here now the only thing is while loop does not support this syntax while it says I will only for check for the condition but then if you want to use this type of syntax you have to use another type of loop which is called a for Loop so in a fourth Loop you can mention three different statements in one line mind you these are three different statements why is because we are using semicolons in between so this is one statement here this is another statement and this is another statement and you can see I'm just pointing High here the same the same code I just want to check if this works compile run oh we have not got any output oh is because the value of I is still 5 let's make it one and by doing that I can just compile this code and run you can see we got all the values make sense right so how a for Loop works so we have to set the initial value which we have which we have done here and then we have to check for the condition and then we have to increment or decrement so let's say I want to print that a decrement order I mean I want to print 4 3 2 1 in that case I will start from 4 and then I have to end the eye should be greater than and equal to 1 and I plus plus so the I minus minus because we are decrementing it compiled one you can see we got high fourth High 3 High 2 and high one so this works okay can we do something more here actually we can uh what if you want to start let's let's again go for the increment part so let's say I'm going for one and it should be less than equal to 4 and then I'm saying I plus plus see most of the time we'll start with one itself right so that's how your uh normal number system works as a human we do that right but computer normally start there counting from zero it's because of the binary format uh because then by the way we have zero to one in fact in the number system also we start with Zero versus zero to nine right uh in fact once we start with the array concept there as well the number starts with zero so in general in computers the number system starts with zero so we can also say let's start with zero but the question is if I start with 0 where I have to end it is it four or five or three because we want to print only four times right so let's keep it four it which is equal to 4 and then we also have 0 here so let's try to compile and run you can see we got uh five values it starts from zero and it is ending at four okay that is an issue how do I stop it at three I want to print 0 1 2 3 that's it four four iterations so in that case what you can do is you can stop it at three now this is something which will take time for you if you're doing this for the first time when you're starting with zero you have to end before that you have to end before the count so if you want to do it four times you have to end it at three okay now let's try this I will say compile and run you can see we got four times now one of the common syntax you might be seeing in most of the examples or if you're if you're referring a book or online content you can also stop this as four but you have to say less than four because you're not including 4 here you're saying less than 4 not equal to 4. so this is one of the common syntax you can say it starts with zero and it ends less than four and now if you try this you can see we got four values make sense okay uh now let's try something else so Loops normally work well when you try to print some pattern or when you try to do something repeatedly so what I will do is let's try something so what I would try is I want to try this one I want to try this one I want to try to print the number of days I will say one two three four five six and seven because we have seven days and also every day I want to print uh 24 hours that looks good right we have talked about this example right so what I will do is first of all I want to print all the days so I will say one two seven including seven so I have to say equal to and then here I will just print the number I will say okay I will say day one two three four uh imagine day one means Monday day two means Tuesday and and so on so if I try to do that I will say compile and run you can see we got all the days now with days I want to print the number of hours okay I know that will be tricky or maybe we can we can actually print from uh 9 00 am to 6 PM because that's that's most of the work hours right so let's say if I want to build a calendar in fact I don't want to print till seven five makes sense because uh we have sixth and seventh day as holiday so let's try this I will compile and then we got five days in this five years I want to print from nine a.m to 6 p.m how do I do that so I can simply print with the help of statements I can say uh this is one or 9 am right or I will simply say nine and then I can say 10 I can just print this once again so this is 10 and so on right I can print it till 18 because 6 PM is 18. if you go for 24 hours of clock uh but then don't you think we are doing this thing repeatedly I have to basically type so many lines so of course we can use a loop here right so what if we can use a inner loop we have talked about nested Loops here we are doing it with four so I can say for and I have to give a count right so I can say J we'll start with from where now this is tricky should we start J from one we can so I can say one and J less than so many hours we are doing here we are doing nine hours right so I can say nine and J plus plus right and then I can just give a loop open and loop close and then we can print the RS here now what hours are to print I cannot print nine because nine is not nine it will print nine every time okay example if I show you this let's say clear in fact what we'll do do you know before printing nine I will give a tab so that it will give some indentation before it Sprints this is what I was saying you can see we got day one and we got nine hours here but then it is printing 999 I don't want to open 999. maybe I want to put an attendance here or maybe a calendar where we can Mark whatever what we are doing going to do so what we can do here is I want to uh say okay I should not be printing nine after this space I can say I want to print let's say value of J but J will print from one to nine let me show you what I'm saying clear compile run so you can see it is printing one to nine I don't want to point one to nine I want to print from nine to eighteen in that case can I simply add this with eight because it will start with one so one plus eight is nine right which it will start from nine and every time it will increment the value of J so you will get some other values I will just compile this and run oh something went wrong okay now what is happening here is we should have talked about this before but it's if we are never late right so what what is happening here is we were expecting it will print nine but what is happening is when we are saying that we are adding a string with a number it will coin calculate right so here we were expecting that J and 8 which is 1 and 8 will get added to get nine but actually what is happening is it is getting concatenated so we have to mention hey you know uh instead of adding a string to a number the number what you can do is you can put a bracket here so I want this to resolve by itself I mean this should be solved in a separate way so whatever number you are adding here 1 plus 8 will be a separate thing and then you are concatenating that with a string let's try that compile and run oh it is working I guess no it is not working actually oh okay we cannot add six right because it's the 17th R which gets over Okay so let's say we are printing for 9 hours which is from nine to five okay which also includes the the fifth uh the fifth 17th R okay that's tricky okay in fact you know what should I do I should actually print uh colon here or maybe a dash hyphen from this R to this R this is what happens when you take the example randomly so I will say J plus nine so what I was saying is this you know uh from nine to yeah this makes sense now so you can see we can we have actually built a calendar for this week so day one from nine to ten we can do something here from 10 to 11 we can do something here we can set up we can set a meeting calendar something like that uh so what I'm doing is we are printing J Plus 8 which is nine and then J plus nine which is ten for the first iteration as the value of J increases it will have different values here okay make sense right so this is what uh we have achieved in this example so that's how basically your for Loop works okay this is nested formula we have seen that as well okay now once you understood this let me just comment this part I don't want to do that I have a question for you now think about this and let me know what could be the sequence here see when we write this in three uh in the same line these three things how do you think they're executing is it something like this is getting executed first and then this one and then this one and later on after executing all these three statements it is executing this statement what do you think okay let me give you the answer so the moment you execute the for Loop let me just you know this is the best time to run a debug mode so I will say Run start debugging taking some time to start the debug okay so I will say step over so you can see it is executing the uh for Loop but we have not studied that yet so let me start the for Loop the moment you start off for we can see the value of I is one it just check for the condition as well but it is not it has not incremented yet because the value of I is still one so that means these two things will be executed first and then it will execute the block and then if I step over it will execute the next iteration in fact you know what I will do for the next iteration I will say step into the value of I is 3 okay it is not doing that but anyway so it is executing the body and then it is incrementing okay so that's what I wanted to show you cool uh so Point remember it will execute the this statement first it will it will be done only once and then it will check for the condition then we execute the block and then it will increment it I mean whatever you want to do in that situation in that portion in fact if you want to uh skip any of this step you can actually so that what if you want to skip this from here it's your choice you can just keep it empty the only thing is since you are skipping that if you want to make it work you have to say I plus plus of course you have to increment somewhere otherwise it will be an infinite Loop you can actually skip the instruction part from here and you can write it outside as I mentioned your choice it's just that you have to put the semicolon that's compulsory you have to have two semicolons there and now let's compile run you can see it it works so that's how you can work with fall I hope you enjoyed so we have basically seen three Loops right while loop do while loop and forth Loop now the question is which one to use in fact doing the discussion of this loose we have talked about it which one to use but just to summarize things see if you have of course all the loops works in the same way you can use any any of them at any point but the thing is if you know how many iterations you want to go for example let's say if you want to print from 1 to 100 you normally use for Loop if you because we know the numbers right because in photo we mentioned all the three things the initiation value condition and the increment decrement part but what about reading a file because in reading a file we don't need a counter right because when you read a file how it doesn't matter how many characters you have you have to pick up each character right so there is no initial point it's just that you will read the file the moment you have end of file you will stop the loop so using a while loop there makes sense can I use for loop as a while loop yes we can if you remember we have seen a code where you can just give two semicolons and in between the semicolons you can put the condition even that works how when to use do while loop now whenever you use a while loop and if you think hey even if the condition is false you still want to execute something at least once dual makes sense but expert my experience most of the time you are going to use for Loop if you want to read a file re-database while loop makes sense and I have I have hardly used do while loop in my entire career but yes there there might be some use cases where you have to use Dubai Loop so which is the best Loop in terms of frequency of use it will be for Loop in terms of the usage for database files Network while loop makes sense once we have talked about the basics of java it's time to move towards some Concepts now at the start itself we have mentioned that Java is object oriented programming now what do you mean by object oriented programming so when you talk about object in real world everything is object right in fact if I'm using uh a pen that's an object if I'm using a mouse that's an object if I'm using a seller type I don't know why I have sellotape on my table but that's an object right so everything around you is an object and the weird thing is in the programming world even they treat humans as objects but that's fine everything is an object now everything has two things in fact every object has two things an object knows something an object does something example let's say if you talk about this pen this pen is an object right so this pen knows something so when I say no something it simply means it has some properties some physical properties right you can see this pen right so we have a a color for this pen I'm very bad with Colors by the way but we have a color for this pen there's a height uh there's a kind of pen it also has a brand I'm not sure which company pen it is it doesn't matter so there are some properties for the pen right and the same way if we talk about this uh this remote okay this is this has some properties height color uh the number of buttons so this is what a remote knows and also a remote does something example I can on my AC by pressing some button so it has a behavior as well so basically every object knows something every object does something and that's what we are going to create in Java as well now whatever project you are making you will be having uh different objects now specific to Java from now it's it's also about thinking okay so it's not just about coding something even when you start making something in your mind you have to treat everything as an object example let's say if you want to add two numbers the first thing you should which should come to your mind is okay I need an object which can add two numbers getting a point right everything you have to think in object way so if you come back to programming if you look at this example of course we don't have an object here in this code but you have to remember whatever I want to do you have to do with the help of object so what we have this discussed till now is there's a concept of object oriented programming so basically we have certain things here we have a concept of object and this object will have a property and this will have Behavior so every object will have a property and they also have a behavior the properties can have multiple properties and multiple behaviors example let's say if I as a human if I'm an object I know something I know a lot of things I know how to talk how to walk how to dance how to uh type code right and I behave based on what I know so I can dance because I know how to dance so that's your property but you know how to dance that's a behavior when you're dancing when you're talking when you're walking right so I know my age as well I know a lot of different so whatever I know here is properties what I what I do is behaviors okay but how do we create objects so in in Java if you want to create an object we have to first create a class okay now why we need object and Clause concept so first of all we have talked about object everything has to be done with the help of object in real life we can't do anything without objects so that's how it works so in Java as well if you want to do something you have to create an object but how will you get an object and who creates this object so let's talk about it now if we talk about the concept of object who creates object in real world example let's say if you talk about this table when I'm with I have my systems now this table is created by a carpenter okay so when you go to a carpenter by saying Hey I want a table Carpenter will say okay uh I will give you a table but tell me the exact dimensions okay uh maybe take the carpenter the actual guy who is making this table he is very technical so we have to explain in technical details and that's why we have sample sign between who says okay you give me the details okay I will forward it to the carpenter so basically you give the details to the to that person 30 percent and then that third person gives a detail to the carpenter because Carpenter understands One technical details and you understand on the later terms I can say hey I want a table where I can keep my computer where I can keep my monitors my keyboard mouse mobile phone and all the all the different things which I have on the on the table here so that person in the middle person will explain everything to a technique or the carpenter okay that's the dimension a person wants uh this is the height which I want based on my height I want this table to be and uh so all the details right so got the point so how do you give that design so as a novel person you give a design in a particular way and in programming you can give that design in a class file so you have to create a class file which will act as a blueprint for the carpenter or for the for the for the sum for someone who can create object for you in Java now who creates objects in Java it is your jvm again what is jvm what is JR will have a digital discussion there but at this point understand this we have a jvm who will say okay it's my job to get the object but you give me a blueprint okay so basically you create a class file now this class file gets compiled to create a byte code now that byte code goes to the jvm and that's where you get the object okay so in the last video we have talked about what are classes and object right and we have seen the theory of it in this video let's try to create a class and we'll also create an object of it now first of all where do we get a class and what class I want to create as I mentioned before in Java what you want to do you have to do that inside a class so in that class you'll be having methods you'll be having variables how do we do that that will say in some time but the first question is where we have to create a class now the class can be created in the same file so example in demo.java we already have our class right we have a demo class and here with this class I can create another class here and we can surely do that so what I can do is I can create a class here and I will name this class as calculator now basically imagine what I want to do here is I just want to add two numbers so even if you want to add two numbers we'll do that with the help of a class now what if you don't use a class and normally how you do it so let's try that first I want to just add two numbers and I will remove this extra comments from here now how do we add two numbers it's very simple you use variables let's say int num1 comma num2 so we got two variables here and then we have to give them some value as well so I can just come back here I can give them values I can say num1 is equal to 4. and num2 is equal to 5 simple numbers right in fact you know what works well is to have this in two different lines again coding standards if you have variables on different lines it is much much easier to read and much easier to maintain the code as well now once you have these two variables if I want to add these two and of course when you add these two numbers you will get the output you will get the result save results somewhere so I will say int result equal to num1 Plus num2 so you can see we got these two values and then we are adding them okay so in result we got the value and of course you can print this value so you can simply come back here and you can say result print result that perfectly works now the twist here is I don't want to add these two values here of course this code will work I just want to see if this works before we do something I will say compile and run so you can see we got nine but I don't want to add these two values here so how do we represent that in a separate class uh it's something like let's say if you want to call someone YouTube you will not manufacture a phone by yourself you will just buy the phone now this phone company they will Design the phone they will make the phone example if you talk about iPhone in let's say you know nowadays they don't mention that on the back of the phone but initial days they used to say uh okay let's talk about some other phone first you know most of the phones doesn't matter how costly it is uh so they it is mentioned it is where it is made uh in the previous the previous uh manufacturing how was China so most of the phone was having made in China right uh but then if you think about Apple they are smart in this case they say designed in California and assembled in China so even they believe designing is more important than assembling and that's what you have to do here because manufacturing of your object which you do here will be done by jvm what we are concerned about is designing of it so when you design it you can just put come back here and you can say class and I will just name it as a calculator I can just open the curly packets and close so this is basically where you will design your stuff you will put your things example if you talk about calculator and if I mentioned that before every object will have two things properties and methods you will do that in this particular calculator now what I want to do is I want to add these two values right so if you want to add that's a behavior right that's an action action will be done with the help of methods so here I will say public so how do we get a method so we have to specify the access to it which is public I want this to be called from anywhere else and I want this to return me a value so once that is called it will perform some operation and it will give back the value so I will say ain't that's the written type so whatever it returns we have to specify the written type of it as I mentioned before Java is a statically typed language which means you have to specify for all the data whatever data you're working with when you create a variable you have to mention whatever variable you want to create when you return you have to mention what type of data you're going to return so that's end and what could be My Method name so method will also have a name so here I will say the method name is ADD and this ad will perform an operation now at this point you can see I'm not passing any value I'm just saying add and when it calls I just want to print in ad I just want to see if this get really get called okay so at this point I don't want to add I just want to see if this is getting called and also we are returning a value right and that's what the errors it says it says the method must return a result of type int okay uh so I have to return something so irrespective what type of calculus I'm doing I have to return something 's a return so that's how you've written the value you have to use a written keyword and then you will give uh any value it doesn't matter any integer value so I'm at this point I'm giving 0 because we are not concerned about this value but the question is how do I call this ad okay so this is basically a plus a class will have methods a class can also have variables what if you want to get variables here it's very easy just outside the method and inside the class you can just mention the variables at this point I just want to get a variable a just to show you that how do we get a variable we are not going to use this a anymore but let's let's keep it there so in a class this is how you design your class you do you put two things there you put your variable and you put the method the variable name is a the method name is ADD makes sense right and here we are just adding these two values putting that into result but we are not going to use this anyway so we'll sub comment this part and comment see the ultimate AIM now is how do we call that add method that's tricky so in order to call the add method can we directly do that can we just directly call a method like this so that's a method right now if you're coming from some other languages it might be supported where you can simply mention the method name and it will work at this point you can see it is not working we are we are getting the error itself in fact there are two type of way you can check the address you can directly see in the IDE if you're using it otherwise you can just compile the code to see the exact error it says cannot find symbol okay that's weird the ad method is there right but it still says cannot find symbol you know why because if you can see in the error itself it mentions that the symbol add which is a method it's not it cannot find that method in the location demo oh okay so when you call AD is not able to find that in the same class but it's not in the same class right in a different class how do I mention that hey you know this ad is it belongs to a different class for that basically what it says is the design is not enough basically what you need is also the object example let's say if someone comes in the world by saying hey no we have this amazing idea and we wanted to buy this idea nobody will buy the ideas why do you buy the devices no one will give you a uh I mean you will not buy a phone odd idea which using which you can call someone it says you know this device can call but then you don't have a device to call uh so basically we need an object a physical object in the world of programming we need a virtual object so for this class which is a design I want the object the instance which I can use how do I do that how do I create the instance now for that you have to come back here and you have to say Hey you know I have a calculator class and I want to create an object of it so basically imagine calculator as a type example if you want to get a variable like num1 you specify a type right which is int so here as well imagine Calcutt is a type and then you are creating an a reference of it let's say calc they are also variable the calc is also variable the difference between these two is this num1 and num2 they are called primitive values or primitive variables is because ain't and all those type uh flow double they are primitive this calc is of type calculator so we we normally call them as a reference variable so that's done uh let's say we have this calc calculated calc and then using this calc I can say calc dot add but still it will not work the reason we are just creating a reference we are not getting an object yet okay how do we get an object now first of all who creates an object and every time you get an object in this world you know we are talking about carbon neutral we are talking about how can we reduce the e-fest in the same way uh every time you buy a new phone basically you are creating an e-vest because when when you buy a phone the companies are manufacturing new phones okay so basically you are increasing the E-Waste in the same way in jvm as well every time you get the object it will consume some space okay how do we do that first how do we create an object how do we consume that space now for that you have when you want to consume the space you will simply say Hey you know I want to create a new device so you have to say new but whatever device it will ask you in that case you have to mention I want to create a object of type calculator and you have to also give the round brackets now this is how you create the object okay this is how you get an object of course we'll talk more about this uh how do we I mean what's the difference between this line and this part but at this point just to keep it simple this is how you create the object first you mention the class name then you mention the reference variable name or variable name using which you can access the methods of calculator and then you send new keyword that's how you consume this space but how much space you need what what are the variables you have to initialize that can be done with the help of this thing now it has a special method in Java called Constructor but what is Constructor we'll talk about that later how it is different from methods we'll discuss that in detail later but at this point with this line we got the object and once you got the object you can simply call the add method your job is done but will it really work let's try so what I can do is I can just clear this and I can go back here compile and run you can see we got in ad basically you were able to call in ad that's great okay but if you can see this add method actually returns a value so don't you think we should accept the value as well if someone gives you something you have to accept it as well so here I can say end result so whatever it returns I will put that into this result variable and I will just print it and let's see what happens so compile run oh it works you can see it says in add and we are also printing the value of result which is zero y 0 is because we are returning 0 but what if you return something else maybe I'm going to return based on the values which I have here oh that means here I don't want to print something what I want to do is I want to create a variable called as let's say R is equal to I want to add num1 plus num2 and then whatever addition I'm getting I will just return the value now it will make much more sense so I didn't show if you're doing 0 I should I should be able to return the value so now 1 plus num2 should be assigned to R and I want to return the r but the only problem is I mean let me compile this code to see what happens compile oh we got errors and you know I love errors because errors will teach you a lot of things it says num1 and num2 cannot find which location in calculator and you have to mention hey you know I was actually talking about number num2 which belongs to demo in fact the main method so what you can actually do is uh can we when you're calling ad can we just pass these two values something like this four and five right and then since you are passing those values example let's say if I ask you hey you know add two numbers the obvious question from your side would be what numbers give me the values first right so I will say Okay add 15 plus 20 then you will say okay that's 35. so that means I have to pass a value so when I call you to add values I have to I have to give you the values and then of course you have to accept those values it's not like I am giving you the values and you are not listening to me so I have to accept those values I will say N1 comma N2 different variable names I have to give int and then here instead of adding num1 and num2 I will add num n 1 and 2. the variable name can be number random two as well just to just to differentiate between these two I'm just giving different different variable names so the idea here is whatever value you are passing 4 and 5 will be assigned to this N1 and N2 and that's how you can pass the value make sense right and anything else let's try this let's see if this works I will just clear this and compile okay I've done comparison two times my bad so you can see we got nine so it is working basically whatever value you're passing it is getting passed here but there's one more thing when you have those values in a variable why you have to mention numbers here we are hard putting values not a good idea so what you can do is instead of writing 4 here we can write number one and instead of writing 5 here I can write num2 simple stuff so whatever value you have here you are passing it here and then that gets assigned to num1 and N2 and then you're adding the value let's try compile run it works so what we have done is we have created the class how to get a class it's very simple you say class and you mention the class name which is calculator and then in that class you can create variables you can create methods this is how you specify what object knows right variables data and what object does an object can add two values now how do we use this class basically you have to create an object of it so as you can see this is how you create the object you say Hey you know jvm I want a new object you see you you use a new keyword there and then jvm says okay I will give you the object but also tell me design and this is how you mentioned the design you say calculator now this will give you a new object of what type calculator so you're assigning that to a variable called calc of type calculator example when you say 5 that's an integer type that's why you say int num2 this is an object of calculator type so you will assign that to calc which is a calculator type variable now once you got the object reference here you can just use that reference and you can call the add method by passing two values return the value and print simple stuff that's what we have done in this video so till this point we have seen some programming thing and we were able to execute them we have talked about classes variable methods and object relation as well now it's time to actually see what is happening behind the scene because if you understand what is happening behind the scene then the further cooling will make sense right so every time I introduce A New Concept we can actually relate what is happening behind the scene now to understand that let's focus on some keywords okay if you remember we when we started this videos at the start of the video I mentioned that to compile the Java code you need a tool right in fact you have to do setup as well and that tool is your jdk now jdk here stands for Java development kit maybe I can just write it here so basically why do we need it why do we need a development kit see the thing is when you as a programmer when you type a code you have to convert that code into byte code because ultimately the what runs on the machine is your is a bytecode so of course you cannot type A bytecode I mean you can don't try it but the idea is you will type the Java code you will compile it and it will convert into bytecode which will run on a machine but which machine now for that we have one more term here which is called a jvm now basically jvm stands for Java virtual machine so basically this is your machine which runs the code so of course you have to compile the code with the help of jdk and you have to run the code on jvm now apart from this there is some more things in the picture so what happens is when you want to run your code so what code I'm talking about I'm talking about the byte code here so assuming that you have already compiled and you have a byte code with you you need to get a space okay so let's say this is your space where you are going to run your code and this space is basically your j v m now this is where you actually execute the code now why it is called jvm is because we have a machine and to make deep Java platform independent we went for a virtual layer on top of it so it doesn't matter on which machine you have your jvm it will work the idea is to have jvm on every machine now jvm is going to run your code and behind the scene computer will convert that into its own away and it will run so basically you need a jvm to run your code but then what happens is for your code let's say this is your file here if this is your file which you want to execute most of the time your code is dependent on some external file not your files which you have created maybe you have created five files apart from the code which you have written you have used some inbuilt classes some inbuil libraries and that's where we need some extra things here so maybe we need some extra files from which you are going to use in your code okay now question is where you will find all this code and that's where we have one extra layer here on top of it which is your JRE now JRE stands for Java runtime environment so basically we have a concept of JRE which is Java runtime environment now in this Jr you'll be having all the extra files you need extra classes in the classes it also checks your byte code basically it validates your byte code or maybe you want to load a class so here GRE will help you but ultimately your code will run on jvm so jvm is very important because that's where that's where your code runs but even to run your code you need some extra files and that's what GRE will provide you and the name itself says right Java runtime environment so it's an environment where you can run your code and the beauty is jvm is a part of GRE so you can't simply say hey we don't need GRE we need if we don't have a GRE you don't have a jvm right so basically every time you when you say you have Java in your machine to run the code you actually have Jr with it so you have a JRE and inside that you have a jvm but then as a developer you install jdk right right now jdk acts like a upper layer so every time you install jdk you also get these two as well so the the top layer here is your jdk so jdk when you install jdk you will get the updated JRE and also you will get the updated jvm but by default on a client machine you will find jrd and jvm which will have extra files and which will have where you can run the code in fact you might have seen this sometime when you when you install some software it will say Hey you know in this machine I'm not able to find.net framework or I'm not able to find Java it's because the software will be running on those particular softwares I mean the application will run on dotnet platform or on Java of course there are other platforms as well just to give an example we have dotnet Java and that's why you know you'll have you install extra softwares the same thing if you don't have the updated version of GRE and if you try to run some Advanced software it might not work and that's why you know sometime on Windows uh it says Java is getting updated for the same reason because to support extra the new softwares so this is basically your jvm Jr and jdk so jdk will have everything JRE and jvm and in jvm you execute the thing now in the next video we'll try to expand more on jvm so in the previous video we were able to work with a calculator and basically we're adding we were able to add two numbers right so what we did is in this calculator we have created this method called add and then we have accepted two parameters and then we were able to add them and we were able to return the value now of course the place from where you are calling this method uh returns a value and that's what you're saving in result and you are putting it that now in this video let's try to understand more about methods of course in the entire course we will be using different types of methods we'll explain how this method method is behaving this way but at this point let's understand about methods more so what I will do is just to simplify this I will remove this class and I will also remove the entire section from here now if you can see C by default even if you have one class here which is demo by default we have one method which is main now main is a method which is a start of execution so it defines hey this is where we have to start the execution so by default we we are anyway using a method right but then if you understand the actual development of any kind of product it can be a car or a mobile phone or a software now we normally create modules example if we talk about a car we have different modules there right we have wheels we have uh doors we have Bonnet we have engine right so all these things come together to to form a car in the same way if you talk about a big software we break down this offer into small parts and we try to create those as a component now in Java we can create components with the help of classes right so we can say hey if you want to build a car maybe let's create a class called Wheels let's get a class called uh engine let's create a class called seeds right so all these things comes together now every class here is which whatever class you work with let's say wheel Class A Wheel class will also have its own behavior right so we'll have some data we have some Behavior so here if you talk about Behavior if you want to Define hey this is how the wheel will move this is how the uh the engine will work now all those things comes under a behavior and we do that with the help of methods just to simplify this let's say I create a class and I will link this class as ABC it can be any component or maybe I can I can say this is a computer of course even a computer is made up of multiple components but just to simplify things let's say we have a computer it's a class okay and then in this class I can Define the behavior what a computer can do a computer can run games a computer can play music so I can say hey I want this computer to me to play music in fact in my childhood this is what we used to do when we used to get computer to access uh I was around I was in my 11th standard when I got full-fledged use of computers and at that point uh when I say I I got to use computer what the thing I will do of course we can use paint we can use Ms docs which is the word or we can use some amazing media player to play music right so here let me say uh this computer can play music okay now how do we Define that's a behavior we do that with the help of a round bracket and then this play music will do something so we have to open the brackets and close this is where you will Define what is a behavior of this particular method and every method we also have to specify what type of access you're giving of course we'll talk about access later but at this point let's make it public I just want it to be accessed from anywhere and also a method will do something right so that's what we are doing here now this method will do something and it will give you something in return example let's say if I ask someone here so let's say if I have Hershey and if I say hey I want a pen now at this point as a external method I'm asking her to give me the pen right so what I'm expecting from hush a pen right maybe I want to just get some information maybe I can just ask hush hey can you just call someone to send this information so at this point if I am asking her to call someone I'm not expecting anything in return so what I'm doing is when I say I don't want anything in return is wider so of course your method can give you back something you can say a method can give you uh integer value a method can give you a pen as well so this method which is play music is not giving you anything it's just performing some operation now of course we can write multiple lines of code here to play the music but just to keep it simple I will say music playing or playing music it doesn't matter so we are just writing this statement here now this is my method that's how you define the behavior we can have some extra methods as well I can say public and this is get me a pen okay this is a method name I have and this method will give me something so I will say turn so what it will return so let's say I want I want to get a pen but at this point we don't know how to get a pen right so let's say I will I will return a five value now if we talk about this five this five is a number right or maybe I can just return pen now if you can see this pen is a string now string is a special type in Java which is a combination of different characters so we have to put that in double quotes now we are returning a pen so here we can't say void because when you say y it means you're not expecting anything into in return but here we are doing that we are returning a pen in a string format so we have to say I am returning a string so whatever type you written here you have to mention that we're going to type here makes sense right so basically we got two methods and then it is also possible that when I say hey get me a pen hush will say okay I will get you a pen but you have to give me money first of course right no one will give you something for free so I have to provide the money here so if you ask a method to do something it comes under your design how you want to design it do you want your methods to exp accept something from you or what type of data you want to return so here I'm saying this get me pen will it will get some integer value so let's say I'm passing 10. so normally one or not 10 but available which is num I will say cost so basically you have to pass a number let's say 10 so one pen will cost to 10 Rupees and then if you want to call this get me a pen you have to pass the value as well make sense right now we have two methods how do we create how do we call them so from this main which is in some other class if you want to access this method the first thing you have to do is you have to create an object of a computer as I mentioned before every time you have a class which is just an idea right class is a blueprint an idea and if you really want to use those ideas you really need an object how can we use something without the object right even if you want to call someone you need a phone with you a physical phone not a virtual concept which using which you can call someone okay so let's say here we have to get object of computer so we'll say computer comp of course you can give any any reference name here we can also say obj which is very common so you can say computer object equal to new computer and that's how you create the object right so this is a line normally we use to create object but only these two things are actually getting the object here this is a reference variable and now using this obj I can actually call both the methods I can call uh play music so let's try with play music only and let me clear this screen here so we'll say Java C demo.java and you can see we got the the comparison done let's run this code I will say Java space demo it says music playing so this is working uh can I call get me a pen let's try so I can say obj dot get me a pen but if you can see get me a pen is asking you to pass a integer value so let me I will pass 10 here and I can just run this the only thing is if you say get me a pen it returns a value which is a string format that also means that when you call get me a pen here it will return you something it will give you something so I will put that into a string Str so I can say string Str of course you can use any variable name but that's a string we are not using integer double here we are working with string and of course we'll have a separate session or separate video on string at that point we'll understand that here what I will do is I will just try to print this Str just to see what it is printing I hope it will print a pen and yeah that's what we got we got music playing and we got pen cool now there's one more thing here which is what if it in this cost so let's say you're passing 10 that works but then what if someone is passing letter two now of course we'll not get any pen for two rupees right of course it at this point it will work because we are not doing any any checking I don't want to return a pen when the cost is 2. so how do we do that I want the minimum value for this cost to be let's say 10 Rupees in that case you can remember we have talked about if else so we can actually use if else here so we can say if the cost is less than or the cost is greater than equal to 10 then return pen otherwise don't return pen right it's very simple if your if the cost is more than 10 or equal to 10 then you have to return pen otherwise don't return pen someone is calling this method you have to return something in this case you have to write else return and you can say return nothing you know the weird thing is at the time of recording there is a phone called Nothing phone so basically what you're returning here or nothing phone ignore my poor jokes but yeah so what that's what you're doing here you are returning nothing and you can see I am passing 2 here and of course the consumer will be false it should return nothing let's try compile run and you can see we got nothing okay so this is working and this is what we wanted you can do one more thing you can simply say you don't even have to mention the else keyword you can simply say return nothing but then you will say it will execute return nothing anyway right the thing is the moment your code sees return pen here it will just jump out of this method it will not execute the further statement what I'm saying is if the value let's say is 12 not 2 12 of course this will pass the condition and then once it is true it will execute return pen the moment you set return keyword in a function or a method it will return the value it will just go back it will stop the execution and that's what we will be getting here so if I compile this code and run you can see it says pen it's not printing nothing but yes if the value is 2 in that case this condition will be false this one will be false and it will not execute the return statement of if it was execute return nothing and that's why it will Point nothing now if you can observe there is one difference between these two methods here if you can see we have get me a pen and we have Main the differences of static keyword and we have not talked about static gate so let me talk more about the methods and this is important for the further Concepts and very interesting so let's say again let's go back to our calculator to get some more understanding of it so I will say it's not a computer it's a calculator of course in a computerizable we can do calculations but just to keep it simple let's go with calculator so we got this class here and let me also get the object here and there is no compulsion that a class should have the variables and methods we can keep it empty as well that perfectly works so you can see we got calculator obj equal to new calculator and then here I want to let's say add two numbers I know I know you'll be saying we have done that before but let me just do that once more and you'll understand what I'm doing so you can see I'm saying public end I want to add two numbers why int is because I'm expecting some written value and it will accept two values one is N1 and 1 is N2 and here I want to return the value of course you can simply return the addition of it so you have a choice you can create a separate variable uh what I'm saying is you can create a variable like result and you can say N1 plus N2 and here you can say return result this works uh you know we can actually do a shortcut here if you look at this variable result is only used once we have assigned the value and then we are returning a value so can we just do this can we just say return N1 plus N2 we don't have to create a new variable okay so you will get used to it once you start making progress you will get used to it so you can see we got a calculator which can add two values and then returning the addition of that this perfectly works right so what I will do here is I will come back to my code I will say obj dot I can call this add method which will take two values three and let's say four so it will return a value right so I will say int R1 so int R1 is equal to this addition and I can actually print this value so let's compile this code uh not running directly first you have to compile the code and run you can see we got seven this works so what we are doing is we are calling add by passing two values which is three and four what if you want to add three values that would be tricky so of course you will say I mean think about this if you want to add three values the first thing comes to your mind is it's easy we can change this particular method which accepts three values right and of course you have to pass three values because if you pass only two values uh your compiler will feel bad compiler will say Hey you know uh you have a method which accepts three values and you're passing only two values not fair okay so you will say okay I will pass to I will pass three values and that perfectly works I will just compile this code and run and you can you can see we got seven oh we got seven because we are still adding N1 plus N2 we are not adding N3 so let's compile and run you can see we got 12 this is working but there's only one little problem here not a little problem actually big problem what if now you want to add two numbers sometimes you want to add three numbers sometimes you want to add two numbers again it will not work your compiler will say Hey you know we are expecting three values and you're passing only two now unfair right okay how do we solve this problem maybe if there is one solution which comes to my mind is what if you can just create another method which can accept two values I can say public int add one which will accept int N1 comma int and 2 and this will return N1 plus N2 now this perfectly works the only thing you have to do is you have to remember what method you have to call because add one adds two numbers and add as three numbers of course you can make it more effective by naming the method name as add two numbers add three numbers okay we we were able to solve that but the problem is the moment you have more methods you have to maintain the documentation for those methods and also it's very difficult to remember the method names add method looks much better right instead of adding two numbers adding three numbers so can we do this can we have the same method name that can solve our problem like this of course I have two methods but then but they have the same names the only thing which is changing is the parameters if you can see this ad take three parameters this ad takes two parameters so that's right we can have the method name of with the same name the only requirement is they should have different parameters example if you make it same parameter here again it will not work it will say it's a duplicate method add we can't do that so basically we need to have different parameters and it's not like they have to be in numbers they can also be of a type what I'm saying is what if you want to have the same method I will I can just reuse my code here and instead of int I can also have a double value so I can say double N1 and N2 and I can just simply return the only thing is when you double when you have double and int you return with double because double plus eight will give you double that's it so it's not just about the number of parameters it's also about the type of parameters so you can have different number of parameters or you can have same number of parameters but of different type now this concept in Java is called method over loading because you have two methods with the same name in fact three methods here with the same name what is different is parameters it is called method over loading now likewise we also have one more concept is called method overriding where you also have the same name but things work differently there it's more about inheritance and thing at this point we are focusing only on the overloading part how about this see every method will have a name and parameters what if you have the same parameter but different written type even that will not work it has to be with the parameter itself this return doesn't matter what matters is the name and the type now let's try to understand what is there inside the jvm so what I will do is I will just go back here and let me draw a bigger jvm because that's what you're focusing now if you talk about the outer box for this that's your jrd outer box for that is your jdk but let's focus on jvm here and also name it j b m now what we normally do here is this is where you execute the code right now question arise how exactly we are going to execute the code of course there is something called a runtime or the runner which will do that here we'll focus more on the memory level what happens on when you create a variable when you store a variable in your memory or inside jvm memory we have we can categorize into two parts the first one is this this is how it looks like of course I want you to guess so basically this is a area where you can store or your data and that has a feature which is last in first out example let's say if you send data which is five six so let's say if you send data five six seven now the first value which entered was five second value is six third value is seven now when you try to get the value you will get 7 first because that is last in first out now this is called stack so basically you have a memory which is called stack memory now apart from this memory you have one more which is you call as Heap now on purpose I am drawing this type of image where it represents that it expands right so this memory here is your hip so basically we have a stack and we have a heap memory so in the stack you store data in a different sequence and in Heap you have a open space now before we jump into this memory how it works let's focus more on this code okay remember when we talked about calculator and then we have talked about these two variables now here when you talk about this N1 and N2 variable the variable but they are local variables okay just to reiterate those are called local variables now what do you mean by local and what are different type of variables we have so if I create a variable here let's say num now in a calculator we have a num variable and then in a method like add we have N1 and N2 that's right the N1 and 2 are part of add method now here the N1 and N2 become your local variable and num here becomes your instance variable that's right the word is instance now what happens is the moment you create this the moment you have main now the beauty is every method will have its own stack that's right it's not just one stack every method will have its own stack and if you can see in this code we have two methods right so of course you will be having two stack what I will do is I will just redraw this tag to accommodate more data so what I will do is I will just zoom it a bit so let's say we have a stack here okay this is your first stack and a stack will have two section okay and basically it will have two columns and multiple rows depend upon how much data you want to introduce now if we talk about this main this particular stack is actually your main stack so I can say this is my main stack now in this stack you will be having your data so what data I'm talking about if you go to the main method if you can see we have a variable a okay now that's also a local variable okay so I can say hey I have a variable which is called a here so I'm just reading a there now what's a value at this point there is no value for a so I will just ignore that part maybe let's not even mention that once we talk about string then it will make much more sense let's start with the variable here maybe I can create some more variable just to understand so I will say int data is equal to 10. so what I'm doing is I'm creating a variable which is a local variable again inside the main method and you can see we can say this is my data variable and the value for that is and straightforward we can assign the value 10 because that's the value so basically your stack will have a key and a value a key would be name of a variable and value will be 10 the value itself now coming back to R1 here okay let's ignore this obj as of now and mind you this obj is also available so it was a part of this tag so data obj R1 they all are variable so it says Okay I want to have R1 as well so I got R1 variable but what is a value for R1 and that's where it will basically call the add method okay so it will call the add method but we don't have our ad method in the memory as of now right so if you see in this memory you know where we have ADD so basically what we have to do is it will create a new stack out of it so we got another stack and the name for this stack would be add and of course it will have two sections and multiple rows so two column multiple rows now if you see in this particular ad we have two variables N1 and two so of course we have to define those two variables N1 and N2 what are the values for these two variables we are passing them right three and four the moment you pass these two values it will go to N1 and N2 it will assign those values so the value for n 1 is 3 the value for N2 is 4 okay that's good and then you are executing add method so in add method what we are doing is we are just adding those two values uh three and four which is seven it will return the value whatever value you have returned it will return the value with this particular method to R1 so that's right we got R1 as 7. I hope this makes sense cool uh so we have done the changes there as well so R1 is now seven what else now what happens to this method ad we have called it right so there is a special method area where you have a different stack of calling methods so when main call add add will execute First Once ad is over it will be gone at this point let's not talk about the function calling let's only focus on the data areas so basically this is what we do there right but what happens with the object here now that's tricky and what happens to this num variable where do we put in a variable in the add stack in the main stack no if you can see num is not a part of add is not a part of main the beaut is you can actually print the value of or you can use the value of you can use a variable num inside a method I can actually print the value of num here so maybe I can set the value as let's say 5 and I can actually use this num inside a method see using a particular variable and the declaring the variable inside a method are two different things at this point we are using it we are not declaring it inside a method N1 and N2 they are declared inside the add method but not num and that's why if you if I if I can show you if I compile and run it still works you can see we got num which is five so we can use it but the question is where it resides not does it resides in add stack or main stack the the answer is and that's where we have a heap memory there right now inside the Heap memory what happens is just focus on the line number 20 here on line number 20 what we're doing is we are saying calculator obj now if remember we have talked about this multiple times this obj is not an object now people coming from C plus plus you might be having this urge of calling obj's object but no that's a reference variable but that's a variable right now this variable where it will get created it was declared inside main so of course we have to say o b j here but the question is what will be the value now for that you have to understand what is happening here now when you say new calculator it will simply create a new object where it will create the object inside the Heap memory this is where you will get your object and inside this object you will be having two things actually so let me just draw the first object quite bigger and later on we can draw it small so this is your first object the object will have two sections the first section will have all the variables all the instance variable not local variable you know why because local variables are part of a stack so the part of the stack not of Heap but if you talk about the instance variable like we have num here they are part of the Heap memory and this is where you can say variable name or num the value for that is let's say five we have assigned five right and then this particular object will also have methods and this in this Heap we are saying this is an object of calculator it will also have the method declaration so it will have ADD method but then it is only have the definition of it the actual area which ad will consume will be of Stack okay if that makes sense of course we can have extra methods as well at this point we only have one now this particular object will also have its own address let's say the address is one zero one of course right when you store the data it has to get some address now that's one zero one of course it will be a complex address at this point I just want to keep it simple one zero one now this one zero one will be stored in the stack so every time when you say obj dot add the actual execution starts from from stack itself you say okay someone is trying to call example if you talk about line number 21 here we are saying obj dot add that means we are using obj object to call the admit method but it belongs to orbital object right which is the object here so it will simply use this particular address it will jump to the object and it will call the add method so can we say there is a link between stack and hip yes because of the address right and that's why you can use this object multiple times and that's important in future we are going to talk about Anonymous object as well and we'll discuss why this link is important and it will make much more sense at this point just imagine there's a link every time you say open J dot add you are going from Main looking at the stack then you are jumping towards the Heap to look for the object I hope that makes sense the good thing is we can actually create multiple objects I can just copy this line or maybe I can say code reuse and I can create another reference which is obj1 and we got a new object here that's right in total we got two calculator objects so with this change is what we are getting is we will be getting a new object here let's say the address is 105 and it will have the method it will have it will also have num variable and the value will be 5 of course you can change it and you can have ADD method okay this is num okay not n but imagine that's num and you'll be having obj one here because in main we have one more Declaration of obj1 it will have the address 105 and you can imagine there's a link between stack and Heap okay now when I say these two different objects have two different values or two different num variable but they have the same values of now right example if I come back here you know what I will do just to simplify this thing more I will just comment all the other print statements to see only one output so here what I want to do is I want to print obj dot that's right if you want to even use the variable the instance variable you have to use the object name which is obj1 in fact the first object is obj the second object is obj one dot num so you can see we got two objects and we are printing the value of it each num value and if I run this code you can see we got five and five you know why because that's the value you have assigned now my question to you is before printing the value now if I say obj dot num is equal to eight you can see I'm changing the value of only one object what do you think will it reflect on both the object or only one object let's try so when I say compile and as one you can see it is impacting only one object it's because there are two different objects right example let's say if I have this phone this is Samsung and let's say I have the exact other Samsung phone and one of it so let's say if the glass of one phone is is broken will it affect the other other phone of course not there are individual phones and damaging one phone will not affect the other phone so that's the important point to remember here so yeah that's it about the data area so they have different areas we have stack and Heap in this video we'll talk about adding now first of all why do we need an array so in this video we'll talk about the need of an array if we talk about a variable so normally when you create a variable it's very simple right in a variable you can store a value now this can be of type integers flow double uh string doesn't matter you have a variable and you can store values in that example let's say if you talk about let's say int variables so how do we do that how do we work with into variable so we say int I is equal to 5. so what happens is you have a variable here which is I which has a value of 5. what if you have one more variable here which is let's say J and it has a value which is six maybe one more you have end K is equal to seven so you can see we have three variables here and you have three values now first of all why do we need three variables here is because we have three values what if you can save all these three values in one variable now why I'm doing that is because the moment you have more variables you have to work with multiple variables you have to remember those variable names uh you have to document them instead of that if you can save that in one particular variable it will be easier for you to maintain the data and also it's not just three values you may you might have ten values you might have 20 values you might have 100 values you don't want to create 100 variables right so for that we can use a concept called array here now how it works we can create a simple variable let's say num and in this num instead of storing one value I can store three values so let let's say if I have five six and seven I'm storing these three values of course we can put comma to separate them and then we can put them in a curly brackets to represent that this is a bunch of values I want to store and I want to assign that to a variable num now being a simple integer variable it will not work you cannot simply take a integer variable by assigning four values reason the moment you create a variable here it occupies some space now if you come back to this particular variable here of course this will occupy some space now in that limited space you can't store multiple values right you have to mention somewhere hey this particular num here is not a normal variable it's a a variable which can hold multiple values and with the way you do that is by mentioning a square bracket you mentioned hey that's an array which can hold multiple values now if you want to imagine this you can imagine a array as a tray of Cups so let's say from a kitchen if you want to carry four cups uh with you to the hall of course you can carry one cup at a time or maybe two cups because you have two hands or maybe you can carry just four cups by dipping your fingers in the cup but the ideal way is you can carry a tray now that tray becomes your array which can hold multiple cups so that's your array so why do we need to add it of course if you want to store multiple values in one particular variable we can use array here the beauty is it's not just limited to three values you can store 100 values it's just that once you define the size of an array you have to stick with it example let's say uh if you want to define a array let's say num1 and if you want this to be an array and you don't have initial values here we at this point if we talk about these three values you know what are the values are but what if you don't know the values in that case you can say new int array so you can specify okay so I don't know the values it's just that I want to create an array this is how you do it I want to create a array and you have to also mention the size here let's say four so this particular num1 will have four values not more than that okay it's a size is fixed you can't increase dynamically now once we know why do we need an array let's continue with the example now once we know why do we need an array let's start the implementation so in this video let's try to create an array now how do we do that in fact in the last video we have seen our basic syntax so let's use it so we can simply create a we can mention the type of the type of area we are using which is int and we can give a name to it let's say nums and then we have to put a round we have to put a square bracket to Define that's an array equal to and then if you already have the values here you can assign the values itself or you can specify the values dynamically so here what I will do is I will put a color packet here and since I know the values I can directly put the values here let's say 3 7 2 four so let's say we have these four values okay and now I got an array with four values it's it's that simple the only problem is how will I use those values how will I print those values example let's say I want to print all the values here or maybe even this first value let's say I want to print three because at the start if you remember uh when we when I mentioned that instead of using four variables I can use an array in the four variables it actually makes sense because every value will have a different variable name it's easier to fetch here how will you fetch the first element from an array now what happens is if I want to represent that array in a memory structure what happens is see if you have one variable you can create one box right but if you have multiple variables you have to create multiple boxes here so one two three four now this is your array which is names numbers I've got the same code which you are doing and then in this array you'll be having these four values which is three seven 2 4. now what it means is if you want to fetch the value of course in one variable you have four values now if you want to differentiate between all these values you have to use some numbers right some index numbers uh the way you do it for the table columns right so we have numbers in the same way here we have to give them numbers now if you are assuming that this will be number one two three four uh no that's not how your computer works so add a numbering starts with zero the point remember it starts with zero zero one two three now those are the index values in fact you know what should I do I should convert that into different color 0 1 2 3. now this numbers in yellow color are the index index numbers for the array okay so three seven two four there are the values but 0 1 2 3 is the index that means if I want to fetch the values I have to mention nums in describe but you have to mention the number which index value you want to fetch I will say I want to fetch zero now in this case if we do that let's compile and run you can see we got three this is working so whatever value you have here you're able to print it here what if I want to change this I want to print let's say I want to print the value at number one so compile run you can see we got seven so basically you can actually print this values one by one okay that sounds good uh how about updating the value so let's say I want to change the value 7 here I want to set 7 to 6. so basically I want to change the second value and I want the value to be 6. in that case you can say index off so when I say second value it's actually one because the numbering starts with zero so to say nums of 1 is equal to 6. now that's how you change the value and now if you try to fetch the same value here let's compile and run you can say we got six I hope that makes sense uh so basically you can assign the value you can print the value as well what if you want to make it dynamic okay how do I do that I don't have this values let's say and then still I want to create an array in that case instead of using this syntax here what you can do is you can say new int and in this square bracket you can mention the number the size of the array which is full now this is another syntax the only thing is by default all the values of this array will be zero so of course you've got four boxes but the values are zero and if I don't set the value here let's check what is the current value for the index number one which is zero so all the four values are zero uh I want to change the value so what I can do is you can specify I want to change the index value 0 to let's say number four and the nums of index one so every every index have to set the value let's say eight and nums of 2 is let's say three I'm just giving random values okay it's not like I have all these values prepared in my mind but let's say for three the value is nine okay so we got these values here now how it looks like in the memory it's very simple you got an array with four boxes so the index value is zero one two three and the actual value so before assigning the value of the values was Zero zero zero zero but now since we have assigned the value of the number the value has been changed the values are four eight three nine okay yeah so for eight three nine those are the values you have now this is how you store the value now if you can also fetch the value let's see what happens so at number one uh index number one we have value which is eight and you can see we got eight so this is working but what if you want to print all the values now that will be tricky uh let's try so what you can do is if you want to fetch all the values of course you can just print this statement multiple times right the only difference you have to do is you have to say index of 0 index of one index of 2 index of three and you can simply fetch all the values right so compile and then you can see we got all the values this is working yeah but don't you think we are doing the same thing multiple times it's just that the numbers are changing that to in sequence you know what we can do here that's right we can use a loop so I can just remove all the extra statement just by using one statement I can put that into a loop now the main question is when I start a loop here the loop will start of course you have to say int I but what should be the initial value for I it should be zero you know why because the index value starts with zero and then less than should I say equal to we can we just have to say 3 there because the size of the array support you have to stop at 3 and I plus plus that's one way or you can say less than four whichever format you follow okay and then just put that into a curly brackets now we can just shift this statement in the block so that it will execute so basically we are using a forward Loop to to print this now the only thing is if you can say we are mentioning 0 which means it will the loop the loop will run for four times it will simply fetch the index 0 I mean zeroth element every time or first element every time we have to change this we have to make it I so that the value of I keep changing here and you will get new value at a time every time so compile and run you can see we got the values so this is how you can use an array of course there are a lot of things related to array how can we create multi-dimensional are they can we use a different kind of loop so till this point we were able to understand what is an array right so basically we have seen this example where uh you know we have an array which has four elements right we have three seven two and four now if you look at this can you say this is array of elements of course it makes sense right so we can say this is array of elements now can I create something like this let's say I want two different arrays so I got this as a first array let's say four elements and whatever elements we can have here let's say five two six one so we got this array and then I also want one more array here right let's say we got in fact we need two molari and of course they will have some values it doesn't matter I'm not concerned about the values as of now we have some values there okay and of course every array need to have a name for it so can I say this is uh N1 array this is N2 array this is N3 array we can give any name to an array right in this case I'm just naming them as N1 N2 and M3 okay so if you're going to have multiple values as one array can we have multiple arrays combined to form one big array what I'm saying is can we just have an array of array something like this that looks weird but can I use a different color just to make you understand this what if you can just have an array so this is a big array which has three elements and this particular array the bigger element let's say the outer array we name it as uh nums so this nums array will be having three different arrays that sounds good right now this is called multi-dimensional array so ignore my spelling that but this is called a multi-dimensional array now how will you represent this it's very simple uh if you want to represent this we can simply say hey I have a nums array which is multi-dimension which will have array of array the way you can achieve that is very simple you just have to put two square packet because one square packet represents that's an array now can we say the nums array is of array of array yes we can and we have to mention the type as well so it will be of type integer let's say and then you have to say new and you have to mention again 2 square bracket now but what will be the values here see if you have a single array it's very simple you can just say the size is 4 but here if we talk about the size how will you define it so if you compare this particular thing can we say we have one big array which has three internal arrays to it so can I say this is three internal analysis and each array has four elements let me repeat in this big array nums we have three small arrays and each array has four elements okay that's how you define it now how will you assign a value to everything so what you can do is if you want to represent that in a good diagram here what I can do is I will just draw that once again so we have one big box and in this box we have three sections and each section has array and each array has four elements one two three one two three simple right now how will you give numbers to it so outer array will have number as well so it will be 0 0 1 2 and each array inside will be having their own indexing uh let's say 0 1 2 3. now let's say if I want to represent a particular element here now what element so let me assume it what element I'm talking about let's say I want to Target uh this element in fact let me just fill some values here five two six one uh three two one seven four eight one two random numbers okay nothing in my mind I'm just giving random values Here and Now I want to Target let's say the first value five how will you target five what will be the output for how will you find this value so 5 here becomes 0 comma 0 simple right because this is zeroth row zeroth column uh if I want to represent this six so this 6 would be 0 comma 2. if you want to represent this 7 so what do you think in your mind if you can think about this this will be 1 comma 3 and if I want to represent this 4 here this will be 2 comma 0 this 2 would be 2 comma three okay that's how you represent the values here quite simple right so let's try to create this in a code now so what I will do is I will just remove all the extras things from here I don't want student anymore and let me just simplify this stuff all the code gone and I'm I'm just assuming that you are doing The Code by yourself after watching each video and that's how you do it right it's not just about watching videos it's also about implementing it uh so what I will do now here is I will just create a array of nums but this will be a two dimensional array or a multi-dimensional array and I can say ain't here so we want three rows and four columns now there are different ways of saying there's three rows four columns or I can say we have three rows and each row has one particular array of size four as simple as that and here I can just come back and I can by default all the values are 0 right and in fact let me just do that let me print all the values now how do you print it we can use a loop here now the only thing is we have to use two loops or nested Loops we have talked about nested Loop before as well but let's try so what I will do is I will say int I is equal to 0 and I less than equal to okay I less than now what should be the value it should be 3 for the Outer Loop and then I plus plus and then for the inner loop see the outer loop will hover towards all the rows now we have to go inside the row as well so to go to inside the row we have to say I ain't I mean J is equal to 0 and J less than 4 and J plus plus and here let me print the value so I will simply say nums of so what is representing the outer loop it's I which represents the outer box and J for the inner inner box because for every row we have to start from 0 again right example if you see here uh it will start with 0 and then for every row it will go for 0 1 2 3 right once you reach 3 here it will jump to the next row and again it will start from 0 1 2 3 then it will jump to the last row and then again it will be 0 1 2 3. right so if you want to represent a number you have to say nums of I and J because I represents 0 and J represents every column here okay let me just run this code let's see what happens I will say clear compile and run you can see we got all zeros maybe you wanted to print this in a box structure what you can do is instead of printing Ln because Ln will point on a new line you can say let me print a value and let me also give a space here for after every number and then once you complete the inner loop once you print one row just come on the new new line so I will say just print again it will come on new line let's see if this works compile run oh we got it so we got a two dimensional array right the only thing is I want different values here so I can assign but I can assign them manually I think this is a good time to talk about how do you fetch the random values so you know we have this math class in Java which will give you some random values right so this is a inbuilt class which will generate a random value and to understand that let me just go to the random function random function returns a double value so it returns a double value if I store that value in temp or maybe random the variable name is also random the only thing is will not get integer value and also I guess the random function gives you a value which is in points decimal points greater than or equal to yeah basically it will give you value less than one so what I can simply do is I can multiply the number by 100 let's see what happens so whatever value I receive I will just multiply that by 100 again right I'm just doing some experiment here now once you multiply that we have to also uh do a typecasting now we have to convert that double value into int now whenever you want to convert something we can use something called a type casting so double value will get converted into int here and I don't want this to be generated on only once so let's change the value for I will just cut this part I will use it later initial value for random is zero and I want to find the new value every time to assign the values here and I can simply use the same Loop right let me just print that statement so that I will have it somewhere and I will just copy and paste it here to assign the values this time I will not print values I will assign the values so I can say nums of I and J is equal to this value this formula I think this should work I I just I'm not sure what will be the value it will assign but no we don't even need this variable basically but how do I know what value it is printing so let me also print the value of what it is assigning so that I will know what is happening so compile and run oh it's assigning zero values why I think it should be typecasted after we do the calculation compile run oh got it so you can see we got this random values uh so what was happening before is we were typecasting the random values and then we were multiplying by 100. what you have to do is since random will give you in point values like 0.12 or 0.74 so all these zero point values we just have to multiply that by 100 so that you will get a double digit number at least and you will simply Typecast it later of course it will have 72 points on values I'm just typecasting it and then yeah so let me just remove this printing so that you will get the exact box here compile and run if you want single digit I think multiplying by 10 should work I'm just assuming yeah A1 multiplying by 10 works because we only want single digit yeah that's how you Generate random values that's how you print it the random values are fun yeah so one of the library which we have used is math okay we can do one more thing here so while printing we have used a normal for Loop right can we use uh enhanced for Loop can we let's try so in enhanced model what you do is you use a for Loop and then from the array it gives you the first element right now coming back to our drawing board here if we talk about this particular array or this array here it will give you one value at a time right so we can simply accept that in an integer variable but what if two dimensional array here if we talk about hey nums I want the first element Norms will throw the entire array to you it will not throw one element it will throw the entire array and that's what you have to handle so here you can say ain't I will say n now n here is not a simple array or it's not a simple variable it's an array itself it is coming from nums okay so let me repeat n is not a simple variable it's an array a single dimensional array so we have to say with square bracket now once you get the array from that array we have to get the first value so we have to use one more Loop here and we have to say m colon n now m is taking the value from the array of N and then we can simply print the value so I will just remove this bracket I will say print M because that's the element and with that I also want to give a space and now here I just want to write into in the second line okay so both are doing the same thing let me prove I'll clear this compile run so this is printed by the first Loop this is printed by the second Loop the enhanced for Loop so that's why we use two dimensional array with different types of Loops so let's say we have talked about multi-dimensional array right now in multi-dimension Array of course you specify the number of rows you want so if you look at the code here we have specified what are the rows we have and in each big array we have add a small array which is of size 4 right and then we were assuming that every row or every array inside the array will have the same size example if you look at this three areas here they have the same size but what's the guarantee that they will be having the same size what if you have an array here which is of size three uh here you have a size four here you have a size two can we do that can we have the internal arrays or the columns basically having less values it is possible so what I'm saying is this 4 is not fixed we can simply say hey I don't know the number of columns I have yes we can do that so the internal array or the inside array can have different size of elements now this type of array is called a jagged array so before this we talked about normal or regular arrays where you have you have to specify the size at this point we are not doing that see we have to at least specify how many areas we have but internally those are those rows we don't have to specify how many columns we have that we can manage but how how we are going to manage so what we can do is once you specify that I I'm not sure about the number of columns here which is dag data then we have to individually specify how many columns you have what I'm saying is you have to say index of 0 is equal to for the first ad you have to specify how many values you have so you have to say new and three so that's the first thing we have and then likewise we can specify the other values I can say copy paste and paste for the first index I have the size let's say four for the second index I have a lesser size two so that's why we to manage this one now this is your Jagged array okay I mean this one this is your jack Daddy not a normal array because individually for every column we are specifying what's the size of it now if you if you try to imagine this in a new structure let me draw it here so you got a box this is the outer outer thing and in this you have three rows and each row will have a box now the first box will be of size three the second box will be of size 4 and the third box will be half size two okay so we have different size of array in different at different location so for zeroth index we have three for first index we have two or sorry four for the second index we have two that's what we have done here okay now once we specify that the thing is how will you save the value inside this now the problem is the number of rows are fixed but I don't even want to mention the number here the way you can solve that problem is by saying Norms of or maybe you can say nums dot length this will this will show you the number of rows you have but for every row okay in fact you know what we should do we should save that value somewhere um or maybe we can say this nums of I for every row I want to know the size so now basically we have we have specifically the size of each array so for the first array I have three values for the second I have four values so for every row how many what's the length we have we have here that's what we are specifying and now while printing as well instead of using normal for Loop let's use enhance for Loop and the beauty about now enhance for the paste you don't have to mention the size anywhere it will automatically detect where the values are okay so if I try to compile this code and run you can say this is the values you have the first row we got three second row we got four and the third row we got two okay now this is the jagged arrays so once we know about Jack Douglas let's go back to normal array what we can also do is we can skip this part now can we create three-dimensional array we can every time you want to increase the dimension you just have to specify extra square bracket and extra Loops for every every square bracket so now you can imagine if I want to draw this I don't know how do we how do we draw this but let's try so we have the outer box in which you specify how many rows you have we have three rows zero one two in this you have an array of size 4 so one two three four and in each box you have one more array here which is of size five okay let me just do that in a small let me expand this and in this we have one more array of size five okay uh this is index number zero one two three four and then this is zero one two three so if I want to fetch this value here which has value X is six if you want to fetch this six you have to say zero comma zero comma two right and of course in every box here we'll be having the same thing we have the box here which has four elements and each box will be having multiple arrays yeah so we have added here as well and if I want to fetch let's say the third element here we have to say one comma one comma three okay that's how you fetch it so the more array you create it will create a box internally to it so that's how the array Works uh now this is not jacked anymore this is three-dimensional array so I hope this makes sense uh we have talked about Jack that is and three-dimensional in one video in this video let's talk about the drawbacks of array of course we have seen a lot of good things about array we can store multiple values and then we had some fun with the multi-dimensional array and the 3D array Jack that is but let's talk about some drawbacks what I will do for that is let me remove the entire stuff and let's create a single array here so let's say when you create an array of norms and let's say this is of int type so of course you have to say new int and you have to give a bracket under this was for the size four now first of all we all beeping Java is an object okay the moment you're saying new keyword that means it's an object right it will be created inside the Heap memory but what it does is the moment you say this line inside the Heap memory it will occupy a space and the memory location it will take is Conti it's continuous okay which means it will simply occupy the space continuously and the problem with this is once you specify the size here you cannot go back so once you say four now you cannot expand the size it's fixed so in future if you want to create or if you want to expand the array you can't see one of the solution what you can do is you can at that point if you want to say hey I want to increase the size of the array just create a new array of that size and copy all the elements that's a solution but it will also consume some time and there will be some computation will be happening so if you know the size is fixed you can go with add a the second problem is every time you want to search something it will Traverse between the elements so for searching for inserting value it does consume a lot of time the last problem which is we we mentioned about Interior right but what if you want to have an array of different types let's say if you want to have integer string double in the same average unfortunately you can't do that I mean there's a solution uh if you want to have object of student integer string there is a solution of object but that's not that's not an optimal solution so again they are not really a drawback what I've talked about they that's how the array works but then you know when you say something is drawback you say that's a drawback because there is something new which came up which has all these features now the concept which I'm talking about is collections now this is not a good time to start with collection it will come in later but uh just because of collections we are talking about some drawbacks of adding but other Addies are great they are fast you can store multiple values but yeah there are some things which might have been done to improve it but that's where we have collections right we'll talk about collection later and it's not like just because of collection will not be using ad anymore they have different use cases every time you know you have a fixed size and if you want to store multiple values as they will be the first preference so once we have talked about array I mean basically which we have talked about why do we need array and how to create an array let's do something more with that in now in fact in the previous video we have seen how to create the array and then how to print the values now in this video let's try to see if we can find a better way of printing this value see what is happening now is let's say I mean of course we know the length of this array here right which is four ah it can also be five or any numbers I mentioned before right it can be 100 200 or whatever size you want to mention and you can insert the value now before I do anything else I just want to show you one more thing what if the value of the size of array is 6 and you are assigning only four values here now in that case the other indexes will also have some values right let's see I just want to see what are those values before we continue so compile and run you can see we got the values but then the last two values are zero remember we have talked about this by default the values for the array or integer array will be zero and that's what is happening Here and Now what I'm going to do is I want to uh see if I can get this value see the thing is why I should know the length of the array it might also lead to errors example if I say 7 here by mistake and if I try to fetch the value see this is where it will give you an exception now till this point we have not talked about exception of we have a separate topic what are exceptions at this point just remember exceptions are runtime errors okay so the address which comes in runtime at compile time there is no problem when you compile this code it will not uh blame you it will not shout but the moment you run this code it will say hey something is wrong here so that's exception now what is the problem the problem is we have six values index number star starts with zero it ends at five not at six and here it says index 6 is out of bounds so basically we are trying to go beyond the limit okay so how do we solve this problem so instead of specifying the number hardware values we can do one thing you know this arrays they have a property called length so we can simply use this length as a variable or a property which will show which will tell us what's the actual length of the so that you will not make any mistake the only thing you have to remember is you have to put that you have to make sure that it's less than not greater than okay and I will I will clear this I will say compile and run you can see we got the values and there's no problem the only thing is the size is 6 and then we are printing only we are assigning only four values let me go back to four values and you can see we got four values here now this is one of the good addition in terms of the property which you can use so that's about length here now what we can do is uh let's say if I want to have a string array or maybe I let me create a array of student can we do that uh so remember one thing so if I create a student here and of course I can create one object I can get multiple objects here and let's say I have a integer I have a student which has some values so let's say integer roll number okay and the string name and int marks of course not a good way to uh maintain the records we don't want to maintain marks but just for the example okay so we have student and then we have these three variables okay now mind you these are the instance variable because they belong to a class not to a method I just want to comment the entire section here and what I will do is I will create student object so let me create the first object I will say student S1 is equal to new student and I want to assign all the values to the student so I will say S1 dot roll number which is 1 and S1 dot name which is let's say Naveen and S1 dot marks which is let's say 88 okay so we got the first object now maybe I want to create more objects what I can do is I can just copy this code and paste and let's say three objects so we want three objects here and I will say this is two this is three and let's say well number two let's say this is hush and marks let's say 67 and if I say this is 3 let's say this is kitten and let's say the marks is 97. okay so we got these Marks here right and then basically they have your objects now I I made one mistake here if you can I don't know if you have observed but if you can see I'm still assigning the values to S1 don't you think it should be S2 S3 uh so it should be S2 S2 and S2 and there here we have to say S3 S3 and S3 so basically we got three objects here now what I will do is I want to create an array of students right we have created the array of integers but then how do we do that array of students it's very simple you say student and then you have to say students because we are creating multiple students here that's a variable name equal to now this students here is not one variable it's an array so I have to say array and then we have to say new now in this case when you say we have INT in the same way here it should be student right student array of let's say three students will be having so all these objects you know we got three objects here S1 S2 and S3 they will become part of this array example I can say student students of 0 is equal to S1 so the first object the first reference is getting stored here in the first index and likewise we have this is S1 S2 and this is S2 S3 so you can see we got three objects in the array so of course you need these objects otherwise this will not work now if you are thinking at this point we are creating present object no we are not getting three student object most of the people have this confusion that on this line we are creating three student object now we are creating an array which can hold student references it will not create those object by itself you have to manually create the objects and assign to an array now this thing will make much more sense when we start working with databases when you fetch data about a student it will give you a lot of data you can basically fetch those data in a student array now what's the advantage the advantages let's say if you want to print all the details of a student so what you can simply do is you can use an array which will start with 0 and I is less than equal to okay I is less than students dot length which we have seen and then I plus plus and then here we can simply print but how do you print the values so what we'll do is you have to print one value at a time you have to say S1 because if you try to fetch the value of a student it will give you a address format okay example let me show you something at this point uh let me just comment this section and let me print the student object itself and let's see what happens so if I try to print the S1 value you tell me what will happen what the what will be the output I mean think about that I will just compile this and run you can see it is not printing the value of a student it is printing something else see in the upcoming videos will solve this problem at this point it's printing the address and we don't know what this is so what we can do is explicitly you have to mention S1 dot name and you have to maybe you can do a concatenation here and you can say S1 dot marks I just I don't want to print the roll number at this point so I'm printing on you this too and now if you compile and run you can see we got the student details okay so can we do it here so let me just uncomment this section the only thing is you have to print this particular statement cut and paste now the only problem is it will always refer to S1 we don't want to refer to S1 right the student value will keep changing so I can say students square bracket I right so this value will keep changing right this will be S1 S2 S3 in every iteration the same can be done here so I can say students of I that should make sense and let's see if this works let me go back here compile and run you can see we got the values right so basically we can work with normal objects as well uh before this we have worked with integer of course you can work with string as well but we just went uh one step ahead by creating an array of students okay so I hope you got some idea regarding how do we get an array of students in this video we'll talk about for each Loop now why do we need another loop when we already have this particular Loop here okay for that let's understand let's comment the entire section from here because I want to use uh the simple example which we already have here right so if you remember we have worked with this array which has four numbers so let's work with normal integers now if you want to print all the values this is what we did right we used a normal variable normal for Loop in which you have a counter I which starts from 0 to the last value of the length of the array less than that which is three and we were able to print all the values the only thing is why you need to do the counter and then you have you should also know the length of the array and then you have to specify the index value here to fetch the value I mean this works but then can we have a better solution for this and that's where we got an amazing Loop which only works with array and array type of data so in future we're going to talk about collection as well and that also it is used but since we only know addeds at this point let's try to use another type of loop now think about this can we just say hey for Loop you know what I want you to do iterate between all the values and give me one value at a time and of course I will decide what to do with that value example here as well we are able to fetch a particular value of uh array and then we can do whatever we want to do with that of course at this point we're just printing it but we have an option of doing multiple things here as well I would say hey for Loop you gave me a value not the index number give me the value so what Loop says okay okay let me try so whatever value of order will give you you can store that in n a variable but then from where you will get the value you will get the value from the nums with which is the array so I can give a colon and we can say nums this is the Syntax for for each loop I know this will be confusing at the start but once you get used to it you will love it what is happening here is if you compare with the upper Loop in the upper Loop we are using a counter here we are not using a counter we are simply saying from these nums I it doesn't matter how many values you have four values 10 values 50 values I don't care you just give me one value at a time and that one value will go into n now you can decide what to do with that at this point just to keep it simple we are printing it okay so you can see I just got the value n and I'm printing it no I no counter no increment no need to check for the length no exception right and uh this is a beauty about it it will just iterate between the nums and iterate till the last element so if you had five elements it will iterate five times if you have four elements it will iterate four times and every time it will give the value to n and that's what you are printing it here let me prove that let me just compile this code and run you can see we've got the values this is better right you can also use this with the thing here which we have done for student I will just comment this part and uncomment the entire section from here everything is uncommented the only thing is I don't want to use the novel for loop I want to use the enhance for them so normally instead of saying that this is a for each Loop we say enhanced for Loop okay so what we can do is we can say for and here okay the only thing is when normal photo we use ain't because we want to maintain the counter if we talk about the example which we have done for normal integers here also we use we are using int because the type of values in nums is int but now we are working with students right so here when you say student let's say stud colon so we have to mention the student type not int because from the students array you are getting a student reference and you will get the values from students so it doesn't matter how many students you have it will fetch one student at a time and then with that student you can print the same thing so whatever you're printing here you can print that here the only thing is this two represent one particular student at the time so you can simply say student dot num and you can just concatenate so what we are doing here is we are doing concatenation the student name and then we are giving a colon so to do that we have to use a plus operator in between to concatenate and I can also say stood dot let's say marks let's see if this works now we are working with student not with normal variables compile and run that's it you got the output so you got Naveen and you've got number so this is your enhanced for Loop yeah some people do say it's a forage Loop but let's call it as enhanced for Loop because in the upcoming in the latest version of java not the latest one I'm talking about Java 8 which came way back it has a forage method so just to not get confused let's call this as a enhanced for Loop now one of the reasons why people say it's a foraged loop is because in other languages this type of Loops are called for it that's why in Java also people have a tendency to call it forage but let's call it enhance for Loop so I hope you got the idea and I would like you to work on this practice this otherwise it will be confusing so practice this multiple times and you will get used to it it's time to talk about a very simple concept and complex at the same time because to use it's very simple but when you try to understand what is happening behind the scene that's where it gets complicated so let's try to understand that in this topic of string so in this we'll just talk about what is string now basically we have worked with string before example whenever you try to create a variable we can have we have to specify the type of it right example we have worked with integer float double Boolean in the same way if you want to store a character stream let's say if you have multiple characters something like this let's say uh you want to store a name like Naveen now this Naveen is not a single character right it's a bunch of characters and when you want to store this together of course you can use a character array we will talk about either later but then we can create a character array here which is a array of different uh different characters or we can create a string here now how do we represent that in a string now one thing to remember whenever you talk about string in Java you have to put that in double quotes the way you work with characters in characters you use single quote here we'll be using double quotes So if you want to store Naveen so you have to first put that into double quotes and then where you will store this of course you have to you you need a variable here right so what I will do is I will create a variable called name now this variable name needs a type now what type of this this is of type string now one thing to observe when you talk about primitive data types we have end float double character uh Boolean and many more right now all those actually starts with small letter right uh this string s starts with capital which means that string in Java is not a primitive type it's a class basically if you go to string class you will find string is actually a class okay that actually makes some sense but then when you say class we have seen this right whenever you work with classes and if you want to create object of it we use a new keyword right we are not doing it here so this does not look like a correct syntax it works but it's not something we are used to right whenever you work with class we normally create a new object okay this works but then why this will work that we'll discuss later at this point if you want to go in a general sense if you want if you have a class the way you can create a string is by the way you can get object of string is by saying new string and you can give the round packet now this is how you create a string variable a string object now basically this is a variable right reference variable which is referring to a string object now what might be happening behind the scene so it's very it's not that difficult uh if you imagine this as your jvm The Big Box inside this of course you will be having your stack memory right now this is your stack and then of course in this uh you will be creating a reference variable which is name and then it will have some address but this address or the object will be created inside Heap memory imagine this is your HEAP memory you know we should make this a big one this time let's say a bigger Heap memory so that I can write some multiple things there now the reason we are creating a heap memory in another structural format is because there is no fixed size to it it may expand depend upon your requirement so we got a heap here now when you talk about this line line number eight when you say new string of course it will create a new object for you you got object here right and it will have let's say some address I will say the address here is one zero five so of course you will create a variable which is name here and then the address is one zero five so there's a link here and of course in this particular box there is no data it's it's empty right okay uh this empty makes sense actually but then if you try to print this let's try let's see what it prints if you try to print this data here I will compile this code and run you can see it spin it prints nothing basically it prints blank thing okay it's because it's blank now how will you provide data now in this case what you can do is in this new string in the Constructor you can pass this string whichever you want to example if I want to store Naveen that's the D time storing ignore this original this is given by the IDE but this is our value which is Naveen so inside this string Constructor you are passing the data which is Naveen right now if you try to print this you can say we got Naveen here okay so that means in this thing uh we are creating the data which is Naveen that's what we are expecting as of now let's see if that works okay now there's also a concept of hash and equals right so what I will do here is I will try to print the hash code for this one so if you try to print the hash code and if you run this code you can say this is the hash code for this depend upon data depending up on some calculation now what else we can do here so example let's say if I want to concatenate this Naveen with hello so let's say I want to print hello Naveen what you can do is uh of course you can write a different statement to print hello and then you can print the name but then string by default does not work with any other operators like subtraction division but only there's one exception which is plus orbital so you can use string with plus operator okay so let's say I want to print hello and Naveen both so in this case you can print hello and then to concatenate to mix this two you will use a plus operator here so you can simply say plus and now with this compile and run you can see we got hello nabin I hope that makes sense so you can use plus to do to print this what else we can do uh we have certain methods here which we can use now since it's a class of course it will have some methods right so there are some handy methods here example you can check uh what is a character of a particular location so let's say I want to know what is a character at location two I mean index number one so if I pass index 1 here it will give me a character at index 1. so if you look at this Naveen it is n is index 0 a in is index one then two then three then four and if you try to compile and run this you can see we got a so yeah this is how you can basically use certain methods there which is character add or you can also concatenate two strings okay so let's say if you concatenate my name with I will give a space and ready so basically what I'm doing is I'm concatening Naveen with ready here let's see if that works okay so that we got Naveen ready so that's how we can basically use this now since strings are so important for the programmers what we got here is uh let me just remove let me just remove this line yeah so string is so important for programmers what we got in Java is instead of writing this new string you can actually you know directly write Naveen here even that works so behind this it will create an object you don't have to worry about it so when you say string name equal to Naveen it will create the object for you you don't have to worry about object creation of those stuff so most of the time you will see this syntax hardly you will say uh string name new string and then Naveen that's hardly anyone do that so this is famous and it even this works you can still try this out and that's why before when we were using string we directly assign the value it was working so in the previous video we have talked about string right now we talk about this string uh what might be happening behind the scene example let's say if you store this Naveen of course it will be getting stored in the in the hip memory but now what I want to do is before printing Naveen here let's say I want to append something so let's I will say name is equal to I want to add my name with let's say ready here I'm giving a space so that we can differentiate these two words so of course now when I say name it is will it will be Naveen ready right so basically we are trying to change the value now if we talk about integer float double basically you can change values of it right and it actually makes sense that's why we call them as variables and here also we are trying to do that but first of all let's check if this works so if I go back and compile and run it works you can see that the new data for the Naveen is Naveen ready right and that works but what if I say that's not the case you're not changing the existing string let me repeat you're not changing from Naveen to Naveen Reddy so if you're thinking there will be a variable here where you have Naveen you're making it Naveen ready uh no that's not the case that's not how string works okay so what happens so what I would what I will do is I'll just remove this part now so what is happening basically you know since strings are used heavily in Java now most of the time you try to reuse the strings the same string you'll be using multiple times example let's say uh if if before I show you something let's say I create a string here which is S1 is equal to Naveen and you can see we have a capital M this time so this two data are actually different okay so I will say string S2 is equal to and I will say Naveen again now what do you think how many objects we are creating behind the scene think about this okay so if you're thinking we got two objects actually not we got two references we got S1 S2 of course in your stack you'll be having S1 S2 here but in total you got only one object I know that sounds tricky here right and let me prove that point to you uh see what I want to prove is the value for this field and this field will be equal the way the way I can do that is by saying s out S1 is equal to equal to S2 if this is true then they are same so if I run you can say it got true that means the value for the addresses are same in total we got only one object so here what happens is we have a special area inside your hip memory so this is your HEAP that's your jvm and that's your stack so inside your hip you will be having a special area called string constant pool and by the name itself you can see it says constant that's right every string which you create which you use in Java which is a string literal is actually a constant you can't change it example if I if I write something like Naveen here I cannot change it it's done it's Naveen everywhere okay so you can't change the data so example what happens is every time you try to use a variable example if you execute the line number eight it checks okay we need Naveen variable do we already have Naveen in the string constant pool no we don't have it so let's create Naveen here so it will create a string variable Naveen it will have some address let's say one zero one and then the name variable here will get this address so this name variable is actually referring to this particular address okay then it goes for the next statement let's execute this part Let's ignore this line as of now let's focus on line number 12 and 13. so line number 12 says S1 is equal to Naveen okay so it checks do we have Naveen with capital N in the string pull no we don't have it so it will create Naveen here and let's say the address is 1 0 3 so it will store 103 here okay now that makes sense right and that's why you're referring to that particular location now what happens on line number 13 line number 13 says string S2 is equal to Naveen it will go to string constant pool again and check hey do we have Naveen with capital n yes we do have now it will not create a new object for you it will simply use this address one zero three okay and that's the beauty yeah that's how you're trying to save the memory otherwise you have you could have created multiple variables with the same data so it doesn't make sense right and that's why it uses a string constant pool now my question to you is what is happening here now when I say you can't change the data that means you cannot simply update this particular Naveen by Naveen ready no that's not happening the wind is still there it will be it's a constant you can't change this now on line number nine what is happening is it is creating a new object which will have a data which is Naveen ready it let's say it has the address which is 105. so on line number nine basically what you are doing is you are changing this address from one zero one to one zero five you're just changing the address you're not changing the actual data you are basically creating a new object okay now you might be thinking what will happen to Naveen here the thing is now this particular object is eligible for garbage collection it will be removed after some time just to get some memory back okay so Point remember is once you create this object you can't change it and that's why we have some terms to remember and the terms are so there's a concept of mutable strings and immutable string now what is mutable means mutable means change something which can be changed and immutable means unchanged or you cannot change it so basically by default strings are immutable once you create the object you can't change it and that's very important remember it's immutable string okay so whatever data you're creating here is immutable but what if you want mutable string and that's why we have two different classes in Java to use instead of using string you can use something called a string buffer or a string Builder now both are very similar with one difference that difference will discuss later but they both are similar and they both provide you a way to implement a immutable string okay in this video we'll work with string buffer so what we have is we have a string and where we have worked with it right so what I will do is remove everything and let's work with string buffer so I will say string buffer and let's create a reference for it equal to new string buffer now when we talk about string buffer of course in this string buffer you will have a string and which is actually mutable you can change it now to make it more efficient you know what string buffer does string buffer will give you a buffer size okay which is of 16 bytes so I can just try to print that first I will say SB Dot ah so we can know the capacity of this string buffer even if it is empty it will give a capacity of 16 characters and you you got it there in fact you can also provide some initial string here so let's say uh if I say Naveen here now Naveen is of five characters right this is how you basically create a string buffer in bracket you mentioned this string so it will create a string of the object and the beauty is you can change this data okay now the thing is uh if we talk about capacity what it will be so capacity is should be still 16 right no it actually changes if you can see the capacity will become 21 it's because it will store my name and it will also give me a 16 space extra now why it gives me that space is because every time you change data and if it consumes a continuous memory location inside your hip memory and what if there's no continuous location next I mean what if there is no location available there is no space available in that case it has to relocate so to reduce the relocation of the string it gives you a buffer okay so that's the capacity and now we know how it works we can also print the length of the string we can say SB dot length and length is available in normal string as well so if I compile and run you can say the length is five so length n capacities are two different things okay now can I append data see the ultimate thing was can I change my data which is Naveen in this case the beauty is string buffer will give you a lot of different methods to do that you can see we have a method called append so you can use append and you can append any data which you want to so let's say I want to append ready here and of course I want to give a space between Naveen Naveen and ready so you can append and you can print the data so you can print SB as it is let's first we have to compile the code run and you can see we got nothing ready that works next what if you want to assign string buffer data to string is example if I do that you can see it will give you error it says type mismatch cannot convert from string buffer to string of course right STIs of type string so in that case you can get this string back uh do we have a method for it let's see yes we do have two string we can use that I think that's the only method we can use yeah so let's use two string so basically you can use two string to convert the string buffer data I mean it will get the string format it was stored in Str and you can print it okay we can use that what else we can do with string buffer if I come back here if I say SB dot you can see we have so many methods here okay so we have append we have gas it will give you a character we have done that okay so we can also delete something we can delete a character at an index so let's say I'm going to delete a character at location two if you try to delete that data compile and run you can see we got oh something is missing the V is missing there because that is at index number two okay so the idea is we basically we can change it and you can experiment it a lot you can try to use different methods we can delete we can insert also in between let's say I want to insert a Java at the start you can do that so you can say insert you can mention the index value where you want to insert a desert location and the data we want to which you want to insert so I will say Java space now what it will do is it will add Java at index 0. I mean that is start itself so if I run you can say it says Java of course you can add Java in between as well let's say I want to add after fifth location I think that should work after the space is six so let's try that compiled one you can see Naveen Java ready so it works okay so yeah that's how we can basically use insert we have more methods to explore and I would recommend you to do that can we see one more we can also get this substring let's say if you have a very big string you can specify the starting point ending point using substring you can set the length as well so if you set the length of the stream let's say I want to set the length of 30. so if you want to add your data and if you want to have a specific length which is study here it will save the data which is whatever you have now been ready will be stored and all the other values will be null let's see so after Naveen ready it will add some extra data here okay so that you can replace that later cool or we can do that we can also set a minimum or maximum data I think minimum capacity method which we can use I think the method name is ensure capacity yeah okay so we can say we can we can set the initial capacity to 100 so it by minimum capacity will be 100 okay that's how you have different methods now what's the difference between string buffer and string Builder so as I mentioned we also have string Builder right there's only one difference string buffer is thread safe and string Builder is not now what is thread safe means that will understand once you talk about thread concept so at that point also uh we'll talk about difference between string buffer and string Builder but whatever methods you have in string buffer it's almost same as a string Builder so if you know one you know the other one as well okay so that's about string buffer and some of the methods of string in this video we'll talk about static keyword from the start itself when we have written the first code we are looking at the static keyword and the question Still Remains right what is the static so we know what is method is we know what is written type we know it is class but again problem gets stuck here in static itself now to make you understand this let's take an example here so let's say we have a example of mobile phone and every mobile phone has a brand so brand will be spraying Services string brand and and we can have a default value for it we can have int which is price and then string Network okay and we can also make it for a specific brand as well but let's keep it simple let's have a mobile phone different brands different price different network and we'll say name okay now see name itself of a mobile phone changes right so we used to say normal phones then we got smartphones and now we have foldable phones right so we can have a different name to phones nowadays every phone is called a smartphone now because that's how that's what we call initially it used to call mobile then we used to call that phone right so we have a name here as well what do you call a mobile phone so now it's a SmartPhone okay so if you want to work with this mobile phone of course you have to create an object right so if I say mobile obj if I will say mobile obj1 and equal to new mobile and of course every mobile will have a different parameter here what I will do is I will just remove this network just to keep it simple we just want three variables here obj one dot brand I want to set the brand here let's say the brand is Apple uh since I'm using an Apple phone here and then the price let's say 1500 dollars and what else we have we have a name so what do you call a mobile phone so um currently if you talk about Apple phones or any phones nowadays we call them as a smartphone okay I know this variable doesn't make sense but just for the example I'm saying a name is equal to smartphone right now if you talk about this phone here we have all these values right now of course if I create multiple objects it should have a different value of course every object is different so we can have a different value to it if I come back to Apple let's say the next phone is of Samsung and the price is let's say 1700 and then we got we also call them as smartphone okay the only thing is you have to change it here as well object 2 obser 2 and then I can print the values as well um you know what I will do I will just create a method which will print everything I will say public wide so it should print all the details here so it should print brand so that I don't have to print that every time and I will say price and I will print name as well now by doing this what I'm doing is instead of printing it here every value I can simply call obj1 Dot show and obj2 Dot show so it will print its own value not other values and if I come back here compile and run you can see we got the values right nothing complex here the thing is if we talk about these variables here they are called instance variable right now if you create a variable inside a method that's a local variable but this are your instance variable and every variable for a particular object is different right the value will be different now what I want is I want to have the name to be same for all the objects see because what is happening is in in your memory so let's say we have a jvm a big jvm and inside this you have a stack where you have all the reference and locations and address and then you have a heap memory so in total you can see we got two objects right one with mobile obj one and one is object 2 and then of course they have the address as well so this is one zero one one zero three and of course this will be one zero one one zero three and you will be referring that right so this is referred here this is referred here now that's how we can basically use it and every object will have a different value but what if I want to have a common value for all the objects the problem with this is if I want to make sure that the name variable should not be a part of the object because that's an instance variable now it will be a part of the object and every object will have a different value I want this name to be common for all how do I do that now if you make this variable as static Now by doing that what we are doing is we are making this name variable common to all the object what I mean by that is let's say after some time if I say obj1 dot name if I change the name to let's say a phone here just a phone not a smartphone anymore if you make this as a phone and if you see the values it will affect all the objects you can see we have we are trying to change the name of obj1 but we are actually changing the name of obj2 as well you can say this is also phone now the reason is the static variable is actually shared by all the objects so basically will not be having individual static variable for every object we have a common variable which will be used by all the objects imagine there is a common variable here name and all the objects will be referring to the same variable so if you change the value for this it will affect all the variable all the objects okay now this is why we use static now the beauty is see why we have to refer brand with obj1 price with obj1 is because every object will have a different value for it now don't you think name is a static variable and it will be common for all the objects and that's why you can actually call name with the help of a class name itself you can simply say mobile dot name so static variables should be called with their class name not with objects you can call with objects but avoid it and that's why you can see it gives you warning as well it says the static field mobile.name should be accessed in a static way which is using a mobile uh the class name which is mobile okay so the point remember here is static variables are actually shared by different objects and if you want to refer to a static variable you can use a class name so there's a different area in your jvm where you have all the static variables and here as well I should say mobile now the thing is in non-static methods you can use static variables there is no harm okay and that's the point to remember in the next video we'll talk about what if you create a static method yes we can do that you can create static method and the question is can I use instance variables there okay we'll see that in the next video but here we talked about what is a static keyword so static keyword means you are making something as a class member not an object member so static this name here belongs to a class not to the object and that's why you are able to use this with the help of a class name okay and then yours you're saving memory as well because not every object will have their own value for it or their own copy for it so that's about static variable now we know what is static variable static methods right now let's try to expand this more to simplify this what I will do is I will remove the static method for some time and I will also decrease this code let's only work with one object now and here see normally when you work with variables so we you now we know that this is an instance variable and this is a static variable right the name now to instantiate the instance variable what we do of course we can instantiate the variables from here but then what if you want to instantiate the variable on this line if you want to give some value not instantiate if you want to initialize the variable what you will do so basically we have seen one concept right which is Constructor so using Constructor we can say Okay public mobile and we can give some default values to it right so by default the brand will be empty string and by default the price will be let's say 200 right so this is the default brand default uh text and default number right which we want to give what about name I can also give a default name to a name which is phone in this case let's say we can do that we can initialize this static variable inside a Constructor and there's no harm in that but the problem is every time you create an object every time you create one more object it will call the Constructor right and don't you think it makes sense for this two to initialize every time but why name has to initialize every time I feel that the name variable which is static should be initialized only once what do you feel of course the same thing right how will you do that so if you mention that here of course every time you call the Constructor it will install we don't want to do that not to achieve that we have a special block called a static block now in the static block we can basically initialize the variable the static variable now the good thing about this is this static block will be called only once irrespective how many objects you create it will call the static block only once so what I will do is just to demonstrate that I will create one more object here let's say and I will not assign any value to it just to default values so object to Second object so it will call the Constructor two times it will call the static block only once to prove that what I will do is I will just print something I will say in static block now the Syntax for static block is very simple it's static keyword and then you just open and close there is no bracket or something it's just a simple block okay and in the Constructor as well I will say in Constructor okay and now with this let me just compile this code okay there is an issue I have written one extra okay I have removed the country package from here so compile and run so you can see it is calling the static block only once and just calling Constructor two times there's one more issue why static block is called first even before the Constructor it is calling setting block and that's weird is it the sequence let's try let me just put this after the Constructor let's see if there's any make doesn't make any sense anyway Java does not follow the sequence but just to confirm you can see the static block is still getting called first now to understand that let's go to the diagram here see what happens you know with there are two words we and we only focus on one word every time you get the object that two steps one your class loads and then your objects are okay so first your class loads and then your object gets instantiated so there are two steps every time you create the object there are two steps class loads and object will then will get initialized the thing is the class will load only once so basically in your jvm you have a special area here which is called a class loader okay and this will have all the class which are loaded and by default of course you don't have any class loaded right so the class will get loaded from from your library to the class loader and it will happen only once so for the first object when it sees hey there's object creation it will load the class first and then it will create the object now every time you load a class it will call the static block now since glass loading happens first and that's what you will call this edit block first and then it will create the object and that's why it will call the Constructor after select block now irrespective of how many objects you create the class is already loaded right so you can simply create a next object without loading because it's already loaded now my question is if that is how it is working what if I don't create any object I'm not creating an object now so if I don't create the object of course it will not call the Constructor but what about static block it will not even call the static block if you can see we don't have any output oh okay so that means if you if you don't create the object it will not load the class as well but what if you want to do that what if you want to load the class how would you do that now for that we have a special class inside Java which is called a class class yeah that's a class okay I know Java has been weird we have object class we have class class but that's how it is designed so Java class if you go there you can you can see it's a class and it has a lot of methods to it one of the method which we want to use here is for name now fourth name is a method which does one thing which loads your class and you just have to mention your class name what's your class name or the class name is mobile so in this you can mention the class name that's it okay the only problem is it throws an exception okay at this point we have not talked about exception but just ignore this line uh we'll talk about this in detail later ones which start with the exception concept but as of now just to make it work I'm just writing this line ignore it what's important is the class dot for name will load your class if I go to font name you can see for them is a method and it's a type method so you can directly class you can call this with a class name and this particular method do they have any definition mentioned okay so it says the system class loader is used so basically it will load the system class loader to load this class okay so which class mobile class okay so now if I just write this line it will not create the object it will just instantiate it compile run you can say it says in New static block okay that means you're just loading the class so basically that's how you load your class you can simply use the this line to load your class normally when you build a project you don't actually use this feature yes there are some situation where you use it example when you start a jdbc we'll talk about this one but we normally don't use it I'm just using this just to show you how to call a static block how to load a class okay I hope this video makes sense where we talked about static block to so static block is used to initialize the static variables so that's it from this video in which we talked about static Block in the earlier videos we have talked about static keyword static variable static methods and yeah that's super static now once we talked about static variable it's time to talk about static methods now what I will do is I will just try to remove all this content from here just to keep it simple and let's make a static static method now the method which we have here show is not a static method right it's a instance method it's because we have not mentioned a static keyword now whenever you want to work with a non-static method you need to create an object of it of course if you want to call you have to create object of Mobile in fact you know actually we need those objects here and yeah let's keep it here just to use it now let me create a simple static method so I will say public uh static void show one again if you can see I'm using a different method name just to differentiate it and here if I try to first of all I will just print in static method now how do we call the static method to call show we have to get object right you can't simply come back here and say mobile dot so this will not work if you try to call mobile.show it will give you an error it says let's see the error it says cannot make a static reference to a non-static method that means you cannot call a non-stick method with the help of a class name and that's why you need object here which we can see we're using obj1 object two but can I call show one from mobile let's try and we can you can see that there's no problem so you can call a static method directly with the help of a class name okay that's what we are doing here we're actually calling it with a class name now the question is this looks cool right starting method can be used directly with a class name but can I call can I use us variables here let's try to do the same thing I will just copy this line and paste it here let's see what happens the moment you do that you can say there's no problem with name so that means you can use a static variable inside a static method but you cannot use a non-static variable inside a static method the question is why I mean think about this brand and price they are instance variable that means it will be different for different objects now when you call show with obj1 we know that in this show when you're printing brand and price you're talking about obj1 when you call show with object 2 you know that the brand and price will be of object 2. but then when you call mobile.show1 how do we know which Which object you are referring to so this brand belongs to obj1 or it belongs to visual two and that's why it says hey there's a confusion right let's not do that let's not have a non-static variable directly getting access in a static method it's not possible uh this time I have added a directly keyword that that means entirely is possible now how would you do that indirectly it's very simple what if you can actually pass the object itself you can say hey I want to run show one for obj object obj one object now since you are passing the object you have to also accept it here so you can say mobile obj and now now since we have the object I can refer that with the help of obj1 dot brand so basically we cannot directly use a non-static variable inside a static method but if you have object reference then you can use it because now we can differentiate so the brand which I'm talking about is for obj one not for object two so that's how you have to do it so that's how you work with static method now if You observe here from start we are using this right the main method is static why in this world we have a main static is because if you don't make main static if this was a syntax the problem is if you don't mention static that that means main becomes a non-static method which means if you want to call Main we have to first create the object of demo oh that means you cannot call Main without the object of demo but the question is we all know that main is a starting point of execution if the execution has not started how can you create object of demo okay so there's a deadlock here right uh so to solve that problem what we can do is we can make this variable as static so to solve that problem we can make this main method as static the moment you do that we are saying hey you know to call Main we don't need object of demo problem solved right so yeah this is how we can solve this problem and this is what static methods are uh anything else I think it looks good in the next video let's try to see something more about static there's a concept of static block let's talk about encapsulation now I know that's a new term but when we started with oops concept now encapsulation is a part of oops so when I say let's Implement object oriented programming so yeah we have talked about class we have talked about objects and the next step is encapsulation here now by word what encapsulation means you know we have the concept of capsules where we have medicines powder there so basically we can imagine capsule as something which is keeping it close keeping it tight and no one from the outside world can use it that makes sense right so what we can do here is let's say let's design a class okay and let's name this class as human I know we can use different names but let's go with human here and I know we are aliens but let's say let's say this is human now here when you have this human class a human will have some parameters right example uh if you remember we have talked about this before as a human or as a person we know a lot of different stuff so in our mind you have a lot of different concepts or you know a lot of things example you know your name you know your where you stay you know your favorite actor you know your favorite trainer of course you can name you can use my name there but just kidding so we have different information right now can you imagine this that all the information are actually stored in your brain as a variable box I'm a big fan of pixel movie which is I don't know the exact movie name but you know uh Pac-Man basically eats people and then they become pixels so you can imagine the data in your mind is Cube okay and in that box you have the data example your name is stored in a box which name Naveen or which names name is equal to and in the Box we'll be having a value for mates Naveen let's say if you are your favorite actor so you'll be having my favorite actor and the Box Inside the Box will be having a value so what I'm saying is all the data is stored in your brain in the format of boxes variables now of course uh you can use these variables right so let's say my name is Naveen and let's say if I'm walking on a street and if someone is asking my name I might provide my name what if someone is asking my email address I might provide them my email address depend upon who that person is what if someone is asking for my uh children's name my wife name my family name my Facebook password no I'm I'm not going to share those things it's because there's something called privacy right and yeah I know a lot of people don't believe in privacy but of course no one should be able to get your data directly right it's not like your data is available for the entire world to see it's there it's closed no one can see the data of course they can see my Facebook profile but no one can see the data inside your brain that's important if they want to know your data they have to ask and you can decide do you want to share or not in the same way let's say we have this human class and in this human class we have a variable called age and we have a string which is named that's it just two variables and now if I want to access those data of course I have to create object of human here and let's say this is the human object Neo human and once you got this object with this object you can fetch data right uh of course you can set the values as well and for this particular object I can say obj dot h is let's say 11. and obj Dot name is let's say Naveen I know that's not my age but at this point it's okay so we got these values and we got this object created here now what I want to do is what if someone asked me hey human or maybe someone is trying to directly trying to fetch the data example if I say s out and if I try to say obj dot name now what we are doing is just by using the object anyone can fetch data okay and this will work okay it will not give any issues I will say compile and run you can see we are able to fetch data and syntactically there is no issue but then if you really think that we have to implement software for the physical world or the real world it should match with the real world not all the data are directly accessible and we want to control it so one way to do that is what you can do is you can make your variables private now what do you mean by this private keyword of course we'll have a detailed discussion later at this point private simply means this particular variable which is age is accessible only in the same class let me repeat this very this age variable is accessible in the same class the moment you make it private it's private no one can no one from the outside world can use it of course I can use my data because that's private to me but you can't access my data okay that's probably to me right the same thing can be done for the the invariable here and if You observe the moment I do that we got an error here I mean of course vs code helps you that vs code says hey you know this variable age is not visible you know why it's not visible because it's private now going forward this is what you have to do every time you create a instance variable let me repeat every time you create a instance variable make it private no one from the outside world should be able to use it now you will say hey that doing that we are actually Breaking All the purpose of building a software right if no one is able to access this data then why we are even creating these variables that's right the reason why we're getting this variable is because we can store values and someone should be able to access them they should be able to access them but not directly okay so there should be some indirect way yeah we have indirect way so the way they should be able to access this data is with the help of methods example let's say if you want to know my name you have to ask for it and I will decide should I should I share the data with you or not so if you say hey what's your name I will say okay I will ask my brain what's my name because most of the time I don't remember my own name so I will say hey what's my name my brain will say okay your name is Naveen and then from my methods which is my mouth or my by using my handwriting or by typing somewhere there is a way I have to share that data with you right I have some methods Behavior I can talk I can write I can type Now using those behaviors I can share the information okay not like anyone can just come to me by it they will break my head and say okay I got the data now that's not how it how it should work so how do I come how do I uh restrict it so what we can do is we can make it private we are just restricting it the only way to access them is with the help of methods so what I can do is temporarily I will just comment these two sections and I will assign the value here itself and we can do that I will say name is Naveen and then here coming back to public I will say public I want to get data I want to say hey get age okay and of course you can use any method name it doesn't matter but let's say get age and this will return a integer value so I will say int here and this will return so this will basically return the age now the question is can I access age here and the answer is yes you can you can access the age variable inside a method of a same class that's right this get age is defined in the same class that's why you are able to access that and if you see age here or name here so we are trying to get the data of another class demo and human there are two different classes okay makes sense now now we can also do it for the name so for the name I will say public string uh string get name and this will basically return a name okay so we got two methods get age and get name and then we are returning the age and name now if someone wants to fetch it I can simply say Hey you know don't call name that will not work because name is a variable which is private you can access the methods you can say obj dot get name that's how you can fetch a value of course you can also fetch the age what I can do is I can just say plus I can give a colon in between and I can say obj dot get age okay now I think we should be able to access it let's try and compile run oh it's working can you see that we got Naveen and Levin so the point remember is every time you create a variable which is private we are making sure that it is restricted no one from the outside world should be able to use it so this age variable this name variable will be used by their own methods that actually makes some sense okay but there is one little problem here what if I want to assign the value what if I don't specify the value here cut and cut let's say we have the variables but don't I don't assign the value now of course by if you don't assign the value default value will be 0 and a null string but I want to how do I set the value now for getting the data we have said get for setting data can we say set let's try so here for age I will say public now when you're setting the value you are not getting in return something right so you will say void and you will say set age okay now it's just that when you are setting the age you have to ask hey what is the value so maybe you can also accept a value here maybe I can say a right and then here I can say age is equal to a now what I mean by that is of course you have a method which is accepting a value assigning that to age but how it will work so let's come back here which the the portion which we have commented I want to set the age right so instead of setting the age to the variable I can say obj dot set age we are using a method now not the variable and in this particular bracket you can pass the argument and let's say the age is 30. so now we are passing the value now this value will be assigned to this a and from a a this will be assigned to age can we do the same thing for string let's try I will say public void sect name and of course we have to also accept the name but what's the format of a format of name it is string name I will say end this time and here I can say name is equal to n so whatever value you pass will be assigned to This n and from n it will be assigned to name now the same thing need to be changed here so I will just cut this part and I will say cut this part as well I will say set name and in the double quotes I will pass the name and the name here is Naveen and we will use a different name this time I will set a d okay and now I will just come back here clear the screen compile Run Okay so that we got ready and study so what we are doing is we are assigning the value with the help of methods here and we are getting the value with the help of methods here so that's how we can access this data now what we are doing is we are making your variable private and then the only way to access them is with the help of methods right so basically we are binding our data with the methods the only way to access that data is with methods so no one from the outside world can use your variables directly so somewhere we are encapsulating the data and methods and that is encapsulation okay simple stuff right uh so yeah that's it from this video let's try to understand this more what is this get and set methods uh can we use different names or only this name will work that we'll discuss let's try to understand what these methods are or can we change the name say get Aid set age makes sense but can we use some other names actually we can instead of saying get age we can say let's say we can say ABC and set age we can say x y z we can do that okay there is no harm in that the only thing is when you call this methods of course you have to change the name I will say this is XYZ so when you set the value you have to call XYZ and then when you fetch the value you have to say ABC and it surely works if I uh try to compile this and run you can see it works now the problem with ABC and XYZ or any name is your method name should Define what user what is your intention because most of the time it's not just about writing code it's also about reading code when you write a code once it will be read by multiple people in the entire lifetime of a software why not use proper words which explains what that method is doing so instead of saying ABC if I say get age each it is actually makes sense right it is much more readable what you're doing there right and as I mentioned before it is not a compulsion to use the same name but then as a developer we have adopted this standard whenever you want to get the value of a variable the method name would be get Space the other variable name the variable name which is age to set the value you will say set and you will use the variable name which is age okay so that's how we can use this so This is called a getter so for the variable user this is a getter and to when you want to set the value so the method is called Setter so we can say that's those are Getters and Setters uh in fact the beauty is if I delete the entire part okay let me just do that I know we are we need to type that once again actually you don't most of the ideas have this amazing feature you can just right click and there's an option of source in fact it doesn't matter which ID you are using uh you will see the option of source or insert code something like this so it will say source action what do you want you want to get you you have to say generate Getters and suggest for me click on that for both the variables and click on OK you can say it will generate the methods for you and you see that variable names it's same get age set age and then we got get name set name okay and those are called Getters and Setters and of course there is no compulsion to have both the methods you can also you can only have get name you don't want to have set name that's your choice there's no compassion that there should be both it depends upon you how you want to implement it for this example I want to assign value to H name I want to get the values as well and that's why I'm using this show so only thing is for every variable you need two methods Getters and Setters okay so yeah that's it uh but there's only one issue now if I use the code which is given by the IDE you can see this new keyword here we got this in this video let's talk about this because in the previous video we were doing something and then suddenly we got this keyword so it's important now to talk about this keyword here now what I will do is I will just remove this or I'll just change this variable name back to A and N and of course here we have to say A and N right and let me remove this this keyword from here just to see if this works now if I remove this keyword from there and you find on this code compile okay there is some problem with the variable names I think we are still calling x y z so it should be set age and we are still calling ABC it should be get H the same thing which we have done in the previous video okay so if I save this and if I go back and compile so compile done running done so you can see we got the values right the thing is when you talk about this variable like age and when you're passing a here we are using two different variables right now the point to remember here is this a variable here is a local variable this age variable here is a instance variable right if you want to understand that with a diagram uh you can just go back here we have a stack memory remember we have talked about stack and Heap so we got a stack here and then we got Heap here now the moment you create the human object we got this object here human so in this we have a human object and of course it will have some address let's say one zero one so we got obj and the address is 101 that's the first object we got and there is a link here which we can cannot ignore and then we have some more variables you can see if you execute and of course this was my main stack uh now we have executing which method so if you say set age we are executing set age method right Now set age will have its own stack here let's say we have a set age stack here you know what I just have to zoom it a bit and here let me create segments so we got multiple segments here and of course this will be linked with uh this particular variable here and don't you think this this box which is address once the one is an object of human so it will also have the instance variable how many instance variable we have we have age and name so we got age and we got name here now if you look at the set Age Now set age this particular stack uh is of set age every method will have its own stack and in this we got a variable called a and the value for a which we are passing is study so we got 30 here okay and then the same value 30 we are assigning to age that means we are changing this variable and we are assigning a value which is study there if that makes sense okay so both the value for a and age study and that's why when you fetch data when you say get age it will return the value of age in the object which is study in this case and that is perfectly working now the problem is what if I just make this particular variable age as age I mean a as h now what I want is I want my local variable and my instance variable both to have the same variable name what do you mean by that is if I remove this a from here the variable name here will be H so both the variable name the local variable and the instance variable have the same name then it should work right what we are trying to do is the age variable will get 30 here so of course in this stack we got 30 and if you see we are assigning H is equal to age so I'm expecting that this age will get the value from this particular age it should work right let's try if I go back to my code and compile and run oh can you see that the value for age is still zero that means the assignment which we are assuming that it will be 30 it is still zero why the reason is if you look at line number 11. and if you see it we are saying age is equal to age the problem is by default now see this age can be local variable this age can be instance variable right because the names are same so can we do this can we say it is possible that the age here is actually a local variable it is also possible that the age is assigning value to itself maybe this age is not even an instance variable that means this age is assigning value to itself and this age here which we have created it is still zero and that's why we are getting 0. how do we solve this problem so somewhere we have to mention hey you know this particular age here is not a instance variable this is not a local variable this is an instance variable so remember this point the preference will always give always be given to local variable so in case of confusion it is always the local variable we have to mention by some way hey this is not local variable that's an instance variable how do you mention that so in order to say that's an instance variable can we do this can we say a human can we get object here itself object one equal to new human and then we can say obj1 dot h now what we are doing is we are saying this age is a instance variable and you might be thinking hey how can we use age with a object see we can do that because we're in the same class okay we were not able to do that here because we were in different class but again the problem is we are saying this age is instance and this age is local this should work let's see if this is a problem or we have solved the problem let's run this oh it is still zero what is happening let's try to understand more now when you execute this set age method what we are doing is we are creating a new object oh that's the issue we are basically creating a new object okay um how do you solve this problem we don't want to get new object because the moment you create new object what will happen here is it will create a new object let's say 103 and this will have this will also have age equal to name equal to and what value you are signing here your assigning value which study and then here in this set age you will create obj1 which will have the address 103 and there's a link here so the value which you are changing is of this age not this one we want to assign the value to this particular age that means creating a new object is not a good idea okay how do I clear this let's remove this in fact you know this is a problem this is creating a new object okay if I don't want to get a new object how do I access this obj oh I got an idea here what if with this 30 if I can pass the object itself can we will it solve the problem with 30 if you can pass this particular object and we can accept the object here what type of object it is it is human human obj and then here we can simply assign the value so what I'm doing is if you if you observe correctly we are not creating a new object we are basically passing the reference of the old object so here it will be not 1 0 3 it will be 1 0 1. okay so this obj1 here will get the address 1 0 1 and we can simply say obj1.h and now we are not referring to this address we are referring to this address so we can change the age value 30 and let's see if this works I hope this will work compile sorry compile run oh it is working it's working so basically the important thing is how do I differentiate between a instance variable and a local variable is by passing the object itself but there is one little problem here the problem is if you if you read this line it says obj dot set age and then you're passing the object of the obj object as well why we have to do this why you have to mention obj two times knows this Java says hey you don't worry why you have to pass the object two times I mean you're calling the method by using the object you're also passing the object don't do that don't pass the object by default I will give you this obj you don't even have to accept this object what if Java says hey don't worry don't create the object don't pass the object I will give you the object and that object my friend in fact you know when we had a human here we don't even need this human line here we could have removed that and even obj would have worked so your Jawa says hey why do you even use this obj we can when we can simply use this now this is a keyword which represents the current object now when I say current object what it means the object which is calling the method set H is called by obj which is this object here in this method we can actually use that object with the help of this keyword so this refers to the current object who is calling the set set age I hope that makes sense so from this point we are always going to use this which represents the current object okay so here as well instead of using name or N I should have used name the only thing is we have to refer the first name here is this dot name which is the object itself the current object I hope uh this all this thing have made sense this is what this keyword is in this video we'll talk about Constructors now what are Constructors now look at this example here which is of human now when you talk about human we have some variables right of course when you talk about these two variables these are instance variable and then we have seen in the image as well uh basically whenever you create an object example in this we are just creating one object and that object is getting created inside the Heap memory you can see this is the object we got and of course with that object we have assigned the value for the age we have assigned the value for the name as well which which is ready but I have not written it here but we know that the object is created now the thing is when you create the object if You observe the line number 29 on this line when you create the object what happens when you create the object in the hip memory it will just create a blank object example if I try to remove this if I keep it empty it will create a object with a value so the value would be I mean it will have a variable which is age and the value will be 0 and it will have a variable called name and the value will be null right I will just I will just try to print those things uh what if I do is I will just print the same statement before assigning the value right so I'm printing it here and I will comment this section so that's how you comment right I don't want this line to be executed and now if I go back here and compile and run you can see we got null null basically is when you don't have the data assigned to object type that's right string is an object type I will discuss about string later in detail as of now just remember by default the the value font string is null and the default value for integer is zero okay that means if you don't assign these values there will be default values what I want here is of course there is a way you can assign the values later but what if I want to assign the value when the object is created that's right on this line itself I want to assign the value that means the moment I get the object I don't want to have a null value I want to have some default values example let's say by default every object created the name should be something else the name should be let's say Babu or something else I'm not sure but can we have a default name can we have a default age so let's say for my application uh this application can be used only by those people who are 12 and above so I can simply say hey the minimum age for this is 12 so by default the value for the age should be 12. can we do that so how do we achieve that of course we can assign the values here or we can use something called a Constructor now how a Constructor looks like so a Constructor looks like a method itself let me repeat a Constructor looks like a method itself that means a Constructor will have a name a Constructor will have the round packets example if we talk about methods here we have a name and we have a round bracket in the same way for the Constructor as well we have to use that so what I will do is I will say public now the only changes in the Constructor you don't specify the written type okay so we can directly mention the customer name here but what should be my Constructor name now there's a compulsion that your Constructor name should be same as your class name so Michael what is my class name it's human and then since it looks like a method we can give a round bracket and we can open and close the curly brackets now one thing I wanted to mention you can see with the cuddly packets are mentioned just I mean for the opening brackets are just mentioned on the same line and here the curry packets are mentioned on the new line both Works Some people prefer to use on the same line Some people prefer to use on next line so anything works now in this Constructor I can assign the values but at this point let's not assign the value I will just simply print the statement I will say s out and here I will print in Constructor so you can see we are printing in Constructor here so that's our Constructor okay so let me repeat what is Constructor instructor is a special method that's why because it looks like a method it has the same name as a class name so those are the rules you have to remember the Constructor will have the same name as a class name a Constructor never returns anything so don't don't have to mention ain't float or doing anything there so just specify the access which is public and then mention the name which is human and in this you can do whatever you want okay so I'm just printing in Constructor so let me just try and compile so comparison done there is no problem code one thing to observe nowhere see when you talk about this methods like get age or set age all these methods we have called them right so this is where you are calling them Constructor you can see we are not calling it but still Constructor will be called okay so even if you don't call it it will be called now when it will be called every time you create an object it will call the Constructor so every time you create the object it will call the Constructor example let's say if I create two objects I will just copy this line and paste it here of course I should have a different name for the reference obj1 now you can see we have two objects now when you say you have two objects and if I try to compile and run this you can see we got in Constructor two times so every time you create a new object it will create a Constructor or it will call the Constructor okay if that makes sense uh what we can do is if we can remove this line and I can assign the values I can say age is equal to 12 and name is equal to let's say by default John of course you can have any name but let's say John now in this case I can see if this if these values are assigned what I can also do is I can just comment this section let's say I'm not assigning the values let's see what are the default values compiled one you can see we got John and 12. so by default you assign this value okay so this is where Constructors are helpful now you might be thinking hey we can assign the values here itself right uh see there's a idea there's a standards which we follow whenever you want to perform any operation always do that in a method don't openly do it in the statement like here if you want to perform some operations like assignment or calculation always doing the methods and Constructor is like method right so you can assign the values here there is no harm in terms of compilation you can assign the values here as well but we have to follow standards right and also just try to think about something Advanced let's say you are trying to connect with the database now in that scenario your application is trying to connect with a database or the network you need that connection right now where you would like the connection you can write the connections in the Constructor and that's why this becomes very important now if you remember we have also talked about method overloading so when you have two methods with the same name but different parameters those are called method overloading now can we do that in Constructor that's tricky right okay so now we know what is constructed right but what about this I mean can I pass the values here what if I don't want to assign values like 12 and John what if I want to customize it so what I want is every time I create the object I should have an option of passing those values not 12 and John every time can we do that of course you can assign the values later with the help of set methods but what if I have an idea where hey you know I okay let's let's go with this object as well the second object and in the second object I want to pass the values the first object I want it to be default the second object I want the age to be let's say 18 and the name should be let's say Naveen can we do that if you can see this line is giving you error it says hey you know you don't have a Constructor which accepts two values so those two values of type integer and string you don't have it and if you go up actually you don't have it you don't have a Constructor which takes two parameters can we do that let's try so I will say public human and I want this human to take two parameters so I will say int a comma string name and then here I can just come back and I can say age is equal to a so not the default value which I want to assign here I want to assign the value which is sent by the main method so if you can see we are passing 18 and Naveen here right so I can just go back here and H is equal to a name is equal to n now if you look at this particular Constructor here it is taking some parameter so this Constructor is called a parameterized Constructor oh I should say this is n now what is the difference between normal Constructor and a parameterized Constructor is in fact it's normal constraint is also called a default Constructor now we'll discuss why this caused default Constructor so this is called a default Constructor this is called a parameterized Constructor because we have parameters there and whatever value you pass here will be assigned now you can use both syntax you can either create an object by not specifying any parameter or arguments and you can create a object with the help of values which we are passing it here so this is parameter wise and this is default now what I will do just to explain this is I will just copy the same line and I will print the values for obj1 this time not for obj obj one so basically we are printing both and let's see what's the output is compile run you can see we got the first object drawn 12. the second object is not John and 12 which is Naveen and 18 because you are passing those values now what I will do is timeline I will remove this part just to explain you something see when I mentioned that every time let me just comment this section as well see every time you create the object it will call the Constructor right but then if you see I have removed the Constructor will this work and it was working before remember when we started this example of human we were not having any Constructor it was still working at this point your Java will say hey don't worry if you don't mention Constructor there I will create a Constructor for you and that's why we say that's a default Constructor so behind the scene you will get this Constructor here which is public human this is what you will get even if you don't mention okay the only thing is it will be default it will be blank if you don't mention but if you want to do something of course you have to mention that explicitly and we can use the same code or I can actually ask my IDE to give me the code so you can simply go to source and action and you can say hey I want to create a Constructor but which Constructor so I want a Constructor which takes both these arguments and I also want a default Constructor Source action I want a Constructor which is default you can say we got two Constructors there's only one change the syntax which we have returned was a and name and here if you can see we have age and name and to make it work we have to use this keyword I hope to remember this keyword video previously and that's how it works it has the same way which works the only thing is we have to assign the values here I would say age is equal to 12 and the name is John so that's how we can basically use a default Constructor and a parameter-wise Constructor of course we can create some more consider here what if you want to just specify the name not the roll number so you can say I want a Constructor Source action and I want a Constructor which takes only name so you can you can say we got a Constructor which only takes a name now what happens to this type of objects example if I create an object here let's say the second object and this time I'm not passing two values I'm just passing one value which is name in this case the age will be zero so what you can do is even if you're accepting name you can assign the age by your certificates at 12. you can say age because there is no confusion or you can also say this dot h just to maintain the uniformity okay uh so that's how we use Constructors default Constructors parameterized Constructors in this video let's talk about this and super now this is a very super concept so let's focus so what I will do is I'll just remove all this thing just to make it simple and clear this part as well and for this I don't even need uh this particular classes so let's let me just close these files and keep it simple okay so we have everything very simple code here now what I will do is just to understand this and super I will create a class called a I know this is a very simple name but just for the example we can take them and then we got a class B okay we got two classes and then I'm also saying that this B Class extends a of course we are doing an hat and CR and it makes sense because we just talked about inheritance and then what I will do is I will create a Constructor for both of course we can do that right now before creating Constructor let me say if I create object of B of course we can do that right so this B Class we call an object of B Class here and we can do that let me create a Constructor of B here now how do we get a Constructor it's very simple you specify public and then you say B back it open and then here I will type in B I'm just trying to keep it as simple as possible that's why we have taken Class A and B and then a very simple statement now what I will do is I will try to compile this code and you tell me what will be the output comparison done right if I run this code there will be some output right now think about this we are creating the object of B okay we are instantiating it and we have learned about this that whenever you create the object it will call the Constructor of a class so the object we are getting is a b and we have a Constructor which is B here which is which prints a statement so of course I'm expecting that it will execute the Constructor and it will and you that's why you got in B that perfectly makes sense and we have talked about this for a long time The Twist here is what if I have a Constructor in a as well and here I am printing in a now what do you think will be the output because of course we are creating the object of B not a so it should call the Constructor of B and that makes sense but if you compile this code you will see something strange if I compile this code and run oh it is also executing the Constructor of a now that's weird for us right because we are creating object of B it should only call the constitution of B but it is also calling the Constructor of a and that sounds weird now we'll understand that but at this point just remember whenever you get object of a class it will call the Constructor of subclass and superclass both a is a superclass here B is a plus it will call the Constructor of both why we'll talk about it in some time but it calls the constitute of both now let me just add a Twist here in B let me create a parameterized Constructor and we have seen that right so what is parameters Constructor a Constructor which takes a value let's say this is n okay and here I will print in B int because we have a Constructor which takes a integer parameter I'm just printing e just in just to differentiate between these two statements now with this if I compile this code and run what do you think what will be the output compile and run still it is in a and in B why not in B int is because when you are creating the object you are not passing any value here basically you are calling a default Constructor but if you pass a value let's say 5 in this case you are specifically mentioning hey you know I'm creating an object of B and I'm passing a integer here so it will call the Constructor of B which takes integer Now by doing this if I run this code or you can see it is printing in a and in B int okay that actually makes sense it will call the concept of a but what if I also have a parameterized Constructor in a as well which takes a n and it also planes in a end now since we were not having a parameters Constructor it was calling a default Constructor that's what I'm assuming at this point now when you have a parameterized Constructor for both for A and B both now there is an expectation that it will print in a int and in B int and that's what I'm thinking I'm not sure what you're thinking uh let's drive let's see if this works oh no what is happening here it is calling the parameters constitute of B that actually makes sense but why the default concept of a it should have been called this this one right let's understand why it is first of all Calling The Constructor of parent class why in the first place the thing is and remember this point every Constructor in Java has a method which is there even if you don't mention okay and that method is super now this method is there even if you don't mention you can imagine that's like a John Cena I don't know if you know the John Cena memes but that's just that you can't say it okay so super is there you can't say it even if you don't mention super it is there so by default every Constructor the first statement is super and every Constructor not just this one every Constructor here as well super okay so every Constructor will have super same goes for a as well it will also have super and super irrespective which class it is it will have this statement super even if you don't mention it is there okay that's a point to remember now let's try to understand how it is working let's do a dry run the moment you execute this statement what is happening it is calling the Constructor of B which is parameterized so it is calling this particular method so when you execute this method the first thing it sees is super not the system.out.pental N what it is saying is super now what is super now super method simply means call The Constructor of a superclass so let me repeat super means call The Constructor of a superclass now watch this superclass this is a superclass it is calling the Constructor of a superclass but but which one the parameterized one so the default one if you can see we are saying default we are not passing any parameter here so just calling this particular Constructor and that's what is printing in a and that's why if you can see the output it is print first printing in a and then once it executes the a Constructor it will execute the remaining part of B Constructor which is this part in B int and that's why we got in Bay end okay that means if I don't want to call this Constructor if I want to call the parameter wise one we have to do only one change which is what if in this super you pass a number so N is a value right it will have a value the same value you are passing to this super now what we are saying is hey Java I know you are going to execute this super glass Constructor but I am passing a parameter there so now this time you have to call the parameterized Constructor by doing those changes let's compile and run here we go it is printing in a int and in Bay end so super is a very handy tool it's there already you don't have to mention that but if you want to make some changes of course possible may you have to mention so if you want to pass a parameterized Constructor then explicitly you have to mention super I hope that made some sense okay now what I want is I want to call the default Constructor of B okay I'm not passing any parameter here I'm calling a default of B so of course if I execute this if I compile and run you can see it's printing in a and in B why is because when you are calling the default Constructor here in default concept of B it will execute first it is calling this super that means it is calling this middle Constructor which is printing in a and then it is printing in B it will continue with this execution of consider B right but what I want now is I know you got the point what I want is I want to execute in a int and then I want to print in B so the output which I want is in a INT in B so how can we play with that so what if here if I pass a value let's say 5 or any number doesn't matter if I pass a number it will try to create object of B with a default Constructor it will execute the default Constructor but now we are calling a superclass Constructor by passing a value so which one which computer will be called of course the second one and it will print in a INT in B let's try compile run and that's what we got so that's how we can play with super and that is fun right now I have a question for you maybe you have observed it or not see in B Class when we are saying super it actually makes sense because we have a super class for B okay this is a here is a superclass but when you say super for a what is a super class for a we have not mentioned anything here right okay now that's why you have to introduce A New Concept here every class in Java that repeat every class in Java extends the object class so object is a class in Java which has a lot of methods we'll talk about those methods later but every class in Java extends this class even if you don't mention so even if you don't mention this it is extending it again you can imagine this as a John Cena it's there you can't see it okay so even if you don't mention or you mention it's the same thing So when you say super here you're basically calling a Constructor of the object class okay an object class also we have a Constructor this is what you are calling cool right uh so even if you don't mention it is there now you might be thinking the b class must be extending a and object both no B Class extends a a class extends object so this is multi-level inheritance okay there is no multiple still now okay so yeah that's how it works we have object class so with this super represents the Constructor of the object class so yeah so we have talked about super but what is this okay now let me introduce this here the output which I want now is I want to execute both the Constructor of B okay so let me just keep it default now let me say super I want to execute both the Constructor of B so I want only one object okay with one object can I execute both the Constructor of course I want to execute the Constructor of a which is default but I want to execute the Constructor of both P so what I'm saying is I want in a I want in B and in B int is it possible uh for that what we can do is see we are creating the Constructor let me pass a value here let me pass 5. so of course by doing that what we are doing is we are calling the Constructor which is the parameterized one and we are calling super and that's what he's calling the superclass Constructor but then we have one more method called this we have talked about this keyword right now what this this will do is this will execute the construct of a same class let me repeat this will execute the Constructor of a same class which is this one we are not passing in a parameter so it will call the construct of a same class and then this Constructor is calling the construct of a superclass let's try if this works compile run and you can see that we got in a in B in B int I mean of course this will execute first this method but this method this particular method B Constructor is executing this which is the default Constructor the default Constructor executes the super which goes here it will execute in a it will execute in B and then in Bay end so that's what is which is this keyword and also this method and super method so this method execute the constitute of the same class super method executes the construct of a super class in this video let's talk about the naming conventions now if you see the code which we have written till now somewhere I was using capital letters I was using small letters when to use what and then what if you want to join two words how will you make it more readable now that's where Java says it follows a naming Convention of camel casing so basically it uses camel casing now what camera kissing means or let's talk about in general how do we name our classes now whenever you create a Class A Class name should start with a capital letter so when you talk about class it should start with a capital letter example if you want to get a class called calculator the C should be Capital now on the other hand if you talk about variables or methods so for variable and methods what you will follow is you will start with a small letter example if your variable name can be marks a variable method name can be show so it will always start with small class capital variable method small in fact we also have a concept of interfaces which we'll discuss later so if we talk about classes and interface we'll have to make it capital let's say interface is runnable so basically this is the convention which which we follow class interface we have capital letters variable methods we have small letters we also have a concept of constants which we have talked about uh so when you talk about constants it will have a capital but all capital example if we talk about the value of pi if we talk about a constant of brand so it doesn't matter what your constant is so you can have all capital here now why do we do this we do this just to see and understand what could be the thing which you are doing example if you say if you see this type of things you can guess hey it can be a class or interface if you look at this you know this is a very this is a variable if you look at this you know this is a method and we look at this this you know these are these are constants it makes your code much more readable and that's important modern right it's very easy to write a code which works it's difficult to write a code which anyone can read and that's why you should follow this convention so this is how you use the main convention but what if you have a word which which has two letters example if you talk about show my marks if I want to do this so you have to say show my mark because if you say show my marks it is actually a bit difficult to read right so what you can do is every time you combine words make sure that the first step of the second word or the first letter make it capital it makes it much more readable compared to small letters now this is what camel kissing means you know we have camel so it goes up and down so we have small letters then we have in between we have Capital small again we have Capital so this is camel casing now apart from this there are different languages which follow snake casing rule where we use underscores example in some languages they follow snake casing they use underscore and people who jump between different languages they get this tendency of using underscore so again Java will not give you any error but if you follow the Java pattern Java developers will not like this so if you're writing a code where you have a Java team or everyone that works on Java try to follow camel casing and but again it will not give you either it perfectly Works underscore works in the names but that's what we follow we follow camera guessing here not snake casing yeah uh if you have the class name which is again uh I want to say my data is a class so you can say m is capital because that's a class name now I will give you I will show you some names and you have to guess what those are example if I say h that's variable right okay what if I say data what is this this is a constant because everything is capital okay uh yeah in in terms of constant if you want to say my data which is a constant you have to use my data but again everything is capital right so at this point you can use underscore just to differentiate this and again these are the good practices which I'm showing you how about this if I use something like human but I use a round bracket any guess yeah that's a Constructor why is a Constructor is because the first is capital that means it represents a class name and we also have a round packet method like something so that's a Constructor so this is a naming convention which we follow and try to use good names for your variables your classes your methods so that it will be easy for others to read okay so now we know how to create the object right so we have a class let's say we have a class A and then in this class A we have a show method now how do we call show method that's very simple first you have to create the object of a and then you can call the show method now what happens behind the scene so of course uh you have your big jvm here and then in this you will be having your stack memory and then you'll be be having your hip area as well now in this Heap memory so if you look at line number 14 it will create an object let's say the address is 1 0 1 and then in this you will be having a variable or methods we don't have a variable as of now so let's say we have a method we just show so of course in order to call this show we need object so we have done that and then we have obj here the address will be 1 0 1 and it will refer here and then every time you want to call show basically you have to say obj dot show that's how it works right okay now just to clear the things this obj here is not called the object it is called a reference variable right so this is your reference variable what is called a reference variable because it is referring to an object the object is here so this is your object okay so the question is if I want to create this object is this line compulsory or do we have a shortcut now to understand this mode Let's also use a concept of Constructor so let's say we have a Constructor here which is a and then I'm just trying to print something here it says object created so of course we know every time you create the object it will call the Constructor as well so it should print so if I compile and run this code you can see it says object created and it is printing in a show now actually if you look at this line here there are two steps not one example so let's say if you create a local variable called marks okay and then you set the marks for it you set the value for it let's say 99 we had everyone hates this type of people right anyway so we have this variable so what we are doing is we are first of all declaring the variable and then we are as a value the same thing can be done you can do it in two two steps we can declare the object and then we can say obj equal to new a now this is called reference creation right this is where you're creating a reference and then this is where you are creating an object and assigning the value to obj that means this is I mean when you say new a this is where you create the object let me show that let me remove everything from here and say new a this is object creation not the entire line when you say new a Constructor that is your object creation the moment you do that in your hip memory of course we don't have all this data now so ignore all this data which is which is there let me just remove this this is not there imagine there is no object so this line will create an object doesn't matter what's the address let's say 501 and it will have this show method okay the only thing is we are not doing anything we are not creating any variable inside stack example when you say ain't marks the moment you do that it will create a int variable called Marks here the value will be the value will be blank and whatever value you want to assign you can assign that here but if you can see we are not doing anything here we are just creating a object that is created inside the Heap memory nothing in static and if you don't trust me let me just show you the proof If I compile this code and run it says object created this is the object and this type of objects are called Anonymous object you know why it's because this type of object don't have a name right what do you call when you have a reference it is called a referenced object okay because there is a reference in this tag so when you say obj there's a reference here for the anonymous object there is no reference the only problem with this type of object is you can't reuse them once you have written this line you got the new object in the Heap how will you use it that's the problem right so you should use this object only when it is required so let's say if you want to call show you can actually call this okay so you can say new a dot show this is object right this is object so using the object you can call show the only thing is you can't use the same object again so if I compile and run it worked the only thing is you can't use this object once more so if you write this line once more what will happen now I mentioned that you can't reuse it what happens when you do that you can see it says object created so it has created two objects every time you say new a it creates a new object so whenever you want to use a normal object you can use this of objects now let's talk more about oops concept now oops basically stands for object oriented programming now in object-oriented programming we have talked about object class encapsulation right now let's talk more the concepts now one of the most important thing in the concept of Ops is called inheritance now what is inheritance now there are two words actually here which which will confuse people is is and has now what these two words are now if we talk about a computer now of course computer itself is abstract concept right now A a computer can be a laptop a computer can be a desktop a computer can be a mobile phone that's right so even if you talk about this phone this is a computer in the form of mobile phone of course and the computer itself is an abstract concept no one owns a computer you basically buy a hardware which can be a desktop laptop or a phone now can I say that if you talk about a desktop so I'm talking about the desktop which is this big and it will have certain parts right a desktop will have a motherboard a desktop will have a hard drive CPU Ram so when you talk about this thing it is desktop has a motherboard desktop has a hard drive it has a CPU right so the important keyword there is has if we talk about her house a house will have a TV a house will have a sofa AC bed so all those things what a house has right so house is an important word but at this point let's not focus on has in this video let's talk about is now what is is okay so example if you talk about a laptop so can we say a laptop is a a computer so what it means is laptop got it features from a computer so can we take another example here if we talk about a car let's say let's say if you talk about a Fortuner which is very famous in India so this car is actually a car right so Fortuner is a car so that's how you represent something you represent a device or a physical entity by referring to its upper model right you try to take the features of it now coming back to the enhance Concept in real world example let's say if we talk about the human world you know when I was in my 10th standard so just for the example okay nothing not that important but when I was in my 10 standard I did my 10th in 2005. at that point Nokia released a lot of smartphones right of course Nokia is not there anymore but Nokia was one of the most important factor in the smartphone industry now when they came up with a smartphone one of the most famous smartphone which they built is double six double zero I'm not sure if you have seen that I'm not even sure if that you're that old maybe you were not even born in that year but but yeah so it was double six double zero uh amazing phone if you can just search on Google uh Nokia depositor double zero you will see that phone now a lot of my friends were having that phone it was costly phone at that point it was around 10 000 rupees and I was not having any phones so they used to ask me hey which phone you have I used to say hey I don't have a phone but then I realized hey till when I will say that uh the thing is next time when people used to ask me which phone I have I used to say I have double three one zero now I was not having a new phone now this is my father's phone and we know the concept right father's phone is ears phone in fact in in general when we say our parents property it's your property whatever they own it's yours I don't believe in that okay I'm not a big fan of inheritance on nepotism but yeah you get everything uh so I used to say I have that phone okay so this is it heightens now how will you use this concept in programming now in Java what you have is you have different classes right example if you create a Simba class here and let me get a very simple class let's say we have a class here called a calculator now what features a calculator will have a calculator will have some variables here and then it will have some methods let's say method is ADD method is sub okay and then method is multi so we have different methods here or let's also have division why division should be the left there so we have all these methods right and it is working so you can basically create object of calculator and you can access all these methods no harm in that but what if you want to create a advanced calculator now of course let's say uh if someone comes to you and say hey you know you made an awesome calculator and now I want you to create a scientific calculator or advanced calculator now you have a choice you can edit the same class but there's only one thing what if you enhance the same class you add some more methods here now you want to give calculator to someone someone asking you for the calculator object they basically access all the basic features and advanced features as well so it is good to create a submit class okay so you can get a separate class and you can say advanced calculator right and then this will have all the advanced features of course it will have all the basic features and it will have the advanced features as well so we have basic features Advanced features both but don't you think you need to Define these basic features again and again why we already have those features here right we already have those features here can I just use those features in my new advanced calculator so what I'm saying is basically this advanced calculator is going to inherit the properties and features from calculator so I can also say Advanced character is a calculator and it will get all the features of calculator okay uh it will make much more sense once once we start coding but what we are doing here is we are doing inheritance so basically inheritance is a very important feature using which you can just copy the feature from the upper class so this is your can you can I say this is a parent class so this is a pattern class and this is a child class because child class is accessing the feature of parent class okay so whatever I earn it's my money plus my parents money is my money that's how the system works right we can also say this is a super class we have different words for it that's it and we can say this is a subclass we can also say this is a base class not your base and this is a derived class so it depends upon how you represent that if you say this is a parent class then you have to say it's a child class if you say that's a super class then it becomes a subclass if you say this is a base class you have to say this is a derived class that's how it works okay now this is what inheritance is so in theory we have talked about what is an Essence right basically you can just use the features of a superclass let's try to Implement that in this code of course will not be working with humans anymore let me just remove the entire code from here and all gone and let me also make sure that this is empty gone so we have a very simple method which is main here now what I want to do is I want to take the same example of calculator here so let me get a class called calc and of course I can just create this thing uh in some other files as well that actually makes sense but just to keep it simple I'm using the same file here so we got a class called calculator and in this I can add some methods and some variables so let's say I will Define some methods here and I will say public and it can add two numbers it will accept two integers as well I will say N1 comma and two so we got two variables right and then I can just return N1 plus N2 okay now let's say in this calculator we have two methods one is ADD and one is sub which is sub here and there's there will be a subtraction operation so we got two methods right now one thing to remember if you look at file explorer every time you get a file it also creates a class file so for every class in your code it will have a class files we have used a calculator a computer human student before and that's why we have all these classes what I will do is I will just delete them okay so you can see all these classes are gone in fact let me also delete the demo one to see what happens and that's fine you can also delete you can delete all the class files so every time you compile the code it will also create a class files for you let me let me show you that so I will say Java C and if I mention the the file name which is demo.java the moment I say enter it will create two class files because we have two classes if I go to explore you can see we got demo.class and we also have calc.class now when you run this code if you say Java and if you say demo uh it will just run the class files and as you can see we are we don't have any code here for calculator so it will only run the demo class and that's how it works so if I just try to write this code here which which we already have and if I try to create object of it let's try creating object of it in fact it let me know one more thing before we start let me for this example let me cut this part and create a new file and let me say this is calc dot Java uh and yes we have this code here so you can see now we have two files and before I continue I will just delete this class files again okay class file deleted and now let me just compile this code once again so if you can see when I'm compiling it it is only compiling the demo file it is not combine the class dot Java right but when I create an object of it if I create object of calc obj equal to new calc of course if you want to use it we have to create object first and now if I recompile this and let's look at the class files we got the class file for class as well so if you are not using calc it will not compile it it will just say hey my responsibility is to compile the Java file I'm doing it but if you want to work with the class the calc.class as well or crack.java as well you have to create object and that's how it compiles now once the compilation done you can Simply Save the value in R1 or we can say obj dot add it will add two numbers let's say four and five and then we can also find R2 which will be result of subtraction so I can say sub I will say 7 and 3. okay so basically you can see we got these two methods and when you try to print the values I will print R1 and I will give a space and also print R2 so basically I'm just printing two values back to back and if I say come file again because we have made some changes and run you can say we got 994 so four plus five is nine seven minus three is four and that's what we are getting here everything is working fine right now there are two conditions here why we are going for inheritance the first one is what if you want to add some extra features you want so let's say if someone is asking you for hey you know uh Naveen I want a calculator and I want two features addition and subtraction what will be the cost I would say okay I will charge you ten dollars now let's say if the same person come back and say Hey you know I want a calculator which has four features addition subtraction division multiplication and maybe at this at this point I want to charge fifteen dollars now of course I can add those two methods here or I can create a new plus you know why is because if I edit the same file if I add more features here I can't sell calculator for ten dollars now I have to make a new product worth fifteen dollars I don't have calculator I have advanced calculator which does four operations I want both the models I want to sell the 10 calculator and a 15 calculator that's one reason okay so let me just do that so let me create a new calculator and I will say this is advanced calculator which has all the features dot Java and in this I will basically have two more methods which is public and multi which will take two values and it should return N1 into N2 and likewise we'll have one more method here for division so this is div and division we have to say slash that's it so we got advanced calculator which has two features but now if I create an object of advanced calculator now instead of normal calculator I can I can access multiplication and division I can't access add and sub and that's why you can see we got another here it says the method add is undefined for type advanced calculator that means in this we don't have add-in Supply so what do you think what could be solution here think about this so one solution which I can see is just write those two methods here of course we can do that okay we can just copy copy and we can paste it here but don't you think we are writing the same code again and again now when you repeat the same code it is called redundancy right we have same code multiple times and redundancy is a crime so make sure that you don't repeat the values you don't repeat the methods definitions and classes the point is if I don't Define it here how would I access them that's where the inheritance concept comes into picture so United says hey don't worry if you already have a class called calc you can simply say extends calc your job is done okay so you simply say advanced calculator extends calculator and now all the features of calculator comes in here and there's one more Beauty even if you don't have this Java file that's fine for inheritance you need a class file not a Java file so even if you delete this it will still work okay so that's the first first reason even if you don't have a code ready with you if you do even if you don't have a Java file you can still use a class file okay so that's the beauty of inheritance we are simply saying extends calculator which is this class now what do you think will it work let's try so you can see there's no problem add-in subtraction working we can add some more variables here I can just copy this code and paste paste another same line this is my R3 this is my R4 and I can just say this is multiplication okay so multiplying number like uh let's say is 5 and 3 okay and then dividing the number let's say 15 by four let's do this and now when I perform this operation of course I want to print R3 and R4 as well on the same line I'm lazy to write multiple lines of code here and R4 so we are printing everything in in the same line let's try what happens compile no problem run how it works you can see that addition is nine uh suppression is four multiplication is 15 if I'm not wrong yeah and division is is three so 15 divided by 4 will give you a quotient of three okay cool this is working right so this is how we use inheritance so important thing here is this advanced calculator here is called a subclass this calculator here is called a superclass and to inherit we use a keyword called extends okay and this is basically a relationship of is a uh advanced calculator is a calculator and that's why we say is important about that cool can I can I have multiple classes in hitting each other we can that will say in the upcoming videos at this point we just went for a simple inheritance where we just use the feature of our superclass you can say parent has all these two features child is just extending it or using it I can say this is nepotism so so basically we were able to do two advanced calculator which uses a feature of calculator right so this is extends now what if in future let's say we got one more requirement we want to build a let's say scientific calculator and I don't know the operations I will just say advanced calculator I'm very bad with names I will just use this and this advanced calculator will have extra feature let's say it also has a feature of finding the power of a number okay so let's say public int I can simply say power I want to do that so it will take two numbers the first value N1 and I also want a power let's say N2 and let's say this will return okay how do we do the power so basically we have a math class I can just simply use it matte dot yeah we have a function of power just take double value but that's fine I will just pass integer double also takes integer value N1 into and the problem is it also Returns the double value if you can see Power returns a double value so what I will do is let me return double value okay so you have a choice you can return double value or you can just Typecast it with int and you can do that otherwise double is okay so you can see we have a power function which takes this method and it it calculates now the thing is if I want to create object of power when I will do that I will just create that in demo I will say hey if someone wants to try very advanced calculator they can do that okay it's just that very advanced calculator if you can see we don't have any other methods okay now that's that's an issue right now I know in your mind you have a solution right you have a choice you can just add you can just type all these methods here or we can come back here and say extends okay now it will extends what if we can also say calculator but then this will only have addition suppression division multiplication but not the feature of advanced calculator which is oh it will not have this two as well because this two is there in advanced calculator it will only have ADD and sub you can see multi and division not found in fact you know I should also have R3 in fact this should be of type double double R5 equal to obj Dot Power by passing two values one is let's say four raised to two at least we know the output it should be 16 uh and I also want to print R5 oh we are printing everything in one line great and you can see multi individual does not not working so if you want to make it work you can't just extend calculator you have to extends advanced calculator so you can see we have multiple hierarchy here so very advanced calculator is extending advanced calculator advanced calculator is extending calculator so if you try to compile this code let's see if this works compile and run you can see it's working you you got 16 at the end you got points over because we are returning a double value this works so if you're trying to imagine this what we are doing is so we have a calculator and then advanced calculator is extending calculator and then we got very advanced calculator is extending from advanced calculator now this is when you are saying multiple where we are doing inheritance right so this is called multi-level enhance so basically we have multi-level inheritance here because we have multiple level but in the earlier example we were having a just two right so we were having this two now when you say only two classes this is called single level s and I know because I became a doctor there but you got the point right so if you have multiple layers that is multi-level patterns and then you say single that is single when you have only two classes now the very advanced calculator here is taking the feature from advanced calculator advanced calculator is taking the feature from calculator so by default all the character stuff will come to varied once and that is multi-level so in the last video we have talked about what is single level inheritance and then we have talked about multi-level right now it is multi-level so when you have the class which is getting headed by someone else and then someone else something else is getting inhated by something else it is multi-level now can we have a different structure here let's take one more example so if you say we have a we have B and we have C now this is multi-level right and of course when you have a and b that's it this is single level but what if you have a and b and then you have C where your a is your parent in fact in this situation as well if you try to expand this one here as well we have this a as a parent and then this B becomes a child now b b becomes a child for a but then B is also a parent for C right so C becomes child here now in the same way if we talk about this a b c here we are saying both are parents so we have a as parent and B as parent and then C becomes a child so C is anything from A and B both now is it possible in Java let's try in fact it is possible in other languages like C plus plus let's try in Java what I will do is I will just go back to the same class okay same file demo and here let me get a class called a and of course a will have some methods this should be capital A and then Class B and Class C okay we have this uh three uh classes here okay and let's say C is okay let's try this first if B extends from A and C extends from B okay this works we can say there is no problem here but what if I want the seed to extend from A and B both as you can see this syntax is not working it gives you an error as well you can't put a comma when you say extends a comma B okay does it means we can use extends extends B now even this will not work so it doesn't matter what combination you do multiple inheritance in Java will not work yeah so this thing will not work multiple enhancements will not work but the question is why so let's try to understand that here now let's say if we go back to this particular example here let's say you create object of B okay in this scenario if you create object of B let's say obj now obj can call all the methods of b and a both so let's say in B we have a method called show okay let's let's say there is a show method and then from obj you are trying to call show will this work yes this will work let's say in a we have a method called ABC now if you try to call obj Dot ABC yes this will work but what happens you know when you say obj dot ABC it will try to search for it first in the b class in b class we don't have it so it will say okay I don't have that method maybe my parent class will have so it will go to a by checking hey do you have ABC parent will say yes I have now in this case it will work the same thing is applicable for c as well let's say if you create object of C which is obj again and then if you try to call a method let's say let's say a has a method called X B has a method called Y and C has a method called Z so let's say we have Z method and then when you're trying to call Z this will work because in C itself we have the method if you say obj dot y will this work yes because it is coming from B now when you say obj dot X it will also work because it will call from a so first it will search in C in C we don't have X it will go to B in B also we don't have X it will go to a yes in a we have X okay it works but now if you come back to this example let's say if you get object of C as object so let's say this is C object okay and then if you let's say in this a we have a method called show okay and that's how that's how we're working so let's say if you if you call show now now of course c will say okay I don't have a method so will this work let me ask my parent so it will go to a a will say okay I have show problem solved this will work right engine I'm not talking about Java let's say in general this is how it works now let's say we have in B we have a method called X okay now it will say obj dot X will this work now it will search and see and see we don't have it in a also we don't have it in B we have it so this will also work okay but what if there's a method called Y which is there in a and b both okay and now if you are trying to say obj dot y the problem is from say we don't have it and a and b both has this method which one you will choose that's a confusion right uh it is almost same like let's say if you want to call someone your phone is dead and then now you want to call so you can ask your patents and both the parents has a phone which one which phone you will choose now if you chose your dad's phone your mom will feel bad if you if you choose your mom's phone your dad will feel bad again I'm just giving an example here I'm not saying it literally happens but in fact they will be happy that you're not using their phone but uh yeah that's a confusion we have here right now this problem is called Ambi Beauty problem okay so ambiguity simply means we have we have a confusion here which one to choose now this problem is there in multiple languages which supports multiple enlightens Java says oh that's a problem let's remove the feature itself okay I know that's a weird way of solving the problem but yeah they have removed the problem itself so it's not like they found a solution they said instead of finding a solution let's remove the feature that's right in Java multiple inheritance does not work okay so in the it's a very famous interview question uh do we support multiple entities in Java and the answer is no there is a indirect way not actually a way it's just that it is possible by going all through steps but yeah that's how Java is implemented in new feed in new versions they have done something which changes it uh with the help of interfaces but we don't know interfaces yet so let's talk let's say Java does not support multiple enhancements oh okay this makes sense so once we talked about inheritance and Method overloading as well right we have talked about method overloading in this video let's talk about method overriding that's right so we have two different method overloading and Method overriding so let's talk about overriding here so for that what I will do is I will just try to keep it simple again and let's use the class A and inside this class A let's say we have a method so I will just remove this part and here I'll say public void show just to keep it simple I'm using a very simple example here which is uh show which prints something let's say it prints in show okay now if you have a method inside a class of course you can create the object and you can call the method right and we have seen that so let's say if I go back to my main method if I say a obj equal to new a and I can simply say obj dot show and it will work right and it should work that's what we have done from a long time so let's compile this code and run and you can see it prints in show just to keep it simple we have we have went with a very simple example now what I want to do is I want to get a class which is Class B and I want this class b to extend Class A and of course when you talk about inheritance this method show it also will be available inside Class B right so if I create object of Class B yes of a it will also work because that's how many items works right so if I go back compile and run it works you can see we got in show what I will do now is I will also print in a show just to differentiate that this is getting printed in the a class okay now the thing is what if you want to have a different definition of it okay why do we need different definition let's say we got two methods here public wide config so let's say we have this method which is called config I don't know what it will do just to keep it simple I will just say s out and I will say this is in a config okay and let's say they have different implementation they have different statements at this point we are not focusing on statements we are here to understand the concept right so let's say it does something show method and config does something and I can actually call both the methods not just one I can call config as well okay and if I compile and run this code it works you can see it says in a show in a config now let's say I want a different definition for sure okay not for config so basically what I want is when when it comes to B and when I call show it should print in B show how do I do that how do I change the execution so what I want is when I call show it should not call the show of a because show of a will print in a show I want to use config as it is so I can't even remove this inheritance if I remove this and if I Define the methods of course I can Define show but then I want this config to be coming from a so of course you have to keep it you have to say Class B extends a because we want config what we don't want of a is show because it is printing in a show I want to do something else and that's an audio you can say public void show you can write the same method name or maybe a different name you can say in show one I mean so one and you can print here in B show I mean of course you can do this the only thing is if you come if you execute this code now it will still print in a show you know why is because we are calling show and both these methods are different one with name show and one with name show one okay that means if I call show one then it will work then it will print in B show but what I want is I want them to have a same name I want my this method which is so should override the method here is it possible and the answer is yes in Java we get this amazing feature where you can basically override the methods of the passing class so of course you want everything except this part okay I mean except this show method I want config but I don't want show now let me give you one more example here remember when we talked about inheritance I have used an example where I was not having any phone in my 10 standard and my father had a phone which is double three one zero now if you ask me at that point which phone I have I used to say double three one zero because that's my father's phone but after two years when I was in my 12th standard I got my own phone I got Motorola c168 again uh not a smartphone but it was good it was a very slim phone so when now if you ask me which one I have I was I will say I have Motorola c168 my father still had double three one zero so here if you can see if someone asked my father father will say I have double three one zero if you ask me I have a choice of saying double three one zero but I will go for c168 which is my phone right so you always give preference to your own features first example if you call show here it will execute the method of b instead of executing the method of a so basically this method show overrides the method of a here and we can simply execute compile and Quran you can see it prints in Bishop now this will make much more sense when we say we are creating not a but we are getting a calculator which has a method called let's say let me remove config for some time let's say that we have a method called uh add and this will return a value okay and here instead of printing this I can simply accept two values I can say int and one comma int and two and here we have done this before and that's why I'm writing this code very fast against a return N1 plus N2 okay so if your class which is instead of saying B I can say Advance calc which is extending calc it will also have some features but let's say I want to add two numbers which will also take int and it will have the same parameter okay because in method overloading we have seen it can have the same method name but different parameters here in method overriding it will be same name same parameters as well I mean same type of parameters not the name doesn't matter name uh let's say N1 N2 and it will have the same name same number of parameters same type of parameters but it will have a different behavior let's say N1 plus n two plus one I'm just adding extra 1 there so what I want is if I come in this section okay at this point I'm not overriding it I just have ADD method inside calc and if I create the object of advanced calculator by saying advanced calculator obj equal to Nu advanced calculator and if I try to call the add method by passing three and four of course 3 and 4 is 7 so if I get this value somewhere if I say int R1 is equal to and if I print the value for R1 and again since we have done this code before I'm just typing it fast now what do you think what will be the output I am passing 3 and 4. so of course it will first search for this method add inside the same class of course we don't have that method so it will go to the upper class by saying uh hey you know uh calc you are my parenting class I'm searching for ad with you and yes it is available so basically we can just compile this code and run you can see we got seven and that actually works but now I want a different definition for add I just want some changes instead of adding those two numbers it should also add one at the end okay so if I compile this code now and run it will print eight so this ad is overriding the add method of calculator okay just remove this statement that's extra so this concept is called method over writing so this method is overriding the method here I hope that makes sense right in this video let's talk about packages the thing is whenever you have a file doesn't matter is it a music files or documents or code we normally put them in a folder right and then we create a folder structure as well in fact I remember in my college days when we used to store different songs you know we used to create different folders like you know we have a songs folder then inside that we have rock songs DJ songs songs romantic songs and in fact in that as well when you go to sad songs we can have normal sad songs ultimate sad songs okay now that's exaggeration but we do that right we create a structure there in the same way when you talk about Java project you will not be creating one or two files right it would be having multiple files sometimes your project can have let's say uh 200 files 300 files okay maybe most of the time when you have a complex project now how will you put all these files in one folder you can actually you can put everything in one folder now if you see our project structure here what we did is we have this folder called codes and in this codes we have all the files in fact we only have three files now but just imagine we have more files imagine we have 100 files it's okay to put them in one folder but it's always better if you can create a structure where you can have few files in one folder and few files another folder and then you can have a folder inside a folder now normally this type of pattern in in Java is called packages normally you you store files in a package so yeah that's right the folder is package here and of course one package can be a part of another package the way you have folders inside a folder so how do we do that here in Java so make you understand let's say we have these two files you know advanced calculator and calculator now these two files are related right they both work for the calculator so it is a good idea to put them in one folder now how will you do that you know you have an option here of creating a new folder that's one way all you can what you can do is you can just come back on top of your class and say you can create a package so the keyword which you use to create a package is called package and you can put this class in that particular package let's say the package name is tools right so of course you can create any pack any package name but it should have one it should make some sense so you can see I'm saying that they are tools okay and the thing is you can see we are getting an error it says that declared package tools does not match the expected package the thing is you don't have calc inside the package in fact most of the IDS will give you an option of directly if I click on Quick Fix yeah so you can see most of the IDS doesn't matter which ID you are using in vs code we have this option in most of the famous ID you will get this option of doing something some red red symbol or something like that here we have an option of quick fix if I click on that it says move calc.java to package tools okay that is a good idea we can click on this and you can see it created a folder called tools and now we can actually move this to tools as well the only thing is when you do that and when you go to this particular class okay it has already done the changes you can see I have not written this it is already there thanks to vs code and the IDE uh so basically you get this here so it's always a good idea to put your classes in a package you can create a complex structure if you want you can create a one more folder here let's say we're going to folder called database or maybe DB connections or DB classes or maybe you can say simply said DB now this DB will have all the database connections so let's say if you want to create some files which will connect with mySQL Oracle or it does something with database so you can put all those classes in database folder okay now that's how you create a project structure here okay the only thing is if you want if you have this structure here let's say if you have calc and demo Advanced calc and if you can see both are in a package now the only problem you will face is if you go to demo and you can see demo is not a part of any package normally you put the main class so the class which has a main method separately outside the package so that you can easily access other classes okay when I say easily access another classes can I do that can I create object of calc here so if I say calc obj equal to new calc which was working earlier if you remember our previous examples it's not working now you can say it says calc cannot be resolved what's wrong the problem is demo and calc now they are in two different folders you can't directly access it okay so when I say you cannot directly access it do we have an indirect way uh yes we have an entire way so what you can do is if you if you see this bubble it says import calc that's right if you want to use calc in your particular class you have to say import calc but then calc belong well calc belongs to tools.calc so basically you have to mention the package name and then you have to mention the class name whichever you want to import and now you can see there's no issue how about advanced calculator if I want to use advanced calculator obj1 is equal to new advanced calculator okay now you cannot access access this one you know why is because we have only imported calc not advanced calculator how will you import this you are right just come back here and say import tools dot advanced calculator so whichever class you want to use you can just import them in fact Java internally has a lot of classes example let's say one of the classes which we are going to see in future which is let's say list now when we use least which design build class or with input interface what is interface we'll talk about that later or maybe at this point we can talk about let's say add a list which is a class when we use analyst and when you create object of it I will say list is equal to new add a list now the moment you do that it will also come from a package you can see it is now given given by the IDE it is auto completing it but if I remove this line you can say it will give you error it says allergies cannot be found so we have to import this one as well and this belongs to a package now that package is import java.util dot as a list this is up to you have to import now you might be thinking why we have two dots here Java dot util how it looks like so basically we have a Java folder inside Java folder we have a util folder so it's the folder inside the folder and Java has multiple folders efficiency Java dot we have applet folder we have awt folder beans folder Lang folder IO folder and all this thing so in general whatever class you use in Java belongs to a package let me repeat whichever class in build class which you use in Java belongs to our package now you will be saying okay everything makes sense but don't you think string is also a class and one of our favorite class which is system system is also a class right so when we are using system we are we have never imported it so when I say system.printer then there was no need to import the package but the system belongs to our package and the answer is yes if I go to system and if I go on top here can you see that it says package so this class system class belongs to our package java.lang but we never imported it the thing is by default every Java file will have java.lang by default imported so even if you don't mention that it's there this line is there even if you don't mention the thing is it's actually this not this line what is by default is this java.lang.star now what is this star now in one particular folder if you have multiple files and if you if you want all the files you can simply say start just like we have tools here in tools we only have two files and we are importing both so can we just do that yes we can so we can simply put a start here our job is done and by doing that I've also removed the list we don't even need that you know that so basically if you want to import all the files you can say start okay so that's how you can create a package in Java as I mentioned you can create a folder inside a folder example let's say if I have a folder called other so let's say we have other folder and these tools actually belongs to others so now if I move this you can see we have tools inside the other now how it will look like when you talk about your the calc so you can see it says other dot cap so you have to if you want to go inside the package normally in folder structure we use slash here we use dot so just others dot tools dot uh calc okay that's how you have to import it and of course you can say start it will import everything now my question to you is if I say import other dot star now is this two line same or can I remove this of course right when you say start it should input everything but now if you can see it is giving you error the reason is when you say others.start it so star simply means all the files not all the folders okay so in other if you have some files let's say if I create a new file here I will link this as a DOT Java the moment you do that you can see this a class belongs to other package now you can easily use a but not calc so if you say a obj 2 is equal to new a this will work is because you are saying others dot start it will import all the classes which are all the files in in the other folder not inside a folder which is tools so if you want others I mean tools as well so you have to mention others.tools dot star so that's how basically you use a package structure now there is one thing you know not important at this point but since we're talking about package let's talk about it because you will be seeing those type of structure multiple times when you create a project a big project normally you deploy your project on the server somewhere right and maybe you're creating a library that's right what you do is when you create a project it's not always for your use sometimes you create libraries so that others can use your files now in this case as well you will create a package and then you will give your package to others the only thing is when you are sharing your package to the entire world it should be unique it should have a unique name let me show you something I will go to a website now this is one of the website where you can get the libraries the library simply means if you want to do something and if you don't want to write your the code entirely by yourself you can use some libraries and most of most of the time we do that example let's say if I want to connect my application with mongodb which is a database so I can simply search for mongodb drivers sync this is a driver which we use to connect Java with mongodb so you can use this package in your project okay let me select a version so this is the package okay now the thing is when you talk about this package what should we name of it so this name should be unique on the internet now how will you make something unique that's tricky point right so when you are creating a project how will you make it unique so one way to do that is by reversing your domain name that's right of course right when you're building a library you will also have your website your company website maybe so let's say if you talk about a company like Google so if I'm working for Google and if I'm creating a library so what I will do is I will name I will say this belongs to a package so if I'm making a project for let's say Google and I'm making a calculator or advanced calculator so I will say com dot Google dot uh calculation now this is my package name okay so by making com.google I'm making sure it is unique right only Google can own this domain right it's their domain so that's how you do that so depending upon your project you will see this type of packages in fact in future videos we will be using some packages which have com dot something it means they have just reversed their domain name just to make it unique but again for learning purpose you can name your package anything you want example the way we are doing here pack each other but it should have it should have a meaning that's important now once we talked about packages it's a good time to talk about access modifiers now one of the access modifier we have seen in fact we have seen two private and public apart from this we have more we have protected and default as well so in this video let's try to explore them or one or two videos so what I will do is initially I will start with public now what is public basically let's go to demo and we have we were trying to import the packages and of course we should do that right anyway we don't have to do that java.lang it is it comes already so we don't have to worry about it and now what I will do is I will just go back to my class A let's work with class A now and in this class A you can see that class A is public okay and it belongs to a different package called other and in this class I will just create a variable called int let's say marks so we have this variable called marks and the let's say initial value for Marks is six because we just want to access this is it working or not now how will you access this variable so you can just go back to demo so we have ADD we have a right which has this code which is simple class and then we have our marks let me go back to demo and in this I want to access it so I will just remove all this lines just want to work in fact tools is also not I need now that only work with other and in this let me create a object of a now connected object of a yes we can because we can access it and then I'm just trying to say obj dot marks now I'm not trying to do that you can see it gives you an error it says the field a DOT marks is not visible I mean that's weird it's there right Marx is there the thing is it is not public now when it is not public you cannot access it outside the package you can see that that's the issue we got now what if you have a class in the same file or maybe in the same package let's say in the same package which of which is of demo I will create a file called B dot Java and in B dot Java let me just say because they're in the same package now so no package is same package basically I will try to create a variable called int let's say marks let's see if I can access this marks and demo I will just go back here and I will repeat the same sentence same line in fact two same same statement with the help of B object now this is B the only thing is we have to use a different object name because we cannot have same and using this obj can access marks so you say there is no issue even if marks is not public it's no issue because they are in the same package the moment you have a different package and if you don't mention public it will not work okay that's the issue you can see it is not working now if you want something to be accessed outside the package always make it public and that's why methods most of the time they are public variables should not be public but method should be public example if I say public void show here now show can show is acceptable I can use show from anywhere example if I can come back here obj dot show this will work because that's public but if I don't write public that it will not work but that's one so public means it can be accessed from anywhere okay now apart from public we also have a private so if I talk about private can I use private anywhere private variable can be used in the same class irrespective of the package okay package the secondary part it can be used in the same pack same same class I can't even use outside the class example let's say if you look at this B and if you look at this demo they're in the same package right but now if I create a private variable there here if I make this as a private variable which is which you should always Mark your variables as private the moment you make it private even if the same package you can't access it so private variables can be used in the same class irrespective of the package it's on the same class now let me do iterate when you talk about public it can be used from anywhere okay you can see this table a public can be used from anywhere private can be used from the same class okay now apart from public and private we have two more okay let's talk about those now now if we talk about this a if you don't mention private something should be applicable right if it is not private is not public what is it so by default there is something called a default access modifier okay which is also called private protected but we'll talk about that later but it is called default Now default simply means it can be accessed in the same package it's as simple as that it can be accessed in the same package example if we talk about this B when private was not there it was able to be accessed in the same package so we got private which can be accessed in the same class we got public which can be accessed from anywhere and then we got private which can you only be accessed in the same class and default in the same package okay apart from this we have one more which is called protected now what is protected now let's say we have this class A which has let's say if I make this variable as protected okay and then I'm going back to demo and inside this demo somewhere I'm creating a class in the same package basically it doesn't have to be in the same class or same file in a different package basically let me create class called C and then I'm saying that this class extends a now in this class A so you can see this is protected uh can I use protected in some other package you can see marks is predicted in the other package like demo again Marx is not working so basically if you make it protected you can't use it outside the package before coming there you try to know what we should do let's try to create a class here itself let's say a class called launch and in this class let me have a method which is public void ABC let's say and let me try to create object of a equal to new a and see from here can I try if I try to use marks let me just print marks obj dot marks okay it is working so it works in the same package but it doesn't work in other packages okay so you can see demo is another package but what about a subclass we have class C here and then if I try to create a method public void ABC and you can see this class C looks same in both the packages the difference is this is a different package and it is extending a now and now if I try to access marks it works there's no issue you cannot access marks outside the package but you can access marks outside the package but it should be a subclass if you can see C is a plus of a now if you see this image here this image looks like right let's talk about private private can be used in the same class nowhere else protected can be used in the same class in the same package in the other package but subclass public can be used everywhere default can be used in the same package okay it's it's that simple so this is how you specify the modifiers now how do we use and which one to use basically the idea is very simple try to make your classes public which you are doing and of course we don't have a public here the thing is you can't even make this class public because you cannot have two public classes in the same same file but and the general idea is in one file you should have only one class so try to make your class public next try to keep your variables the instance variables private the methods most of the time will be public but if you think there is a method or a variable which should be accessed only in the subclass of other packages use predicted try to avoid default default is not something we should be using I know from from if you check all this thing we are promoting default a lot but don't do that don't use default stuff example this is default either make it private public protected or public don't keep it default default simply means you don't know what to give the access so give it private predicted or the public in this video let's talk about polymorphism remember we have talked about method overriding and then we say there is something more this is where we are talking about so we have a concept of polymorphism now this is a very important Concept in terms of op and most of the languages which implements Ops has this concept now what is polymorphism now basically as a concept it has two words one is Paulie's second is morphism now Paulie simply means many and morphism means Behavior now what it means is there is something which behaves differently in different situation based on uh yeah based on situation let's say as a human you behave differently at different locations right example for as at home I behave differently at my office I behave differently when I'm with my friends I behave differently right so that situation changes me in the same way because the name also changes right at home I am uh I'm a son husband of a father at office I'm a colleague I'm a boss I'm an employee at my with my friends okay they use different words but then let's not mention that so the important thing is your behavior your name changes at every place right your behavior changes based on that in the same way in the world of object oriented we have a concept of many Behavior polymorphism so the same object or the same reference will have different Behavior based on how you create these questions of it now to implement this there are multiple ways in different languages but before we go into the implementation let's talk about its type so there are two types of polymorphism one is called a compile time polymorphism and second is called a runtime polymorphism of course they have different names uh some compile time is also called early binding runtime problem is also called late binding but we have these two words compile them at one time now what do you mean by compile time see if you can Define your behavior so the behavior is defined at compile time that is compile to polymorphism if the behavior will be defined at runtime that is called a runtime polymorphism so when you compile the file and when you know what thing will be executed at the compile time itself it is compile time now which method will be executed will be decided at runtime that's run time now we have done two examples here which is overloading and over writing so if we talk about overloading it's a part of compile time because at the compile time itself example let's say if you have two methods you have let's say two methods which is ADD which takes two parameters ain't an INT and then we have one more method here which is ADD and when you say ain't ain't in which takes three parameters now both this have the same name but different different parameters that is Method overloading now which method to execute will it will you execute the first or second method will be decided at compile time now let's say you have two classes Class A and Class B two out of two different classes and Class B extends Class A we have talked about this in method overriding and let's say you have a method which is ADD which takes two parameters and also here we also have the add method which takes two parameters now out of this too which one will will get get executed it will be cited at runtime so this that is called a runtime polymorphism in fact there is one more concept which we are going to discuss in the implementation when we talk about Dynamic method dispatch at that point this thing will get more clear but just to generalize what is polymorphism polymorphism means many Behavior so your behavior of your reference or your execution will change based on some situation if it is compile time which is your method overloading where you decide based on the parameters at compile time itself in runtime you're not sure which method will be called because both the methods have the same name same parameters now in this video let's talk about Dynamic method dispatch now once we have went through what is polymorphism we have also mentioned that it is same thing multiple behavior and we have two types of uh polymorphism which is compile time polymorphism runtime polymorphism now to implement runtime polymorphism let's try one more example here and we do that with the help of dynamic method dispatch again to keep it simple we'll use a simple class like Class A and then we got class B and let's say Class B extends Class A okay and let's say we have class C and again we have discussed most of the topic so let's make it quick so we got class C so if you can see we have class A which is getting extended by B and C both so you can say class A is parent and we have two Childers one is B and one is C now what I will do is in this a I will create public void show okay and this show will print let's say in a show okay and we'll be using class a bit later as of now let's only focus on Class A and Class B now my question to everyone is I mean okay let's remove this classy just to remove the confusion let's go step by step so let's say we have these two classes Class B X is Class A now my question to you is how do I create an object of Class B and of course we have talked about this from a long time I can say b obj equal to new B and that's how you get the object right and of course using this obj I can call show there is no harm compile this code and run you can see we got in a show now this is how you create the object right now there is one more thing can I create a reference of a an object of B is it possible since B in B extends a so you can say B is a child of a so can I refer this obj as a and the object of object is B now when you look at this obj what is this obj it's a variable right and what is a type of a variable the type is a here right so the obj type is a but the implementation is B okay that's the point remember and this perfectly works now if you want to imagine this let's let's go with this one more example here which I will write here let's say if I create a class called computer and of course class computer has some methods okay and then class laptop and we all know that laptop is a computer so I can also say laptop extends computer yeah we can say that right and now if I say laptop is a computer if I want to create object of a laptop of course I will do it it's very simple you will say laptop let's say lap is equal to new laptop so let's instead of saying uh lab we say obj1 and then I can just call all the methods but my question is can I refer this obj1 as laptop yes it is a type of laptop I know I can also say this is a computer and that's what you do right when you look at the laptop you can say hey that is a laptop but that's also a computer we are referring that as a computer so we are referring this obj which is an object as a computer but technically it's a laptop right in the same way we can refer this obj of type A so we can mention the type as parent but the object can be of child okay so I've just created this laptop computer just to show you let me remove that so yes it is possible to create a reference of a superclass and the object of a subclass okay and this will surely work let's compile there is no issue let's run and you can say it says initial but the twist is if I have the same method and we have talked about this in method over writing if I have the same method and if I say this is in B show now what it will call Will it call in Asia or in B show as per the method overwriting it should call in B show but look at reference reference is of type A that's tricky right uh let's try in fact what I will do now is just to first let's run this code and I will show you something you can see it says in B show let me initially set the object of a itself and then let's go step by step so I will say compile and run it says in a show in fact I can call this obj show once more but before calling object show what I will do is I will make one change here I can just come back here and you can say obj is equal to new B that's right we can assign a new object to the old variable okay now if you want to understand this with a memory of course we have a stack here right in this tag we have different variables and then we have a heap memory here now initially when you say obj that's your object right and it will create object of a so 1 0 1 is object of a and talking about line number 24. okay line number 24 we have this line so that's an object here and it will have some methods the method name is show itself right and this will have a value which is 1 0 1. so I'm talking about line number 24 okay and 24 this is a link now when you say obj dot show line by 25. so at that point it will look okay so obj dot show that means I have to call this show method of object a so this my friend is an object of a and now on line number 28 we made some changes that is now this particular object is referring to the object of B so we got one more object here which is let's say 1 0 3 and this is an object of B now okay this will also have a show method because that is what we mentioned there and now since we are changing the value here this will be replaced by one zero three there is no one zero one anymore that means this link this link is broken and we got a new link now on line number 29 when you say obj dot show it will call this show method of B so it's not about what is a type of the object or reference you can say type is still a but the object is B and that's what you call the B show let me compile and run you can see at first for this line it is printing in Asia for this 90s printing in B show likewise if I create another class here Class C which extends a and if I have the same method I will just reuse my own code and if I said this is inch C show what will happen if I create if I just go back here and say obj equal to new C and if I say obj dot show what do you think what will happen if I run the for the third time and that's of course right different object will have a different Behavior remember runtime polymorphism and you can see we got in C show so the same object which is obj dot show is behaving differently with different objects polymorphism right so if you say we are not even sure so when you compile this code at that point we are not sure obj dot show will call which method of of which class I mean method of which class it will be decided at runtime and that's what is called runtime or polymorphism and all this concept is called Dynamic method dispatch because that's Dynamic which method will it will call we are not sure okay and that's how it works so Point remember here is irrespective of what type of object you have or what type of variable you create is it type it's a type A but it all depends upon what object you have it is only possible when you have inheritance okay you can't simply say we are getting a new class class D and you will say Okay class D and you can't simply go back here and say obj equal to New D this will not work because D is not extending a if you can see where if we can verify D is not extending a so this will not work this will only work when you have inheritance so only you can have a child objects for the parent variable which is a obj parent type variable let's talk about final keyword okay so the keyword we are talking about now is final now basically final keyword can be used with a variable a final keyword can be used with a method and a final keyword can be used with a class and we'll go step by step so let's start with the final with variable now when you make a variable let's say if I create a variable called num and if I say now the value for Num is 8. and of course when you make a variable of course you can change the valuator right so initially the value was 8 and now you are making it nine there's no problem right and of course you can print the value as well so if I compile this code and execute the decent value is 9. now there's a concept of making constants right so what is constant so if you make your variable constant you can't change the value of it now the way you can do that is by using a final keyword now in different languages we use different keywords I guess in C plus plus we use const in Java we say final so the moment you make your variable as final variable the value the variable becomes constant so once once you assign the value you can't change it okay that's important and that's why you can see we got an error here in fact you can also get this error by compiling the code you can see it says cannot assign a value to a final variable so that's final Yeah final variable num okay so because num is final you can't change the value and this is actually very important so whenever you have a value which will which you think will not change try to make it final it's a good practice to make final variables is because you will not get any unexpected output because if you think the value should be constant then y to even change it example if you want to make a variable for pi we know the value of pi right which which will not change so it's good to make that particular variable as final so it's always a good idea to make a final variable because that's a stand that's what a good practice is so we have talked about what is final variable so final variables simply means making constants now let's talk about final method in fact even before final method let's talk about final class then final method will make much more sense so at this point I will just remove this part so now we know what this final variable is right so what I will do here is I have a class here called let's say calculator okay now I'm making this class and in this or maybe let's keep it simple let's say a so I've got this class a right and now in this class I have some methods let's say the method name is public void show and you know what let's make it calculated just to make just to make it much more sense and I will also say public void add this time I will not return the value I will just print the output here itself I will say a into B and then I will just simply print a plus b I'm just adding it here itself right and then we got the method which is show and here we are saying public here we are saying in calc show Okay so let's say we have this class here which is calc and then we have these two methods and if you want to make it work of course you have to get object of calc you will say calc obj equal to new calc and with this object you can basically call both the methods you can say show you can say add and you can pass two values 4 comma let's say 5 right now with this let me just compile this code and run compile no problem and run you can say there's no issue it is printing in calc show and 9. now what if someone else is trying to create a class called advanced calculator and maybe you maybe that they're trying to extend they're trying to inherit the features from your class which is calc okay and by the way is possible anyone can inherit your method or your classes right because it's available uh what I will do is I want to stop this I want to say Hey you know no one should be able to extend my class you want to stop the inheritance how will you do that so if I make this class as final you can see it will give you an error it says the type advanced calculator cannot be a subclass of the final class that's right so once you make your class Final you are basically stopping the inheritance okay so basically when we talk about final variable it means you cannot change the value of it when you make a class Final it means no one can extend it you're stopping the inheritance okay that makes sense now what if your your class is not final so of course anyone can use it and maybe you don't have an issue of people using your class for your inheritance the problem which we have is someone will create object okay of not your class but advanced calculator and they will use your features and you are okay with it because ultimately you're printing your own class name which is in character or maybe I can say bye Naveen okay so maybe this this Calcutta is by Naveen so if I compile this code and run it will print my name at least I'm getting the credit but what if someone says hey you know I will use your feature of Edition because there is no great mention but I will have my own create here so I will create my own show method method overwriting remember and I can simply print by John I'm not sure who John is but let's say if that person says bye John now what will happen is when you execute the show method and add you can see add is my implementation but they are just overriding show method and it says bye John I don't want this I don't want anyone to override my method how do I do that how do I stop over method overriding in that case you can make your method as final now when you make your method final which we talked about here when you make your method final no one can override it you can see we got an error cannot override the final method okay so that's how we can use final keyword we can use final keyword with variable to make it constant you can use final keyword with the class to stop in heightens and you can you can use final keyword with method to stop method overriding so I hope I don't make sense now in this video let's talk about object class remember when we talked about inheritance I also mentioned that every class in Java extends the object class so in this video let's try to understand what is that so what I will do here is first of all I will create a simple class let's say class A so every class in Java extends the object class right so even if you don't mention it is there but what is there inside this class so what I will do is first of all I will say let me create object of a here I will say a and uh okay so we got the object but then using this object you can you can see we can call so many methods we have not defined any method in this class but still we have the axis of equals method then we got notify hash code uh notify all two string weight so many methods right now what they do so let's understand that so what I will do is in fact this I will say laptop class just for the example and here also let me get object of laptop here so I will say laptop obj equal to new laptop okay now I will try to create some variables here so I will say private int let's say price and let me also get a string variable here which is private string brand okay so we got this two laptops I can say model as well so we got model and price right so if you talk about MacBook or Dell XPS or Lenovo laptop so it doesn't matter which laptop it is we have a model and we have a price here now if you talk about this particular laptop here let me set the values to it and timely you know what I will do I will just remove this private so that I can access it from outside here and I can say oh uh okay so I will say obj dot model I will set the model as let's say Lenovo I don't know is it a laptop yoga maybe and then I will say obj dot price is equal to let's say thousand dollars okay so I got a laptop I am I got the values right what if I try to print the object what happens behind this scene when I try to print the object of course we tried this before and it was giving some weird output let's understand what that output is now if you try to concentrate on this output of course this is not the values which we set but it it prints laptop and it actually makes sense because obj is a laptop object and then we got the at the rate symbol and then we got some weird numbers I'm assuming they are heximal values because you can see we got a d f they are part of X decimal numbers okay so these are the accessible format but what is it let's try to understand that now what if I say every time you try to print end the object it by defaults behind the scene it will call the two string method let me repeat every time you try to print the object it will call to string method even if you don't mention yeah and okay by doing those changes let me just run this code once again compile and run you can see this it prints the same thing that means yes it is getting called but what two string is doing so if I go to two string now first of all two string is not my method if you can see in laptop plus we don't have any method called tostring but in the object class that is you can see we got a two string method which returns a string right that's what we are printing right it returns something what it returns it Returns the class name if you can see it says get class get class method will give you the class name then it calls get name so class name okay that is laptop okay it's working and then we got address symbol in between okay that also makes sense why we got it there and then it prints something in hexa string that's why we got hex decimal values here but what exactly it represents it converts the number into hex decimal string and that number is actually hash code now what is Hash code if you go to Hash code again it belongs to the same class and behind the scene implementation of a hash code works with the it basically generates the hash based on the values you have okay now hash is a very simple Concept in terms of cryptography and anywhere it tries to create a single string of all the data which you have so let's say if you have a object which has five variables it will apply a hash algorithm and it will generate a string a number string which will be of fixed size okay to know more about how passcode you can just go to YouTube and search for what is Hash or go to Google and search for what is Hash but at this point we are returning a hash code okay that's what we got here okay now the thing is we are trying to print this right so body fight is calling two string and that's why we got this output but what if when I try to print the object it will print something which I want and one thing we know that if you don't have a method in your class it will call the method of a superclass but what if I create that method here itself so the public method which returns a string the method name is to string if I implement this method and if I return something if I return say hey that's it and if I clear this and compile and run oh it says hey you know it's not printing the laptop class name and accessible values it's just trying to call tostring which is this it is written hey it's not going for the superclass here that is awesome now let me also try by deleting this thing okay let's not call to string by ourselves let's see if it is getting called automatically it is happening right now whatever you want to return you can do that here right what I want to return is I want to return the model information with a colon between and the price so that it will print that particular data compile and run you can say it says Lenovo Yoga thousand dollars okay so that's how we can basically work with the object class methods if you don't mention it will be coming automatically by the object class now there is one more thing let's say if I create one more object of laptop and this time let's say the values are same let's say this is object one object two this is the object one one okay don't give me suggestions yeah this is two and two now if you compare these two objects are the same yes the values are same but do we have the objects which are same let's try how will you compare this so what I can do is I can just create a Boolean variable here and I can say result is equal to how do I compile object 1 dot obija 2 or obision 1 is equal to equal to Omega 2. let's try I mean we can use double equal to between obj1 and object 2 as well let's try to print the result and let's see what happens if I compile this code and run oh it says false okay maybe using double equal to is not a good idea let's use some inbuilt methods if you can see obj1 actually has a method called Equals it checks for the equals okay that's great it checks for the equals of obj1 and obj2 okay let me try this I hope this will work compile and run oh it is still giving you false why the objects are same right the thing is if you go with the equals method here you can see in our class we don't have equals and the equals is actually coming from the object class it basically compares the two object by their hexa decimal number we don't do that so we want to compare them based on their values so one thing we can do is we can implement the equals method by ourselves so we want the equals method which will return a Boolean value the method name is equals and it will accept only one object I will say this as an object of the same type laptop because we are comparing laptop with laptop I will say this is other and then the object which is calling this equals is this object and we have passed one more object here which is other or maybe I can say this is this and that so this is that object this object so we are comparing two objects now how do I compare I can check if this dot model dot equals so when you talk about this model it's a string right now string has equals implemented properly so we can use string equals to compare the string and this dot we are comparing that dot model right we are comparing two strings here and we have to also compare the price this dot price is equal to equal to that dot price now why I'm using equal to for integer because that's how it works right and we have to also say open and close if this matches then I will return true as I will return false simple right and since we have only one statement I can actually do this right in one line you can see I'm just comparing the values here I'm combining these two strings which is model model and price and price it will not use the equals of object class it will use the equals of my own class let's see if that works compile and run oh it works can you see that we got true now if I change the value let's say it becomes yoga one and if I try to compile and then you can see it says false because we have a different value cool okay in fact you know I can actually simplify this more why to even mention if else anyway this is returning true and false right what I can actually do is the entire stuff from here you can cut this part and say return the entire string because the string itself will compare right it will return true or false I can do that in one line yeah this one way but there are some rules in Java if you are trying to do equals when you when you say two objects are equal of course they should have a same value and they should also have a same hash code so what I mean by that is don't Define the equals and equals method by yourself you can use your ID and you can say Source action and you can see it gives you an option of generate hash code n equals click on that select all the variables which you want to compare and click on OK it will generate the equals method for you and you can see it does multiple checks we have only checks for the value but what if you want to check if the object is not null maybe you want to check for the class name as well maybe you want to check uh if the model is not null before comparing so it does multiple checks for you it's a it's a good practice and that's why I say sometimes you have to prefer your own IDE output and it also implements hash code you know this is how the hashmer is generated basically uh it takes your data and works and applies some algorithm on your data to generate the result if this algorithm can change you can Implement your own algorithm but of course it should have all the variables which should come comes into play example let's say I have one more string here which is serial number and I'm okay if two objects have the same model number and same model same price by different cell number that's okay with me so maybe you can just skip this serial in your equals and hash code okay that's the important thing the thing you have to remember here is the object class has multiple methods and we have worked with two string which is here in fact you know we don't even have to mention two string here we can right click and we say Source action generate two string with both the variables okay it will generate a two string for you which is much which gives you a much better output than what you can type now I know with by asking the code from your IDE you get these two extra annotations here in fact three this is actually we'll discuss that once we talk about annotations at this point is not needed it's not compulsion but it has some importance which we'll discuss later yeah that's how we can use object class we can work with hash code equals tostring and yeah in this video we'll talk about what is downcasting and upcasting now before this we have to understand what is typecasting so let's say we have a double value so let's say double D is equal to let's say 4.5 and then of course I can print this value I can say d and we all know what will be the output it will print 4.5 but what if I want to save this value somewhere okay so example if I say int is I equal to D can we do that can we have the double value inside integer of course it will not support right because 4.5 cannot be stored in integer so yes we can at least store 4 here right so maybe we can remove 5.5 we can at least store four but how will we do that by default Java will not allow you that because Java says I type mismatch cannot convert from double to int it's because it says you know uh you might lose data and we can say hey that's okay for me I am okay to lose data so what you can do is you can Typecast it so you can say hey I don't get what value you send just send that in the integer format I'm okay with it okay so this is typecasting you are converting the type of a value right now by doing this if I try to print I let's see what happens of course we are showing that integer we will lose data so we are losing 0.5 but that's okay we at least we got four now this is called typecasting now coming back to the world of oops can we do this in oops so let's try and we have talked about inheritance we have talked about uh how can we have methods into two different classes how can we override those stuff now just to keep it simple what I will do is I will create a class here and we'll name this class as let's say a again I'm just trying to keep things as simple as possible we could have gone with other other names as well like calculate or something just to keep it simple let's say we have these two classes Class A and Class B and Class B extends a so we are basically trying to achieve in heightens here and now with this what we can do is we can create object of a of course we can do that right we can create object of a and that perfectly works now in this two classes I want to have some methods let's say in a I have a method which is show or maybe I will say show one and in B I can have a method which is public void Show 2 right and then I will just print something here so I can print in a show and now you know what I would say here see we can write any complex logic to it right but the idea here is to understand the concepts that's what you're focusing so in B show now what I want to do is using this a object so if you can see we are creating this obj of type A and also the object is also of a right and now with the help of this obj and you can see I'm not trying to achieve method over writing here there are two different methods so one show two two different methods and I can call show one and you can see I can only call so when I can't call show two what's the reason I can't call show two it's it's very simple we don't have Shoto inside a and a don't even know B exists okay that's a fact B knows what a is but a has no idea what B is I hope that is not the scene in real world your parents knows you so let's let's try this compile and run you can see it says in a show but now my question to you is can I have the reference of a an object of B and then we have seen that it is possible I can actually create this I can have a reference of a and object of B we have done that in Dynamic Methodist patch we can basically do this right now what we are trying to do here is we have an object of class subclass and then we are trying to assign that to a pattern class now we can actually say hey you know what you can do please Typecast it so whatever object you are sending is of child class but Typecast it with a which is a parent class the same thing we have done for the integer interval right now when you are doing this we are basically going up right which is if it if you try to compare this A and B A is a super Class B is a plus so we are trying to say refer I mean objectives of B but refer to a so we are going up which is up casting so this concept here is called up casting and of course this will work there will there is no issue you can still call in a show because in b as well we got the same method even if you have the object of B it will still work okay so this is called upcasting so even this is not compulsory it is something which is which happens implicitly behind the scene so you don't have you don't have to mention the casting part it normally works so this is upcasting now what is downcasting so let's say I'm creating a reference of B now why we have to go for the downcasting first of all see using this even if the object is of B using this obj I can't call show 2. if you can see the object is of B and the method show to belongs to B but still you can't call it reason is because the reference is of a and a has no idea what is this show too okay so to solve that problem what we can do is we can create a new reference let's say obj1 but then I want to use the same object I don't want to get a new object of course you can do this but you're getting a new object can we do this can we say obj1 is equal to obj now the problem is even obj is referring to a new B object but the problem is it is referred to a it's the type of obj is a so it's the same problem of double and int right similar so what we can do is we can do a typecasting here we can say hey I know this is reference of a but the object is of B right so whenever you use assign fine I want the type to be of B and that's how it works this is called down casting so this obj is the parent object or parent reference you are downcasting it to the child reference Now using this new obj1 I can call show two okay so this is downcasting this is downcasting this is up casting in general they are called typecasting okay so you don't have to actually remember this terms every time up casting down casting at after some time you actually you get used to this concept you will implicitly use those things anywhere you want but yes while teaching we have to use some words so upcasting downcasting so yeah that's about this concept but remember this we can create a parent reference and child object and we can also come back to the child reference here in this video let's talk about abstract keyword now what is abstract so before that let's understand something so we know what is class what is object what is in items and how do we do overriding of the methods right now with those knowledge let's talk about abstract keyword so for that what I will do is I will create a class here and let me name this class as let's say a car okay now in this card you will Define some methods let's say if you talk about car a car can drive of course right a car can play music as well public void I mean that's what we do right we play also we also play music in the card you know some people they don't start their car before playing the music or they don't start they don't uh set it on a drive mode before uh before they play the music okay so we have these two methods let's say maybe we can add some more methods in the car maybe you can sleep in your car and other methods but let's say we have this too we have drive and we have play music and of course in this method I can Define some statements let's say when I say it is playing music I can say play music or we can write multiple statement logic is not important the important thing is a concept let's focus on that so let's say in the play music we have this statement and in Drive let's say at this point I'm not sure I want to build my own car but I'm not sure how can I drive a car I know how to implement play music but I don't know how to set up the engine I'm manufacturing my own car so I don't know how to set up the engine how to set up the wheels but a car should drive that's what I know and that's why it's we call it right if you don't have a drive feature then why you will call it a card you can State it's a home theater anyway now if I want to use this car I can simply create an object I can say obj and then I can call the methods I can call both the methods opj dot drive and obj Dot play music of course we have two methods I should be able to call them now if You observe in the drive we are not doing anything why we are not doing anything because I don't know how to implement drive but I know how to implement play music so I'm able to create this object and I'm able to call these two methods and I I think it should work and if I go back and run it works it's not like if you don't Define something it will not work it's working the only thing is when you're defining this method you are actually not implementing anything here you're not writing any statements so it's valid in Java syntax term but as a feature I mean just imagine you're buying a new phone and your phone says it can do this feature this Feature Feature one feature two feature three but then none of the feature works of course you will not buy any phone like that right in the same way if you talk about this card you are writing a method over here which is drive when you're not implementing it why someone should be able able to create object of this why in first place someone should create a class itself right when you don't know how to implement it see the thing is when you build an application you have a structure which you follow you design your application not I'm not talking about the thing you which you see on this screen I'm talking about when you build an application you design on paper what are the classes you need what are the methods you need now at this point you know how to play music but you don't know how to drive now in that case should I remove this method completely if I do that then how will you call it as a car so what you can do is you can say Hey you know I am creating this method drive but I'm not sure what should I implement it here so instead of defining it here so when you open the curly packets and close you are basically defining a method so instead of defining a method you can basically declare a method which means you can put a semicolon at the end when you do that you are declaring a method that also means that the drive feature you had declaring but it is not working and no one should be able to create object of it okay again we'll see that in some time but when I declare it it is giving you an error now first of all why you will declare when you are not defining anything maybe I have this mindset someone else can just come back and they can say they can Implement a car maybe they will build a a wagon let's say and a wagon will extend my car and it is the wagon's responsibility in fact you know instead of saying wagon let's say wagonr it's a very famous car if you search online uh so we have this wagonal car which some of them don't like not not a big fan of it but okay so let's say we have this card wagonal extends card in fact I have a card which is smaller than wagonr which I love so we have this class wagonr extends car here and what I want here is I want this wagonard to implement the drive feature see if you think card is just an abstract concept right no one actually buys a card what you buy is a real car like wagonard or BMW GLS or the Toyota Fortuner or Ford ecosport right so you buy a real car you don't buy a card this is just a concept so when I say drive here a car don't know how to drive but then when you when you create a real car that car will know how to drive so I want this wagonr to be responsible to Define this method okay so I can just copy this and paste it here and here instead of declaring it I will Define so what I want is I want this wagonr to implement it of course the other feature like play music can simply be extended but not drive drive should be implemented here and wagonr I mean the company who built wagonal cars it's their responsibility to define the method here I can say driving of course they will write a complex logic but just to make it simple I'm writing one statement so they are responsible to Define this method and we are not doing it here now if you want to declare a method you can see we are getting an error here you can use an abstract keyword So when you say abstract it means I'm just giving you the abstract idea of a car should have a drive feature but I don't know how to implement it now when you are using my feature and when you call your vehicle as a car it should have a dry feature or whatever object you're creating is a card you need to have a drive feature and that's how you make it so you make a public abstract void drive so you have to use an abstract keyboard if you want to declare a method okay here we are defining it but if you can see even if I do abstract it is giving you an error it says the abstract method Drive in type car can only be defined by an abstract class okay what it simply means is if you have an abstract method abstract method can belong to only abstract class so that means you have to make this class as abstract as well so you can have abstract methods only in the abstract class and whichever class is extending the abstract class will have this drive method if we don't do that it will give you either example if I change the method name you can see it will give you error it says the typewagonr must implement the United abstract method car dot drive so if you are extending an abstract class you need to Define it compulsory otherwise it will not work so you have to Define you have to basically override the method now when someone creates an object of a car in fact first of all you can't create object of abstract class Point very important point and that's why you can see we got an error here it says cannot instantiate the type car it simply means you cannot create object of car so the point remember you cannot create object of an abstract class okay but then connect it to reference of abstract class yes that is possible you can say car obj that is absolutely correct but if you want to create an object you have to get object off not card but wagonr okay and then with this object you can call drive you can call uh play music and we have talked about Dynamic method dispatch and we know when you call drive it will be calling Drive of webinar not of car anybody is it is declared right uh so basically that's how you can create an abstract class that's how you can create an abstract method and basically that's how you can restrict the creation of the object here now a few more points to remember is it compulsory to have an abstract method in the abstract class I mean we know that it is compulsory to have the abstract method you need abstract class right but is it compulsory to have a abstract method in the abstract class let's do that if you come in this section okay you can say there's no problem it's not like a abstract class should have abstract method abstract class can have abstract methods it can have normal methods so you can have an abstract class with which only has abstract methods you can have abstract class which has only implemented methods which are this which are non-abstract methods and you can have a abstract class which can have both normal methods as well as the abstract method and you can create multiple abstract method okay example if I can come back here and say Hey you know I want a car to fly I don't know when this is possible but in the movies you see that right so a car jumps from one building to the building uh okay and I hope you know the movie name but anyway so if you talk about this abstract method which is fly and you can see I'm declaring it here now when you declare a method and when you say you have an abstract class which is getting implemented or which is getting uh extended by another class it becomes compulsory for you to Define one more method here so you can say public not abstract you can say public void you have to Define fly as well otherwise it will not work so you can say a flying okay and now with this you can call the methods you can say obj dot fly and this should work compile run and you can see it says driving play music and flying now there's one important thing here which is what if when you are creating this class wagonr and then you are not able to implement fly what will happen first of all it's compulsory to Define all the methods okay all the abstract methods so you can see in this class we have three methods but only two methods are abstract and that's why it says it should Define fly as well as drive but we are not implementing fly so you can see it says the type again I must implement the United abstract method car.fly but what if you don't do that so in that case if you have a class which is not able to implement all the methods in this case even this class becomes abstract class so you have to make it abstract class and you can see there's no error but the problem now is you can't create object of abstract class right so maybe you have to create a new class here by saying uh updated webinar which can also fly and this will extend now which class you want me to extend the car or the wagonr of course wagonal right because you will get this drive feature from a wagonr so you can simply extend it with wagonr and then here in this class you can basically create object of fly in fact if you are using IDE and if you don't if you are lazy to type you can come to the seller and you can say fix Quick Fix add unimplemented methods and it will do it for you okay and here we can say flying so even you can do that the only thing is now the object will not be of wagonal the object will be of updated wagonr is it the method class name okay it's up it should be updated right so that's how you can create a object so again we are not getting object of abstract class we are creating object of the class which is not not abstract okay now since we are calling this class as abstract class even this class should have a name right so normally we call them as a concrete class so we can say this is a concrete class okay so we have abstract class and then we have concrete class so you can create object of a concrete class not an abstract class so I hope this makes sense uh that's about abstract keyword so let me just reiterate abstract keyword can be used with methods if you want to declare them and if you have any abstract methods you have to make a class as abstract but again on the other hand if you have abstract class there is no compulsion that abstract class should have abstract methods abstract class can have normal methods also I mean all these normal methods no abstract methods even that is possible next Point remember we cannot create object of an abstract class if you want to create object you have to get a class which which implements all the methods a subclass which implements all the methods and then that class will be called as a concrete class and you can create object of it so that's about abstract keyword in this video we'll talk about inner class that's right a class inside a class I know that sounds weird at first but let's let me go with that see most of the time when you build an application you might have a scenario where a class will be used only for that particular purpose okay so let's say you have a class A and then that class needs variables so we can do that example we have a class A here and this class A can have variables this class can have methods so when you have this class A this class can have variables this class can have methods right now this variables and methods only work for this class A so if you want to access them you have to get a class first in the same way let's say if you have a class where that class is totally dependent on Class A there is no other need for it in that case you can create a class inside the class okay the way you do that is by defining a class like a and then you can define a class called B inside it now just the way you have class A here I can create variable that works we can say variable age and then we can have method as well which is public white show now we have these two things we have a variable which is age and then we have a method which show and then we have a class as well so imagine a class having all three things a variable a method and a class and inside this class let's say I have created I create a method which is public void config and in this config I will print something let's say in config just to print something okay and I'm trying to make this example as simple as possible that's why I'm using this class name in real life don't do that make sure that you use proper class names and so let's say we have this scenario now question is how will you call this config so it's very easy to call show in fact I should print something here just to see what it prints in show now question to you is how will you call this show and then we have seen this right you simply have to create this object of a and then you can call obj dot show I mean show will perfectly work right if I compile this code and run you can see it says in Show Now I want to call config how will you do that do that of course you will say if it is a Class B then you have to get object of B to call this config let's try if I try to create object of B by saying B obj one new B you can see it will give you an error it says uh B cannot be resolved oh it's not able to find B the thing is B is not openly available right B is a part of a so if you want to have a b here you need to say a dot b so B belongs to a that's why you have to say this okay now why I'm doing this so when you say if you compile this code and if I go back here can you see that we got two classes and we have seen this right every time you compile a code you will get those number of class files so you've got a class file for demo that makes sense you can see we have demo.class uh you will get a DOT class as well because uh a is a class but we also got B is also classified it should have its own class file and that's right we got it we got B dot class the only thing is B belongs to a so the dollar symbol separates these two so it says b class belongs to a class and that's why it's a dollar B okay that makes sense so if you want to access B you have to say a dot b that's how you access it and you can say obj1 you can say there's no problem in declaring the variable the problem is when you say new now how will you do it if you say B it will not work again it will say cannot able to resolve B in that case if you say a dot b no that's not the proper syntax the thing is if you want to create object of B you need object of a first now you will say why it's very simple if you want to call B if you want to call this show now show is a method right it's a non-static method which means if you want to call show you need object in the same way if you want to access this variable age you need object in the same way if you want to create object of B you need object of a because that is a non-static method okay so I'm focusing but on static here right can I create a static class uh yes we'll see that in some time but we got this object Here and Now using this obj1 I can call the config method if you want to create object of a inner class which is B in this case you need to First specify where it belongs to so B belongs to a and then you need object of a outer class which is obj here to create object of inner class and once you have that you can simply call this methods and that will properly work you can see what in show and in config okay now we have all also focused on a study class right you can also make this class as static and we all know when you refer it to our static class you don't need object so basically you don't call this with the help of object you can directly refer with a dot b now this will work only when the class is static the inner class is static it will not work for non-static class now can we really make study class yes we can provide it there it's an inner class if it is out of class you can't make out of class static you can see it will give you add it says illegal modifier for the class A only public abstract final are permitted so basically you can't use static here static can be used only for the inner class okay I hope that makes sense so that's the concept of uh in a class now once we know about inner class let's talk about Anonymous in a class I know that sounds weird right anonymous anonymous simply means something which doesn't have a name okay so what I will do is I will just try to remove everything here and let's work with this particular class okay and in fact I needed a a class let's say class A is there okay and inside this class A I'm creating a method which is public void show again just to keep it simple I'm using simple names here and here I will print in a show okay and if I want to call a of course I will simply say A obj you need to create object of a and then I will say opj dot show let's see if this works compile and run it works you can see God in a show that perfectly works now the question I have for you is very simple question what if I want to change the behave behavior of show method okay that means I wanted to do something else now I know in your mind you have the idea right we can simply create a class which is B and this class B will extend a so basically basically we are trying to do method over dividing here in this B as well you will be having the same method which is show which will print in B show we just want to do something else and by doing something else what I mean by in this code is I'm just trying to print something else but of course you can do whatever you want I just want to change the behavior of this show method so we created a new class called B and here what we can do is we can create the object of B but the references of a so what we are doing is we are overriding the behavior of show and then you will say hey we have already talked about it right we have talked about method over writing hold on hold your horses let's see if I compile this code and run it says in Bay show but now if you think about this class now first of all uh as we mentioned every time you compile a code it will create all the class files see we got a B because of the previous code let's delete that you can see we got B dot class as well now you will say what's a harm in that see the Harvest the only purpose of this class B was to extend or to extend a class to override the method and maybe it will be used only once now of course if it is using multiple times in different places of course it should create a new you should create a different class but what if we know that this class will be used only once only for this particular use in that scenario why to create a new class oh then you will say if you don't get new class how will you get a new implementation okay let me show you something what if I can simply come back here just before the semicolon and I can Define what should be the new implementation I know this looks tricky but just be with me see till this point we have seen that first you have to create a class and then you create the object right and that's what we are doing from a long time but now this is the first time that I'm saying I want to create object of a but this is the implementation this is the design for it don't go for the original design I will give you new design the new design is I will use the same method name or same method here the only change I want to do here is it will print in new show I will just provide some indentation so that it will look better so what I'm doing here is I'm providing a different implementation for show method okay and let's see if this works I just want to see compile and you can see that it says in new show it is not going for the original method so we are providing a new implementation here now this is called uh in a class okay you will say hey how it's a class we are not creating we are not using a class keyword right that's why we are not using that last keyword and we are not even giving a name to it and that's why it is a inner class because this class is created inside the demo class and also there's no name for the class and that's why if you go back to your files can you can you see this we got demo inside that we have a class but there's no name for it and since we don't have a name for it it says one of course if we create multiple classes you will have a different number it will say 2 3 4 and list goes on and since we don't have a name it is called Anonymous class so this is Anonymous in a class which we are going to use a lot in the upcoming videos uh once we talk about interfaces must we talk about functional interface Lambda Expressions this is very famous concept so just get used to it now if I if you want me to type this code one second I will do that for you I will what I will do is I will just go back here see it is not difficult it's just that you are unfamiliar with this concept and it will take some time okay so what you do is just before the semicolon and after the Constructor open the curly packets and close this is your class and in this class you provide the method which is show and whatever you want to do in show you can do that here you can say I'm printing in new show and then you can call this show so when you call this show it will call the show of the new implementation not this one okay and yeah that's how it works so that is your Anonymous inner class okay so we have talked about abstract class we have talked about inner class in fact we have talked about Anonymous in a class can we just use both let's try what I will do is I will just make this class as an abstract class because I want to create this method show as an abstract method okay let's say this is abstract okay and then uh let's say I'm not doing all those things now I have a question for you if I want to create a object or if I want to instantiate a can I do that of course in the abstract class we have seen we can't do that because you can't instantiate the abstract class and we have found a solution as well you can create a separate class by saying extends a and then you can provide the implementation here you can just go here quick fix implement the method and one more thing you might be saying that I'm reviewing this line multiple times it's just that we have not talked about what is at the rate override will we have a separate topic on that so at that point we'll discuss that so here I just want to say in the show okay and then of course I can create the object of B and that's done with the help of this object obj I can call show again we have seen this in abstract class concept but I want to talk about something else so you can say it says in B show now if you look at the entire purpose of this class B is to implement the show method that's it and if you are using this method only once then why to even create this class can I use something else can I use anonymous inner class here okay so what I'm what I'm saying is I can simply say new a and okay it's not working but can I provide the implementation here and I can create a method which is public void in fact I will just simply paste the code because we already have that code with me and here I will say in new show so what I'm doing is I'm providing the implementation for the abstract class here itself okay and you might be thinking we are still getting new a right is it the object of an abstract class we can't do that right how we are doing this the thing is this is a class here right the anonymous in a class and we are creating the object of not a we are getting the object of this Anonymous inner class that's a point to remember so most of the time when we have interfaces or abstract class we use this concept so if you want to implement uh the interface or the abstract class only once then you can use anonymous inner class and this perfectly works so let me just try it out compile this code and run you can see it works so we can use anonymous in a class for the abstract class as well in fact if you have multiple methods you can still do it if you let's say we have one more abstract metal here which is public void let's say config we can create a method for config as well and since we are using an IDE it give you the method so you can simply use this and you can print in config so it's not just for one method if you have multiple methods you can provide the implementation in the anonymous inner class in this video we'll talk about interfaces now before we start with the interface let's talk about the abstract class now let's say I create abstract class here so if it's abstract class A and in this class let me create some methods now we all know in the abstract class we can create our abstract methods and normal methods as well but let's say if I have abstract class which only has let's say abstract methods if I say public abstract void show and we have public abstract void config now when you have abstract class with two abstract methods of course this works there is no compulsion to have non-abstract methods here or we call them as concrete methods there is no need for them now whenever you have a situation where you have abstract class which only has abstract methods we have another way of doing it now see all these are decided at design time so when you make an application at designing you specify things okay what are the classes I need and what are the abstract class we need so you create this hierarchy of system of course we'll talk about hierarchy in some time why do we need it but at this point we create a hierarchy and when you have a class which only has abstract methods the other alternative for that is to create an interface so instead of creating an abstract class we can create an interface most short time we use this interface in project development so let's understand that so we have the interface a now you will say what is changing there now first of all interface is not a class okay so that's why that's the main difference between abstract class and interface and by default every method in the interface is public abstract so these two keywords actually not required because anywhere they are public abstract so even if you don't mention they are by default public abstract and that's why if you can see when I put a semicolon just after the method there is no error because behind the scene public abstract is there now this is your interface so interface basically means it's like a class just that all the methods are by default public abstract okay but then how will I use this how will I instantiate this the same problem was there with abstract class right we cannot instantiate it now if you look at this how do I instantiate let's try so I will say a obj see there is no problem in creating a reference of it so that also also means we are creating a type the way you create classes so let's say if a is a class you can say a obj so a is a type right in the same way here the interface a is a type okay this works but now I'm going to try to create object for this it will give you an error it says if you see the error it says cannot instantiate the type A because this interface you can't instantiate it okay the what's the solution the solution is see first of all why do we get interface we create interface so that we can design something we can say example let's say if you ask me about how do we create a table I can say or maybe if you ask me how to reach somewhere so I can say go straight take a right go straight take a right take an auto uh take a bus get down at the next stop so what I'm doing is I'm giving you the path right I'm giving a design you have to literally take a walk I will not work for you I will just show the directions in the same way interface says I will tell you what are the methods you need but I will not Implement them it is your job to implement so when I say yard now I'm talking about classes see ultimately you create object of a class interface just show you the design so here what I can do is I can create a class let's say Class B so we have a class B here and then in this class I want to implement these two methods now in terms of abstract class we say extends right because class extends another class here a is the interface we have to use a different keyword here the keyword which we use is implements so whenever you want to implement the interface you say implements keyword and when you say implements it becomes compulsory for you to Define both the methods okay if you fail to Define both the methods by default your class also becomes abstract okay but we don't want to get abstract class right we want to create a concrete class we want to create object of B in that case it's your job to Define both the methods okay there is one way you can use a shortcut here you can just go back here and say quick fix it will ask you to implement the methods or you can do the hard work you can say public void show and then you can define something here I can say I'm defining in show and then we can Define another method as well which is config or public void config and here we can Define in config so basically what you're doing is you are implementing both the methods as I mentioned before if you fail to implement these methods or this class becomes an abstract class but we are implementing it right now when you implement basically you can't create object of a but you can create object of B and you can say it works and now using this obj I can call show I can call uh what are the method we have we have config so we can call both the methods and that perfectly works if I compile and run this code you can say it says in show in config okay what else can I have uh so the way you create variables here also the way you create methods here can I create variables now first of all we don't uh Define variables right we declare them so we can actually so if I say int let's say eight that's a variable I know I don't know why I'm taking this age variable every time but let's say we have eight age here and let's say we have string area so that's the area name but you can see we got an error that means can I I cannot create variables that's weird I just said we can right so the only thing is every variable in the interface we can create actually those variables the only thing is they are by default final and static so all the variables inside the interface are by default final and static okay uh so that means we have to first initialize the variable So when you say it's it's final we have to initialize the variable right okay that's not my age but let's say 44 and let's say area is Mumbai okay so basically what we're defining is we are defining the age which is 44 and the area is Mumbai now this variables are final and static now first of all since they are static which means we can directly use the interface name remember when we use static variable we don't have to use the object example if I want to print the value of name or area here I can simply say a DOT is the interface a DOT area I can directly use it not with the object and if I compile and run it works you can see we got Mumbai if I try to change the value of this if I say a DOT area is equal to let's say if I want to change the area to Hyderabad it's not working you can say it says the final field that's right the area is final it is static we are using directly the interface name and it's also final okay so that's one thing you have to remember now you might be thinking why it has to be static final see first of all you don't instantiate the interface right whatever you instantiate is a class and you are not doing extent you are doing implements what you get in implements is only the methods right you don't get the variables there in implements so that's why it is static because we want to use the interface name and final because interface objects or interface don't have their own memory in the Heap right what you create object office of B not not of a so if you don't have your own memory how can you have a non-final variable so this is interface in fact before talking about the need for interface we have one more thing to add here so let's say if I have a interface and I'm creating a class which implements a one interface here can a class Implement multiple interface example let's say if I have interface which is called X so if I say interface X so B Class implements a and X both and the answer is yes if you can see there is no error here the only thing is whatever method you define here so let's say the method name here is run you can see we got an error here the only thing you have to do is you have to Define run as well so you will say public void run so basically we can have one class implementing multiple interfaces now that's not the case with abstract class right because in abstract class we can we can extern only one class in interface you can have a class implementing multiple interfaces so here I can simply print running okay so what we are doing is we are using multiple methods here we have three methods coming from two different interfaces this interface says two methods show in config and this interface is has only one method which is one and you can see we got three methods here uh there's one more thing let's say we have one more interface which is called uh interface y now the thing is when you talk about class and Class A Class can extend another class right what about interface but can we do interface and interface what keyword we have to use here see whenever you have interface to interface we say extends so interface y extends X so inheritance is is valid here as well now in this y interface you will be having a method called run so that's the inheritance okay so you will get this declaration in the interface y as well let me show you if I remove this X and if I keep y even now we have to use one otherwise if you remove run from here if you you comment this part you can see it will give you error it says the type B must implement the United abstract method one okay but we are implementing y right it's just that y extends X so we have to use one now Point remember here is whenever you have a class to class we say the extends whenever you have class to interface basically a class is in implementing interface so we have to say implements and whenever you have interface to interface we say that extends again okay this is the point you have to remember so again I will comment uncomment this part okay now there's only one thing you have to remember whenever you create a reference of a in phase and object of B which is a class I can only call two methods which is of part A if I try to call run with a it will not work because a has no idea what one method is if you can see in a we don't have one in fact what we will do is I will just remove this code for time in yeah and then if you want to implement one if you want to call one in that case we need to create the reference of X which is obj1 is equal to Nu B now with this obj one I can call one but again I can't call other two two other methods okay so that's one thing you have to remember okay now once we are good with this let's try to understand what's the need of the interface let's try to understand what is a need for the interface now for that let's take an example here so let's say we have a class here and so the example which I'm trying to take here is for the developers let's say when you join a company of course as a developer when you join a company you want to write code right you want to build application and if you have seen the trend whenever you join a company most of the company will give you a laptop to work on and luckily if you get a Macbook that's great so they give you a laptop right so that's that's the thing right if you want to work in a company let's say if you go to our office and if you want to work there you will say hey I want a laptop so in this case let's say you are a developer okay so I will say you are a deaf right of course you're a human but let's say you're a developer and then as a developer what you do you make applications right or maybe I can say you write code I'll set the app you you develop applications right now when you say you are developing an application of course you will say coding okay you're building an application here of course as a company if I want to build a product I have to hire developers now in this case let's say if I hire a developer let's say developer name is Naveen and then if you say new developer and with this object Naveen I can say Naveen dot Dev app right so I want the window develop application and if I compile this code and if I run it perfectly works right now let's say if I say hey you know I will make an application but I need a laptop first I cannot work without laptop but we don't have laptop right so what I will do is I will create a laptop class I will say class laptop and in this class laptop of course in a laptop what you do is you write code and I can say calling right in fact what laptop when you say code you it basically you code the new compile then you run so you do all those stuff right so I'm saying code compile and run and now as a developer what I want is if I if you ask me to write a code what I will do is I will just say hey give me a laptop so basically you have to accept a laptop object here that's right you can also pass the object so this developer will say give me a laptop and then I will write code so here instead of saying this you can simply say lab dot code right now since you have a laptop object you can basically call this method called code and it will work so what I'm doing is I got two classes here which one is developer and second is laptop and inside laptop class I have a method which is code and then once I pass this object I can simply code right now the only thing is when you ask a developer to write a code developer will say hey give me a laptop so that means first you have to create an object of laptop so I was a laptop lap is equal to the new laptop so we got a laptop object of course every developer needs a different different laptop object so let's say if you hire five developers you need five laptops to give them okay I was a lab here now this should work there's no problem if you can see the code everything should look good uh if I run you can see it still works so basically we are saying code compiled one so what you're doing is from the main method you are calling Dev app which is here and then you are passing the laptop object okay so you're passing this object here and when you accept this object you're calling the code method so it will call this code method and you have to execute this part everything works well right now the only thing is do you really need a laptop see in some companies they give you a desktop right so if you go to the office you they will have a desktop where you have a monitor you have a desktop a good desktop which works so why as a developer you are asking for a laptop what if company wants to give you a desktop you can't say hey I cannot work without laptop right you should be able to work on any computer so let's say you also get an option of desktop there in fact in desktop as well I can do the same thing but I will say faster like desktops are always faster than a laptop right so you can see when you're coding uh it says code compiled One faster because it works fast now what if a company says hey you know I we don't have laptops with us but what we have is we have desktop our desk is equal to new desktop and they don't want to pass the laptop object so that when the company says Hey Naveen you have to work you have to make an application and what we are giving you is not laptop we are giving you a desktop now nobody will say I can't work you can see Naveen says I can't develop application because my application only needs laptop you can't do that so writing laptop here is an issue we can't do that right so how do we solve this problem and that's what you know when I was talking about abstract class and interface I told you about the design thing right so when you back an application you have to make it more flexible here we are what you are doing is you're doing a tight coupling here which is you are saying that developer is dependent on a laptop no developer is not dependent on laptop developer is dependent on a computer a computer can be anything your computer can be a laptop a computer can be a desktop so if you want to improve this example what you can do is you can create a class called computer okay and in this computer class you can say public void code and basically you know I want to keep this empty because I don't want to add a code because we don't computer does not exist right it's just a concept and I can say a laptop is actually a computer so when you do that the advantages in fact desktop is also a computer so when you do that what happens is now both this laptop and desktop they are computer so when I'm creating a reference of it so can I say computer lab is equal to new laptop and computer desk is going to do that new desktop we can do that right we can create a reference of a parent class an object of a subclass and that's what we are doing here same thing can be applicable here right as a developer you are focused more on the computer not on laptop or desktop the moment you do that you can say everything is working there's no problem here is because what you are passing here is Dex desk right which desktop basically but that is referred as a computer and what you are referring here is a computer there's no mismatch here you are passing a computer reference you are accepting a computer reference but when you call code it will be called based on what object you are passing so basically we are passing desktop object So when you say code it will call the code of the desktop not of the laptop let me show you so if I compile this code and run you can say it says faster that's desktop but now if I pass a laptop let's say computer company decided to send you a laptop and now when you compile and run you can see it says code compile and when there is no faster here that means that's laptop code so that's how you design the application right to make it Loosely coupled where your developer object is not dependent on the laptop or desktop it is dependent on the computer any computer will do a mobile phone also do because that's a computer now there's one change see if it if you look at this code we're doing nothing right so instead of defining it here can I simply declare this and we know when you're declaring it we can make it abstract and since your method is abstract you can also make your class abstract and problem solved and that's the importance of abstract class right you can create that abstraction layer where you say we are not working with laptop and desktop in real world as well we do that right uh sometimes we don't mention rows or lilies or sunflower we simply say flowers right we don't say apartment or a villa or anything we say house right so we try to generalize things in real life in real in virtual world as well it's good to generalize things instead of specializing things there's one more don't you think we can also use interface instead of using abstract class so we are using interface we don't need public abstract the only change you have to make is instead of using App extends we have to say implements and that's the beauty of interfaces right I know we have written a lot of code maybe you have to watch this video once again to understand and try it by yourself that's when you will understand the power of the interfaces and what you are creating here is not the classes reference we are getting interface reference yeah and that's the importance of the interface in this video let's talk about enums now enums are named constants which we create so basically if you try to have some consonant example you know when you talk about other codes or if you have worked on a networking concept so let's say when you send a request to the server the server will give you some status right if it is accepted the request if it is rejected the request or the resource not found the four not four error so basically you know we have these numbers for every type of data you get from the server and also we can have a name for it now when you know that we have a specific error codes or when you have a specific constants which you want to use instead of creating a different object by yourself you can create are enum now how it looks like let me just give you a simple example so let's say we have enum here and I'm saying I want to go with enum of status so I will say status now in this status I can have different constants which I want to use now of course you can create a class and you can create multiple objects of it so let's say this status can be of running State a fail State success pending so you can have all those things and of course you can create a simple class called status and you can create multiple objects right but just to keep it simple we can use enum where we can Define all the names constant and they said this is pre-built okay example let's say if you talk about status I can have status like running status I can have status and of course you can if you can separate them with the help of comma you can say running then we can have failed uh then we can have pending and let's say success of course you can change the sequence doesn't matter but you can have this constant now the advantage for this is if you want to use an application where you want written a status to the client uh that status can be running the status can be failed pending or success instead of getting returning a string format or a specific object you can simply return this status now how easy it can be it's very easy you can just go back to your main method and you can say I want to have a status so you can create a status reference which is s let's say and this is same as what you use normal variables example if you want to say int I and then you'll send the value as well right let's say five so we have this int as a type then we have this I as a variable and then we have a value which is five in the same way you can have a type as status that's right this is a type of it so all these are objects now this is something different in Java now if you're coming from C plus plus in C plus plus we directly create these status and we have all these constants and it treats in them in a different way but Java says oh everything is classier so basically the status is a class here I know that sounds weird but yeah that's how it is implemented this is a class and all this are actually objects of status so by default you got you got four objects and you can use any of the object here example I want to use let's say a status dot running now that is my object which has been assigned to S now of course you can print this SEO and it will print the running status so I can say compile and run you can say it says running so basically you can use any status here now what are this just to reiterate these are actually named constants so instead of using numbers or string we can use named constants it will make your work bit easy again I'm not saying you have to use this everywhere but if you have a scenario where you want to use constants uh we can we can use this concept and you can use any constant here we are using running I can also use failed even that works compile and run you can see we got a failed status but yes if you try to use a status let's say no idea status now if you can see this status don't have any no idea and that's why you can see we got an error here it says no idea cannot be resolved or is invalid field that's right so you can only use those status which you mentioned here now in other languages we have one more thing you know all this actually has a number uh so basically you get numbering for it maybe one two three four so that you can pick this state as based on numbers in Java it starts with zero so this is zero one two three and if you want to get that you can actually do it so you know you once you get the status you can use a method called ordinal so so different constants here have different numbers this is based on the numbers right or the order of it so this is first second third fourth right but when you have zero one two three so when I try to create the ordinal of running of course it will point zero and that's what you want to see so you can see we got zero but yes if you change it to success which is three in this case you can say we got three in fact you know you can also print all what if you don't want to print one you want to print all here uh so what you can do is instead of getting one status you can get all the status first of all because if you say status doc success it will only give you one status but what if you want to get all so in that case instead of saying this you can say status dot so there's a method here if you can see we got values and that's right why we got methods is because this status is a class and you get some inbuilt methods now from where you are getting these methods we'll talk about it in some time or maybe maybe next video but uh you got methods so you can see status dot values will give you what okay so let me just jump to values just to see what it will give you so status dot value will give you an array you can see it gives you an array so on this side we have to say this is not a normal variable this this is a array and for that maybe I can say SS just to have a different name and once you got this array you can print all how you can just come back here you can print all let's try I'm not sure if you can get all here no compile time issue run or it is spending the address or something I know that okay so what you can do is you can specify the index value I can say 0 it will print running yeah it works but then what if you want to print all and we have seen it right we can use a for Loop here now which one I will use enhanced for Loop so I will just cut this part I will use enhanced for Loop here and every time you iterate of course it will give you status right so what is this added this arrays of type status so it will give you one status at a time so you can say status s colon SS and here you can print the status so basically it will print all the status for you so you can say it is printing all the status now with this s you can also print the order of it just to see how it works so you can say s dot ordinal it will print yeah so it will also paint the order of it 0 1 2 3. yeah so let's expand our example of enum with if else and switch now as I mentioned before these are constants right so basically when you have this constant can we just compare them we can actually the way you compare normal variables or strings so let's take an example here let me just remove int variable there and let me also remove the array let's keep it simple now so let's say I have a status here and S is equal to and the status of which I want to set is let's say uh pending okay initially the state value pending and this value can be coming from anywhere maybe you called a method which returns this success or this status and you want to check what is a status and based on that you want to print some value example if it says running I can print all good if it is failed I can say try again if it is pending I can say please wait if it is success I can say done so how do I know what is this value for that you have to compare right so you can say if the S is equal to equal to status start running if this is a status I will print I will come back here and I will print all good and I can say if else I want to check for the second one I will say if status dot uh if it is matching with failed I can print try again else if if the status is equal to I mean s is equal to status dot let's say pending now in this case I can say please wait else of course you can also do if else else here but we only are left with only one status I can say else I can print done okay so basically you can actually compare the status with the help of if else and we can just come back here say compile and run you can see it works it says please wait because this is pending but what if you got a status which is a running state in that case it will print all good okay so this is how basically you can work with if else but what if you want to work with switch because say in switch there is a limitation which now a switch can support right it supports integer string and many more but here the question is does it support enum and the answer is yes so what I will do here is instead of using IF else I will just reuse the code but I will use a switch here and in this switch you can pass this status as it is there's a change compared to if else which is when you say case you know in if else you basically have to use the status as well but in case you don't have to do that because anywhere the S is of type status right so it knows whatever your passing is basically a part of a status so you can directly use the constant itself so I can say running if it is running I can just print this statement and of course in switch we have to also put a break and then we can check for the second case so that's case uh failed and here we can have try again so of course we have to say break here as well what else we have two more anyway let me just delete the entire stuff from here if part you know I'm so lazy I can just reuse this so the next status is what pending in pending I can say please wait and then the last one is success in check for Success yourself even writing the case we can say default and here we have to give a colon and we have to say done so basically whatever we have done with if else we can also do it with switch and switch actually makes much more sense here because we are working with constants right so switch looks good in the case of enums and now the question is will this work let's try compile and run it works you can see it says all good but yeah if I change the status to let's say success I just want to see if it prints done yeah we got done so this also works with uh switch so that's how we can use with enum if you if it enum and I've also mentioned that enum is a class right so does that mean we can extend any other class uh the thing is no that's that's a thing which is not suitable here you can't extend enum within any other class apart from this everything is same that means you can Define methods you can Define Constructors that's right you can create Constructors you can also create your own variables inside the enum and that's what we are going to do here okay so what I will do now is and there's one more thing is remember with enum we have use certain methods for example if I come back here and if I say uh status was the object s okay here let's do it here so if I say s dot you can see we'll get some methods here and we all know that by default every class in Java extends object and that's why you will get the hash code and then two string Methods which is here but what about this methods ordinal uh name and then uh compared to now from where you are getting all these methods so by default enum in Java extends our enumclass okay let me just prove that to you I will just print it here s dot get class dot get so what I'm doing is I'm just print I'm getting the class name and the super class of it so I'm just I'm just trying to print this super class of our class enum and I will just compile and run you can say it is enum so inum in Java extends E M class and all those extra methods you get it from enumclass okay now once we are good with this let's treat our own class as enum and I will do a different example here instead of using status I can let's try to use laptop because we have to use some Constructor stuff which makes much more sense with the laptop and let's say we have some laptop models which I which I love uh example MacBook then we have Dell XPS then we got surface not a big kind of surface by little surface the Microsoft laptops and we have ThinkPad I used to love ThinkPad when it was with IBM but anyway let's see right and then what we can do is let's say for every laptop I want to define the price as well now how can we do that so what we can do here is just after this MacBook because that's an object right so just after this I can say hey Macbook will cost you let's say two thousand dollars uh XPS will cost you around let's say uh two thousand two hundred dollars Express a bit costly and surface is let's say 1500 I'm not sure the cost and let's say ThinkPad is eighteen hundred dollars so we got this cost for this laptops here right and then okay it gives you an error but we'll solve that error in some time now first of all what is that error so we are trying to say that this is a price but how will your enum will know that you're talking about a prize here now in this case what we can do is let's create a variable int price so that we can have an instance variable and then if You observe here don't you think this is a Constructor you can say it says that a Constructor of laptop that's why we can have a Constructor here so when you create the object by passing a value that's what we are doing here we are saying these are the objects we have and this is a Constructor where we are passing a value okay that means I can simply ask my editor hey you know I need a Constructor here which takes the price and yeah we got a Constructor and now you can see there is no error that means whatever value you're passing here will be assigned to this price and the beauty is this laptop is a class and all these are objects and every object will have a different value for the price it's not like once you define price for one object it will be applicable for all no that's not the case and once you have this let's get back to our main and here let me create a laptop lap is equal to and let's say by default I maybe I want a Macbook here and once you got the hold of MacBook I can let's say I want to print I want to print lab which of course it will print MacBook let's try once yeah it plays MacBook I also want to print the price of it oh now that's tricky because the price is private and we have seen that make make sure that your variables the instance variables are private how to access this now one way to do that is with the help of Getters and Setters the only problem is can we use them here yes we can create our own methods so what we can do here is I can right click and I will say hey I want a Getters and Setters basically you can change the value as well okay so that means if I want to print the price I can just come back here and I can print lab dot get price now this is specifically for Macbook so if I use a different object here I will get a different price and with that if I try to compile and run you can see we got two thousand now what if you want to get all the laptops okay let's try so we'll just comment these two sections first of all you need to get all the laptop that means you need arrays so it's the laptops or in fact you know what you can do you can directly use a loop here and you can say laptop lab colon from the laptop so this is the enum you want all the machines or all the constants so how do we get all the values so dot we have seen this right we can use values here which will give you the array and one by one you can fetch the value so what you can do is you can print the object and also you can print the price let me print the price how do I get the price it's a lab dot get price okay I hope this will work let me just verify once yeah we can do that so I'll say compile run you can see we got all the values and at any point you can change the value as well okay so you try that so try to call set price and pass a particular value here and your price will change okay there is one more thing uh what if I don't specify the value for surface let's say we have a surface here and then I don't mention this value now what will happen you can see we are getting an error why before it was working the thing is see by default we have a default Constructor right the moment you mention a parameterized Constructor it says hey if you want to get a constant you need to set the price now because you have a parameterized Constructor there is no default Constructor here so you can say okay I know how to solve that you can create a default Constructor here so you can just right click and say Source action yes I want a Constructor without the price okay and you can see we got a default Constructor here and it's your choice if you want to specify some price here so you can say price is equal to let's say minimum five hundred dollars you can specify those things and now if you can see the price for surface will be 500. even if you don't mention you will get a default Constructor and the beauty is you create four object it will call the Constructor four times this Constructor three times and this one once let me just prove that point I will just try to print something here I will say in laptop and let me also print the object name itself I can just say Okay this dot so we got a name here let's try to see if this works compile and run okay so that we got the name as well so it says in laptop MacBook in laptop XPS in laptop ThinkPad but not surface is because surface because it was created with the help of default construction we are not printing anything there okay so that's how we use it and and mind you we are using it as a private Constructor not a default or the public it's because we are creating the object in the same class itself and that's how the private Constructor works you can create the object in the same class in this video let's talk about annotations now annotation simply means a supplement to the Impala or to the runtime or we also call it as a metadata so basically what happens you know when you write a code sometimes you want to interact with a compiler by saying something of course it will not change the way your code will work it's just that we want to supply some extra information to the compiler or to the runtime how do we do that so let's say uh we will go for a normal class here so let's say we have a class A and then we got a class B so basically I'm trying to go for method over writing here so of course we have to extend a class and then uh this will have a method which is show and then in this show I'm printing let's say in a show and the same thing I can do in a b as well the only thing is we have to change it here now what we are doing here is we are overriding the show method of a with the show method of BM then we have seen this before right okay let's create the object of B just to understand this so I will say b o b j equal to new B and then I can call obj Dot show and we all know what will be the output uh so if I compile this code and run you can see it says in B show perfect now let's say I change this method name okay not sure but something else I will say show the data which belongs to this class okay so normally in Java we give a meaningful name to a method right and sometimes your method name can be big and yes okay I know this is exaggerated but sometimes you do have a big method names with two or three words or four words and then of course if you want to override this particular method we have to type the same thing here so I will say the data which belongs to this class okay so again I'm just trying to override the method of a class with the method of b class right and of course we have to call this now so I will just try to get it from the suggestion okay so if you can see I'm just trying to call this method method because we have overed it in the method so it should call in B sure right so if I compile this code and if I run this code oh it is printing in a show now we were expecting in B Sure in fact I wanted in B show but we got in a show now this type of problems in programming is called Bugs okay uh bugs are simply logical problems where you are expecting something else and you got something else example you want to say two plus two and the output is 4 but you got five that is logical problem it's not a compile time issue it's not a runtime issue it's The Logical it's the logic which you have written is wrong and one of the most difficult problem to solve is logical problems you know we always say in programming that you know you spend less time in coding and more timing debugging that what is happening here so of course you have only two classes here and it is easy to debug but if you have a lot of classes it will be very difficult now how will you debug of course we are trying to call the method of b class but we got the method of a class right how would you solve this problem so let's try to understand the method name on purpose I change the method names okay I know it might be visible for a few people you can see it belongs here we have S in this method I don't have it and sometimes you can miss it right and then when I'm calling it I'm passing s on purpose I was calling the method of a class this type of problems can arise when you write a code so what you do is you can show your intention to the compiler and compiler will help you here of course right whenever you make a mistake compiler gives you error and looking at the error we can easily understand hey I did something wrong and let's go back and solve this so what if you can ask your compiler hey compiler you know what I'm doing here I'm trying to overwrite the method here of Class A and if it is not happening let me know so you can show your intention and you can show the intention with the help of annotation there are a lot of inbuilt annotations available in Java so one of them is overdrived so you can simply use add rate here and you can say override now you are saying by doing this to your compiler hey you know I'm trying to override the method and your compiler says you know what there is no method called show the data which belongs to this class in the a class and then you will know oh I made a mistake and then you can solve this problem just by putting s of course you can compare these two methods and you can say I got the problem so of course you have to solve the problem so compile is helping you by showing the problem right at least you got some help and solving the problem at compile time is much better than solving the problem at runtime or when you deploy the application to the users and then users will give you a prompt hey you know you made a mistake so let's avoid that embarrassment and let's solve the problem at compile time so at overnight is one of the way you can do that and now if you try to compile and run you will get in Bishop now apart from this override we have different annotations as well uh which we normally use in terms of core Java we don't use most of the annotations of course you can create your own annotations but in general we don't use much now once you start working with the Frameworks example uh when you talk about hibernate framework or spring framework at that point it is mostly driven by the annotations now there are some annotations which works on the methods there are some annotations which works on variables there are some annotations which works on class level so example uh there might be some annotation which you will be using on class level example if I say deprecated now this is one of the annotations we have now what we are saying that we have this class A deprecated simply means you can use it but don't use it it is deprecated soon it will be removed from the Java language or there is a better alternative for this so you can mark your class as duplicated so that if someone tried to use your class they will get to know that this class is deprecated now apart from this you can also use okay these are all class level annotations in fact we are going to see one mode when we start with interfaces okay so if I try to use something on the method level let's see what we have so we have safe varrogs for the variable arguments we have suppressed warning if you want to hide the warnings once we start with hibernate framework we'll also say transient a functional interface we are going to see in the interface concept yeah uh in fact you know when you create your own annotations we can also set the retention so example let's say whenever you use an annotation till what level you want to set it do you want to set for compiler level that simply means if you set a detention for compiler it means once the code is compiled The annotation will not make any sense after that but if you say annotation is applicable for runtime as well so after compiling as well when the code is running at that point also The annotation will be applicable so we have different options there and of course this will make much more sense once we start working with Frameworks so yeah that's about annotations and uh so we have different annotations and remember when we were doing method over writing and if you ask example let's say if I remove this part and if I ask my idea to generate a overnight method so I can just go back here and say override I want to override this particular method by default it will give you at override okay so that's you have to remember now let's talk about functional interface and we know the functional interface is an interface which has only one method so let's let's create one so what I will do is I will create the interface here and I will call this interface as a okay and in this interface let's create one method okay only one method so I can say public abstract void show and public abstract is optional so we can simply say void show right and then I can implement this I can use the I mean how do I create the object of this of course we know we can create a class we can implement this interface and then we can create the object of it right now this interface is called a Sam which is single abstract meta interface or we can call this interface as functional interface and to specify that this is a functional interface we can use an annotation which is a functional interface by doing that we are making sure that we only have one method here okay see if I don't mention functional interface and my intention was to have a functional interface but if I if I write one more method here which is one okay there is no error here and we want errors right so if you have an intention of creating only one thing I mean single abstract method interface which is only one method and then by mistake you return two methods here you want compile time errors right so if you write it functional interface you can see it will give you error it says invalid functional interface annotation a is not a functional interface so it's good to specify The annotation here by specifying a function interface but then why we are giving so much importance to functional interface we'll understand that in next video but let's say let me create the object for it okay now of course I can't simply create a obj equal to new a that is not possible because we cannot instantiate the interface we can do one thing we can create a class here called B we can say B implements a and we can basically Define this method which is public void show and then here we can print in show of course you can do anything here but just to keep it simple I'm saying in show and now instead of creating object of a I can get object of B which is possible right and then I can call all obj dot show this is one way the another way of implementing this is we have already seen which is Anonymous in a class so what I can do here is instead of using this class here I can just remove this entire stuff or maybe I can just comment this part and I can come back it by saying Hey I want to instantiate a by defining its own implementation here okay in fact if I just say enter it will back it's open close okay so you can do this and then here itself we can Define the method which is public void show and you can Define whatever you want to do which is in show so what we are doing is instead of creating another class we are using Anonymous inner class and we know how to use this right and then I can print uh show here let's see if this works compile and run you can say we got in Show now since it is a function interface uh now we are talking about Java 8 features so till this point everything was good till Java 7. now in Java 8 they have done something awesome you know they made sure that Java is no more verbose now what is verbose means is specifying everything making a detailed code it's good most of the time to read the code but sometimes you know you need to shorten the code to reduce the number of lines to reduce the bugs and all those stuff so in Java 8 we got this amazing feature called Lambda expressions and we can use Lambda expression only with functional interface now let's try to use Lambda expression here so what we are doing is we have a functional interface and then we have discussed about it function interfaces interface which only has one method and then we were able to instantiate a with the help of a Anonymous in a class and yeah we were able to call the method now how do we use Lambda expression here now Lambda expression says we just simplify the anonymous inner class okay only for Lambda expression now see if I tell you okay just try to imagine this if I tell you that there's an interface called a and there's a method called show so in your mind you know there is a interface a which has a method show and if I ask you to do this if I say try to complete the sentence if I say a obj equal to now in your mind you know a obj equal to means there should be new a round bracket 30 packets open and then public void show now how you know that this is new a because it's a object right we're trying to instantiate the interface here of course you can't instantiate the interface but we're trying to instantiate the class of interface here a class which implements the interface so basically when you say aobj this is something you can imagine right there is something you can think about in the same way now since you're talking about the interface a which only has one method you also know that the method name is show which returns returns nothing it's avoid type and it's also public by default so from here till here you know it right so if you know it even Kampala should know this right why you as a programmer need to type this code what if Java says hey don't worry I'm there you just don't have to type all those things you just say back it open I will understand what you're trying to do the only thing is you have to put that uh Arrow here that's it this is called a Lambda expression this Arrow here is a Lambda okay and this is the entire expression we have okay so just to redo the same stuff what we are doing is we are removing from here till here and since we are removing this one curly bracket here we have to remove this one as well so first let's remove that Curly bracket and then we'll remove the entire part from here and then we have to put a arrow so by doing this we are asking our compiler hey I will not type the code I will simply say aobj equal to it's your job to complete the sentence see behind the scenes a compiler will do it for you it's there behind the scene new a public void show is there it's just that syntactically the code is getting reduced this is called a syntactical sugar right where you are reducing the code okay so this is how it looks like and also if in this method you only have one statement so if you have multiple statements you need curdly packets but if you have only one statement you can skip that as well if you can skip this semicolon as well because it's not doing anything and you can write everything in one line and look at the beauty you can reduce the code just like that and I know you still have that doubt in your mind will it this work let's try and it's working you see that this is Lambda expression okay can we do something more with this of course I want you to re-watch this video The Cutting part and try it out now let me can we do something else here what if the show method actually accepts a value which is let's say int I okay and then from here also you have to pass the value the only problem is how will you accept the value here now instead of making the changes here what I will do is I will just go back to our previous syntax so that I can show you step by step what we are doing okay so again I'm passing a integer I here and then we have to pass the value here now this is not a method which is not taking any parameter this we are taking a parameter so I have to accept it here as well so you have to say int I on any variable it doesn't matter and then whatever you want to do with that variable that's your choice let's say I want to use it it's your choice if you want to use it or not but I'm printing in show I the value whatever the value of I is which is 5 in this case let's try to run this code to see is it working yeah it is working you can see we got five now can I use a Lambda expression here and the answer is yes what you can do is again from here till here we don't need that and we can remove the study packets as well the only thing I have to do is you have to put a arrow here we don't need this semicolon we can just put everything in one line and that's what we have done and yes if you even if you have variables you can do that not just one variable if you have multiple variables uh you can put multiple variables here let's say a into J as well and then here also you can accept into J just it's just that you have to pass two values let's say five and eight we're not doing anything with J but it's your choice if you want to use it or not as of now we are working with only one variable so let's stick to that let's pass only one value and let's write only one let's see if this works compile and run it works you can see that we got in show five so there's one more thing you don't even need to mention the type of the variable you know why is because we have mentioned it here right when you know that the variable type is end why to mention it okay and one more thing if you have only one variable you don't even need this round brackets you can simply mention the variable name here oh we have reduced the code so much okay so let's try to compile this code and run it works right now let's do something more with this now let's say what if you have a method which returns something what if you have a method which accept multiple values so let's try to do the example for that in the next video but if you see the files here okay this is the older one let me just delete all the class files just a keep it simple delete and let's recompile the code here compile the moment you compile you can see it is creating only two files a DOT class and demo.class of course behind the scene there is anonymous class but it is not creating a class file for you so it's so it saves the amount of files which you create of course the demo.java file will be quite heavy now if you compare the file size before using Lambda expression after using Lambda expression the demo file size will increase but it will not create a new file okay so let's files to manage okay so that's how we use Lambda expression here so let's try to create some more methods which returns something else so let's say instead of creating show let's work with ADD which returns a integer value it will take two parameters and I and in J right let's say we have this and even before we go with the Lambda expression let's try to create normal objects I will say a obj equal to new a and then you can also go with the creating a different class or we can go for anonymous class now we are used to it right and the method name which is public int add which accepts two value into I comma into J and now you want to return I plus J right of course we can create a different variable we can add we can return but again just to reduce the number of lines we are just saying return I plus J okay and then once you have this we can simply uh take the value in result is equal to obj dot add and we can pass to values 5 comma 4 okay and then we can print result simple stuff right now if I go back here and if I see if this works compile and run you can see we got nine so this is working now can I use a Lambda expression here let's try so first of all let's remove the entire part here this is not needed and since we are removing this curly brackets we can remove this one as well so remove and remove and then we just have to put a arrow here okay just missed one semicolon okay so one more thing as we know that this is optional I mean we only have one statement right so we don't need to put curly brackets so we can remove that one as well things are working okay let me just run this before removing it to see if everything is working compile and run oh it's still working and now I can do one more thing we can remove this curly packets now we can write everything in one line as you can see there's one thing whenever you have one statement and that to the statement is return you cannot write return just return so you have to remove that as well so whatever you mention here actually becomes height and type okay so when you have one statement which is returned which is an expression you can see I plus Json expression it becomes a written type a written value one more thing you can remove this ain't and int as well and you can see the code is so small now and that's the beauty of Lambda expression and one more thing so let's say if you are calling a function or the method by passing the object you can also pass the Lambda expression in fact once we start with the topic of collections we'll see that how do we pass the Lambda expression as the argument okay that will be quite fun so yeah so even if you have a method which returns a value you can still use a Lambda expression there so some points remember Lambda expression only works with a functional interface if you don't have functional interface it will not work because if you have multiple methods let's say add and subtract both and when you write this type of line which method you're implementing is it add or is it sub that's the confusion right and that's why it is not allowed so Lambda expression Works only with the functional interface so yeah that's about a function interface which has a method which returns something in this video let's talk about different types of interfaces so when you talk about different types what are the options we have so basically you know we can divide the interfaces so we'll say type of interface we can divide interface into three parts okay the first one can be we can call them as normal interface the second one is your functional interface and the third one is your marker interface now this functional interface is also called as Sam so why it is called sample discuss in some time but let's talk about interface here now when you talk about interface at this point we know that in interface we can only declare methods right other thing is after Java 8 we also got an option of defining the method in the interface but we'll do that later at this point we only know that in the interface we can only declare methods so all the methods and interface are public abstract right now whenever you create the interface which has normal interface where where you have multiple methods it is called a normal interface so example if you create a interface here and let's say the interface name is ABC and in this interface if you have two methods or more that is normal interface so whenever you have an interface with two or more methods that's normal interface and by doing this you might have guessed it right what could be the function interface now basically before talking about function interface let's talk about Sam now Sam stands for single abstract method okay so that simply means if you have the interface and let's say the interface name is ABC again and in this interface if you have only one method that is called a single abstract method interface which is Sam and we can also call it as a functional interface now why this called functional that we'll understand in the next video when we talk about function interface we have a separate video on that and next twin we have is a marker interface now what is Mark interface a marker interface is an interface which has no methods that's right a blank interface now why someone will create a blank interface is to update something to the compiler so let's say if you want to talk to a compiler by saying please allow this example you know uh in Java we have this Concept of Civilization which means you can take the object and you can store the values of the object in your hard drive let me repeat you can take the object let's say object has multiple values so we can create let's say in your HEAP memory okay I will do that here so let's say in your HEAP memory you got an object and this object has different variables a b c and this has some values let's say a value is 10 B value is 20 C value study and then if you want to save this data in the hard drive let's say we have a hard drive here and if you want to store this data we can use a concept of serialization and then you can destroy this object the thing is why we are saving these object is because what if you want to recreate this object later so let's say when you're playing a game and when you're playing this game you want to save the game now now when you save the game let's say when you're playing this game the game takes around 4GB or 5gb in your RAM but when you save the game it will not consume that much of memory right it will consume around in KBS it's because when you restart the game after closing it of course when you start the game you will get a default values now if you want the old values you want to resume the game you will simply load the current stats so let's say after some time you close the game now game is no mode but you have a file in your hard drive and now you just started the game the value for ABC will be zero zero zero what you can do is you can load these values from here now this is called deserialization so this is serialization and this is deserialization okay now by default every object is not allowed to do that so it is not allowed okay you have to give the permission how will you give the permission so you can simply create a class and you can give the permission with the help of Market interface so we have a lot of Market interface in Java uh serialization or serializable then when you talk about the advanced part also we have some interfaces but it's it's important okay so whenever you see an interface which is blank that's a marker interface and maybe in the upcoming videos when you talk about some Frameworks we'll talk about different marker interfaces but that's what it is uh so what is normal interface which has more than two methods or two or more than two methods the functional interface or Sam only has one method now this is actually important for the upcoming videos and try to understand this every time you have an interface which has only one method uh we can use some extra features with it in this video we'll talk about exceptions so before you understand exception we have to first understand different types of Errors so basically when you talk about errors we have different types for it the first one is your compile time error now what this errors are the second one you have is you have runtime errors and the third one is logical error now how do we describe this let's say if I write a statement here and if I try to print something and if I make a spelling mistake let's say if I make this as a capital P of course your IDE gives you an error here but even if you compile this code you will get a compile time error so it says either cannot find symbol print now this type of errors are actually easy to solve because your compiler will show you hey you know you make a you made a mistake here so this is capital P this will not work so you will know okay uh I have to make it small and then it will work right of course when you compile this code you will not find any error now so when you get error like this this is syntactical errors and also a compile time error which are very easy to solve the second time of error we have is run time what is run time without now in fact let's talk about logical first and then we'll come back to runtime now logical error means everything is working there is no compile time issue everything is working and then you what you did is you added two numbers let's say you added two plus two and of course for this the output you were expecting is for and the output will be full and that's very simple right but what if you did some calculation not a simple code like this you did a multi-line of code where the output is not four output is five now it is working there is no compile time issue there is no runtime issues not like at runtime it is getting stopped but the output you are getting is wrong right so you got five where you wanted four now this type of others are called logical errors also sometimes we call them as bugs so even this type of problems are actually difficult to solve because you need to do a lot of testing you know if you heard about this whenever you make an application you have to test your application as well so in that case you will do testing you will check for the inputs and outputs and everything so maybe you will able to solve this problem but the thing is from the developer side you do check compile time issues which compiler will help you there you do proper testing where you can solve logical issues now once everything is tested you can ship your product to the client and client is happy there is no compile time issue there is no logical issues now runtime issues are a bit different the problem is in runtime issues what is what is happening is everything is working I mean compile time issue no problem your code is working as well but suddenly because of some reason the code just stopped working you got errors okay uh basically you know whenever you get errors one time others your execution normally stops and that's an issue let me get an example here so let's say uh you made an application where you your application opens a file a txt file from a C drive okay or maybe a home folder in Unix system till this point it was working because the file was there and now let's say uh you deleted that file so yesterday night you are doing something some experiment with your system and then the file is no more and now today when you are trying to use the application application is trying to find the file the file is not there it will give you a runtime error the execution will stop in between now you will say what's the big issue right you can restart the application yes you can and everything will work provided the application is not that critical if you are using a calculator and calculator stops in between that's okay you can reopen it when you're playing a game and suddenly The Game Stops you can do it but let's say you are into a game battle where where you will get let's say one million dollars if you win and suddenly your game crashes now we don't want that right uh we can also go for some applications which are the critical example let's say if you talk about medical softwares you have all those scanning machines and then which where you go inside the machine and then it scans right it throws some place or some things I'm not sure about medical terms but what if the software just stops in between risky what if you're in a plane and then the plane software stops working very critical right so you have to make sure that even if you get the errors runtime errors it should not stop your application I'm not saying they will not be a runtime error I'm just saying we need to handle the runtime errors so this runtime errors we can call them as exceptions and basically we need to handle the exceptions okay so handling the exception is very important and that's what we are going to do in this set of videos so in this video we have talked about what is exceptions exceptions are basically one time matters it's not like we are going to stop the entire metals of course we'll try to check for the conditions you know if when you try to open the file first you have to check is the file exists if the file exists then you open try to open the file those are the things you can avoid runtime errors let's say you're dividing a number by zero so when you divide a number and if you divide by zero it will give you exceptions so even before divide if you can check if the denominator is non-zero then divide so those are the checks we can do but in case if there's exception you had to you have to handle it now let's try to handle the exceptions now we know what is exception let's try to handle them so what I will do here is first of all let's create exceptions right that's why then we'll then we'll know how to handle the exception now even before we talk about handling the exception what type of statements we need to handle so for that what I will do is let's talk about statements since statements can be of two types test statement can be of a normal statement and a statement can be a critical statement okay see in real life as well we try to think about situations which are normal and we try to think about situations which are critical example let's say if you're walking with a kid on the street and or let's say in the mall or in the restaurant and then of course you will grab the hand of kid right but what if a kid just runs in that case if it's a restaurant you will not be that Panic right you will try try to say okay he's roaming around and then you will try to catch the kid but what if the same situation happens on the street a busy highway where you are holding the hand of a kid but kids try to run in that case you will jump to grab the kid because that situation is critical in the same way when you talk about statements it can be normal statements and critical statement example if I write a simple statement here which is int I is equal to 9. now this statement is normal statement because we know what what may go wrong here everything looks good right so this is a normal statement but what if you say ain't J is equal to I divided by or maybe I can say 8 divided by I now this statement can be critical if you look only this statement now why this is critical is because if the value of I is 9 that's completely fine you will get the output example if I just come back here and if I try to print the value of J in fact you know what I will do I will just make this 18 so that it will get some output instead of 0. compile and one this works right but what if the value of I is not 9 it's 0. now in this case first of all let me just create the screen so that we can see the output properly and you can see we got an error it says exception exception in thread main uh which is the exception is this arithmetic exception so if you can see it's arithmetic exception and the message is divided by zero now the moment it happens it actually stops the execution let me show you that if I try to print by here you will see if I try to run this code and run you can see it is not printing by the result is not printing by is because at this point the execution has just stopped okay so that's why it is always better to handle the exception even if you know nothing may go wrong if you think that's critical handle it how that's a question to handle the exception what you can do is you can put this type of statements in some other block now first of all let me just declare the variable here by zero and then this is a statement which is critical right so what I will do is I will just put that in a separate block so I will put that in a separate block some something like this and now what I'm saying is hey Java just try to execute this code okay if it works that's fine otherwise execute the other part don't stop the execution the way you can do that you can say hey Java try to execute this statement so I can say try try to execute okay Java will say okay uh now I know that you are just trying to execute if not it's not working then I will continue with the normal execution here but then what if Java tries to execute this and there's a error in that case it will throw the error and you as a developer you have to catch the error so in this case you will catch the exception the moment it throws the exception it is showing you an object and you have to accept it as an object so you will create an object or you will refer that with an exception class so this exception here is a class and you are referring that with any object will do it's not just a you can say obj as well but most of the time you will say e because it makes sense right exception e you can say obj as well that perfectly works but let me say e here and if you want to print something you can do that here so whatever handling stuff you want to do you can do that in the catch block so I will simply say something went wrong okay so what I'm doing is I'm trying to handle the exception if it works that's great if it is not working it will execute the catch block now let's say the value of I is not zero the value of I is let's say 4. in this case you can see there's no exception so let's try to execute the s with the value 4 and if I run this code you can say it says 4 and buy there's no problem with the value we got 4 because of this statement and we got by of course but then what if something will go wrong what if the value of I is 0. now in this case this will throw the exception so it will execute the catch block and I'm expecting it should print something went wrong let's try compile and run oh you can see that it says something went wrong now even if something goes wrong the execution is still happening the value of J is 0 because before the execution of this try catch the value of J was indeed 0. right that's how you handle the exception and again you don't have to put all the statements here you can you can put normal statements in try as well that completely works but it's better to put only critical statements inside my blog and you can write multiple try blocks uh so one we have it here what if we have some few more statements here you can try catch that as well that that works so I hope you got the idea what is tried try will basically try to execute these statements if everything goes good it will execute the remaining statement if something was wrong if there's exception it will execute the cache block so Point remember the cash block will be executed only in case of exception otherwise cash block will be skipped so that's it from this where we talked about try catch and we have seen how to handle the exception okay so we know that to handle the critical statement we have to use try block and inside that if something goes wrong it will execute the cash block otherwise it will simply execute the remaining statements okay that that makes sense right now what I will do is I will try to print the exception as well so that's why you can actually print the exception so we are saying something went wrong let me just remove one dot from here so that we can save some space and after this I want to print the exception as well I want to see the message what it says so to do that what I will do is I will compile this code and run you can see it says something went wrong okay that because we are printing that apart from it the message which you are printing here which is e it says arithmetic exception oh okay that means apart from exception we also have arithmetic exception cool that also makes sense so just try to remember this point that apart from exception there is one more class here which is called arithmetic exception we'll see that in some time now what I will do is I will try to create some more exceptions here one of fit is this if I create an array let's say the array is of type integer and let's send nums and I will say the size of this array is let's say five now in this case when you have a size five of course by default all the values are 0 of this nums and you can fetch the values let's say at this point I'm trying to print the value of nums of 0 or 1 let's say one of course it will be zero because the values are zero and I will also try to print the nums of five now see when you say nums are 5 what value you will get now first of all the size of this array is 5 and then if you are saying you're trying to fetch one that means you're fetching basically second element because the index number starts with zero so zero one two three four oh so for the five elements the index number ends at four and we are trying to get the value from five okay so basically we are going out of bound okay so let's let's see what happens if we do that uh first of all I just want to make sure that I don't have this exception 0 here or maybe let's let's keep it zero I will say compile and run of course we will get that error because uh the dividing number which is I is still zero let me just make it two see the problem is the moment you get exception here it will directly jump out of this try block it will not even execute the remaining price statements okay and that's why we have not seen those outputs now what I will do is I will just try to compile this once again and run this time if you can see we got a different exception yeah it says something went wrong that actually makes sense because it is executing the cash block but look at the exception the exception is add a index out of bounds exception which means the size of your array is limited and then you are trying to access the element outside it and it says that it says index 5 is out of Bound for the length five okay uh makes sense so basically apart from automatic exception we have other index outer bound exception as well but I have only one issue the issue is why we are printing something went wrong see you know you try to show the message to the user as well and if you can see the actual the problem is with the user what if the value of I is coming from the user what if the value of this 5 here numbers of 5 is also coming from the user now user is not sure what is the problem here it says something went wrong you have to be very specific here you have to say Hey you cannot divide the number by zero so user will know okay the values which I'm entering is actually wrong I should not be entering a denominator which is zero okay or maybe you can say you know you're trying to access the element outside the array the user will know that you know you have to be in your limit I want to print a customized message here so if I print here something like this if I say cannot divide by zero that looks like a good message okay let's not print this a e anymore let's only print cannot divide by zero and let's see what happens okay clear and I will keep the same error the other is still with the array not with the denominator but if you run this code you can see it says cannot divide by zero no that's weird because I'm trying to fetch the value which is outside the limit and it says cannot divide by zero that's wrong this is the error makes sense when the value of I is 0 right so if I compile this code and run now this works you can say it says cannot divide by zero but then if you if the value of I is not 0 in that case as well it is printing cannot divided by zero it's because the moment you get the the exception is in dry it can be from this statement or this statement it will execute the same cash block and in the same cash block we have the same message see what you can do is before printing you can actually check if you can check the type of this e you know is e arithmetic or is e either index Auto bound and then based on that you can print a different message that's one way or what you can do is you can ask your Java itself hey Java I want you to handle different exception so you can be very specific you instead of saying exception I can say it is arithmetic exception now I'm being very specific here I'm saying I'm not talking about exception I'm talking about arithmetic exception when arithmetic exception arises print this message but what if there is an array index out of bound exception let's see if we are able to handle that so if I compile this code now okay first of all let me just make this I as 0 and let's see what happens again I will compile and run you can say it says connector by zero that makes sense because we are able to handle the exception and you can see we are printing buy as well that means we are handling the exception but if you make this as two and if you try to compile this and run oh you can see that we are not printing by this exception you can see it's a there's a cross symbol as well it says other index autobound exception that means we are not handling it even if you mention that in the try block if you don't have a catch block for it that means you're not handling it how do we handle that so in that case what you can do is you can write multiple catch block so you can write catch here and in this sketch you can mention add a index out of bounds exception that's right we have to create one more catch Block in which you'll be having a different type of exception and here we can print stay in your limit again I'm just trying to be witty here but you can print a appropriate message whatever you want to uh now if you try to see what happens if I compile this code once again and one you can see it says stay in your limit there's no abnormal exit of the code it is executing everything and this time it is executing the cache block the second cache block not the first one but what if you make this zero then and again it goes in sequence okay so in try block it goes in sequence it will first execute this statement which may throw arithmic exception and if that works well it will go for this one this might do an exception which is at the index.bound exception so let's see if that works compile and run you can see it says cannot divider zero so you can basically have two different exceptions here now what if there is another type of exception here which we are not sure about you know there are a lot of exception classes it's not like we only have these two in fact in the next video we'll try to understand some of the famous classes or famous exception classes we'll try to work on that but at this point there are multiple exceptions and we are handling only two you know it's always a good idea to handle all the exception but we are not sure what exception it can arise so in that case you know to be on the safer side at the end you can handle something with exception now exception handles everything if you remember the concept of in heightens where you have a parent class child class so exception is a parent class it can handle all the exceptions okay now you can say something went wrong because even you are not sure what went wrong so you can say something went wrong now if there's some some exception which you are not handling in that case if you execute this part to give an example let's say I create a string here string Str and let's say the value of Str is null as of now and body fight will be null and then here what you're doing is you're trying to get the length of this string so what you can do is you can print string SDR dot length now if you try to find a length on a null string it will give you a different type of exception first of all I just want to remove this exception I don't want exception on line number 13 I just want it to work so that it will execute this line which will give you exception and let's see what happens if you say clear and compile and run it says something went wrong now what went wrong is because there's a new type of exception in fact here it's a good time to actually print e because it should print something some exception so that you'll understand I'll try to print this and you can see it says something went wrong because that's what we are doing here and then it prints the exception which is null pointer exception now this is one more exception like this we have a lot of exceptions and we'll understand that in the next video but then before that we have one more thing what should the sequence here the sequence is whenever you have a child and parent make sure that your parent is at the bottom so you can say exception is a top class right parent let me show you how it looks like so if I go to Ultimate exception now arithmetic exceptions are class which extends runtime exception so runtime exceptions are present class for arithmetic but if I click on runtime exception you can see it extends exception here so exception is a patent of parent and in fact this exception actually extends through able so there's one more class called through able which is like the topmost class and yeah so that's how you basically the hierarchy looks like in fact I will show that in the next video as well but make sure that you have at the end but if you try to keep this on top here it will create an issue because the exception can handle everything right at this point if you get the exception it will try to search for a catch which can handle this exception and it looks at Exception by saying hey exception can handle everything right then why we even need this too and that's why it says you know it says this exception cannot it's unreachable cancel that message there it is unreachable because everything is handled by this exception so it's a practice or it's compulsion to have the parent class at the end now as we have seen exception is a super class right and in that we have a lot of subclasses now let's try to understand the entire hierarchy in fact we'll not talk about all the classes let's try to give you the general idea and with this idea we'll understand okay what class belongs where and which exceptions are compensory to handle and which are not okay that's important point to remember so on top we have the object class now why it's on top because every class in Java extends object even if you don't mention it's there right so give let's give the respect to object class now below that we have one class called throwable now if you remember while searching for the class uh we have seen exception class extends variable so that's on top one more trivia here most of the names which ends with able are actually interfaces okay runnables realizable cloneable but coming to exception tribal is also able at the end but it's a class okay that's the only exception which I have seen till now now below this we have two and okay that sounds weird because we have only some exception right so we have exception here and apart from this we have one more which is called error okay now why we have not talked about error yet we are only talking about exception the thing is when we talked about runtime exception we have mentioned that it is something you can handle okay so exceptions are something which you can and you should handle others you can't okay when the others hit it will simply stop the execution normally it doesn't happen but if it happens you can't do anything with that example of this it can be the thread that what if your threat dies example of this can be i o error something goes wrong with the input output we have few more which is your virtual machine error it can be one of the example I can give you here is what if you have out of memory so what if your memory is not there so what how can you handle that okay so this type of Errors you can't actually handle so this is something it's there it will not happen but we cannot handle that even if it happens what you can handle is exception now in exception as well we have multiple options here so we have the first one which is runtime exception so we have the subclass of exception is one-time exception and on the other hand we have a lot of different exceptions here example of this could be a skill exception example of this could be IO exception and many more but we are just trying to understand the simple one so we have these two uh we have many more as I mentioned now in runtime exception also we have lot of different exceptions here we have seen most of it in fact we have seen a arithmetic exception uh then we have add a index out of bounds exception and then we have null Point exception now all this comes on the runtime exception this is an null Point exception now the thing is why you have to remember this thing not everything you have to remember but few of them see all these runtime exceptions are actually called unchecked exceptions and all the other here on on this side they are called checked exception now why they are unchecked and checked most of the time when you write the line your compiler will not force you to handle the exception okay it will say hey it's your choice to handle the exception so all the runtime exception all this all the classes here it's your choice if you want to handle them or not but whenever your compiler sees that there is a line which can raise a exception which is not runtime exception example it can raise uh SQL exception IO exception in that case it will ask you compulsory to handle the exception if you remember when we were trying to load the class with the help of class dot for name on the right side of the method we have mentioned it throws the io exception right so it was forcing you to handle it okay okay we have not talked about those yet but we'll talk about it so basically uh these are all checked and this are all unchecked here and there's something you should not be worrying about it will not happen most of the time and this is something you have to handle by yourself so I hope this diagram makes some sense this is the top class which is there in every Java code and this is the authorable which is a parent class of exception and error in exception we have lot of different exceptions if you get SQL exception in your code you have to handle it compulsory I have exception compulsion apart from this if you have a runtime exception it's your choice to enter it or not but as a good programmer if you want to keep your client happy please handle the exceptions now let's try to to understand a keyword called Throw okay so basically we have two keywords here one is throw second is throws we'll see them one by one so what I will do here is first of all I will just reduce the number of lines we have so that it will be easy for us to understand I will remove all the exception okay I will stick to one which is divided by zero and let's put only one statement here let me also remove this one because not needle we only want to exception arithmetic and if something goes wrong which we have not handling uh that will be handled by exception okay so let's say when you divide this number of course the value of I is 2 so everything is good and you know most of the time people will put curly brackets on the same line and it normally works and this basically preferred okay so let's say when you do this execution of course there's no exception here because we are dividing by two right so if I try to compile this code and run everything is smooth you you can see we are dividing the number 18 by 2 which is nine and that's what you got here because we are printing the value of J and then we are printing by everything is normal now the thing is what if I'm dividing this number by let's say 20. now in this case when you have a greater number of course you can divide 18 by 20. now the question here would be zero if you can see the value is 0 and there's no exception but what if you want to create or you want to generate an exception maybe you are doing something here let's do this thing let's say the value of I is zero so of course you will print cannot divide by zero now instead of printing cannot divide by zero what you can do is let's skip this part let's say let's not print any error let's handle the error if someone is trying to divide a number by zero I want to divide the number by 1 18 divided by one okay so what I'm doing is I'm not printing anything I'm just trying to handle it it will print some output so if I compile and run you can say it says 18 okay I don't want to print added or something if someone enters a denominator 0 I will say maybe I can also print here that's the default output so that if it prints 18 it will also print that's the default output if you can see if I do that so it will say that's a default output it will print 18 okay now what I want is every time this exception it should do this thing so it's not just about printing the message you can actually handle something example let's say if you're trying to connect to an internet and then the network is not there of course it will throw the exception so instead of sending a message to the user we were not able to connect to the server you can simply retry right so what you're doing is you're trying to handle the exception here we just tried of course this is not a good logic I'm just trying to make you understand how we can do it one more example let's say you have two databases one is a primary database second cell backup database you are trying to connect to a primary database and something goes wrong of course you will not simply tell user hey you know we are not able to connect to the primary database you will simply try to connect with the backup database and that's what you can do in the catch block that's what we are doing here with this logic now let's say you are dividing a number by 20. now in this case it will print zero okay but what if you don't want to print 0 you want to print the default output is 18. maybe you won't do that that means what I'm saying is even if the catch block is not getting called I want to call the catch block because in the catch block you are basically trying to handle the exception and if you don't have the exception it will not call the cash block that is not something we want so to solve that problem what you can do is you can basically check if the value of J is 0 okay because we don't want zero in this case if the value of J is 0 you can basically call The Catch block but how do you call the catch block catch is not a simple function right where you can write cats by passing a message node it's not a normal function or normal method what you have to do is you have to basically use a keyword called Throw and you have to create an object of arithmetic exception so you can do that you can simply say new arithmetic exception it's a class right you can create the object it's that simple now what you're doing is you're basically trying to create an error here you're trying to throw the error so this is the exception you got you got the object and you are throwing now since you are throwing catch will catch it okay let's see if this works compile no issue Run Okay so that it says that's the default output it is printing 18. so you got zero here but then we are checking if it is 0 then throw the exception okay uh maybe you want to connect to another database which you have written the code in the catch block so you can do that here okay you can literally call The Catch block with the help of throw keyword there's one more thing what if you also want to print the message somewhere here you want to print e Now by default if you try to compile this code and run yes it will say a rhythmic exception but you can see it is not printing the message I want the message as well remember it was printing slash by zero or divided by zero I want to print such messages so you can actually pass a message in the Constructor you can say I don't want to print zero okay something like that whatever your message you want to send you can do that in the Constructor so you can say it says this is your message I don't want to print zero so if you want to send a message put that in a Constructor it will do it for you don't worry you don't have to manually go and type the message so whatever message you have every time you create this object you can pass the message and that's how we can use a through keyword so so just reiterate row keyword is used to throw the exception okay and then catch block will be catching it now basically we were able to throw the throw that which is arithmetic now to the exception and what if you want to throw your own exception let's say I don't want to throw ultimately exception I want to throw my own exception can we do that can I say my exception you can see uh straightforward it says my exception cannot be resolved that's why we don't have any class called my exception but I want my own exception here or maybe I can say issue of my exception I can say Naveen exception you know what Naveen actually means new okay so it's a it's a term which means new okay anyway so I want to have a new exception here the way you can do that is by creating a class which is called Nomine exception and done oh okay we have to also create a Constructor because we are sending a message so what I will do is I will create public now in exception which accepts a string Str okay string string that works for me and still is not working it says no exception of type novel exception oh I have created a class right the class is there what's the problem I have created my own exception and it's not accepting it see the thing is you can't simply create a class and call it as exception see exception needs special features right and we all know if you want to get something you have two choice you can actually build something or you can just get it from your parents so I can simply say extends exception so what we are doing is we are getting all the features from the exception okay so you can say now in exception extern exception and now it is not crying okay now the only thing is if you try to compile this code okay not this one compile first and run it says something went wrong oh that's weird why something went wrong I want actually to print oh okay the thing is you are doing an exception but you are not catching of an exception I want to print this one that's a default output so what you can do is in shop knowing automatic exception you can say nothing exception here and you can accept the object and then you can print this message that's the default output and also you want to print e to get the message the message is this I want to print something now if I try to compile this code again and run oh it works can you see that it says that's the default output and the error is Naveen exception okay that's good where is this message I don't want to plane zero the thing is the message which you have sent actually is received here but this output when you're trying to print a here is actually handled by the exception class okay this should know that you are accepting a string so what you can do is if you go to exception even exception has a Constructor which takes a string and we all know how to call a superclass Constructor we have seen that right so I can simply say super and pass this message so whatever message you are accepting here just pass the message to the exception and they will take care of it you don't trust me let me show you you can see that it says Naveen exception and it prints the message as well the message is I don't want to print zero so that's how it works you can create your own exception so just reiterate now in exception is not a part of java I'm creating my own exception and yes you can do that the only thing is you have to create a class with the exception and this class need to extend exception or runtime exception your choice let's try with one-time exception and if I clear this compile run it works so you can extend exception or runtime exception and the only thing you have to do is if you're accepting a message you have to pass the message to the runtime exception Constructor and that's how it works now in this video let's talk about throws keyword so we have talked about throw keyword now it's time for throws now this is not a plural form of throw it's not like if you want to throw multiple exceptions you will be using close a lot of people have this confusion those has a different working so before you understand those what I will do is let's talk about a scenario let's say you are writing a method so let's say the method name is D now of course in this method you will write certain statements right and it is possible that all the statements there is one statement here which is critical so let's say this statement is critical and we know whenever you have a critical statement it is always better to write try catch so let's say we have a try here for this and we have a cache block as well so just try to imagine this in a coding perspective say we have a statement which is critical and then you try to handle that with help of try catch and we're doing that in a method D okay and let's say we have one more method here which is called E and in this method as well you are writing some statement out of which there is one statement here let's say the first statement itself which you need to put that in try catch okay because this statement is critical okay so if You observe we have two methods here which is d and e both have a statement which is critical now of course it makes sense to write try catch in each of the method or we have one more Choice here what you can do is let's say we have a method which is C and C method is calling d and e both okay so C method is calling the method and E method of course in C let's say we have some more statements let's say we have method C and then this you have multiple statement out of which the two statements are calling D and E of course you can call methods right and then we have some more statements here now if you try to observe the situation what we are doing is we are trying to handle try cats by ourselves right in the D and E but what if you could say hey you know what d and e both have the same exception so both have a critical statement they will rise the same exception so instead of handling those exception inside try catch in each of the method can we just handle the try catch and see itself so what you can do is you can put try here and catch so when you are basically calling those methods D and E you can put them in try catch because both of the methods has a critical statement in fact the entire method becomes a critical methods right because when you're calling it so instead of handling those things here let's not handle uh using try catch here yes we have a critical statement but will not handle the exception in D and E you can handle the exception in C okay but then even if you work with this it will give you an issue it will say okay there's a problem with d and e but still it will stop the execution so one thing you can do is you can write here throws so you can say throws exception and same goes here you can say throws exception okay so by doing this what you're doing is you're saying hey whatever problem arises in D and E they will not handle it they will just send the exception to the method who is calling it sometimes just call ducking the exception because we are ducking it we are not handling it the method which is we are into let's say d and e says it's not my responsibility so the method which is calling d and e which is C in this case will be handling those exceptions okay and in C of course you have to have try catch if you don't have a try catch in see it will again create a problem for you because it will stop the execution now we have one more thing what if you are not doing try catch here so of course some method will be calling C right so let's say B is calling C method okay now in this case what you can do is you can put try catch here and let's say uh if you don't want to do try catch here we have a main method now let's say main is calling B method here even main can have a try catch so basically you can go up the ladder and you can write try catch in whatever method you want to write so if you write try catch in main then B says okay even I will throw the exception c will say even I will show the exception I will not handle example by myself c will say I want B to handle it B will say I want main to handle it and of course your D and E both says c will handle it okay now that is called ducking the exception for a particular method but it is done with the help of those keyword now this makes sense for checked exceptions okay see for uncheck exceptions it's not compulsion for you to handle the exception but when you you have a checked exception example if you talk about IO exception okay now in terms of IO exception which happens when you use input output let's say you are reading something from the file or you're writing something something to the file or when you're doing SQL which is database connectivity at that point as well when you're finding a query it might raise an exception in that case you will be getting a checked exception and we know we have talked about this in checked exception if you write that statement it reads compulsory example remember we have worked with one of the thing which is called class dot for name and in which you can mention the class name let's say my class name here is demo itself so basically what you're doing is you're writing this statement and this statement says it might throw or it will throw a class not found exception now class not found exception is also comes into hierarchy where this is a checked exception so it is asking you to compulsarily handle this exception so you can put that in a try catch so you can put that here and you can say catch now which exception we have to handle uh the name of the exception is class not found exception and you can print not able to find the class in fact let's say let's write a class called calcare and you know we don't have a calc class nowhere okay now if you try to run this code compile and run you can see it will say not able to find the class now which class we're talking about class is not there in fact I can also print the error message here with this so compile and run you can see it says class not one exception which class calc it's not there but if you do it for demo all there is no issue because we do have a demo class here right so if I compile this and run you can see it works it says the default output blah blah blah but there's no exception there's no problem with this okay so in fact we can also put this we can just cut this part and let's say we are doing that in a separate method so let's say we have a class A and in this class A we have a method which is public void show and in this show we are doing this we are trying to load the class demo here and of course we have to call this now what I will do is I will just try to remove everything else so that we can make our code simple and remove this custom exception as well and you can see we have a very simple code now and if you want to work with so of course you have to get object of a you will say a obj and then you will say obj dot show okay and now if you try to work with this when you say compile and run you can see there is no problem because anyway we are not printing anything we are just trying to load the class of course if you write a static block here that will surely work let's try to do that as well because we have seen this let's create a static block and I will print last loaded in fact another the weird thing is anyway we are writing main method right so the class will be loaded anyway but still we're just trying to see if we're getting some output again the class voltage is not happening because of the load it's only because the main method is there in the demo so anyway it will be called but the idea is we are trying to print something but what if I try to work with a class called calc now in this case if you try to work with calc it will give you an exception you can see it says not able to find the class which is scalp okay cool and now what if I don't write try catch so what I'm doing is I'm saying hey I will not handle the exception so in this case you can simply go back to shows and say throws class not found exception and you can say there's no problem now because this state treatment knows that show is basically throwing the exception so show says I'm not responsible to handle the exception you know when you work for the company there are some employees they don't take responsibility of what went wrong they will simply say hey talk to my manager right so the same thing is happening here it says hey I'm not responsible to handle the exception talk to my manager which is mean because many is calling show so it is made responsibility to handle the exception and that's why you can see we got an error here it says unhandled exception okay so now you have two Choice what you can do is either you can write try catch here or you can say those last not found exception okay so you have this choice this is not recommended okay never do that with main because if you see when you're throwing the exception for main the problem is who is calling main it's jvm now you are asking jvm hey jvm you have to handle this not a good idea because JV will say okay if you throw it to me I will simply stop the execution so it's not a good idea to throw exception for main but yeah for other classes let's say for sure because we know who is calling show it's your main that domain will be handling it so don't write those here okay you can just use try catch in fact you can ask your IDE you can say Quick Fix uh surround with try catch and done you don't have to do anything here and this might be new print a stack Trace basically what it does is it prints the entire stack which method is calling which method and you can actually track the entire method calls so that you know because of which method the problem has been arised okay so if I compile this code and run you can see this is the entire stack so basically your main is calling show and behind the scene we are calling class dot fortnite which is behind the scene is calling this and that so there's a hierarchy of method which is getting called that's what the print stack Trace does so it's helpful to debug the application but it's also good if you want to print a message okay so yeah that's about ducking the exceptions and using this Rose keyword in fact we are going to use this a lot in the upcoming sessions so you will get used to it now it's a good time to talk about how to take input from the user I know you have been waiting for this one a long time because we have talked about different concepts and maybe you had this question in your mind okay everything looks good we talked about objects classes and heightens polymorphism but how do I take the input from the user because in between as well I've mentioned that what if this value comes from the user and at the same point you might be thinking how Okay so let's try to answer that in this video so basically if you want to print something on the console so this is a console right on the right hand side whatever you have here it's a console and if you want to print something on the console what you do is you say system dot out dot println right now you have to understand this first see print Ln is a method now if I ask you just to think about it print Ln method belongs to which class or it's a method of which class now this is the interview question a lot of people say that it is a class it's it's a method of system class but that's not the case see if I press my control button on windows and command on Mac and if I click on this if I hover on this print Ln if I click on it you can see it gives you definition it says print Ln and print Ln method belongs to print Stream So plainstream is a class and println is a method of print stream okay that means if you want to call Print Ln you first have to create object of a print stream but okay don't worry you don't have to create the object the beauty is the object is actually already created can you see that it we already have a out object and that's why we are using this object for a long time so out is the object of print stream but this object is created as a static variable inside the system class okay it's a it's inside system class now since it is static so we can use out with the help of system dot out and once you have the access to the out object you can call println and that's what we have here system.out.println so if I want to print something if I want to print let's say hello of course I can do that and I can just compile this code and run it perfectly works now let's say I want to take the entry from the user I have to say hey enter a number now in this case when a user enters the number how will I accept it that that's tricky right so what do you think what could be a solution here what is the method we have to use now by any chance if you're coming from CC plus plus we use C out C in in C plus plus we use printf scanf in C right and it's straightforward right there are straightforward methods but in terms of java when you say system.out.printl and it prints right it is out and that's why it says out it is outputting so to input there should be in right let's see do we have in so if I go to system and if I scroll down to out yes there is out here and need to find in this is also in if you can see in is a static method and this object in is of type input stream oh we do have it okay so if I go back here and if I try to use system dot in dot oh there should be a method right because to print we have a print method for the input there should be read oh there's read okay great it works you can see we have a read method and read method returns uh into value right it Returns the end value that's great the only thing is it might throw an exception and if you can see which exception is IO exception and we know what is IO exception now so you have a choice you can handle the exception and this is checked exception which means you have to handle it it's your job to handle it compiler will force you to do that so you can either use try catch here or you can be evil and you can say throws exceptions not a good idea but it works for this code since we are learning so we can do that but when you are making an application don't do that try to use try catch here okay so system.in.read will give you the integer value that's great what I will do is I will just put that in a variable called num because it returns a value right read will give you the value so you have to put that in a variable and once you have the variable you can perform any operation if you want or you can print it that's your choice let's go back here and compile no problem if you try to read oh it says entire number I'm excited let's enter 5 and enter oh we got five but we also got three now that's weird I don't want three there okay let's try another number let's see with three oh we got fifty one what is happening here the thing is this system dot in dot read actually gives you the ASCII value for the number you enter example let's say if I run this code once again and if I enter capital A it prints 65. so every character on your keyboard has a ASCII value the same way for a Capital Area 65 for small age is 97 I guess for 0 which is 48 example if I enter the value of 0 it is 48 for Value 1 it is 49 so this goes on right so basically it is not giving you the exact value it is giving you the asciate representation of that value okay that is tricky actually so what one solution you have is what if you subtract the number by 48 or maybe it's not not 48 your subtract the number by 48 not by 48 minus num it is none minus 48 and now if you try to earn this call you have to first compile this code compile and run let's enter number five oh it works now can you see that because now we don't want to ASCII value we want whatever ask about you are getting by subtracting it right so if I enter six okay if I enter 6 it works okay now it works so we were able to manage it right this is not an ideal way of taking the input but that's what Java provides you Java says we will give you this method read which will give you ASCII value what if you want to get a bigger number what if you want to get example if I give a bigger number here let's say 55 you can see it only gives you 5 because it reads only one character at a time oh that means if you want to read multiple characters we have to use a loop but let's not do that let's not take headache off by ourselves so Java says don't worry I will give you a specialized class to work with this so instead of using system.in dot read we have a special option which is there's a concept called buffered reader now buffer reader is basically a class which works with IO okay and it belongs to a package java.io so if you want to use buffer data you need to import this package so IO exception buffer data board belongs to IO package now whatever class we have used to allow most of them belong to java.lang which is Auto Import but if you want to use any class which is not there you have to explicitly mention the package name okay so let's create object or buffer data so we'll say buffer data BF is equal to new buffered reader okay but what next the thing is if you want to read we have to actually pass the in object can you see that if I go to Constructor buffer leader buffer data Constructor says okay I will work but first you have to pass an object of a reader okay now which date is talking about so basically there is one more class which you have to work with I know this code will be bit lengthy because that's how Java is implemented it but let's use it in fact there is a better way as well which we'll see in the next topic so we have to pass the input stream reader so we have to get object of input stream reader let's say in in this case and I will say new input stream reader okay and whatever object you are creating here just pass it here your job is done but there's a problem you can see it input stream reader also needs an object of input stream okay in this case what you can do is it needs object of input stream right and if you remember when we talked about in object it is input stream object so what you can do here is you can pass system dot in I know it is bit complex and that's why in later version of java they have introduced better solution but I just want to show you this option as well which was there and people were using it for a long time okay so you have to say input stream reader in and then we have to get the object of input stream reader then you can create object of buffer data by passing the object of input stream now once you have it you can create a num variable and you can use buffered reader to read a line so you can see we have a read line the only thing is read line will give you the string okay we don't want string we want integer so to do that we have to use a special class called integer dot pass int which we have seen already and integer.pass int not in okay so whatever string you're getting basically you will get a number in the string format you just have to convert that into integer and now if I try to print number it should work otherwise we have wasted so much of time compile and run let's enter 55 it works doesn't matter how big your number is it will accept it you can say we got the number so basically this is how you take the input from the user there's another way which I will show you in some time but the important thing is whenever you use buffer leader it is trying to access input stream reader and the beauty is buffer data can take the input from anywhere not just from system keyboard it can also take input from the file from the network and you can mention wherever you want to take the input from you can mention it here you can mention the file you can mention all those other stuff now the thing is buffer reader whenever you're using it it's actually a resource now why it's a resource is because let's say when you are trying to read from a file of course file is a resource right and when you open the file it is your responsibility to close it when you open the network it is your responsibility to close it when you open the database connection it is your responsibility to close it how will you close it so once your job is done at the end you can just come back here and say BF Dot close consider we have a closed method so it is all always a good idea to close the resources again it's not compulsory it will not give you error but a good practice is close the resources okay otherwise you are making resources you are licking the file you're leaking data you are keeping the resource busy and the thing is if you are using a resource no one else can use it okay so don't do that don't be a evil person here so you close the resource okay so this is how you take the input and I know you are waiting for the better way of doing this let me show you a simple way so instead of using buffer data we can use one more technique here which is a scanner now scanner was introduced later in Java so if you can see if I go to scanner class scanner class was introduced in 1.5 so people before 1.5 were using buffer data I know bad days but yeah we got scanner here we can create object of scanner let's say SC we can use any object name doesn't matter and we can say scanner now scanner takes the system dot in that's constant because we have to mention from where we are getting the input are you getting the input from the Soul are you getting the input from the file you have to mention that here and once you get this scanner object the beauty is in scanner you can say SC Dot and there are so many methods you can see that we got next line which will give you string you can say next int oh that means it automatically gives you integer value you can also say next chart uh it will it has so many methods but we are going to use next int not next line because if we use next line you have to pass that into integer so let's use next tint which will not create a lot of headache for us and run enter a number 567 and we got it so I know scanner looks much better than buffer leader but yeah it was introduced later so people before that were using buffer data so we have seen it in fact in the next video we'll try to understand more about how do you close the sources okay and that's why we are teaching this concept now because now we know what is IO exception and all those stuff so yeah you have two choice of taking the input from the user you can use buffer Twitter or you can use a scanner class okay now in this video let's try to implement try with finally our private resources basically so what I will do is I will just remove everything else of course we'll do this once again we'll try to use buffer data because we love it right okay we are not doing an exception as of now uh we are not running any code for that let me remove that as well and here what I'm going to do is let's try to understand try now if we talk about try of course in this try you can do whatever you want normally we put the critical statements right and then if there's a critical statement which is throwing an exception what you execute is a catch block right and we know how to write a cash block the thing is is it compulsory to have a try with catch there's no compulsion you can write you can write try but you can see if I don't mention catch it gives you an error the thing is if you have a try and if you don't want to mention the catch you can basically write finally here so yeah so try can be used with finally if you don't want to write catch if you don't have a scenario where you want to handle the exception you can write finally but then what's the use of try when you're not catching it of course right try is responsible to handle the exception and when it it will simply close the exception which will catch will catch it and then it will solve it or it will print something finally what finally is doing see the thing is even if you have a catch block here let's let's work with catch block and in this cache block I will say exception e and then let me create a scenario which is int I is equal to zero I know you'll be thinking I'm just using the same example just for Simplicity I just want to keep this thing simple and here I want to say J is equal to 18 divided by I I don't know why I'm using 18 every time it's just because we have used it before now we know this will throw an exception so it is our job to print something here so I will say get lost oh so it will say something went wrong ignore that get lost so something went wrong right okay so if you get the exception it will call the catch block right now sometimes you what happens you know you want to print something you want to do something which should be executed irrespective if you get the exception or not example let's say if I want to print buy here in the try block the thing is if you compile this code and if you run this code you can see it says something went wrong it is not printing by you know why it's because in the try block the moment you get the exception here it will call the catch block and that's what it's not printing a buy it will actually jump out of the try block right so one of the solution you can have is you can just write this in Cache block as well okay so that irrespective if you get the exception or not it will print by so you can say it is printing Buy in the catch block but if you don't have the exception it will print by which is coming from the try block okay so at least we are printing buy now of course in the real project you will not be printing by the in real project you will do something there are some set of statements which you want to do is respect you if you get the exception or not now instead of doing those things here one of the amazing thing we got is finally so what you can do is you can write your statement inside finally now finally has this amazing feature which is irrespect you if you got the exception or not it will execute the final block so in case of exception as well it will execute the finally block let me show you that to you compile and run it says buy no problem even if I get the exception compile and run it says buy so finally block now this block will be executed irrespective of the exception okay and the beauty is you can actually skip the cash block if you just want to use try and finally but why you will do that why in this world you will write try with finally you're not handling the exception right see finally makes sense whenever you want to close the resource that's right whenever you want to close the resource that's where you will be using finally okay what I'm talking about is let's say we are not doing this we are using a variable called num and the value for Num initially will be zero and what I want is is I want to take the input from the user and we know how to do that right so basically we have to get object of buffer data of course we can use scanner here but let's use buffer data so buffer reader b r equal to new buffered reader and we know if you want to work with buffer data first you have to get object of input stream reader we'll say in is equal to new input stream reader which will take the object of system Dot in right and here you can basically pass this in which you have created here okay and using this buffer data basically we can say okay we can num is equal to b r dot read line and then we have to pass it right we have to say integer dot pass int okay now it says it might show the error right but then you will say hey we are handling right no actually we are not handling we don't have a catch block here right so basically we are not handling the exception and that's what it says so what we have to do is we have to either handle the exception or we can Quick Fix and throws okay this time it is throwing two exceptions number format exception and IO exception because we are passing it okay that's fine with us okay and let's say I'm printing the num value as well now after doing all those things we know one thing right whenever you open the resource example is a resource right it's always better to close the resource now my question to you is where should I close the resource should I close the resource in try block if I am handling the exceptional do that in the catch block of course we have tried two statements right instead of that the finally block is normally used to close resource so whenever you want to close the connection whenever you want to close the resource always use finary block it is meant for closing the resources okay the only thing is can you see that we are creating the reference inside the try block the scope is getting over itself here so what I can do is I can declare it outside and I can use it inside so when I declare outside even the final block can use it and I can make it null initially and now you can see there's no problem so basically we are closing the resource in the final block so Point remember here is the finally block is used to close the resource it can be for file for the input for for the connections network connection database connection everything finally will be used for closing it makes sense right okay before I go ahead I just want to do one more thing don't you think we are passing in here so can I instead of passing in can I pass the entire state see ultimately we need object right an object is created by this line here so actually we can do this so we don't need this line so most of the time when you read somewhere it will show this type of statements and that's completely fine it works okay now one more thing so try with finally works you can close it here but you know we can actually optimize this mode in the data version of java they have introduced A New Concept which is try with resources where you don't even have to mention finally block that's good right but how will you do that so what this says in the trial itself create a round bracket and then you create your declaration whatever object you are creating create them inside your try block I mean inside the brackets and you have to declare that as well but for reader so you will not mention that here now so what I'm doing is I'm creating the object in the the dry itself now watch The Adventure of this the advantage is once the try gets over this resource will be closed automatically it's because if you go to buffer data buffered reader extends reader and if I go to reader you can see it says closable and if I go to closable it is auto closable so whenever you have a class or the interface which is auto closable it means that will be closed automatically with the help of try you don't have to do the closing manually so this is try with resources so you have this syntax option you can use Drive finally that depends upon how you want to implement this I prefer to use try with finally because I know which resources I can I'm closing and sometimes you have multiple object created so it's good to use finally but if you think you can make a shortcut this also works so this here is called try with resources in this video we'll talk about threads now what are threads now think about this when you run the application of course the software which you have written it will be running on an OS right and below the OS you have a layer which is called Hardware so if you want to represent this what we normally have is we have a software which you built of course so let's say this is your software and this software basically runs on and Os of course on this OS you'll be having multiple softwares right and underlying that is the hardware right so this is where you have the physical set example in this uh you will be having your CPU you'll be having your RAM this is what we use to run applications right now if we talk about ram ram acts like a temporary memory for your processing and your CPU is basically something which executes something okay so let's say in in your software if even if you want to add two numbers let's keep it very simple let's say if you want to add two numbers by saying two plus two now that operation will go to OS and Os will say okay you want to add two numbers let me just send the request to the CPU of course CPU will do the processing right now CPU says okay that's a task I'll just have to add two numbers it will do some processing and then you got the output the thing is for a simple operation it's so simple right but then what if you are running a big software let's say uh this is your software and apart from this you have multiple softwares which you want to run on the same OS now of course in your in your system you will be having multiple uh softwares right and then sometimes you want to run that at the same time so that means OS should support multiple software working at the same time so can we say that that's multitasking right so we have multiple tasks running at the same time okay now most of the modern os does support that the thing is it's not like they are executing this at the same time so this on the CPU there's a concept of time sharing example let's say this software one they software to this software three uh let me just remove this part for time media so let's say if you have three softwares here which is which you want to run on the OS uh they will be going in time sharing so your Osa OS can say hey uh the first software you execute it will say Okay second software now you execute and then they will be executing badly by showing the time okay that's one thing so that's why we can achieve multiple tasking and that's why you know at the same time you can browse a web page you can listen to a music you can type a code so you can do all those things at the same time because you have a concept of multitasking but if you talk about one single task okay what if you want to break this break down this task into multiple things it is possible that in the same software you have multiple process or multiple things to run at the same time let me give an example here let's say you are writing something on the editor so we are using vs code right now the moment you write something on vs code let's say if I say A B C uh you can see the moment I type it it is showing me that it is showing the text on the software it is also showing us the error right so that means these two things are happening at the same time maybe uh we can also enable some sound effects the moment I type A to create a sound effect so we are doing multiple thing at the same time in the same software so that means it's not just about multitasking it's also about dividing your task into small things the smallest unit how can we do that and that's where we have a concept of threads so in the same task in the same program you can have multiple threads running at the same time okay so example when you play game you know any game which you play Maybe Mario or Cricket or football I'm talking about video games okay now at that point what happens you know if you talk about Football let's say if you have multiple players running at the same time the ball is moving at the same time uh the audience is cheating for at the same time so you have multiple things happening at the same time so that is possible with the help of multiple threads in fact if you see your activity manager on Mac or if you go to task match on Windows you can see whatever task you have example I have a Discord helper here uh we got login window we got code helper so all this software is another thing right stream direct Zoom OS zoom zoom software everything is running now all those are tasks and individual tasks if you can see we have threads there so the kernel is consuming highest number of threads 524 then we got Java because we are running Java behind the scene so Java is using 20 or 20 42 threads right so in a task as well you have the smallest unit the beauty is they can run parallely and it they are lightweight basically they don't they they also share the resources it's not like if you have 42 threads they will consume 42 different resources now they multiple threads can share resources and that's why threads are important to understand so in the upcoming videos we'll focus more on threads so I hope you got the just what is threads and uh yeah just to redefine that's a smallest unit which you can work with and of course you can have one multiple choice at the same time now once we know why we need threads let's try to create multiple threads here and of course to create multiple threads it's not like just because we have a concept of threads we are going to use in every application what if I say most of the time you don't even create threads by yourself see when you build applications we use certain Frameworks right now behind the scene this Frameworks will create threads for you so if you work on a big project there's a high chance that you will not even write threads by yourself but then it's always better to get Hands-On on some underlying Concepts right so let's do it here so what I will do is let's say let's go for a imaginary concept here let's say I have a class hi whose job is to print high okay or maybe I will just say a Class A whose job is to print high so what I will do is I will try to create a function here public void show and in this show I want to print hi nothing fancy just hi here the only thing is I want to print this High multiple times so I will simply use a loop here I can say int I equal to 1 and I less than equal to 10 and I plus plus okay and then we are putting this inside a loop so of course when I call this particular function which is show of course it will execute height 10 times right now let's say I I just copy this code and I paste it here now what I'm going to do is we have two classes with this with the same concept almost because they both have the same method name which is show they are both are running a loop for 10 times the only thing is the second Loop actually prints hello the first function here prints hi the second one prints hello that's the only difference and of course the class name is different and now I will go back to my main method and here I want to create object of these two classes so we got a class we got B Class let's create object for them so I will say a obj1 is equal to new a so we got the object for a and then I can say a object 2 is equal to new B and we got the object for B okay simple stuff and now I want to call those methods of course I can simply say obj.1.0 and obituary what I'm doing is I got object of both the classes and I'm calling the show method and I'm thinking it should work and why not it will white will not work we have done so many Java codes to allow it everything was working let's see what happens so I will compile this code and you can see there's no problem there's no compile time issue and if I run this code oh okay uh we got the output right so we got all highs here and we got all hellos right now that's how your system basically works you know your execution will start from main main will say okay uh I mean when your jvm is executing the Java code it will start the execution from Main and from when it says okay this is the first line which I want to execute the first line is simply create the object now behind the scene in the Heap memory you will get this object okay now the second line says okay create the B object it will say Okay B object done and since everything goes in sequence it will complete this task this two tasks then it will go for the next statement which is calling the show method of a class right so it will say okay and now I have to actually move so if you try to imagine a pointer here not the pointer in different languages I'm saying just saying the which statement is getting executed okay where is your executor here so the executor says okay my job is to execute this show method right so the executor will directly jump to this method which is show okay now it will start executing this method but when you are executing this method there is a hold on demo right I mean the main method is not continuing now so main will continue for the next statement only when the previous statement is done executing so once you complete this show method execution then only it will go for the next show right and that's what it will do so it will execute this show here it will run this Loop for 10 times and it will print high 10 times now once this show method is done with his work it will go back to the main method and it will continue for the next method calling so that everything basically goes in a sequence right now what I want to achieve here is I want to say Hey you know when obj one dot show is getting executed at the same time parallely I want to execute this show as well okay so if you want to execute two things at the same time or two behave at the same time since methods are behaviors we can use something called threads okay so we can say Okay I want to execute this two objects but then they are not normal objects you can't execute normal objects in multiple threads or you can't execute normal object execution simultaneously so you have to use multiple threads here so how do you make your normal objects as threads now that's tricky right it's actually very simple if you want to make these classes or these objects as threads just extend with a class called thread it's that simple the moment you extend with threads or with a third class now this class is not a ordinary class this is a thread okay the same thing you can do for B so you can say thread and exit exchange thread okay and then you can come back here and our job is done right now we don't have normal classes we have threads oh is it so simple okay let's see compile and run oh it's not that simple you can see they are still running in sequence they are not running in parallel what I want to do is I want these two methods to run parallel so while it is printing High it should also print hello on the side by side okay it's not it's not working so what I am imagining with my code is what should happen is see uh there is a main method right so main will continue its work here so main will keep executing everything now there will be a point where main wants to execute the show method but with two different threads so I want a show method here and I want a show method here of course this show method the first one is of a the second one is of B but then your our main When Calls show it should be with the new thread so I want this to be a new thread I want this to be a new thread okay now just by mentioning that this class is thread it will not get new thread for you basically we want two threads now the way you do that is by not calling show what you have to do is you have to call a method called start because when you are thinking that at this point I want to start a new thread you know we want to start a new thread here we want to start a new thread here we can do that with the help of a start method so we have to call this method and that's what we are doing here so you have to say start for both the threads okay see we are saying that those are the obj102 but there are threads now now the only thing is when you call start it should also say start right is so now think is behind the scene when you go to start so you can see if I go to start start is a method belongs to a thread class so of course you can do your work here but the thing is it will create new thread for you but it will call not the start method even if you call it it calls a run method so this thing you have to remember in every thread you need to have a run method that's how it works so when you say start here it's it's almost like you know when you are when you want when you want to participate in a race right so we have multiple we have multiple Lanes there right so we have multiple Lanes multiple people are waiting to start the game here and of course you took a position to run but then you will not run you will wait for it right what you will wait for you will wait for some volunteer here to say start right the moment they they shoot a gun or they say start you go right in the same way the moment you say start here it will execute this one method okay that's the point you have to remember okay let's see if this works now uh we have done everything what we could have done to make it work and let's say it works oh okay still not working now what is happening here see the thing is what if I say they are actually running parallel the only thing is when we are saying that see we are starting with this one right obj1 and then we are assuming that obj1 will execute one high and parallely the second thread which is the B thread will execute hello at the same time but the moment you give some time because the machine is so fast now the moment I give I have given some time to the eighth where to execute it is executing the entire thing at the same time okay let me show you what I'm saying what if I do this for 100. okay and let's see what changes so if I compile this code and run if you can see if I scroll up things are happening in parallel okay so you can see we got few highs here and then we got few hellos and then we got few highs we got few hellos okay things are running in parallel so these two methods are actually running at the same time yeah so what happens is behind the scene there's a concept of scheduler okay so in your OS we have a concept of scheduler now scheduler will say okay if you want to execute something you have to come to me first now it doesn't matter how many threads have you have in your system of course in my machine I have shown you that in the last video If you go to activity monitor you can see we have so many threads here we have five 24 threads running and only for one task in total we I got 2382 threads now of course you can't execute all at the same time right now the thing is nowadays we have multi-core CPUs so you have multiple cores there right uh and the beauty is you can execute multiple thread at the same time so you can execute let's say if I have four cores I can execute fourth at the same time if I have eight core CPU which is OCTA code I can execute eight threads but we have 2 380 94 threads now how will execute all so of course you can't execute all at the same time right so they go for the time sharing so scheduler in between so let's say this is your OS here okay so there's a concept of scheduler which is responsible to basically allow or thread to execute let's say we have multiple threads waiting to execute okay so let's say we have eight threads here and then we only have two cores available so on your CPU basically you have only two cores right so security knows you can actually run only two threads at the same time so what it does is it basically allows let's say uh This Thread and this thread to execute and then after they they execute they will allow This Thread and this thread to execute right so it is schedule a job to allow which thread to go to execute on the OS okay uh and that's why so when when your scheduler allows some time for your threads to execute your thread might be executing this number of iteration at the same time okay and that's what's what is happening here so basically we are running multiple pledge at the same time so yeah that's how you create multiple threads with the help of the authority class okay now let's try to execute this thing simultaneously so what I want is I want to of course they are running at the same time but what I want here is I want to execute High and Hello one after the other so example if you can see the output here we are printing so many highs and then we are going for the hellos right how do we do that see the thing is you cannot control your scheduler you can only suggest your scheduler to give the priority so yeah we have a concept of threads priority as well example we have two threads right we have obj one object two and we have a concept of threads priority so let's say let's I just want to understand what is the current priority of the thread so I can say obj one dot this is a method called get priority which will give you the current priority of a thread okay let's try to print this let's see let's see what happens compile there is an issue okay we forgot to put one loan bracket and compile this code and run you can see it is printing all this thing and we got thread variety where it's here so you can see we got five okay so since we are uh starting the threads after printing the variety you can see that's why the we got the value first but we got the priority as five now what is this by ID what's the range basically so the range of the priority goes from 1 to 10. so one is the least priority 10 is the highest priority or maximum priority and five is default priority which is normal okay so by default every thread will be having a normal priority so what you can do is you can change the priority of a thread uh maybe you want the second thread to have a higher priority so you can say obj dot set priority and you can mention the value so of course you can mention one which is the lowest or you can mention 10 which is the highest or you can use some constants so thread class has some constants which we can use example we have Max priority we got Min priority we got numpy ID right so if you want to specify maximum priority you can do that with the help of this constant otherwise you can also say let's say if you want to set the priority nine so you can say Max by T minus 1 that will be 9 right but then by doing this it's not like you will get all hello first and then it will be high so even if you see we are Printing High and then it is printing hello and it's still hi hello hi hello it's it's working in uh simultaneously the only thing is when you are specifying that this the priority for this thread is the maximum you are basically suggesting to your scheduler hey you know this thread should get a highest priority it's not like scheduler will give you that variety scheduler is see different schedule have different algorithms to work with and if it feels okay now we have multiple threads and they are reaching to a point at the same time scheduler can say okay now I will say the priority or maybe a scheduler will also see who will take the lowest time so if there's a threat which will take less time so it might go for that one so again as I mentioned different scheduler has a different algorithm to work with using priority you can only suggest what should be done okay that's one so we know now how to set the priority and get the priority apart from that if you still want to make this work one thing we can do here is after printing hi see what is happening is as a scheduler will give you some time to execute right so let's say schedule says Hey thread a you can execute for let's say one millisecond and we were assuming that in one millisecond it will print one high but no my machine is so fast in in fact most of the machines are so fast nowadays right uh in that one millisecond it is executing this number of highs how do I control this so one thing we can do is after printing every High let's ask our thread to wait okay so the way you can do that is by using thread dot there's a method called sleep now in this clip you can basically mention for how much time you want this thread to sleep so you have to specify that in milliseconds So when you say sleep you are asking your thread to wait wait for 10 milliseconds and then continue okay so what we are basically trying to do is once you print high at this particular threader the A3 will go into weight stage right waiting stage and then the B thread will execute hello and again once B thread done is work you can just put a sleep there as well now the only thing is this Sleep Method will throw as a remember we talked about checked exception so this Sleep Method basically throws a interrupted exception so what you have to do is we have to put a try catch all we can set those but let's put try catch here so with this suggestion I can say surround with try catch and our job is done right and just to simplify this what I can also do is I can put this in one line so you can see we got try catch and then just to reduce the number of lines we have we I'm just writing that in one line the catch block the same thing can be done for the hello as well and you can see we got the sleeper as well so we got two threads and now I hope it will work let's compile and run okay see that we are getting high hello in sequence uh if you start from here you can say we got hi hello hi hello and then go on but if you can see this is where you got two two hello at the same time now it is possible that after doing this calculation after doing this iteration of hi hello two threads are going back to schedule at the same time and schedule has given the chance to hello first so it all depends upon how you work with threads how I mean not you you can only make multiple so it's all depend upon your schedule how to run this and that's why when you play games sometime you know you can see some of the characters are not moving a sequence or not in sync because the threads are not working properly there so as a programmer you can just optimize it more okay you cannot control it one thing I can do is if I want the hello to come after that maybe I can just put a a sleep here as well maybe of two milliseconds so after so I I just want both both of them to start with a gap in between of two milliseconds so compile and run and if you can see I hope there will not be any High hello which got two time print you can see now it's all once it's because there's a gap in between hi and hello who is reaches to the schedule at the same time so yeah that's how you can optimize again as I mentioned we are just trying to optimize it there's no guarantee that you will get this output every time oh we got it cancel that we got two hellos here again my idea was just to optimize it and it depends upon your schedule how to work with this or maybe I can just if I make it five then we can control that so try this out and let me know how it is for you and your machine so we have understood two methods uh two things from this video one is the priority so you can set the different priority for the threads main priority Max priority and normal priority and you can set any number between one to ten and by doing this you're only suggesting to your scheduler to give the current threat my example IIT if you set it that way and then we have talked about sleep now when you say sleep it actually goes into a waiting stage again we'll talk about uh what is that waiting stage or block State bit later but uh yeah that's a stage which follows there are multiple way is to create a thread one thing we have seen which is of thread by showing a class by thread but we have more options but why do we need more options now think about this let's say this class A want to extend some other class let's say we have one more class here um and the class name is z and this P the it I want this to be a parent class and a to be a child class so I want to basically make this a as thread as well as I want to make this as a subclass of Z and we all know right the multiple inheritance in Java is not possible so how do we solve this problem so this idea of extending Authority is not actually a good idea so the other option we have is okay let's go back the other option we have is if you click on this threader If You observe thread is a class which implements Venable okay and if you go to the enable you can see we have the method called run now this is important right if you remember in the code which we have written we have went with a class name we have went with a method name called run right that actually means that if I implement or enable here instead of using a thread even this should work right because it has another method so what I will do now is instead of extending with a thread I will simply implement it with the help of an interface called runnable okay so both the side now I have a choice of extending a class as well if you have some other class because we can have a class which is extending another class or implementing an interface at the same time but the point is you can create thread like this as well you are making this class A as runnable okay now by doing this what how can you work with this don't mind you go down first of all let me just remove this we don't need this code in fact you know before I continue I just want to make some more changes to the code uh the first the first thing I'm going to change instead of having uh let's say 100 question let's go for five iterations so that we can see the output fast and yeah that's the thing okay so the start will not work you know why the start will not work is because in the vulnerable interface we don't have a start method the start method before it was coming from third class okay that's weird so we can't use this right see the idea is if you want to if you have a runnable class example this a is actually a runnable class right a class which is implementing an enabled so in that case if you want to work with start you can't do that so in that case what you can do is you can create a two different threads you can say T1 this is your first thread and then you got the two this is your second thread right so we got two threads here and instead of saying obj one dot start we have to say T1 dot start because we know that start method belongs to a thread and to also right you can see now we are able to call start now let's see if this works I will just go back here and compile run oh that's weird we have not got any output see a thing is yes we got two threads yes we are saying start it it's actually creating two threads behind the scene but it's not doing anything because these two threads have no idea about this obj1 ob2 they have no idea about these two classes so in that case what we do is if you go to threaded Constructor you can see thread has multiple Constructor one of the Constructor takes as an enable object that means obj1 here is actually a runnable object you know let me prove that so instead of using the a can I use runnable and we have seen that right we can create a reference of the interface and object of a class even here we have to say the reference of the interface and object of a class okay so what we can now do is in this particular Constructor of thread we can pass obj1 and here we can pass object 2. so we are passing the runnable object in the thread class or the thread Constructor now there's a link right so when you say start it will know that which run run which run to call so this start will call the Run of these two classes let's see if this works I will say clear compile run oh it worked and see that we got hi hello hi hello and it is working so basically whatever you want to a thread we have two choice you can extend our thread class or you can Implement a enable interface the only thing is this enable interface will not have the thread methods in that case you need to create a separate thread object and then we can use these features okay now if you remember we have also learned about Lambda expression right so like let's reduce this code by implementing Lambda expression okay and let's do that step by step now if I ask you one thing the entire class A why we are getting this class we are creating this class is because we want to implement runnable right okay that makes sense how about if I remove this class so of course this variable will not work example let's say if I comment the entire section here okay when this part come in the entire section you can say this will give you another so I want to create an enable obj1 but I can't create a because we don't have a so can I say news enable of course we can right we have seen how do we uh create how do we use anonymous class to create the object so imagine this is anonymous class right now in this Anonymous class we have to implement the method which is that in learnable now which method is there the same method which we have here so can I just copy this code from here and I can say copy and paste let me remove the entire comment section so basically what we're doing is we are instant we are trying to instantiate our interface with with the help of a Anonymous class here and in this class which is which has a method which is run and basically we can we are basically implementing this Venable here itself right okay that works let me just try if this works before we continue so I will say compile and run it worked and see that we got the output okay that's great okay what else we can do uh can we do it for the second one as well yeah we can right but before we go for that one can I convert this into a Lambda expression and we have seen that right Lambda expression works whenever you have a functional interface and runnable is actually a functional interface which means I can use Lambda here and in Lambda this is optional so I can remove this I can put a arrow here and I can remove this bracket as well okay this looks cool and I can just put that back here okay so as you can see I have written everything in one line for the try so that you know it will be over in one line and now uh can you can we do it for the second object as well of course I can I can just copy this code and the only thing we know we need to change is the hello part right so from high we have to make this hello that's the only change I have to make and I don't even need these two classes now I can remove them and see the number of lines we have reduced and also if you know that this class would be used only one so this makes much more sense to use right uh the anonymous class and we can convert that into Lambda as well so this is the uh the Lambda version of it cool and now we can simply run this and this should work compile and run and it works and that's the beauty about Lambda Express right and you can reduce the number of lines you have and there's no extra classes see it will be tricky to type the entire thing from start to end because till this point what we are doing is we are trying to convert the anonymous class into Lambda expression which is easy by typing the Lambda in the first scope will be difficult so just try it out multiple times you might make mistakes with the syntax But ultimately you will get used to it so those are the two options we have creating with the help of extending with a thread and by implementing the interface runnable in this video we'll talk about race conditions now we have talked about threads right and till this point everything looks good because we now we know that threads are important if you want to execute multiple things at the same time our threads will be useful now sometimes we create two threads sometimes we go for multiple threads maybe eight threads ten threads it depends upon your application and as I mentioned before there are certain times when you actually write threads by yourself otherwise most of the time uh your Frameworks will do it for you okay let's say you create your own threads okay and then we know that threads are good on the other hand we have one more concept right so let's let me just write it down here so if we talk about a concept like threads threads are important we know right apart from this we have one more concept which is called mutation so if I say mutation here now what do you mean by mutation mutation simply means that you can change something example if you talk about variables so primitive types variables or object type variables we can change is them right example if you have ain't I so let's say if I say ain't I is equal to 5 and then later on I'm changing this value of I I'm saying I plus plus maybe after some time I'm assigning the value for I to 6 I can change this value right and it is actually useful to mutate a value of and of course that's what we do with digital processing right so when I say you got data you want to process it most of the time you might want to uh change the value of your variable and that makes sense right so that is mutation but then if you remember when we talked about strings uh we have mentioned that strings are immutable right so there are some Concepts which which makes them immutable so which is good mutation or immutability uh at this point if you if you can change something notation looks good right so yeah so till this point mutation looks good but then let's try to discuss something more what about threads our threads are good of course if you want to make things faster threads are good but then what if you mix both of this what if you are doing mutation with multiple threads will this work so what I'm saying is what if you have two threads let's say T1 and T2 and both are trying to work with the same variable I so T1 changes the value of I T2 changes the value of I now it will create instability in your code I mean think about this but what if you just have one bank account and you got two cards two debit cards of course as a single person you cannot use two cards at the same time but what if you have given the other card mind you I'm talking about the same account so we have one bank account and we have two cards what if you give one one of the card to your friend and then you went to two different ATM machines okay and now you are trying to uh withdraw the money so let's say you in your account you have 10 000 rupees and both of you at the same time at the same second you try to withdraw the money you try to withdraw let's say seven thousand seven thousand so in total you're trying to withdraw 14 000 rupees the balance is only ten thousand rupees so that's an issue right if you try to do things simultaneously and we'll also do that in the code as well just to see what happens so threads and mutation together is not a good idea okay and that's why if you are working with threads always try to make sure that you work with data which are immutable or if you think that there's a method which is which can do some mutation make it thread safe now what is the concept of thread safe so thread safe simply means so if I say thread save thread safe simply means that only one thread can work with that at one point example if you have a method let's say show and the show method so there are two methods we're going to call so T1 and T2 so only one of them can be able to get into show at one time so if T1 is executing a show T2 cannot execute so okay that's the Restriction we can add but how let's see that in the code here so what I will do is first of all I will remove these two threads or maybe we can just use these two threads as they are so you can see we got two threads here the only thing is I want to make sure uh the iteration is not for five the iteration is there for let's say 1000 and maybe I don't want to print something I don't want to apply a Sleep Method just keep it clean and let me just clip this clean as well so you can see we got two threads and both are doing something but as of now you can see we are not writing any code here the for Loop is basically empty so what we can basically do here is first of all what I want to achieve just a very simple thing let's say I have a class here called counter and in this counter basically I have a method which is public wide increment okay so that's the increment method we have and then let's also create a variable so let's say the variable is Count okay and by default the value for count will be 0 because that's an instance variable and then inside this increment I'm just saying count plus plus okay so basically what we have is we have a class called counter and in this we have a variable called count and then every time someone calls increment it will be doing count plus plus and of course if I call increment once the value of account will be one if I call increment 10 times the value of count will be 10 right but then what I will do now is I'm going to call this counter or this this increment with a multiple times so before we even use that let me create the object of counter I will say counter C is equal to new counter and then from this two threads what we have we can simply say C dot increment so basically I am going to call increment multiple times now you tell me how many times you are going to call it so of course I'm going to I'm saying that increment in both the threads and each thread is going to do this incrementation increment for 1000 iterations right now when I say 1000 10 000 in total this increment will be called for 2000 right okay let me just Retreat we have two threads each three is calling increment thousand times so in total the increment will be called 2 000 times right so if I go back here and after it starts I just want to print the value of count let's see what the what's the current value of count is now you tell me what will be the current value of count so if I compile and run now when I run this code I want this to be 2000 right let's say what happens oh it's only one one six the weird thing is every time we run this code you will get a different output for this what's happening see what we are trying to do here is let me just do that on my pad here see we've got a main thread right this is your main thread and on line number uh 34 and 35 what we are doing is we are creating two two new threads we got T1 and T2 now T1 jobs is to call increment thousand times right and T2 job is to call increment thousand times and ultimately we want this count variable to be around 2000 right but the thing is the main is doing nothing right Main in main we are saying hey just print the value of count now we were expecting that T1 and T2 will complete the work and then main will print count but that's not the case anyway these two we have two threads right T1 T2 and they are running any anyway now when these two threads are running in any case they are running right main is doing nothing so main says okay my job is done I have started these two threads and now I will simply print the value of count so in between the iterations maybe T1 has done some iterations T2 has done some iterations main is printing count anyway we have to ask main hey you know don't print count here let them come back so as main continues let them come back let T1 and T2 complete their work by doing 100 iterations when T1 comes back and T2 comes back after this just try to print the value of count and I'm sure at that point the count will be two thousand so what I can do is uh how do I ask main to wait for T1 T2 so one thing you can do is you can say T1 dot join now join in a special method which allows your main thread to wait for the other two threads to come back and join okay so if at this point if you want to wait for these two threads you can use join there okay but the only thing is join me through an exception so I can simply say add those declaration you can see we got close exception anyway we have done with the join and now I think it should be good we should be able to see two thousand so if I compile and run oh okay we were expecting 2 000 but we got okay now we got two thousand now we got 1746 and that's why you know threads are sometimes be unpredictable because they have a different behavior and unfortunately they cannot control directly uh so what is happening here the example which we have taken before uh which was about the bank account it is possible then when we have two threads so we got T1 and T2 they both are executing the same method and in this method what is happening so when I say increment it is actually getting the value of count if you see we are saying count plus plus right it's actually two steps first it will get the value of count the current value of count and then it will say Okay count is equal to count plus one because that's how count plus plus works right so there are two steps get the value for count and whatever new value you get just add it with one and then assign it to count back so let's say when T1 reaches to count the value of count is zero right so if I just say this is the count value is 0 then 0 plus 1 is 1. so the new count value is one again if we trade this part so this will be one the value of count is one right so T2 goes there T2 says okay uh give me the value of count it says one plus one is equal to two now again T 1 goes back T1 says okay new value is two I will say 2 plus 1 is equal to three and that's how the iteration should go right but what if both the threads reaches to this count at the same time so let's say T1 and T2 both are reaching to do count at the same time they both got the value which is three they both said okay three plus one is equal to 4. we got two iterations I mean we called increment two times but the incremented happens only once okay that's what happens when you have two threads working with the same variable or shared variable oh okay so mutation and threads are not good and every time you run this you will get different output now how do you make this consistent okay you might be thinking why we got two thousand years because maybe your threads are not going to the increment at the same time okay so that's not the case in fact if you go with a bigger number if I go with ten thousand and ten thousand you can see it will lose lot of different uh bits in between compile and run you can see 7000 we are losing 7000 values we got nine eight thousand values lost right and the chances of getting 20 000 is way less now so the point is this is not a good way now how do you make sure that only one thread works with that at one time that's why you can do it right so example event T1 is working with increment T2 cannot work with increment okay so T2 has to wait in that case you can use a very simple keyword you can use a keyword called Synchro nice you just use this one keyword your jaw will make sure that this increment will be called only by one method at a time so if T1 is working with increment T2 has to wait so if I compile this code now and run you can see we got 20 000. now it doesn't matter how many times we run this you will get 20 000 because now both the threads are not going to the diet method at the same time that's how we used two threads with the shared variables and you make them as synchronized so basically we saved ourselves from this condition now in this video we will talk about different diff of a threader now if you remember we have started with creating a thread then we uh talked about the thread is running and then we have talked about Sleep Method we have talked about joint method so basically we were basically playing with the thread States right where the thread was running and then we asked this way to wage come back right so how do we what are different states available try to understand that see every time you create a new thread that goes into a new state okay so this is your new state now apart from this new state we also have something called a runnable state okay so we have uh the naval state now after that we also have a running State then we have a waiting state or we can say also say block state and then the last date is we have is dead okay so we have all the states available for the thread now when you say new state what exactly happens so when you create a new third example in the code if you remember when we said uh thread T1 equal to new thread at that point it becomes into a new uh the new state now the moment you say start you know we have this method we just start so when you say start it goes to an enabled state so maybe we can say start so when you say start it says it goes into an enable state now there's a difference between vulnerable state and running the difference is when your thread is actually running on the CPU that is running state but when your thread is executing and then uh it is waiting for the scheduler that is into an enable state so example when your thread is running with the help of run method that is your running State now what if while you are running this and then maybe you want to keep it on hold with the help of Sleep Method so that goes into waiting State now apart from sleep we also have a method like weight which can make it go into wedding state right so when your thread is running State you can also say wait now wait will take it to the waiting state right now how will you come back now you will come back with the help of notify the only thing is when you say notify you will not come back to running State you will go into the enable State now whenever the scheduler is assigning with the task or whenever scheduler says it's your turn at that point it will go into running State uh in fact if you if your 30 is running and if you want to keep it there you can also say stop there's also stop is available but that is deprecated actually don't don't stop threads okay it will create instability in your software but yeah once the work is over it will automatically going to stop State uh you can also send a thread from the runnable to dead even with the help of stop even you can do that uh what else we have here okay so basically if we have this option so whenever you create a thread that is good that goes into a new state and then when you say start it goes into enable now depend upon your availability of your CPU that goes into running State now while running it will execute certain things and then if at that point if you want to say wait you can do that with the help of sleep or you can do that with the help of weight now the only thing is when you say wait you have to use a notify method to come back the only thing is when you say notify it will not come back to running status it will go to the learnable state right so it goes from the waiting to learnable from running to waiting waiting to enable that's the cycle it follows and then again from runnable to a running State yeah those are the states available we have this number of states in Threads especially in Java now let's talk about collection the funny thing is in this topic we have three different types of collection it starts with small say which is a collection API so let me just write down somewhere uh so we have a concept called The Connection API then we have a collection and we have collections now this is the only thing I think it's confusing in this topic now when I talk about collection API I'm talking about a concept when we talk about collection I'm talking about an interface and when you talk about these collections with s it is your class okay uh so if I say hey something belongs to collection API that means it belongs to this concept if I say collection I'm talking about the interfaces and then there are some classes which implement this and then we'll talk about collections which is a class and it has multiple methods to work with okay now why I'm talking about collection here so the thing is whenever you want to group The Elements right let's say if you have one value you can store that in one variable right I can simply say int uh num is equal to five and I'm happy right the only thing is what if you want to have multiple values now that's what problem creates how do you store multiple values and I know you know the answer right the answer is array and we have worked with it right so I can simply create a nums here which is an array and I can say new int and bracket I can mention the size which is five now this is your added right and it perfectly works okay then why we are talking about this discoloration topic see the thing is if we talk about working with data if you talk about working with algorithms there is a concept called data structures right and I hope you have heard about that in multiple places why data structures are very important the way you fetch data the way you the way you store data is very very important now sometimes example let's say if you store data in a box here now sometimes you want to save data and you want to fetch the last element okay so whatever element has been inserted last you want to maybe you want to fetch that or maybe uh you you have another type of data where you want to insert data in this values and the value you want to fetch is the first value right so this is where I'm talking about the last in first out and this is what I'm talking about first in first out just take a normal queue right example if you want to take a ticket you go you stand in a queue right in the same way this concept which I'm talking about here is Q uh the concept which I'm talking about here is stack right now apart from this there are multiple data structures which one to work with right and we can actually Implement all these data structures with the help of array as well there's no harm in that so as a programmer you need to do it right and it's good I know it's fun the only thing is when you work on our project you don't want to actually work with an array where you create this structure by yourself and that's where they thought hey can we just create an API and the beauty is it actually introduced in the second version of java 1.2 The Collection API where you can actually work with all these data structures apart from this we also have linked list right so you can work with all these things by using some inbuilt classes so it makes your work much much easier and also when you want to fetch data when you want an expandable array that's right the thing is the attic which we have worked with it is fixed size example if I create an idea of size 5 it will be five right you can't extend it but if you have a scenario where you want to expand an array maybe you want an element you want an array of seven now uh so of course in the existing area you can't do that okay now that's one problem of course you can find a solution for this as well what you can do is you can create a new model let's say nums two and you can say new int and you can mention seven here so you got a new array of size 7 and then you can copy all the elements from the above array but the thing is you have to manually create an array you have to copy the array and it sometimes it becomes complex to do all these things what if you can just get a class or this feature where you can create an array which is dynamic right so it's quite exciting to work with creation API where you can store data and you can use different algorithms to work with it what if you want to sort the array what if you want to create an array of unique values what if you want to have an added to add is one where you have all the keys one you have all the values okay so another of course as I mentioned before in other you can do it but then what if you can get some API using which it will help you in writing a code now that's where we can we are going to use collection API of course array makes sense in most of the cases when you know that the length will be fixed so you will be using array now whenever you have a specific requirement of different algorithms and structures that's where you will be using collection API okay now once we know the theory of collection let's start the implementation and of course this will make much more sense once we do it so if you talk about collection and I'm talking about the collection interface now right remember we talked about collection API which is a concept right so in this concept we have multiple classes interface to work with so the first interface we are exploring here is called a collection interface and if you can see the collection interface belongs to java.utl package as we have seen before most of the classes which we have used belongs to java.lank package and that's why we have not imported them but if we talk about this collection interface it belongs to a java.util package in fact when you talk about all the collection classes interface belongs to this package which is java.util okay so we have to import that as well so you can do that with the help of import and you can mention the package name and the interface name which is collection now once you say collection you can create a collection of numbers let's say and then I can say new okay now the only problem here is I can't say new collection right because collection is the interface right now you might be saying hey you know it's simple you can just create Anonymous class provide the implementation maybe I can try but then if you can see the collection interface has so many methods and we have to Define all the methods see when you talk about API API simply means a a library or some feature which you can simply use it you don't have to implement them then what's the use of using an API right so what I will do now is I will remove the entire part I don't want to I don't want to Define this method by myself the second option we have is if you don't want to create your own class can we use some in the classes and that's right we have so many classes available which you can work with so if I want to represent this so basically on top we have something called a collection right now this is an interface which we are using now there are certain more interfaces which actually implements The Collection one of them is list we have q we have set so all these base all these classes or all these interfaces basically implements or basically extends The Collection interface and individually these interfaces have their own class implementation example for list one of the most famous one is at the list so we can use this for Q also we have different classes for set we have a very famous class which is called hash set so we have different classes implementation example for Q we have something called DQ which we can use uh so for at least we have any list uh apart from other list we also have linked list which we can use if you have worked with data structures before we have a concept of linked list as well and then in most of the languages we Implement them by ourselves right but then in collection API we get the implementation in fact inside also we have something called a linked has set but then the most famous one or simpler one is to use other list and has set for this set now apart from this if you talk about the concept of collection the concept I'm talking about collection API uh we have one more thing to work with we have something called a map right and map has multiple implementation which we'll see when we start with map okay so at this point let's try to use either list or we can use set but let's start with a list here so what I will do is instead of saying new collection I can say new add a list and you can see this is a class ignore this angular packet I know it is coming from multiple times but ignore that time bin so we got this collection nums equal to new other list now in this numps basically we can add values okay uh now how do we add value which is very simple you can just simply say nums dot let's explore the methods you can see uh we have method like add we have methods add all contains uh equals for each okay we'll say for each as well which is very interesting uh then we have notify notify all uh remove then we have we can also convert a collection into array we have to add it there and then we have a weight method but at this point I just want to add the values right so I can just come back here and I can say add and in this ad I can add values so let's say I want to add value which is 6. so since this is numbers I'm expecting that this will only have numbers so I will just copy this code and I will try to add multiple values so let's say this is 5 this is 8 and this is 2. so basically we got this value right now ignore this uh yellow zigzag lines here we'll talk about that later okay but yeah that's an important concept which is coming up but at this point I got this collection right and I want to print this now if you have an array of course you can also get array with this elements but the problem there is you need to you cannot simply print an array right you have to use a loop to print each element but that's not the case with collection I can simply come back here and I can say s out and I can print nums and you can see if I try to run this code if I say compile okay so when you compile this code there's no error but you can see there's a warning here again we'll talk about that watching in some time but let's run this quota and see what happens and you can see we got the output so we can directly print a collection that's a beauty okay uh can we print this value one by one actually we can okay so what you can do is you can use a loop here so I can say for we can use index values let's let's see if do we have index option here so against the nums dot uh if I say get or unfortunately we don't have the indexing for collection API or collection interface here no issue I can use uh enhanced for Loop remember we have used enhanced model with the loop so I can say int and colon nums and I can print each n value so I don't have to do this again how do we use index that we'll see later but at this point this should work okay the only problem here is okay let's let's talk about the problem which we are getting this great uh this yellow line here so the thing is when you talk about a collection when you're getting nums here right see in Array you specify the type as well you say eight nums right so you know that in this Nubs you'll be having only integers but when you talk about collection nowhere we have talked about the type of it how it will work okay and that's where what you have to do is you can mention the type and that's the error you're getting here because it says the values which I have added here is not actually numbers this are object type that's right uh as we talked about integer float classes all these classes basically extends the object class and by default The Collection API works with objects and this 6 5 8 2 they're not integers they are objects and that's why you cannot store that into integer how do we solve this problem so if you want to solve this problem we can use a concept called generics okay now how it works so basically where should we mention the type maybe what they could have done is you know this is what they have done in 1.2 1.3 1.4 but the realized one problem right which is by default all the values here are objects so how do we specify maybe they could have created some extra classes by saying integer list integer collection string collection what if you have your custom class like student so it will be difficult for them to create all the classes right so what they did is they have given you one option where you can use angular brackets here so just after the collection use the angular bracket and in that you mentioned the type of element you want to work with so we are working with integers so integer is basically a class ah which is the vapor class for the int type because int is primitive integer is a class and collection normally work with objects not the Primitive values so if you want to store integer numbers which is you have to use a class integer and you can see now there's no problem there is no yellow zigzag lines and we got the there's no error here as well let's see if this works I will say compile and run and you can say we got the values now one more thing why this type here is so important see it also helps you to do to remove the bugs of your code now what bugs it can be example let's say if I don't mention integer here okay let's say remove if I remove this part and everything is object right and then here what I'm doing is I am uh we can use an answer the only thing is I can't use object here I have I can't use in I have to use object and then I am converting this object into integer so we can do that by typecasting it here so I can just type casting it to integer in fact we can do it outside as well let me just do that so I will say int num is equal to I'm getting a num variable again and I'm converting this n to the integer type okay so you can see we got num and what if this with this num I'm performing some operation because I want to perform the operation that's why I can't use object and now if you try to work with this you can see again we got the warning because we are not using type there and if I run this code it works there is no problem but what if by mistake you are doing the code and you have added a number but that too in double quotes let's say five now what will happen Okay so let's see if I compile this code there is no problem if I run this code we got an exception and it's bad right and this is also runtime exception this is runtime error basically we don't want this and that's why I think this type of problem should come in compile time itself so if you mention the type with the help of generics here and if I mention integer here as well now if you can see we will get the compile diameter not run time error and that's why if you compile this code okay you can see we got a compile time around and that's why it is better to put this type every time so whenever you work with collection always mention the type there cool so that's how we use analyst and it has multiple methods now the only problem is we are not using the we are not able to get the index value right see the thing is if we talk about analyst as I mentioned in the diagram as well analyst is a class if you can see here so it's basically a class which implements a list right and if you talk about this list list does have a method called get because list works with index in the values so which one to use should I use a collection here or the list so if you want to use if you just want to add values and fetch collection makes sense but if you really want to work with index value instead of collection we should use list because list supports index value and you can see now I'm working with uh list here and now if I want to print a particular index value let's say I want to fetch the second index value so I can say nums dot get as you can see we have a get method and in this get you can mention two so which is it will in fact it will give you three it will give you eight which is at index number two right let's try I will say in fact let me just not print this Loop compile and okay run and you can see we got eight so if you want to work with index you can use list and list has some extra methods if you can see against a nums dot add was that uh we got get we can also set the value in between with index value and anything else yeah we also have index of so if you have an element if you want to know the index of it example let's say I want to see I want to find the index of this two value so what I can do is I can say s out I will say nums Dot index of I want to print the index of 2. right so 2 I'm talking about the value now or maybe you know this is this looks like quite confusing so I will say five so index value is one if I compile and run you can see we got one there so yeah it has multiple methods and I would recommend you to explore more methods of list and that's how you use what you work with list and other list now to this point we have talked about least right now list is one of the collection API concept it it supports index values right so we can create a list and we can work with indexes there the only thing is when you talk about list of course it's great uh it does support multiple values example let's say and that's a feature now I'm not talking about the drawbacks of it so I can say I can add a element let's say six again if you can see I have two I have two elements with the same value which is six now if you are wondering what is this e here again this is given by my vs code it simply means element and if you see if I try to run this code if I compile this code and run you can see okay we're not printing all the values is it no we're not printing all the values so I will just remove this two lines and focus only on the for Loop here and let's see if this works now so I will say compile and run you can see we got oh uh I guess we are doubling as well right anyway you can see we have a list which has same values so yes your list does support repeated values but sometimes you want to have a collection where you can only have a unique values remember when we talked about the collection as an interface it is also extended by a set interface Now set supports unique values okay so what I will do now is I will have the same values okay but the instead of using list I will say set and let me import the package for this I will say control space enter and we got a package for set as well and then I want to specify not an analyst okay because I see the thing is if we talk about set yes set extends collection but the class which implements set is different and the class name is Hash set okay I can import the package for this as well and done apart from this the syntax will remain same we also specify the type here the generic type which is integer and okay let me remove the list input let's only have set and has set and you can see we can have the same method add also here and we can print all the values the only thing is here I will be very specific that I am working for integers um let me remove this and I also want to print the value now that's it okay now when you have these values let me just compile and run you can see now the value is 2 5 6 8. first of all we can't have a repeated value you can say six and six uh we only got 1 6 here the second thing is it is not following these sequence remember the if you can see the sequence is six five eight two but we are getting the values in sorted format is it really sorted let's try what I will do is I will just make this okay first of all we know that we cannot have repeated values I mean we can add it it's just that it will not have the same value again uh let me just make this as 54. I will make this as 82 I will make this as 21 and this is let's say 62. again we have a bigger values now let's see if that really is sorted I will say compile and run if you can see it's not actually sorted it's 82 21 54 62. okay this is not sorted and it is not go even going for a sequence and that's how this set works set will not give you value in this sorted format okay it basically uses uh and it also doesn't have a index value example if I say nums dot for at least remember we had a get here we don't have get I mean we don't have a get which takes element we do have a get class but we don't want it we want a get method with using which you can specify the index value set does not support index value so certain things to remember for set so set basically is a collection of a unique value if you see documentation it says a collection that contains no duplicate elements and uh it does it does not even have uh index value is it not mentioned anywhere anyway so you can't have index value I know when I mentioned about collection API it was introduced in 1.2 and also uh it is also called collection framework okay instead of saying uh API you can also say its collection framework cool um so this is your set and that's how it works what if you want a sorted value so instead of using hash set we can use something called a tree set so let me import the package for this import and now if you try to work with this so you can see we got all the sorted values so basically this tree set actually extends if you see it extends a abstract set and implements a navigational set and if I go to navigational set you can see this interface actually extends a sorted set and that's why all the elements in this tree set will be sorted okay it is a set but then it is sorted set so it depends upon what type of elements you want to work with if you want to trace a particular element then preset makes sense but if you want to uh want to have a set which will have only unique values there is no need for auditing you can go for hash set there one more thing instead of saying set here we can also say collection even that works in because anyway uh the tree set implements set and set extends collection right so we can use that as well one more thing to add if you if you look at collection and if you look at our diagram on top of collection we have one more interface which is called iterable uh since we can iterate between the elements right with the help of enhanced for Loop or different we have different options so it actually extends a tribal and if you go to a table we have an option called iterator so that even that is one option you can use iterator method which will give you a iterator itself let me show you how that works instead of using a for loop I can say nums Dot iterator right so we got the iterator now this iterator will return will give you iterator back the Twitter object I would say this iterator is integer I will import the package and here I can say uh values equal to so from this iterator I got all the values here and then from this values I can print the values I know that sounds weird right but let's do that I'm not using a for Loop so how do you print all the values so basically values has a method called has next and it also has a method called uh next which will give you the next element okay it will give your next element but it will only give you one element right I can put that in a loop to get all the elements uh but which Loop we have to use in fact what I will do is I will just try to plant this next element but the problem is it will only give you one element how will I use a loop here because if I use a loop I need to know how many elements we have right so one thing we can do here is we can use a while loop because it has a method called values dot has next now has next will tell you that do we have a next element okay and let's try if that works compile and run and you can see we got the values so even this we can also use iterator that's also an option and that's why we got the top most interface which is iterator so that's how we use set a Twitter and reset we can also try it with the linked has set but then yeah internally they work differently but you can get the values okay so we have talked about least we have talked about set of course we have different classes as well for different structures and this is a famous one now there is one more which is called map Now map is not an implementation or it does not extend collection interface but it's a part of collection concept relation API right so what I will do now is let's work with map here but why do we need map see the thing is when you work with list let's say we have a list here and list of values we have okay so we have list of values let's say the values are 56 23 67 and 92. now we have this badges right and let's say uh if it if you work with analyst here if you work with list basically for this values if you want to work with these values you need to work with the indexes why do you have to say 0 1 2 3 right but sometimes it's better to have a name or a different kind of index instead of having zero and one two three uh example let's say if you talk about phone book we have a name and a number right so can we have something like this I want to say these are my students marks right so I want to say this marks belongs to Naveen uh this marks belongs to let's say hush uh this marks is belong to social and we got Kiran so instead of having index value can we have a name to it okay instead of having this values so can we do it and that is possible with the help of a concept of key and a value pair so we have a key and a value so example this value which is 56 23 67 92 there are values and the key for this is Naveen the key for this is Hersh the key for this is social and the key for this is Kiran right and we can achieve this two things in one data type or data structure which is called a map so this map map is basically a collection of not map is a collection of key and a value pair how do we achieve that so what we can do is we can remove the entire section from here just to keep it clean and let's work with Okay we don't even need these Imports now so let's create a map so where you can create a map is map and okay let's import the package for this again java.utl and we can say this is students that's a map of students and new Now map itself is an interface if you can see and it supports key in a value pair now if you remember when we talked about analyst it was having either right because it represents elements in map we have KV it is just a character representation of what you are going to insert now this key this K and this V can be replaced with data types like string integer integer float or we can have student string whatever type of you want to work with so now we have to look for a class which implements this so the class name is hashmap right and in this as well we have to mention the type of it the type of key we are working with is string and the type of value you're working with here is integer okay and of course it can be anything it can be string student it can be storing string that's your choice whatever data you're entering but we got this map here now once you've got this map I can simply add some values here how do we add values it's very simple you say students dot now we don't have a add method we have a put method so we have to use put and we can specify two things the key in a value and as per our diagram we have seen the key I want to insert is Naveen and the value is what 56 and likewise we can have all the values here copy paste paste paste and this is hush and the value here is 23. this is social the value is 67 Kiran it's 92. so we got all this key and a value right and then if you want to print it you can you can actually do it before this we were using list and then for every value here we were using index but now we have a logical name to it it is much easier to work with names right uh so I will say s out and here let me print these students basically I'm printing the map itself I will compile this code and run and you can see we got a map we got Naveen 56 we got social 67 we got killer 92 and hers 23. now one thing to observe even this is not following a sequence you can see we got Naveen first then we got social we got Kiran we got harsh okay it looks like more of a set right let's Explore More uh okay so we can do that otherwise if you want to fetch a particular student only one student you can say student dot you can say get and in this get you can specify the key let's say I want to get the marks for not all the students but let's say hush I can specify the key here and if I compile this code and run you can see we got 23 that's the marks for hush okay we can do that okay one more thing I want to print all the values but then what I will do now is let me say during my school days we used to after the exams when the paper is checked we used to get the paper back to calculate the marks right so we used to turn the page we used to calculate each and every marks and at the end once we add all the values of all the marks of each question if that marks is more than the marks which you got on the paper which is checked by your professor we used to go and collect it right of course if you got extra marks then you don't go there but let's say uh has got 23 and after calculation we have changed it so let's say students.put and now I'm saying hush and the new value is let's say 45. there are a lot of mistakes in the paper so let's say we got 45 here now tell me will it add a new student as hush or will it replace the existing value let's see what happens if I compile this code and if I run you can see for Hirsch we only got one Hersh here which is 45 we don't have two hush that means the key cannot be repeated keys are unique the values can be example I can have two students with the same number and of course we do get same marks right if you copied anyway so we cannot have keys repeated so can we say these keys are actually a set that's right and the values are least here so we have a set and a list combination to get to get a map here okay so what I want to do now is I want to print all the values and that's why you can see there's no sequence here because it's a set okay I want to print all the values but one by one I want to use a for Loop here can we do it the problem is we don't have see if you work with list and if you use a for Loop it will give you one value at a time right there's no one value here we have two key and a value so what you can do is first of all let me just get all the keys and let's see how it works I will say students I want to get all the keys so do we have a method which will give me all the keys if I go down down down yeah you can see that we have a key set oh we were right it's a set uh so I'm printing all the key set now so if I compile this code and run okay so that we got all the keys if key set gives me all the keys if I run a loop on this if I say for what type of case set it's a string right so I can say string Name colon it is coming from students.key set okay so what I'm doing is this key set will give me a set right and from that set you are fetching one name at a time and with that name I'm printing first of all name as it is and I will give a space in between or maybe a colon and then I can provide the I want to get the value how do you get the value so you can say students dot remember we have worked with this get and you can pass the key which is name of course right your key is in is in in this name or maybe I can just to make it simple instead of same name I can say key itself and here as well I can say key here I can say key right simple and now compile Run Okay so that we got key and a value there so that's how you can print all the values from a map now why we are using our put method here is because when we say add we are adding new element when you say put it says try to add the element if you already have this key just replace it let's put okay apart from this do we have some more methods to explore so if I say students dot we got a put we can also say get if we can also get values all the values the way you have key set which gives you all the keys when you say value it will give you all the values uh apart from this we can use uh remove to remove the particular element or the particular entry or we can replace as well yeah those are the methods we have and please explore those methods to understand this mode so that's how basically you can use map in fact apart from this we also have a hash table so if you can see we have hash table here and both works almost same if you compile this code and if you run this you can see we got the same output so what's the difference between hash table and hash map if you go here and if you go on top okay so if you can see in the hash table code it says the new collection implementation which is Hash table is synchronized now it depends upon which you want to use so if you want to use a synchronized version you can use hash table if you want normal we can use hashmap now what is synchronized if you remember we have worked with threads so if you have multiple threads working then it's better to use hash table to make it synchronized right so if you go up let's go to hashmap to see what it says so if you go to Hash map okay it says if you want to work with multiple threads and if you still want to use hashmap then use synchronized externally so if you want to use synchronized use hash table otherwise Map works so yeah that's how basically you can use map and uh that's how you can fetch the values in this video let's try to work with sorting so let's say if you have a collection and you want to sort it how will you do it so first of all I will create a list of values I will say list of uh I will say integers this time and then let's say nums equal to new add a list right and then we can import this okay now one thing to observe when you talk about this type here on the left hand side there is no compulsion to add that on it inside okay at the start when generic concept came it was compulsory for us to assign the values or to set the type on both the side but after 1.7 they have kept it optional if you specify the left hand side on the right hand side you can simply keep empty bracket that completely works okay so in this added let me add some values I will say nums dot add also four and likewise let me add some elements here okay so let's say we have these values here and what our do is of course if I print this value so it will be printed as it is and it follows the sequence as we have seen so if I compile this code and run you can see we got the values now what I want is I want to sort this values how do I do it now we have a very special class called collections okay as I mentioned in the first video uh we have a collection class as well and this collection class has lot of methods one of the method is called sort and in this sort you can pass the array or address which you want to sort okay now collection class belongs to util package okay and then once you say sort and after that if you try to print these values and let's say the output if I run this code you can see we got three four seven nine so sorting is actually very easy in terms of collection here right now thing is what if you want to sort this based on your own logic how will you do it example let's say the values are 43 uh 31 and then 72 and 9 or let's say 29. so we got all these values right and then I want to sort them based on their last digit not the entire number because again if you sort this value it will be sorted based on the values right I want to sort based on the last digit so I want 31 to be first and then I want 42 then I want 72 then 43 then 29 so this one first then this 2 then this 3 and then this nine how do I change a logic here say by default sort will take its own logic right in case if you want to have your own logic in that case what you can do is we can use something called a comparator as you can see we have a comparator here okay how do I use it so if you talk about comparator let's try to use comparator here so if I use compare it vector and if you go there comparator is basically an interface okay so how do I use comparator so I can say comparator comp is equal to new comparator now this is an interface right and we know we have multiple ways of implementing an interface we can create a class which implements the interface or we can use something called an anonymous interface Anonymous class here okay this comparator works with integer and I can specify that here as well which is integer in terms of anonymous class such as compensate to specify on the ident side as well okay we can use this comparator here and in this I will Define the method which method we have in comparator so if you look at this comparator we have a method called compare okay that makes sense we can use compare here uh what's the Syntax for compare it is okay not this one the syntax is int compare and pass the two values so T is a type here so integer integer okay so I will say public int compare and we have to specify two values basically one is your uh I will say int I comma ain't J see when you basically sort the values you know normally you compare two values example let's say if I give you uh four values here two three let's say we got four we got three we got one we got six if I want to sort this at one time I will compare two values I want it to be ascending order so smaller value come first and then the bigger value go second so I will compare these two values if the first value is greater than second value I will swap so after swap it will be four three oh sorry three four one six then I will compare these two values uh then it will be three one four six then we'll compare these two values so this will be three one six or sorry 346 because the first value is smaller than second value no swap in this case we have swapped it because it was a bigger value now once you got one of three one four six we'll do this iteration multiple times again if you compare this two this will be one three four six then this two it will be one three four six now we don't have to do that we don't have to compare the last two because in the first iteration you know the biggest value comes at the end so we got we got that right so we have to do that only three times and again for the next iteration we can say one three I know this is sorted but still we have to continue the iteration first compare one first two one three four six and you're done right so this is how basically you sort the elements the the aim here is when you sort elements uh this is one of the algorithm for sorting basically we have to compare two values and swap that's basically basic rule right so here as well when you say you are sorting you have to just sort of specify when to swap and not to swap so when you compare I and J if you say okay now we want to compare and how do you specify that we have to swap so if you return one it will swap if you return minus one it will not swap it's that simple okay so what I will do is I will I will check and I want to check the last number right so I will check if how do you get the last number oh mod 10. if I mod 10 is greater than the J mod 10 in that case swap else you can say return -1 okay so this is how basically you specify the logic and I can still see an error here what is the error the compare method is correct okay there should not be any error here okay let's see what happens so what I will do is I will pass this com object here itself I will say com and okay there's still an error I don't see any reason to get the error did I forgot any colon or something things are looking good let's compile this one let's see what happens compile there is an issue is not abstract does not override okay it is giving an issue with the compare method is it the right method when I'm using compare okay that's right okay do we have to use the class names here integer and integer oh it was because of the class name my bad yeah so we have to use a class email right because collection works with the vapor classes and now I will try to compile this code and okay compile two times and run you can see we've got distorted values we got one two three nine so basically what we are doing is we are changing the way it sorts the value if you want to specify your own logic we have to pass an object of a comparator now in this object relation of comparator you have to specify the logic on based on which you want to sort this uh in fact let me give you a challenge here what you can do is instead of creating the list of integers create a list of string and sort this string array with the help of the length of the string okay that will that will be awesome right try that this is how you sort the value based on the comparator so what what is comparator basically so comparator is a concept or an interface using which you can specify your own Logic for sorting okay that makes sense right now I will do something else here what I will do now is instead of having a list of integers let me have a list of students okay so what I will do is I will get a class here called student and let's say we have int age for a student and string name and I also want to have uh two streamers so that I can print the object as it is so this is Source action I want to get a two string method name because I want to print the student values as it is and let me also have a Constructor using which I can assign the values to them I will say Source I want a Constructor which can take both the values okay this makes sense let me remove this at override for time being okay so we got the Constructor we got two strings what I will do now is instead of creating a list of integers let me create a list of students I will say student here and then because that type of student right and we cannot add numbers now I will say these are stoods uh to replace this with stoods or maybe let's have numbers what's an issue and then here instead of adding a number we have to add a student right so I have to say no student and this will be value one or let's say we have to specifying age right let's say age 21 and name is let's say Naveen then let me just create some more objects here so let's say age 12 name is let's say John and then H we got here is 18. let's say puddle and we got age 20. let's say this is Kiran okay so we got this four student here with different age and different names now what I would do is I want to sort them okay timely let me just comment this section I want to print the edit as it is and let's see what happens if you compile and run you can see we got all the values the only thing is they are getting printed in a weird way I want to print them one by one so we can use a enhanced for Loop here so I can say student as Colon's nums and we should have used studs there now what I can do is I will replace numps with stoids okay this should solve the issue and done okay so we got some students and I want to print one student at a time let's see if that works compile and run okay so we got all these values now what I want to do is I want to sort these values based on their age how will I do that in that case I have to say sort okay let me just try natural sorting the moment you do that it is not working can you do that with the sword weather is not working okay let me pass the comparator object the only thing is now this comparator is not working with integer it is working with students so it is a student now and both this object so again we have to compare two students right so we need to show it at a time so I will say student and student let's say this is I and J now how do we compare them we are comparing that age right so we have to say I dot h is greater than J dot h then swap it's so simple right since we have worked with the integers it should work normally and if you want this code okay so that we got all the students with the ascending order of age we got 12 18 20 and 21 this works right so this is how basically we can use compat and we can sort a student as well but there's one more thing why we have to pass a comp here why not if I just say student white is not com why it's not sorting it's because this student we have not specified the logic for the student right because if you go to the integer class let me just type integer here just to see if you go to integer class you can say integer class Implement something called a comparable and by that's why by default the sort works with integers our student is not implementing comparable so if you want to have a natural sorting you can also Implement something called a comparable the only thing is this compatible interface has a method called compared to okay so we have to Define that method so I can just come back here and say implement this comparative method this is the signature okay and we are comparing this with a student itself and let me say this is that okay the the name of the variable is that will I will tell you why I've said that but now what I'm doing is I'm adding a method called compared to and I'm implementing something called a compatible now and then this comparable actually works with a student we have to mention that as well now if you go back you can see there's no error now so sort method does work with student provided you are implementing comparable if you if your students are not compatible how you can how can you sort them that's what it says but then if you try to compile this code and run it will not work the way we want it you can see this is not sorted and if we are not using compared to now I'm not using comparator what I'm using is I'm using compared to off compatible so whatever logic we have written here the same logic I have to write here okay now the only thing is we have to compare two students right one student is that so we can say this is that what about the first student the thing is we are defining this method inside the class itself right so what we can do is we can say this remember this keyword current object so this dot age the current object the current student we are comparing the current student with the other student it's something like when we don't have a compatible and if you have two students here we need a third person to compare two students but now I'm giving this power to the student itself they can compare themselves so if I'm comparing with someone else I'm this and that is that okay and let's see if that works now compile and run oh it works inside we got 12 18 20 21. so these are basically we can use compatible and comparator so you have two choice either you can make your class implements compatible if you don't want to do this you can also use comparator in fact even if you mention comparable you can still override the logic by comparator that's your choice okay one more thing before we close this let's say I want to use comparator here if a pass comparator and I want to you I want to reduce this code remove a Lambda expression this is a functional interface we can use Lambda here as well so what I can do is I can remove this entire part from here to here I know you have to watch this video multiple times to make it work uh I can put an arrow and we can put this curly brackets as well one more thing instead of using IF else we can also use a title operator here so the same logic I can just comment this part I can say return I dot h is greater than J dot h return one otherwise return minus 1. so instead of using IF else I'm using turn it operator we can remove this part now since we only have one statement and that to return we don't need return that is optional we can remove curly brackets as well and we can write everything in one line again watch this video multiple times to understand what I'm what I'm doing here and also in the Lambda expression you don't have to specify the type you can simply say I and J and your job is done you can see that entire comparator went into one line and that's why functional interface are so important let's see if this works and what I will do is I will just remove this from comparable just to see if that works without implementing compatible Implement run it works what is comparator if you want to specify on which logic you want to sort the elements you can use comparator what is compatible if you want to give a power to the class itself to compile itself or to compare its object to itself we can use uh compatible there in this video we'll talk about stream API now before we understand stream API first of all it's a New Concept it came in 1.8 Java 1.8 basically and it provides some amazing feature the only thing is why do we need those features we'll understand that first so what I will do is first of all I want to create a list of values okay so let's create a list here and I will say this is of type integer and I will say nums and of course just to keep it simple I'm going with the default names so that you you don't have to focus much on the naming convention I want you to focus on the concept so we have the analyst here and in this at least I want to have some values we have nums dot let's add some values I would say add four in fact you know what I'm going to do instead of adding values like this there's another another way you is using which you can add values in the list uh which is there's a class called arrays and arrays dot as list and you can simply pass the values here instead of manually typing the values even this works so as list is a method which gives you a list and it belongs to a class called arrays okay this is much simpler right now once you got this list what if you want to print these values of course I can say s out and I can say nums and if by doing this we can simply print all the values and you can see we got all the values now what if I want to perform some operation on this okay now what kind of operation I'm talking about so let's say I want to uh double all these values and in fact I want to First apply the filter here so let's say I don't want odd numbers I only want to focus on even numbers example we have 4 and then we have two ignore this dot dot a basically it represents an array so we got this 4 and 2 I want to filter them and whatever filter of whatever value you get after filtering I want to double those values so example 4 and 2 becomes 8 and 4 and at the end I want to reduce it so I want to say okay 8 plus 4 is 12. so I want the output to be 12. the way you can do that first of all I want to apply the filter right so we can use a for Loop and we can go from the first row to last value so I can say int n colon from nums and once you get this value I want to apply the filter because see I don't want to add all the values I want to add only the odd numbers and that to have to doubling it I want to add even numbers basically so we can check if my n mod 2 is equal to equal to 0 that means that's a even number if that's a even number I will first of all double the values I will say n is equal to n into 2. so doubling done and then once you have doubled it I want to add it into a to a sum let's say I will create a in some variable the initial value is zero and whatever doubling you get you simply say sum is equal to sum plus n okay uh so by doing this what I'm doing is from all the values which we have here let's say we have some more values let's say 6 as well and let me add one more value here which is three so you can see we have so many values but I want to first of all filter only even numbers the example I want to fetch this 4 uh then just 2 then this 6 I want to Double H value so example 8 plus 4 becomes 8 2 becomes 4 and 6 becomes twelve so this is 8 plus 4 is your 12 12 plus 12 is 24. I want the output to be 24 and let's see do we really get 24 here let me print the value for sum compile and run and you can see we got 24. now this is one way of Performing the operation on your list now sometimes you perform such an easy operation and sometime maybe you do something by using which the values in fact the original values gets changed so instead of doing those things uh we got this amazing way of doing this calculation which is called a stream API now it was introduced in uh in later version before this people used to work with this way but in in stream we got a different way of doing this okay now basically we got this new thing which is called Stream So if I jump to stream which belongs to java.utl.stream package and if I click on stream you can see stream frame is an interface and it provides you multiple methods like we got filter that makes sense right if you want to apply the filter on the stream or the collection we can use filter if you want to change a value example we have doubled the values right that can be done with the help of map and we'll individually see this methods later so if you go down you have sorted in fact there is also a method called reduce we are going to use that as well as well okay we'll see that later so basically stream provides a lot of methods to work with this now how exactly do you stream that we'll see in the next video but before that I want to show you one thing select so let's say we got we were able to do this right at this point I will just determine this section because we'll talk about that later I want to do one more thing before we start with stream which is how do I print all these values a simple splinting so we can use a normal for Loop right the normal for Loop Works in this way we can use int I is equal to 0 and int are a less than uh we have to get the nums dot length or size yeah the method name is size and then we have to say I plus plus so we have basically we have to use normal for Loop and then we can print each value I can say num start get I can pass the index value and if I clear this compile and run and you can see we got all the values here okay there's one way I can comment this section the another way of doing this is using a normal for loop I can say for int n is equal to num and here I can simply print the value of num even this works right now you tell me which is better using a normal for Loop or the enhanced for Loop see normal photo what it does is it basically goes to the loop oh it basically goes to the list by saying Hey I want to fetch the first element I want to fetch this second element right on the other hand if we talk about the enhanced for Loop it says the array itself will give you the value and that value goes into n right now again this 2 looks good but then we have a better way of doing it so I can just comment this section the better way is very simple what you can do is you can say nums dot now we got a new method called for each method Now using this for H it will give you one value at a time you can save that in n and then you can simply come back here and you can say I want to print this n here so for each is a part of the list interface you can simply say for each and then it will give you one value and you can do whatever you want to do with that value so I'm just printing it here in here and if I compile this and run you can see we got the same output so we have three options now we can use a normal for Loop we can use the enhanced for Loop or we can use a for each method so let's understand what this for H and what is this statement inside this this looks weird right yeah I know we have worked with Lambda expression it looks like Lambda expression but what is happening behind the scene to understand that what I will do is I will just remove the entire formula we don't need that and let me just push this down this will be required later once we talk about how do we stream API but at this point let's only focus on for each year so what I will do is first of all I will remove this section or maybe I can just comment it so that we can use we can see this later here at this point let's try to use nums so when I say nums dot you can see there is a method called for each now for each method if you see and if you go to this definition for each basically got introduced in 1.8 okay and what it basically does is it takes an object of consumer you can see for each method okay let me just go to definition itself so you can see for each method takes an object of a consumer okay that sounds weird right let's see what this consumer is so if I want to work with foliage I need to create object of consumer okay so the moment you say you have to get object I can simply say consumer and you can see consumer is a interface which belongs to our package java.util.function okay that's new and this is a interface which is a functional interface that means Lambda expression will be applicable and the only method we have to work with this is accept okay that means I have to create object of consumer which takes the type of integer because if you see the consumer interface you have to specify type as well so I would say consumer integer and then I will come back here let me say con is equal to how do I create an object of it it's very simple you can say new consumer now since this is the interface we have to define the class which is anonymous class okay that is also simple the only thing is we have to define a method also the method name is public void is it white let me just check once consumer has a method called accept which is of type void okay which returns a void and the method name is except I will say that and then it takes the integer value I mean whatever type you mentioned I will say integer let's say n okay now the thing is once you got this consumer object you can simply pass an object in this I can simply say con so for each needs an object and we are passing the object of consumer our job is done right the thing is when you talk about for each what it does is it gives you one value at a time okay now whatever value you are getting here or for it is giving you that goes into this consumer object and the consumer object has only one method which is except okay now this except will accept a number that's right the forage will give you a number right that goes into this n now you can decide what you want to do this with n okay I would say I want to just I just want to print it I mean that's your choice you can do whatever you want to do with this n at this point I'm just printing it okay and now let's see if this works I will just go back and clear the screen compile run and you can say it's working you you are able to print all the values okay and you can see the entire code all the other codes are commented so they are not running this code is running and it is printing all the values makes sense now we also know that the consumer interface is a functional interface which means we can use Lambda expression here that also means from here to here we can delete this part let's delete that let's delete this curly brackets and I hope till this point you have you've got a Mastery in Lambda expression and I can give a arrow here that looks good and since we only have one statement you don't need to put curly brackets I will remove that as well and I can write everything in one line that looks cool right and then we don't even have to mention the integer here and we don't even have to mention a bracket when you have only one variable makes sense right let's see if this works after doing all those changes compile and run there's no problem now what we are doing is we are assigning this expression to con right now whenever we have con we can actually use this so instead of using con here I can simply use this particular code and replace this Con and that's it we don't even have to create a reference for the consumer and this is what we have seen earlier right if you compare the Urgent example this looks similar in fact exact same so that's how your for each works it gives you the value that is your n and you can do whatever you want to do with that and you can print it you can add it you can save it in database that's your choice okay so that's how basically we use a forage now once you understand forage let's go for the actual stream API so basically we were we were having this collection or at least right where do we have values and then we were able to print that with the help of for each and now we know what is happening behind the scene right what if you want to do this with a stream API now let me just give you the introduction of stream API here and how it looks like so if we talk about stream stream is an interface right so if you can see uh we have seen that before as well stream is basically an interface right the thing is if I go back to nums and if I say dot you can see now in the list we got this new method called stream again before 1.8 the method was not there it was introduced later but we got this method called strain now what this train does is it gives you or it Returns the object of stream okay so that means if I want to accept the values I can say stream of integers and let me just name this as S1 so this is our stream which is called S1 here and we got all the values so whatever values you have in this list is available in this S1 now with this stream you can perform any operation and the beauty is it will not going to affect the nums so whatever changes you make to this stream if you change the values if you double the values it will not going it is not going to affect nums now why this is important see sometimes when you have a data and then if you want to perform some operation it's not like always you want to change the existing values remember when we talked about threads when you have multiple threads and then you are performing some operation mutation it's not good right so whatever changes you want to do do that in this stream and use it at least you have your original values with you okay now that means I can also print the entire thing with the help of for each I can say S1 dot for each mind you again in this stream of S1 we got all the values which are there in the list and let me describe that works I will say compile and run and you can see we got the values here now one of the thing about stream is once you use the stream you can't reuse it and that's why the name is stream example if you talk about the flow of water the stream right once example if you let's say if you're sitting near the river or the bank of river basically if the water passes you can't retouch the same water in the same way once you have worked with a stream you can't reuse it example let's say if I try to print this multiple times let's say two times it will not give an issue with the list okay you can simply say nums dot forage multiple times there's no issue but if you try to do that with the stream once again you can see you will get an error compile time there is no issue run time it will work once the second time it will say uh the stream has already been operated a pound or close that means you can use stream only once okay uh that makes sense so we can't reuse it but then what is the benefit of using stream here the benefit is stream provides you a lot of methods to work with example if I say S1 dot uh we have methods example we have methods like filter remember when we talked about the other example where we were removing all the odd values or we wanted to only get even even numbers we can do that in stream as well now how it looks like I can simply say S1 dot we can use a method called filter okay now what this filter will do is filter will say okay I want to apply the filter here but give me a condition so for any particular value let's say n what you want to do with this so I can say I can use a Lambda expression I can say n mod 2 equal to equal zeros I only want a even number here so this filter and if you jump to this filter you can see filter returns a stream so what we are doing is when we say S1 dot filter it will give you basically a new stream to work with let's say S2 again as I mentioned before S1 is used right so when I say S1 dot filter your S1 has been used now now you got a new stream now this S2 basically will have only the even numbers let me prove that let me print S2 and let's see what values you get here so I will say compile and run you can see we only got even numbers now say what we are doing here will have a separate video on this how Filter Works but the idea is the filter is filtering the value based on the even or odd you can specify whatever logic you want to but at this point I'm just specifying a logic of a condition of even right now once you've got this stream I can create another stream where I'm doubling the value I can say stream and I can say integer let's say S3 because it will give you a new stream I can do that operation on the new stream which is S2 on S2 basically I'm trying to double the value right so you can do that with map in this map for whatever value you get uh you can perform some operation on that at this point I'm just doubling the value right as I mentioned you can use any logic at this point I'm just doubling it and now let's try to see how this stream works I'm saying S3 now because you can't use S2 S2 is done and if I compile and run you can see we got double the values we got 8 4 and 12. so yeah stream provides you this amazing methods on which you can perform the operation on one stream it will give you a new stream you can perform the operation on the other stream it will give you a new stream uh in fact you can also use some methods which will not give you a stream but a last output example let's say if I say S3 dot there's a method called reduce now in this reduce if you say 0 comma again I will explain this logic later what I've did but let's say if I try to do this if I say C comma e c plus e now what this values will do it again don't focus on the logic now just focus on my reduce this radius will not give me a new stream this radius will give me a value of type whatever type you mentioned with the integer with the stream which is integer so it will give me uh into values I can say in result equal to S3 dot reduce and instead of using a forage now why we can't use forage because reduce will give you a single value which is there in the result I can simply come back and I can print the result and I can just compile this code and run and we can see we got 24. again let me repeat just ignore what is happening inside we'll talk about this in the next video what is happening inside this idea is stream makes your work Easy by having this functions if you have a function like filter map reduce now this I don't know if you know about this concept of Big Data example in this world we are getting so much of data right now if you want to use all this data first of all you have to apply the filter on it there are some wanted data there are some unwanted data so you can apply the filter once you up once you apply the filter the data which you got you can transform it something meaningful you can do that with the help of map and then you can make a graph out of it right with the help of something called reduce and then we were able to print the result let me just see if that works compile no issue run and if you call 24 and this is remember we have used a for Loop which was giving 24. the same thing we have done with the help of stream now the beauty is uh you can actually write everything in one line let me show you something I will just comment this entire section and here what I can do is I can say nums dot I can say stream it will give me a stream of values on the same stream I can say dot I can apply a filter and this filter can be uh we can very logic as I mentioned we are going for even numbers zero and on the same stream because see nums dot stream num Sita at least right it the stream method will give you the stream on that stream we are applying a filter again we we will get a new stream and on the new stream we are applying a map which is uh n into two and then this stream will give you a new stream on which you are applying reduce where we are saying 0 comma C plus e or C comma e which will be giving me c plus e and that's it this radius will give you one value right and that one value you can store in a result so this nums dot stream let me just give a tab here yeah so this basically nums dot stream will give you a stream on that stream you are playing a filter on that stream it we are playing a map on the stream you are applying a reduce and then you got one value at a time and this looks much better than the for loop I know for the newcomers this will be difficult but once you get used to it you'll always prefer to use stream okay and uh one of my favorite uh trainer omenta says venkat subramanyam says it's not difficult it's just unfamiliar okay so uh this is how it works and you can compare with the for Loop and this looks much clearer in fact it's much more readable right on a stream you are applying a filter on that filter applied you are applying a map and then you are reducing now let's try to understand what is happening here when you talk about filter what it does when you talk about map what it does we understood what it does in the forage it takes the object of consumer but I'm not sure what it will do here so what I will do is just simplify the code I will remove everything and also let me remove all this thing so that it will look cool you know simple code okay so let's go step by step if we talk about this filter and if I say okay let me just go to filter and you can see filter needs an object you can see there's a parameter it needs an object of a predicate now what is predicate I will just go back here and if I type indicate here so if I say predicate and you can see it belongs to a package java.ital.function and if I click on predicate here predicate is a interface which has a method called test it returns you a Boolean value okay now based on this Boolean value it will say okay it can be either true or false if from a stream you know from a stream when you apply a filter for a particular value if this if this is true it will go ahead otherwise it will stop it's something like applying a filter on a river so let's say if you have a reverse flowing and then you have apply a filter there and if you want only the pure water to pass all the garbage will be stuck somewhere you can do that with the help of a filter I hope that makes sense right let's Implement predicate here so of course pretty ticket needs a type you'd want to work with and then let me say this is a p is equal to and I will say new predicate now this predicates the interface so we have to of course Define it but what method it has so if I go back and if I say Quick Fix add the implementation method so the method name is test okay remove that so method name is test and it takes the integer value and based on some condition which you can specify you can specify is it true or false so what I can do here is I can check if the value t or whatever value you want to have let's say the value is n naught I don't like DQ T later let's go with n so if n is mod and mod 2 is equal to 0 in that case return true I can give a tab and I can say else return false our problem solved right okay so if it is true if the value of n is even it is true then if this is true I will return true or I will turn false and now what I can do is instead of this condition here I can put P right even that should work let's try compile and run and you can say it was it is working so basically this filter needs the object of predicate which specifies the logic of when to pass the value and when to stop the value now if I try to reduce this code don't you think this condition here itself returns true then why to write such a big line we can actually return n mod 2 equal to 0 because ultimately we need a Boolean value right it can be true or false so we can do this also the predicate itself is a functional interface that means we can use Lambda expression here so we can remove from here to here we can remove the entire part we don't even need to specify the type and we don't need this curly bracket we can put a arrow here and then since we only have one statement you don't even need curly brackets you don't even need a new key written keyword we can write everything in one line and since we have only one parameter we can remove this one and you can see the p is equal to this expression and whenever you have the expression or whenever you have P you can write this expression and this is what we have earlier right so now we know how Filter Works let's see how Map works if I go to map map needs object of a function okay what this function is Now function is this functional interface which takes two types okay makes sense and there's a method called apply okay so what I can do is I can have the object of map or not the map the object of function now this function takes two types one it is what it accepts and what it returns and say let's say fun because that is fun a new function and again this is a function interface which has only one method let me just add that here so this is a method which we want to override which is apply now what I want to do whatever value you get let's say instead of uh and a TV let's say n whatever you want to do with this n it will just apply that value so let's say for this n what I want to do is I want to return n into 2. so whatever value I'm getting I just want to double it again as I mentioned logic if anything but I'm doubling it here okay makes sense and we also know that this is a functional interface okay first of all let me take this fun from here and put it here so we can say fun and let's see if that works I will say compile and run and you can see we got 24 it works what we can also do is from here to here this is a function interface so we can remove this part and we can use Lambda here I'll just do that quickly now we have mastered Lambda and we have only one statement which is written so we can remove that we can remove this curly brackets we can remove the same integer we can remove this package and this is what we got and now we know wherever you have fun you can write this and this is what we had before so we don't even need this right the same thing can be done for reduce if I click on reduce you can see reduce takes two parameters one is the type and second is the operation okay now what is this binary operator so binary operator basically is it extends by function by function as a method called apply okay let me simplify this for you I know this is a lot of implementation there let me simplify this thing now when I talk about videos how do you reduce a value example let's say if I ask you to add three numbers let's say one two three how will you add them now in your mind you are doing this so if I say one two three and let's say four also how will you add them of course you cannot add all the ways at the same time you add two values at the same time so first of all you will add one and two which will give you three then you will add 3 and 3 which will give you six and then you will add 6 and 4 which will give you ten right so you add two values at a time or maybe if you want to simplify this you can start with zero so you can say 0 plus 1 is 1 then 1 plus 2 is 3. okay so you have to start with the initial value remember when we created int sum we have assigned the value to of sum is 0 that is just that is this zero and every time you will simply add two values so if I if I want to add two values can I say this is c and e the carry because see this one is a carry here right this three is a carry this 6 is a carry this ten is a 10 is the output of course but this one three and six they are carry which can be represented with the help of C and then we have this e which is the element right c and e and what you're doing is you just have to say c plus e and that's what we have done here the zero is the initial value what you want to work with this value and then the c and e are the two variables which is going to work with this thing and the C plus e is the operation is that simple okay of course you can do this with the implementation but I think you got the point right so stream is cool right it works with we can apply multiple functions here in fact let me show you one more thing what if I don't want a int result in between I want a stream itself but I want this stream to be sorted so I will say integer and I will say sorted values but I don't want to apply reduce because it just will give you one value I only want to filter them based on the condition and then I want to say sort that's it the moment you say sorted you will get a sorted stream and maybe you can also print this values directly instead of using a foliage let's try that compile and run oh it's you have we have to use for it okay so I will say sorted values dot for each and you can say whatever value you get just print it okay let's try compile and run and you can see we got the value inserted format so basically we have multiple methods to work with in fact there is one more thing what if you want to do the filtering but with multiple threads but if I you have only one thread right what if you want to use multiple threads in that case there is another function called parallel stream which will create multiple threads for you to make this faster the only thing is when you say sort parallel stream doesn't make sense because sorting will need all the elements together uh so let's not don't use parallel stream when you want to sort but yes if you want to filter parallel stream works okay so yeah that's how we use stream API and I hope you got the idea about map filter and reduce