[Music] welcome back aliens my name is D ready and in this video we're going to talk about code Java in this particular course which is a lendy video you're going to learn everything from the basics of code Java to the end of code 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 getting 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 lendy 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 run running on jvm so even if you are working on some other language let's say cotlin Scala groovy ultimately you working on Java technology and that's why if you learn code Java it is much easier for you to jump to different languages and I've have also observed irrespective of which interv you're going for it can be for development or testing and doesn't matter which language you're working on there maybe python C C++ 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 ready I'm a corporate trainer software developer and currently focusing more on blockchain but Java is my first love so I'm working on Java from 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 sometime I have to watch my own videos to revise the concept but the 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 have worked on 12 different languages you can name it maybe C++ C python Java JavaScript uh go language so I love most of the languages some languages I learned just for the projects but then when I was learning those languages I was I was feeling hey it's it's it's so easy it's because I have learned Java yes people say Java is difficult yes to start with it is difficult maybe if you want to learn Java as your first language it kind of difficult because if you're coming from other language 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 V which is right once Run Anywhere it 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 son so now the treadmark of java belongs to Oracle 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 in every every 6 months not the major updates but they are working hard to make Java as the most powerful language so I hope you are excited for the entire course we are going to start from the basic as I mentioned and you will learn a lot of different 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 are 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 can use wordpad or you can use notepad++ 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 where 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 IDE you want to use in this course we are going to use use VSS code now there are multiple options available we have Eclipse intellig idea we have net beans in fact I have worked on a lot on net beans at the start of my career I have worked on Eclipse as well for some few years and then I have worked on intelligent idea for 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 Cod is because it's a very lightweight Ed 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 jdk first in jdk we have two versions We have a propriety jdk of Oracle 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 coretto we have different implementation you can just explore them but I'm going to use Oracle jdk here yes at one point Oracle made 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 6 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 it respect you if you 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 there will be Min TX in the new updates new featur features but uh overall it will be same in case if there's something new happening which is changing the basic concept uh we are going to re-record that video in the in the series okay now which one version to choose here should we go for Java 19 or Java 17 the thing is with every 6 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 LDS so if I click on Java 17 here and you can depend upon which OS you're using I'm using Windows here for the KJ 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 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 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 using you can download one I'm using Windows here I will click on download and yeah it's starting starting soon and you can see it's 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 these two softwares so the first screen might might look different for both of us you can see for me 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 next everything is smooth and depending upon your machine speed it will change uh the speed might change okay uh that's jdk install but how do you verify if you have JK okay we'll do that in sometime 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 uh nothing to specify just by defa it will be there so just keep it 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 jdk is installed in fact you have to check one more thing which is Java C version for the compiler and you can see this getting giving me 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 in system variables if you want this to be applied toout the system 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 path variable I will not create new if you don't have a path variable Cate click on okay 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 okay for me it's already there so I will just remove this and click on okay okay 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 uh 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'll 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 uh dark high contrast or light high contrast I would say go for dark because it's good for development uh it will not strain your 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 folders 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 Ides 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 EXT exensions which we are going to do later 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 of course we know what to why to run right but why do we deug 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 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 we 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 do 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 uh inside desktop I want I want to create a folder here we'll say this is we'll have a short name we'll say 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 in Mobile sometimes you will be seeing a font size and a small font okay I don't want to welcome I 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 you are going to write our first code right so we have already seen how to we install jdk how do we uh 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 uh if you're using uh Windows you'll be having an option of command promp uh 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 we'll 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 uh so we'll use that and we'll also check if my ja Java C is working you can see Java C 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. Java now there 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 Java now different languages have different extension when you talk about C we say C when you say C++ language we say CPP when you say JavaScript we use JS in the same way in Java we say 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 extension 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 we also have int code for AI assistants 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 the scene 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 do you want to do in this code do you want to uh create a game or do you want to build a rocket which will go to mass it 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 right 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 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 uh if you want to test some code or stuff we have an option which was introduced in Java 9 so basically basically you can write your code in one line you can get the output example uh so even before we jump to hello world let me show you something so we have a we have a concept of jshell 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 jshell or I will type jell and it will give you a jshell 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 JL is if you want to experement something example why do we write code why do we build application 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 2 + 3 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 - 6 and you can see we got three so this is working but what if we 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 and what the definition is just remember that there's 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 inbu method in Java so you don't 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 bracket 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 mention hey something is getting ended so we got a semicolon there and now what you can do is uh let's try to run this code now errors right errors are very important in your life uh in your programming life basically see you got the error it says cannot find a simple call Print it's not working okay cool no problem so let me just clear the screen clear compan is not working ignore that we'll say so let me open JL 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 six at the and 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.out.print see there will be a point where you I will 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 uh 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 see we got six 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 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 codes 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 codes the way I've done it here so in this double codes 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 codes no ready telescope 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 jshell but will it work in this if I try to copy this in fact why don't copy when we can type it so let's type system.out.println and here we'll say hello world and let's give a semicolon at the end it was working on jshell but when you work with Java files because this is how you build a project right J is not to build projects it's just for the experiment if you are learning a language is good but if you want to make it work you can do if you want to build a big application say 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 a 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 co you can see that this is the 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 jell so you can if you want to do something with jell 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 jell because jell says you just pass a 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 we can write this code so in your machine you will be having something called jvm 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 Java 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 works on which environment so underlying we have your OS so this is your OS and then your OS is working on a hardware right so you'll be having an Hardware it can be a laptop it can be a desktop it can be a phone doesn't matter you have an 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 jvm 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 U and we don't even do that right we don't even write a code which directly works on the hardware and we don't even write a 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 jvm will not understand what you're talking about and one more one more thing we have talked about right Java is platform indep dependent 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 particular OS and let's say if it is Windows in my 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 bite code so you have to convert your code into a unreadable format for a normal human being in and this is called a bite code because jvm only understands bite code but can you write bite code of course not you only understands English type of languages right so whatever we have return the code we have return system.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 more of readable this is not something which you can read so basically you have to convert this into a Java code or bite 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 bite code and that bite code goes on the jvm it will run okay makes sense right so that means when you write something it will go into bite code and then bite 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 jvm will start with the first file and then of course that file will depend upon other 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 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 it's not like you have to follow a sequence create this file then that 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 mean it is looking for a specific signature okay so when I say signature it means the entire method signature uh the method name what are the values you are passing and all the stuff and of course all this ter 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 Main and Main has a particular signature again what each now there's a space in between here if you can see this is a space uh I have let left that space intentionally because we'll add certain things there so at this point just remember there's a space here there's there's a box meing 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 jvm will ask you for main so we have to put that into main so so what the 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 uh and in this we'll say string a and that's it this is a synx you have to follow now there's 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 a 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 Java say 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 name same and we'll say Open Bracket and close and we'll give a ination so basically you have to follow this thing always give a tab so every time you open a cly B give a tab it looks good 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 x and the suggestions uh these are the extra suggestions coming from your vs code but you know that part in fact is it because happening of this extension let me just remove this so let's not use that extension because it will give you suggestion it's good for development but not for teaching purpose so I did ask 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 create the screen and compile you can see there's 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 bite code remember we have talked about a bite 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 bite code and the extension for the bite code is do class file the extension for Java code is do 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 see we got hello world so yeah that's how that's how it works so simple stuff that's how 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 p 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 your cooking something in a kitchen right so where do we 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 you 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 you need 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 run 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 J 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 run 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 develop ERS inside which you'll be having something called J and in this you'll be having jvm so as a developer you'll be installing jdk jdk will have j j will have jvm but to run something on any other machine let's say if you have made 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 have write on your machine you can run it on any other machine provided you have J and jvm installed so that's how your Java Works 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 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 bite code which will be having do class file and then you can run the do class file in your jvm basically it's a part of JRE which will have jvm as well as libraries lot of classes and then everything is a part of a JRE that JRE you'll 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 a j 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 jdk 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 8 years or 5 years you will be having new version of java of course next 5 years you will get 10 new versions and every new version will have minor updates but the base Thing Remains same right uh so whatever you're learning is actually from java 1 itself the system do do do printland is same in Java 1 so don't worry about the new updates the code concept will remain same okay cool now once you have uh 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 some buy something so you got Amazon which delivers everything at home so you got Amazon as online service you wanted 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 as processing on it so whatever whenever we build a project or when we build an 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'll 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 send as data we will apply a processing on that so we will do some work in between and 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 means 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 Del 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 when you say 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 say I want to store a 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 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 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 U 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 naven and it will have a variable name so maybe I can say this is user so user is naven so basically what you do is you create a box and inside the inside the box you'll be having values now this box are actually called variables so we can name name this box as a variable okay and so what we got till now we got that a box will be called a variable and you'll be having a name to it you'll 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 box 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 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 a number this is also a number but in number as well we have a category inside it so one of the category I will mention is this we can if you want to store numbers we can store that in something called int now int means integer okay so we can store data in the integer format but we cannot store 6.5 in int because 6.5 is a real number so int will accept numbers but why it is called int we not natural numbers or whole numbers is because maybe you want to store negative values as well so remember your Math's concept so we have negative numbers to positive values so everything will be considered as int so we can store minus 5 + 5 that's your choice okay so this makes sense so 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 Valu so that I can do some processing on it now what kind of processing I want to do maybe I can I want to add two numbers maybe 2+ 2 or 5+ 6 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.out.print whatever operation you want to perform you want to say 3 + 5 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 comp compile it first so the first step is compilation and then running and you can see we got the output which is eight just ignore this percentage symbol as of now uh actually there's one more thing I 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 operations so you can write multiple statement for different per operation you want to perform so you can say 8 + 7 so we are doing addition here and then 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 or maybe I can print 15 on new line so what you can do is instead of using print you can actually use something called print Len now print Len means print new line so it will print and it will go on new line so we can do that here as well so just say Len Len at the End Print Len which is print new line and now compile run and you can see we got the value we got five and 15 so from now we'll not be using print we'll be using Ln print Ln okay so again print Ln 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 of course we need box right so I can say num one I can as I mentioned I can use any variable name so let's say num one 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 we have to mention the variable name and then we have to mention the value for for it and at the end of course you can put a semicolon so we can see we got num equal to three and we got a 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 end so it's good to put semic at the end and anyway Java will give you eror if you don't do that next thing we 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 assign 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 the value to the left hand side okay remember this equal to we take the value from the right hand side it will assign the value to 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 can I create more variables we can actually so let me just rename the Val variable from num to num one and here as well I will print num one okay and then I will create one more variable which is num two is equal to so every time you want to get a variable just mention the type which is int of whatever type of variable you're creating at this point we are working with numbers so we'll say int num to equal to we can assign the value which is five so we got three and five here what you can do is you can actually perform the operation here itself so you can say num 1 plus num two 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 num one plus num two 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 the scene it is consuming a lot of memory uh but it's working right as of 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 will use less space right at this point it works so we can say num one plus so whatever oper you want to perform you can do that here and instead of printing and addition 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're 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 num one we have to also mention the type of it at this point this is int and we have to assign the value for it in in the upcoming videos we'll talk about how can you uh create a variable which will be having a name with 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 do we create a variable with the help of the data type and the value as well now what are the other option we have so we have worked with int right do we have other options so B 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 we'll keep it question mark okay at this point we are not discussing about the second one the first one is primitive so basically in Java we have lot of inbu 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 whatever data you normally have normally we work with numbers we work with uh Point values we work with characters we work with true or false now for that we have this categor 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 buan 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're starting with negative value so that goes into integer whenever you have Point values 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 bite we have short we have int and we have long for float we have another option which is of double and then of course we have float and then car is characters are characters so in 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 variable this time let's say float uh 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 so we can simply say float and then uh we can have a value let's say seven name to it right uh 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 P values will be having a DOT right so we can say 6.5 something like this uh so float marks equal to 6.5 that makes sense right okay cool uh in the same way we have also mentioned we have sub typ to integer we can have a bite 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 integer 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 int 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 and I of course if a value goes beyond that range uh we have to go for a long format now long supports eight bytes now of course when you have more bytes it will 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 in provides you that range but then then what if you want to save memory you don't want to go for in 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 this scen and then we have the next range which is of bite now bite is only one bite okay so 8 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 uh it goes from negative values right so it isus 2 2 to 8 2 + 2 to 8 - 1 okay so which is 2 to 8 is what it is oh we starting with negative value right so it is 2 to7 and 2 to 7 and the range so how do we calculate 2 7 is 128 so it starts from- 128 to + 127 so 128 -1 127 that's how you can calculate the range for all the 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 will have a value let's say five or six or maximum 127 you can go for bite now how do we create that variable it's very simple you use a type which is bite 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 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 CV by default we 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 8 bytes now what do 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 5 6 7 8 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 would 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 or 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 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 in other languages like C you have only one bite it's because it normally occupies the integer range it normally occupies the character range asky 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 uni code not asky by default it will support uni code 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 C 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 now double quotes is for string right for character we have to use single quote and you have to put your character into single code and remember it will only accept a single character and next we have of type Boolean so if you want to work with bullan now bullan 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 Zer and one it is true and false so it's not like true is one and false is zero it's only true and false it is normally used for conditions if you are want to check for a condition based on that you will do something uh that's why we can use true 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 bullan 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 sem single quote or 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 bite is done let's create some variables with different type so int we have already seen so we will say num one this is how you assign integer value but if you want to go with a B so you can say bite b or we'll say b y equal to now can I assign a value which is 129 you can see 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 in to bite so this is int which is 129 it will if you try to save that in a bite it will not work but what if you say 127 that's the last value you can store compile okay there is a problem with result let me just remove this section and compile no issue okay so this is how you save bite value what if you want to store short so you can say short sh equal to and uh you can assign value which is let's say 558 any 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 eror it says again possible ly conversion from double to float because by default 5.8 is double if you want to mention here 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 car c is equal to in single code you can mention a character K uh then we can go for Bullion which is bullion 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 don't expect anything on the console compile or there's an error it says cannot find symbol bull okay my bad it is bullion the only problem is when you work with multiple language at the same time you get confused somewhere so that that's bullan not bull so in the notes as well I need to change it so I will say this is bullan 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 uh data types we can work with and all these are in build apart from this we Al we also have some more data types which we'll see later they are not type of primitive but this is PRI 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 byard them you will get used to it the more practice you do with this varibles 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 an error we can't even put double quotes there it will give you ER as well and you can see you can't even put double Cote so you have to put single code because that's how that what character represents and the character can be number as well so you can put a character here which is eight which is a character itself right are also character no error 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 nine or this eight these things are actually called literals because they are 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 we'll come back to this so we are basically working with literals now okay now what kind ofs we can have 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 so I will say 0 B 101 so basically if you want to put a b number like one1 which is five so you can put B and then you can put a zero let me just try if this works I will say system do do do print and then let me print number one let's see if this works put a semicolon at the end compile there's 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 7 e so e also supported right so let me just try compile run and you can see we got 126 I don't even know 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 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 compile run and you can see we got the output of course in the output it will give you normal number but as a programmer it helps you for the number of zeros you entering there so these are basically literals right uh let's try with float this time so let's say this float and we are saying num one 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 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 this assigning one Zer here so this is basically 12 into 10 10 or 12 into 10 into 10 10 and then it will adjust it according to its own uh way and we have one we have discussed about the Boolean it is only true and false there's 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 bullan remember this okay how about character let's try uh so when I say character let's say C and I want to assign a character here which is 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 so 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 Val value it's a five 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 you can see that it is getting compiled and if I try to run this code we got a because we are printing it there my bad I would just just have to put that here so first we have to increment the value then we can print it so C++ simply means we are changing the value okay this is 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 cre a variable for numbers you will say int float double or long in fact we have sub types as as well for INT and then if you want to store a character we can use G if you want to store a Bullen value we have bull option and then we also have a string which we'll talk about that later at this point we have lot of different types now there's one one confusion what if you want to change the type of a variable uh can we really do that so unfortunately we can't we can't change the type of a variable but then can you assign a value to the type example let's say uh we have a variable which is of type bite so I'm using a bite variable which is B and it will have a value now of course we have talked about a range of byte as well you can have maximum 127 plus I mean plus 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 bite 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're 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 bite has a small range so your compiler says hey you're trying to assign a integer value to bite and at this point you will lose you might lose some data so basically we are trying to convert it and we 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 bite is small and integer is a bigger one so of course you can put a a small item 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 bite 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 compile will say hey I 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 bite so in this case you can say hey compiler give me one second here what uh 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 bite and you can mention the variable name a so what you're doing is you're converting integer value into the bite format okay now this concept 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're trying to store a bite value into a integer variable so it becomes an integer when you assign it right so this is a conversion okay so when you do it explicitly that is casting when you is opening if it is happening automatically that is conversion now this makes sense right now is it means you can do it with anything uh not exactly there are certain types example you can't store a character value into a Boolean value okay because Boolean only supports true and 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 bite if it is long as well that completely works now what if you have a scenario where you want to store maybe of type uh a float value now let's say you have a 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 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 even this will not work so what you have to do is you have to do 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 = 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 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 it example the value of 5 it is 5.6 so here when you convert that it becomes five 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 bite now what will happen when you talk about bite here what do you think we don't have Point values so if you have a bigger number uh 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 a modular operator so what modular will do is it will divide this number so whenever we try to assign a integer value to bite which which we are doing here so when you're trying to assign a integer value to a bite 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 the complete range of bite is 256 right from - 128 to + 1227 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 it will be one I will show you that in the code so coming back to the code let's try all these operation which we talked about so what we'll do is we'll start with a integer value we can say we or maybe we can start with bite so we can say bite 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.out.println and here let's print the value of B so when you save that and when you go back here when you when you write run this so we say Java C and hello do Java compilation 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 uh 257 it will not work first of all it will say error you can see we got the eror it says incompatible types because it is integer type now 257 okay so we can say 27 here now next what I want 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 bite value to integer because this 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 we can actually do in single step again not recommend it for a big project or not even for uh a small project don't don't even try that in your company uh for learning purpose we are doing it so basically we got a shortcut we can say Javas space hello. Java directly maybe it is getting influenced by other languages where you simply mention the command and a file name no need to mention compile and run sub step let's try this and you can see it works so you can see we got one 27 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 for whenever you have single file will use this but remember this is just a shortcut I'll timately 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 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 uh if you have a older version of java this might not work so if it's not working in your machine that means you have a 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 int K and now you are trying to assign uh or maybe let's let's get a bite value by K is equal to a and let's come in 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 the I mean the type of a is integer right you can't assign a integer to a a bite 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 bite so we are doing a type casting here so when you run this code now it will work you can see 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 bite 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 bite 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 create a float variable you have to put at 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 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're trying to run this code sorry 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 6 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 casting 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 bite value and the value of a bite let's say b or maybe let's say a and the value is 10 here okay and then we got a bite which is B is equal to 20 and then when you perform the operation maybe maybe this should be 120 right 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 was what 300 right so when you say 300 here it is going out of bite 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 bite so when you say bite multiply by bite it should be bite right but the problem is this value will go out of range so 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 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 why you can perform operations on 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 you let's go with integer num one 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 num two is five 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 do out. print Len and here we can print the result and just to see if it is working and as 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 Javas space file name with with with do Java say enter and we got 12 so when you say 7 + 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 see we got two so it is working so we can add two numbers we can subtract two numbers how about multiplication we can do that so we can say star num one into num two come back here run 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 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 a slash so yeah we got uh Slash let's try and you can see we got got one that's right one is a quent here right so when you divide 7 by 5 you get two things one is a quent which is one and then you will also get a remainder now in this case when you use slash you will only get a quent not a remainder 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 it what it will give you is only a remainder okay so if I run this code you can see we got two two is a 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 example 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 we do now is let me just remove this result for time bin or maybe I can just commment this part I also don't need this num to let me only focus on the first variable which is num one now let's say I want to uh add this num one by two okay so B basically the value of num one is seven I want to increment the value by two so what we can do is we can say num one is equal to so basically we have to use num one which is num 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 num one okay that looks cool right uh let's try this I will say num one print num one and let's run this code you can see we got nine so this is working so seven became nine so is working 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 we'll say num 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 Valu so have to say equal to two so what we're saying is we want to increment number one by two even this should work let's try a 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 for addition 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 got 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 56 we can divide as well and so you can use slash and all the operators which you have awesome so this is short hand 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 two right we can also add it by one so in this case if you say add it by one the value of num one is seven 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 num one ++ even this is possible let's remove the space in between so even this is possible you can say num one Plus+ which is incrementing so what we are doing here is something called increment and if you on this Cod you can see 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 we discuss that in sometime but this is similar and maybe we have done that before as well when we talked about characters you know we remember C++ we done that so yeah we are incrementing here 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 commment this part and here I will say num minus minus now why you have to comment this part I want to run this so we can say num minus minus let's run this code if see if this works and you can see we got six so that's how you can perform this Ed 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+ we can actually use something called let me just write it in comment so instead of saying num one Plus+ we can also say Plus+ num one you know just to simplify this example instead of saying num one everywhere let's say num so that maybe you will not get you will get confused by that one extra symbol which is one uh just ignore all this operation just focus on this part which is num and num 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+ we can also say plus plus num now what's the difference between this 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 two different Syntax for it why not just simply one uh 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 int and then I'm storing that in result and what I'm storing is num or maybe I will say 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 seven 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+ even this should be doing the same thing right let's try oh there's a Twist here so Plus+ num and num Plus+ are not same I mean it works same if they are 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 why this called pre-increment and why 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 operation 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 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 1 equal to num + 1 so what it will do is it will first fetch the value of num which is seven in this case and then it will increment the value of num and will make it eight okay but then what is assigned to result is seven itself and then you are printing that seven here okay but what will happen when you say let me just write it here so say end result is equal to 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 Plus+ num and num 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 Operators we have talked about arithmetic operators till this point so whatever operation we have done it is for the arithmetic purpose right addition subraction 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 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 we compare things right so it can be comparing two values and if you can check with the help of less than or you can check with the help of greater than you can also compare with two values right if they're 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 it 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 uh 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 five and you want to assign that to a particular variable uh let's say x so you say xal 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 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 uh so we have to use less than so we have to use less than equal to for this particular com 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 relational operator now basically when you compare these two value example let's say if I want to compare if 7 is less than 5 so this will return you 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 7 and five so the output for this will be of course false but if you say 5 is less than six 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 it so let's say we have this our we have our code here and in this what I 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 five 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 bullan uh I can say result is equal to now how do you store a value and 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.println uh 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 synex 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 six and the value for y is five so the first value is less is not less than second value and that's why we got false but if you say 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 six and six and now if you try to run this code of course compile and run 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 see 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 not so not equal to Y and with this if I try to compile and run you can see 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're 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 doesn't matter whatever programming you do see when you 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 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 9.8 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 issue 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 relational and uh relational 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 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 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 comination 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 uh 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 or so it can be and and or now what exactly it means see if you want that your output to be true so let's say if your 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 uh you can use something called a and operator we can use use OD operator or we can use a not operator which is which is with exclamation so the and operator in programming we represent that with the help of and and the or operator so we use erson for and and we we use a pipe for or so we can use erson which is and we can use pip operator 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 first operation gives you true the second operation also gives you true so maybe you can we can do that with the help of these two conditions if both are true then 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 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 when you want such type of output when both if both are true then only it should be true then we can use something called and and operation here but what if you have a different set of 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 sorry 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 true that's great but even even if you have one true that's true uh even if you have one true that's true even if you have one true that's true right uh but if if you have both false then it will be false now if you compare it with this and when you have both true it will give you true if any of them is false you can see if these are false you will get a false here so this is your and and or 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 have one more which we have not talked about which is exort again we'll talk about that in some time but then we have three basic operation 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 uh the other option you have is something called a short circuit so instead of using and you can use double and instead of using R you can use Double R and not is as as it is we don't have do kns there uh now what's the use of this so first of all let's talk about and so let's say if you have x 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 condition here and I want both them to be true that's why you want the output to be true so example if you have a bullan 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 or here so if I remove this and if I put up or 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 you're saving your time that's what it's called short circuit you are saving the time you're 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 for 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 erson so it can be you can say double erson for and double pipe for or and of course not will remain not so example let's say if you already have a bull 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 ampus in 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 hug you have a data and if you want to do something related to that data So based on the data you will change some you will do something you will change the condition all those stuff okay this thing looks cool here but how will you implement this in the the actual code that's what we're going to see now so if you can see uh I of course I I have to go over the same variables I can say int uh X is equal to let's say 7 and in fact you know um yeah let's let's do it on new 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 int B is equal to 9 I'm just getting random numbers here and what I want to do is I want to save the data in a bu variable so I will say buen 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 see 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 them to be true if I want to get the output as true so what you can do is you can use ampon double ampon 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 change your condition you should change your values but just we are learning into we're doing we are learning right so it's F it's fine now let's compile this code and run and you can see we got true since both are true it's true but what if you use a pipe here now pipe simply means or right so even if you are so let's say both the condition are true now and both the 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 see we have S is greater than five 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 or when you have multiple uh delation 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 uh compile and uh 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 uh 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 false it will give you true and and if I compile and run you can see we got true it's because this was false I one false 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 els 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 lot of different Technologies now we have ai ml 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 let let's say at this point so this is a flow of the code and at this point you decide okay uh 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 exec ution actually changes based on what you decide at that point and even if you think about Tesla cars which is uh maybe any driverless cars where 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 what if there's a object in front of a car at that point the car will stop right so this is a point where you decide hey it's time to increase the speed of a car so maybe you can say based on some condition let's increase the speed of a car or decrease the speed of a car 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 condition so here we'll be having a condition if the condition is true and of course we have talked about bull values we have seen relational operators logical operators if you get through you will execute this particular block here I'm inan 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 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 we're 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 bu based on some conditions so so I will simply say system.out.println I will print X here okay in fact I want to I don't I want to print hello and then I also want to print by so I can just reuse the code which I've written here with a proper indentation yeah and then I want to say byy now of course if you execute this particular code here if I said demo. Java and run you can see 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 take 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 um so maybe at this point let me just come in 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 we can D on 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 mention or with the condition which you mentioned if it is true it will execute the 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 see we got hello so this is basically the Syntax for if so how we do 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're doing here if it's just concerned is it true or false so even if you give a true here if is basically happy okay so if don't want a operation there if says you give me true or false I will execute example if I say false here and if I compile run you can see it will it is not printing hello why it is not printing is 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 uh to specify that it's an it's a part of the E 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 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 you should print hello and it's working but what if the value of x is not 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 two condition basically and I want X to be less than 20 so I want want the value to be in between uh 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 uh X now is from 11 to 20 because we are saying greater than 10 which is the value about 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 less than equal 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 you is called as a truth table okay so now what I what I want 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 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 by 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 keyword 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 uh 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 five 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 do out. print Ln 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 taking any condition it will print both what what I want to print is I want to print the greatest number from five and seven 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 uh 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 only 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 we 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 Els and if I compile and run you can see we got seven but if you change the value let's say if I make the value of x as 8 of course the 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 see 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 codes I would 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 brackets so you open the brackets and you closed it and this thing will be 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 cly backets if it is only one statement you can skip it okay so yeah that's it from this video I hope you enjoyed yeah before that uh let me just code 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 = to 8 in y = to 7 and int let's say Z is equal to let's say five or six let's and I want to print the greatest of this 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 invites 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 Z okay but then don't you think these are two different conditions and if you want to combine them and I want both 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 cly brackets in the last video let me just remove that just to keep it simple and now you can see we got uh 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 eight because eight 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 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 you do that this was simple how will you compare again 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 oh 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 why the greatest which is 17 so of course it it should print 17 run you can see we got 17 but what if okay first of all let's do a dry run 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 in 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're 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 simply copy this and paste it here and instead of saying copy P 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 else 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 y with X anyway X is smaller right so now we just have to compare y with Z we are just saving one one oper one operation here and it's very important when you save one one operation you are saving time you're 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 what 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 tary operator now if you want to use ifls on the same line or if you have a shorter condition and if you can write 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 the number is four and if I want to check it this is even or odd so using IF else how we do it is we say if and we check for the condition if n mod 2 is equal to equal to zero that means it's a even number right so what you do is you come back here and you can you don't need cly brackets because I want to write 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 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 result will be zero 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 uh odd I want to set the value for result as 20 okay so for even it is 10 for OD 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 you will say 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 print result nothing fancy came simple stuff and then I want to compile this and run so I will say Java C demo. Java and Java demo and you can see the output is 10 why the output is 10 is because the four 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 compile run 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 tary operator now how it how it works let me just come in this section see ultimately what we are doing is we are saving the value in result variable uh in both the thing we have the same thing right if it is if is true then we we'll set the value for result if it is false for the else part also we set the 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 Z if this is true then we have to do something or or we do something so we can use something called a tary operator here now how it looks like so turn operator 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 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 return 10 if this is false in that case return 20 so you can see out of all this four lines of code we are doing that in one line with the help of turn operator of course it is not suitable for every eils it's not like you will say hey from now I'm not going to use eils that's not that's not the case this is suitable for some examples this here it makes sense because uh we have this short assignment and ultimately you're assigning value to result so this makes sense I can just compile and run you can see we got 20 because the value is odd but if you make the value as even let's say four again and if I compile and run you can see we got 10 so this is how you can basically use a tary operator now if you remember we have used if Els 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 1 2 7 it might be the number of days and you're right so basically what I want here is if the value of n is 1 I want to print Monday if the say this is Monday and we can just paste it multiple times so this is Tuesday this is wedness day Thursday Friday and Saturday you know what I want to keep Sunday as a last day because that's what we say right weekend the Monday the the day should start from Monday okay so I want to print this based on the value of n at this point if you just uh run this it will print it will print uh 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 eels you can check if the value of n is one you can just print uh Monday if the value of n is let's say you can say else if here if the value of n is two 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 are putting into e Els and if you run this code with that changes you can see it is printing Monday it is not printing Tuesday because Tuesday is in ifls and you can apply that in all the conditions here all the statements you can simply put if Els and you can check for n is equal equal to 3 and then list goes on right there's one way there's 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 three 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's Sunday okay so basically this is what we 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 eils 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 block I have written Capital switch my bad what you can do is you can put all this thing inside this block the only Chang is instead of saying if we we don't for the condition we don't for the 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 one it will execute this statement same applies here in fact you know I just want to give a tab to everything else uh and then here I can just say case two 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 four uh this is five this is six 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 value which is seven and now with this seven I'll try to match with all the cases here so it will check if the value which is passed which is seven is it matching with one no it will not execute the block is it matching with two no is it matching with three no Leist goes on it goes on it matches with seven the moment it matches it will execute the 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 eels 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 eils but there 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 you 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 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 eight is not even 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 Val valid number and you can see the value here is eight so it should print enter the value valid number and that's what you we got enter the Val value 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 are different syntax we can use but again that we'll 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 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 eels we have seen tary 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 lot of memes available which says uh you know in our school time when when teacher used to say write this 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 rrate 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 high once if I want to print that multiple times I can just uh copy this I 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 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 into picture so we have a concept like Loops which will rrate 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 we'll 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's there's a finite number five times 10 times 8 times 15 times 20 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 want to 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 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 we'll explore 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 for 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 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 in 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 while 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 it just SK printing okay it's it's not stopping if you can see if I'm try to scroll down it is continuously happening and maybe it will uh F 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 continues 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 1 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 in I is equal to let's say one now most of the time when you use Loops you'll be using a variable name I it's not compulsory 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 I 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 concept of i++ so i++ 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 one and every time you run this Loop the 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 high as well as I will print uh maybe you can you can print both the things the 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 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 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 one and then in the while loop it will check if the value of I which is 1 uh 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 one 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 is two so two is less than four yes and it will continue at at the end when the I value becomes four it will check four is less than equal to four yes it will print high and four 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 1 High 2 High three and high four that's how I lo works now with this I have a question for everyone here and the question is what if at the end I'm just saying s out and I'm saying bu and with this byy I'm printing 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 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 five 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 debug mode so what I can do is I can go to run and I can say start debugging okay so there 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 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 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 do. printer it will print the high and I the value of I which is one 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 high one and then when I say step over it will 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 two can see 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 becomes five here you can see the value of I is five now it is checking and it falls it it will jump out of the loop and it will print by with the value five 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 a nested 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 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 why 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 VAR 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 one 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 one 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 cly 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++ as well and remember this thing we are saying j++ in the inner while loop and then we are saying the i++ in the outer loop okay uh if you want to understand this with an analogy uh just go with a 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 uh Sunday I've already mentioned you know we we we basically want Sunday twice a week but anyway so if you talk about these days every day we have 24 hours right so if you say this is 1 p.m. on Monday again the Monday gets over the clock will start at zero right then 0 1 2 3 4 5 6 and then it continues right as the DAT changes the clock repeats in the same way once your I 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 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 oh we are still printing one here you know what I will do I will also print J so that we 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 three so basically it will print this statement three times because the value of J is less than equal to three so it will iterate three times and then again it will go for the outer loop so the the moment this is false it will execute I ++ 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 two again it will make the value of J as one 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 un 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 uh break point because we don't don't want to debug anymore and I want to remove the inner loop because we want to focus only on the OU Loop now now using this Loop uh we were Printing High four times and then we are printing by as well let me also remove by 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 what happens is when you talk about this line which is line number seven it will assign the value of i as one when it 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 you mean you want to run 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 friend's end at least you should be able to send the 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 I say condition is FAL what it means is example the value of I is five now when you make the value of i as five and if you run this code you can see we 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 uh because that's how you end the statement normally when you write while at the start the curly brackets becomes a statement but then if you write while after the block we have to end it with with the semic 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 run 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 five now which is a false which will give you false here but if you run this code or compile this Cod and run 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 do 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 executes check for the condition and least 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 now there's one more thing if you talk about this y 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 when it is going to end there are some Loops where you're not sure when it will 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're trying to read a file so in a file let's say you have uh th000 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 it can be uh not 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 to 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++ and of course we don't have to do that here now the only thing is while loop does not support this syntax while says I will only for check for the condition but then if you want to use this of syntax you have to use another type of loop which is called a for Loop so in a for 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 five 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 Mak sense right so how a for Loop works so we have to set the initial value which we 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 four and then I have to end the I should be greater than and equal to 1 and I ++ sorry iusus because we are decrementing it compiled run you can see we got high four High three High two and high 1 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 ++ 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 their counting from zero it's because of the binary format uh because in binary we have 0 to one in fact in the number system also we start with zero we say 0 to 9 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 zero 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 which is equal to four and then we also have zero here so let's try to compile and run you can see we got 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 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 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 four here you're saying less than four not equal to four so this is one of the common syntax you can see it start 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 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 1 2 3 4 5 six and seven because we have seven days and also every day I want to print uh 24 hours that looks good right uh 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 1 2 7 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 1 2 3 4 uh imagine day 1 means Monday day two means Tuesday and and and so on so if I try to do that I will say compile and do 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 900 a.m. to 6:00 p.m. 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 7 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 days I want to print from 9 a.m. to 6:00 p.m. how do I do that so I can simply print with the help of statements I can say uh this is 1 or 900 a.m. right or I will simply say 9 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 p.m. 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 four and I have to give a count right so I can say J will start from where now this is tricky should we start J from one we can so I can say one and J less than so how many hours we are doing here we are doing N9 hours right so I can say 9 and j++ right and then I can just give a loop open and loop close and then we can print the RS here now what RS I have to print I cannot print nine because nine is not n it will print nine every time example if I show you this say clear in fact what you do do you know before printing nine I will give a tab so that it will give some indentation before it's print this is what I was saying you can see we got day one and we got 9 hours here but then it is printing 999 I don't want to print 999 maybe I want to put an attendance here or maybe a calendar where we can Mark what what we are doing going to do so what we can do here is I want to uh say Okay I I should not be printing nine after this space I can say I want to print let's say value of J but there J will print from 1 to 9 let me show you what I'm saying clear compile run so you can see it's printing 1 to 9 I don't want to print 1 to 9 I want to print from 9 to 18 in that case can I simply add this with 8 because it will start with 1 so 1 + 8 is 9 right it will start from 9 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 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 concatenate 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 resolved in a separate way so whatever number you're adding here 1 + 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 we cannot add six right because it's the 17th R which gets over Okay so let's say we have for 9 hours which is from 9 to 5 okay which also includes the the fifth the 17th hour okay that's tricky okay in fact you know what should I do I should actually print a colon here or maybe a dash hph from this R to this R this is what happens when you take the example randomly so I will say J + 9 s of saying is this you know from 9 to yeah this makes sense now so you can see we can we we have actually built a calendar for this week so day one from 9 to 10 we can do something here from 10 to 11: we can do something here we can set a meet we can set a meeting calendar something like that uh so what I'm doing is we are printing J + 8 which is 9 and then J + 9 which is 10 for the first iteration as the value of J increases it will have different values here okay makes 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 Ned for 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 or 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 started that yet so let me start the for Loop the moment you start a for lo you can see the value of I is one it has take 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 this this two things will be executed first and then it will execute the block and then if I say step over it will execute the next sitation in fact you know what I will do for the next iteration I will say step into the value of I is three okay is not do 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 to 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++ of course you have to increment somewhere otherwise it will be a infinite Loop you can actually skip the installation 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 works so that's how you can work with for I hope you enjoyed so we have basically seen three Loops right while loop dowi loop and Ford Loop now the question is which one to use in fact doing the discussion of this Loops 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 for we mentioned all the three things the initialization 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 does does matter how many characters you have you have to pick up each character right so there's 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 uh 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 do while make sense but exper my experience most of the time you're going to use for Loop if you want to read a file read database while loop makes sense and have I have hardly used dowi Loop in my entire career but yes there there might be some use cases where you have to use du Loop so which is a 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 objectoriented programming now what do you mean by objectoriented programming so when you talk about object in real world everything is object right in fact if I'm using a a pen that's an object if I'm using a mouse that's an object if I'm using a celp I don't know why I have cot tape 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 we talk about this pen this pen is an object right so this pen knows something so when I say knows something it simply means it has some properties some physical properties right you can see this pin right so we have 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 a pen right and in 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 um 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 my 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 uh but you have to remember whatever you want to do you have to do with the help of object so what we have 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 uh 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 be multiple properties and multiple behaviors uh example let's say if a 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 you can't do anything without objects so that's how it works so in in Java as well if you want to do something you have to create an object but how will you create an object and who creates this object so let's talk about it now if you talk about the concept of object who create object in real world example let's say if you talk about this table where I'm where 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 I will give you a table but tell me the exact dimensions okay uh maybe the carpenter the actual guy who is making this table is very technical so we have to explain in technical details and that's why we have some person in 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 third person and then that third person gives a detail to the carpenter because Carpenter understands on technical details and you understand on the L lay terms I can say Say Hey I want a table where I can keep my computer where I can keep my monitors my keyboard mouse uh mobile phone and all 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 tech 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 normal 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 some 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 J we'll have a d 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 bite code now that bite 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 I 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 see in sometime 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 uh 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 uh variables let's say int uh num one comma num two 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 num one is equal to 4 and num two 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 easy 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 so maybe you have to you want to save this result somewhere so I will say int result equal to num one plus plus num two 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'll 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 you do 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 let's say you know nowadays they don't mention that on their 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 manufacturing Hub 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 back here and you can say class and I will just name it as a calculator I can just open the cly brackets 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 Meth 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 int that's the return return type so whatever it returns we have to specify the return type of it as I mentioned before Java is a statically typed language which means you have to specify for all the data what type of data you're working with when you create a variable you have to mention what type of variable you want to create when you return you have to mention what type of data you're going to return so that's int 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 add I just want to see if this get really get called okay so at this point I'm I don't want to add I just want to see if this is getting called and also we are rning a value right and that's what theas it says it says the method must return a result of type int okay uh so I have to return something so irres to what type of calculation I'm doing I have to return something so will say return so that's how you return return the value you have to use a return keyword and then you will give uh any value doesn't matter any integer value so at this point I'm giving zero because we not concerned about this value but the question is how do I call this add okay so this is basically a Class A Class will have methods a class can also have variables what if you want to create 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 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 so 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 errors 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 add meth is there right but it still says cannot find symbol you know why because if you can see in the arror itself it mentions that the symbol add which is a method is not it cannot find that method in the location demo oh okay so when you call add 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 D 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 know we have this amazing idea and we want you to buy this idea no one will buy the ideas right we buy the devices no one will give you a a I mean you will not buy a phone or a 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 oh 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 num one you specify a type right which is int so here as well imagine calculator as a type and then you are creating an a reference of it let's say Cal now imagine Cal as a variable this num one num two they are also variable the Cal is also variable the difference between these two is this num one and num two they are called primitive values or primitive variables is because int and all those type float double they are primitive this Cal 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 cal calculator Cal and then using this Cal I can say cal. add but still it will not work the reason we are just creating a reference we are not creating an object yet okay how do we create 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 eest in the same way uh every time you buy a new phone basically you are creating an evest because when when you buy a phone the companies are manufacturing new phones okay so basically you are increasing the eest in the same way in jbm as well every time you create 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 what Tye of 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 create an object of course we'll talk more about this 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 a variable name using which you can access the methods of calculator and then you say 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 in 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 why zero is because we are returning zero but what if you return something else maybe I want 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 num 1+ num 2 and and then whatever addition I'm getting I will just return the value now it will make much more sense right instead of returning zero I should I should be able to return the value so num 1 plus num 2 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 a lot of things it says num one and num two cannot find uh which location in calculator and you have to mention hey you know know I was actually talking about num one num two which belongs to demo in fact the main method so what you can actually do is uh can we when you're calling add 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 + 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'm giving you the values and you're 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 num one and num two I will add num N1 and N2 the variable name can be num1 and num2 as well just to just to differentiate between these two and just giving different different variable names so the idea here is whatever value you're passing four and five will be assigned to this N1 and N2 and that's how you can pass the value makes 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 comparation two times my bad so you can see we got nine so it is working basically whatever value are 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 hardcoding values not a good idea so what you can do is instead of writing four here we can write num one and instead of writing five here I can write num two simple stuff so whatever value you have here you are passing it here and then that gets assigned to num one 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 create 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 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 mention 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 Cal of type calculator example when you say five that's a integer type that's why you say int num to this is an object of Cal calculator type so you will assign that to Cal 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 creation 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 coding 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 uh 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 we have to do setup as well and that tool is your jdk now jdk here stands for Java develop M 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 bite code because ultimately the what runs on the ma machine is your is a bite code so of course you cannot type a bite code 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 bite code 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 well what happens is when you want to run your code so what code I'm talking about I'm talking about the bite code here so assuming that you have already compiled and you have a bite 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 jvm now this is where you actually execute the code now why it is called jvm is because we have a machine and to make the Java platform independent we went for a virtual layer on top of it so 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 this say computer will convert that into its own way 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 inbu classes some inbu libraries and that's where we need some some extra things here so maybe we need some extra files from which you going to use in your code okay now question arise where you will find all this code and that's where we have one extra layer here on top of it which is your J now JRE stands for Java runtime environment so basically we have a concept of JRE which is Java runtime environment now in this JRE you'll be having all the extra files you extra classes in classes it also checks your bite code basically it validates your bite code U maybe you want to load a class so here J 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 J 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 G so you can't say simply say hey we don't need G we need if you don't have a g you don't have a jvm right uh so basically every time you when you say you have Java in your machine to run the code you actually have JRE with with it so you have a JRE and inside that you have a jvm but then as a developer you install jdk right now jdk acts like a upper layer so every time you install jdk you also get this two as well so the the top layer here is your jdk so jdk when you install jdk you will get the updated J and also you will get the updated J jvm but by default on a client machine you will find JRE 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 uh 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 on net platform or on Java of course there are other platforms as well just to give an example we have net Java and that's why you know you have you install extra softwares the same thing if you don't have the updated version of gr 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 uh the new softwares so this is basically your jvm JRE 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 were 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 are saving in result and you are printing 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 explain how this meth methods 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 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 Bonet 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 the software 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 create a class called uh engine let's create a class called seats right so all these things comes together now every class here is 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'll 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 simp simplify this let's say I create a class and I will name 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 to play music in fact in my childhood uh this is what we used to do when we used to get computer uh to access uh I was around I was in my 11th standard when I got full-fledged use of computers and at that point 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 me 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 her here and if I say hey I want a pen now at this point as a external method I'm asking HS 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 uh someone to send this information so at this point if I'm asking H 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 why so of course your method can give you back something you can say a method can give you integer value a method can give you a pen as well uh so this method which is play music is not giving you anything it's 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 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 return 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 you 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 a special type in Java which is a combination of different characters so we have to put that in double quotes now we are rning a pen so here we can't say vo because when you say vo it means you're not expecting anything in return but here we doing that we are returning a pen in a string format so we have to say I'm returning a string so whatever type you return here you have to mention that particular 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 accept something from you or what type of data you want to return so here I'm saying this get me pend will get some integer value so let's say I'm passing 10 so normally one not 10 but a variable which is num we'll say cost so basically you have to pass a number let's say 10 so one pen will cost 10 Rupees and then if you want to call this get me a pen you have to pass the value as well makes 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 if you really want to use those ideas you really need an object how can we use something without the object right uh even if you want to call someone you need a phone with you a physical phone not a virtual uh 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 obj equal to new computer and that's how you create the object object right so this is a line normally we use to create object but only these two things are actually creating 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 uh the compilation done let's run this code I will say Javas 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 do 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 I will pass 10 here and uh 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 St Str so I can say string St Str of course you can use any variable name but that's a string uh we are not using integer or double here we 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 St strr 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 let's say two now of course we not get any pen for 2 rupees right of course at this point uh it will work because we're not doing any any checking I don't want to return a pen when the cost is two 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 Els so we can actually use ifls 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's a phone called Nothing phone so basically what you're returning here a 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'm passing two here and of course the condition 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 say 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 get 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 two in that case this condition will be false this one will be false and it will not execute the return statement of if it will execute return nothing and that's why it will print nothing now if you can observe there's one difference between these two methods here if you can see we have get me a pen and we have main the difference is of static keyword and we have not talked about static yet now let me talk more about the methods and this is important for the further Concepts and uh 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 computer as well we can do calculations but just to keep it simple let's go with calculator so we got this uh class here and let me also create the object here and there's 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 over 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 I'm let me just do that once more and you'll understand what I'm doing so you can see I'm saying public in I want to add two numbers why int is because I'm expecting some return value and it it will accept two values one is N1 and one 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 what I'm saying is is you can create a variable like result and you can say N1 + 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 + 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 return 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 oh 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 will 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 except 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 two 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 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 sometime you want to add three numbers sometime you want to add two numbers again it will not work your compile will say Hey you know uh we are expecting three values and you passing only two now unfair right okay how do we solve this problem maybe there's one solution which comes to my mind is what if we can just create another method which can accept two values I can say public int add one which will accept int N1 comma int N2 and this will return N1 + N2 now this perfectly works the only thing you have to do is you you have to remember what method you have to call because add one adds two numbers and add adds three numbers of course you can make it more effective by uh 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 and adding three numbers so can we do this can we have the same method name that can solve a problem like this of course I have two methods 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 int will give you double and that's set 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 overloading 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 overloading 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 we are focusing now if we talk about the outer box for this that's your J outer box for that is your jdk but let's focus on jvm here and I also name it jvm 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 your data and that has a feature which is last in first out example let's say if you send data which is 5 6 so let's say if you send data 5 6 7 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 seven 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'm 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 this 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 they are variable but they are local variables okay just to rrate 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 N2 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 mean now the beauty is every method will have its own stack that's right it's 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'll be having two stack what I will do is I will just redraw this stack 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 you 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 writing a there now what's the value at this point there's no value for a so I will just ignore that part maybe not let's not even mention that once we talk about string then it will make much more sense let's start 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 10 straightforward we can assign the value 10 because that's a 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 the this obj as of now and mind you this obj is also a variable so it will 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 I got R1 variable but what is a value for R1 and that's where it will basically call the ad method okay so it will call the ad method but we don't have a add meth in the memory as of now right so if you see in this memory nowhere we have ADD so basically what you 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 N2 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 N1 is three 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 3 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 7even I hope this makes sense cool uh so we have done the changes there as well so R1 is now 7even what else now what happens to this method add 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 add 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 uh we do there right but what happens with the the object here now that's tricky and what happens to this num variable where do we put the num 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 beauty is you can actually print the value of or you can use the value of you can use a v 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 five and I can actually use this num inside a method see using a particular variable and 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 doing is we are saying calculator obj now remember we have talked about this multiple times this obj is not an object now people coming from C++ you might be having this urge of calling OB obj as 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 mean 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'll 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 variable are part of a stack so the part of the stack not of Heap but if we 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 uh 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 101 of of course right when you store the data it has to get some address now that's 101 of course it will be a complex address at this point I just want to keep it simple 101 now this 101 will be stored in the stack so every time when you say obj do 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 do add that means we are using obj object to all the add method but it belongs to OB 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 Heap yes because of the address right and that's why you can use this object multiple times and that's important in future we're 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 op. 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 changes 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 five of course you can change it and you can have ADD method okay uh this is num okay not n but imagine that's num and he'll be having obj1 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 value or two different num variable but they have the same value as 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 uh 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 obj 1 in fact the first object is obj the second object is obj 1. 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 8 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 run 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 they 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 we have different areas we have stack and Heap in this video we'll talk about array 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 uh let's say int variable so how do we do that how how do we work with int variable so we say in I is equal to 5 so what happens is you have a variable here which is I which has has a value five 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 in K is equal to 7 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 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 you might have 10 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 uh so let's say if I have five 6 and 7 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 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 numb 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 mention 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 array of course if you want to store multiple values in one particular variable we can use aray 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 number one 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 this 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 in 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 it I want to create a int array and you have to also mention the size here let's say four so this particular num one 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 a basic syntax so let's use it uh so we can simply create a we mention the type of the V typee of array 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 cly bracket here and since I know the values I can directly put the values here let's say 3 7 2 4 so let's say we have this 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 1 2 3 4 now this is your array which is names nums of course the same code which you're doing and then in this array you'll be having these four values which is 3 7 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 2 3 4 uh no that's not how your computer works so aray numbering starts with zero the point remember it start with zero 0 1 2 3 now those are the index values in fact you know what I 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 3 7 2 4 they 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 this square bracket 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 uh 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 six so basically I want to change the second value and I want the value to be six in that case you can say index of so when I say second value it's actually one because the numbering start with zero so if 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 see we got six I hope that makes sense uh so basically you can assign the value you can print the value as well uh what if you want to make it Dynamic okay how do I do that I don't have these 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 the square bracket you can mention the number the size of the array which is four now this is another syntax the only thing is by default all the values of this array will be zero so of course you 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 Valu so what I can do is you can specify I want to change the index value zero to let's say number four and then nums of index one so for every index you have to set the value let's say eight and nums of two is let's say uh three I'm just giving random values okay it's not like I have all these values prepared in my mind uh but let's say for three the value is nine okay so we got this 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 0 1 2 3 and the actual value so before assigning the value the values were 0 0 0 0 but now since we have assigned the value the number the value has been changed the values are 4 8 3 9 okay yeah so 48 39 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 zero index of one index of two index of three and you can simply fetch all the values right so compile and run 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 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 in I but what should be the initial value for I it should be zero you know why because index value start with zero and then less than should I say equal to we can we just have to say three there because the size of the array is four you have to stop at three and I ++ that's one way or you can say less than four whichever format you follow okay and then just put that into a cudly brackets now we can just shift this statement in the block so that it will execute so basically we are using a for Loop to to print this now the only thing is if you can see we are mentioning zero which means it will the loop the loop will run for four times it will simply fetch the index zero 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 lot of things related to array how can we create multi-dimensional array 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 uh 7 2 and 4 now if you look at this can you say this is array of ele 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 uh let's say 5 2 6 1 so we we got this array and then I also want one more array here right let's say we got in fact we need two more array and of course they will have some values 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 N3 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 I know that looks weird but uh 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 there 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-dimensional which will have array of array the way you can achieve that is very simple you just have to put two square bracket because one square bracket represent that's an array now can we say the nums array is of array of array yes we can and we have to mention a 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 two square braet now but what will be the values here see if if you have a single array it's very simple you can just say this size is four but here if we talk about the size how would you define it so if you compare this particular thing can we say we have one big array which has three three internal arrays to it so can I say this is three internal arrays 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 uh diagram here what I can do is I will just draw that once again uh so we have one big box and in this box we have three sections and each section has a array and each array has four elements 1 2 3 1 2 3 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 just zoom 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 5 2 6 1 uh 3 2 1 7 4 8 1 2 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 five here becomes 0 comma 0 simple right because this is zeroth row zeroth column uh if I want to represent this 6 so this six would be 0a 2 if you want to represent this seven 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 four here this will be 2 comma 0 uh this two would be 2 comma 3 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 extra things from here I don't want stent 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 twood dimension array or a multi-dimension array and I can say int here so we want three rows and four columns now there are different ways of saying this 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 zero 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 well but let's try so what I will do is I will say int I is equal to Z and I less than equal to okay I less than now what should be the value it should be three for the Outer Loop and then I ++ and then for the inner loop see the outer loop will H 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 in I mean J is equal to z and j less than 4 and J 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 zero again right uh example if you see here uh it will start with zero and then for every row it will go for 0 1 2 3 right once you reach three 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 zow 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 uh maybe you wanted to print this in a box stru structure uh what you can do is instead of printing Ln because Ln will print 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 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 uh the only thing is I want different values here so I can assign but I I can assign them manually I think this is a good time to talk about how do you fetch random values so you know uh we have this math class in Java which will give you some random values right so this is a inbu class which will generate a random value and to understand that let me just go to 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 we'll 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 we just doing some experiment here now once you multiply that we have to also uh do a type casting 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 only once so let's say initial value for I'll 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 uh 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 uh this value this formula I think this should work I I just I'm not sure what will be the value it will assign you know we don't even need this variable basically but how will 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 outs assigning zero values why I think it should be type casted after we do the calculation compile run oh got it so you can see we got this random values uh so what what was happening before is we were typ casting the random values and then we were multiplying by 100 what we have to do is since random will give you in point values like 0.12 or 0 74 all the 0 point values we just have to multiply that by 100 so that you will get a double digit number at least and uh you will simply typ cast it later of course it will have 72 Point some values I I'm just typ casting it and then yeah so let me just remove this printing so that you will get 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 even multiplying by 10 works because we only want single digit yeah that's how you Generate random values that's how you print it 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 a enhanced for Loop can we let's try so in in enhance for Loop 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 you 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 integer variable but what if two Dimension here here if you talk about hey nums I want the first element nums 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 int 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 you 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 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 how 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-dimensional 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 a array small array which is of size four right and then we we're assuming that every row or every array inside the array will have the same size example if you look at this three arrays 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 four 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 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 arrays we have but internally those those rows we have don't have to specify how many columns we have that we can manage but how how we 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 Ja dat then we have to individually specify how many columns you have what I'm saying is you have to say index of zero is equal to for the first add you have to specify how many values you have so you have to say new int 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 how you to manage this one now this is your Jagged array okay I mean this one this is your jaged array 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 four and the third box will be have size two okay so we have different size of array in different at different location so for zero index we have three for the 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 nums of or maybe you can say nums do 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 I may we can say this nums of I for every row I want to know the size so now basically we have we have specific the size of each array so for the first array I have three values for the second array I have four values so for every row how many what's a length we are we have here that's what we are specifying and now while printing as well instead of using normal for Loop let's use enhanced for Loop and the beauty about enhanced for Loop is 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 see 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 Jagged arrays let's go back to normal array uh what we can also do is we can skip this part now can we create three dimension AR 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 0 1 2 in this you have an array of size four so 1 2 3 4 and in each box you have one more array here which is of size five okay let me just do that with a small let me expand this this and in this we have one more array of size five okay uh this is index number 0o 1 2 3 4 and then this is 0 1 2 3 so if I want to fetch this value here which has value that's a six if you want to fetch this six you have to say 0 comma 0 comma 2 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 AR here as well and if I want to fetch let's say the third element here we have to say 1 comma 1 comma 3 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 jaged anymore this is three dimensional array so I hope this makes sense uh we have talked about jaged arrays and three dimensional array in one video in this video let's talk about the drawbacks of array of course we have seen lot of good things about array we can store multiple values and then we had some fun with the multi-dimensional array and 3D array Jagged arrays 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 nums and let's say this is of int type so of course you have to say new int and we have to give a bracket and to specify the size four now first of all before we talk about the drawbacks the array in Java is an object okay the moment you 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 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 for 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 array the second problem is every time you want to search something it will Traverse between the elements so for searching for inserting value uh it does consume a lot of time the last problem which is we we mentioned about in here 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 array unfortunately you can't do that I mean there's a solution if you want to have object of student uh integer string uh there's 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's 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 array but aray arrays are great they are fast uh 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 we'll not be using AR anymore they have different use cases every time you know you have a fixed size and if you want to store multiple values array will be the first reference so once we have talked about array I mean basically we have talked about why do we need array and how to create an array let's do something more with array 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 uh it can also be five or any number as 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 six 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 uh zero and that's what is happening Here and Now what I want 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 seven 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 uh we have a separate topic what are exceptions at this point just remember exceptions are runtime errors okay so the errors which comes in runtime at compile time there's no problem when you compile this code it will not uh blame you it will not shout but that 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 start starts with zero it ends at five not at six and here it says index six 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 hardcoded 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 array 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 six 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 there 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 create multiple objects here and let's say I have a integer I have a student which has some values so let's say integer RO number okay and 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 example okay so we have student and then we have these three variables okay now mind you these are 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 R number which is one and S1 dot name which is let's say naven and S1 do 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 rule number two let's say this is hush and and marks let's say 67 and if I say this is three let's say this is kid and let's say the marks is 97 okay so we got this Marks here right and then basically I have three 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 uh 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 you have to say array and then we have to say new now in this case when you say say you have INT in the same way here it should be student right student array of let's say three student will'll 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 zero 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 this objects otherwise this will not work now if you're thinking at this point we are creating three student object no we are not creating three student object most of the people have this confusion that on this line we are creating three student object no 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 fet those data in a student array now what's the advantage the advantage is let's say if you want to print all the details of a student so what what you can simply do is you can use an array which will start with zero and I is less than equal to okay I is less than students. length which we have seen and then i++ and then here we we can simply print but how do you print the values so we have what you do is you have to print one value at a time you have to say S1 because see 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 commment 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 is printing something else see in the upcoming videos we 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 have to mention S1 do name and you have to maybe you can do a concatenation here and you can say S1 do marks I just I don't want to print the RO number at this point so I'm printing only this two and now if you compile and run you can see got the student details okay so can we do it here so let me just uncommon 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 uh you got some idea regarding how do we create 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 come in 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 Vari normal for Loop in which you have a count I which starts from zero to the last value of the length of the array uh less than that which is three and we 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 in future we're going to talk about collection as well in that also it is used but since we only know added 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 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 give me a value not the index number give me the value so forloop says okay okay let me try so whatever value a for Loop 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 this 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 this is a beauty about it is 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 got the values this is better right uh you can also use this with the thing here which we have done for student I will just come in this part and uncomment the entire section from here everything is uncommented the only thing is I don't want to use the normal for loop I want to use the enhanced for Loop so normally instead of saying that this is a for each Loop we say enhance for Loop okay so what we can do is we can say for and here okay the only thing is when normal for Loop we use int because we want to maintain the counter if we talk about the example which we have done for normal integers here also we 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 stood colon so we have to mention the student type not in 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 student represent one particular student at a time so you can simply say student st. Nom 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 St dot let's say marks let's see if this works now we are working with stent not with normal variables compile and run that's it you got the output so you got naven and you got number so this is your enhanced for Loop yeah some people do say it's a for each 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 uh it has a forage method so just to not get confused let's call this as a enhanced for Loop now one of the reason why people say it's a forage Loop is because in other languages this type of Loops are called forage and 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 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 video let's 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 say you want to store a name like naen now this naven 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 uh we will talk about array later uh 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 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 codes the way you work with characters in characters you use single codes here we'll be using double Cotes so if you want to store navine 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 int Flo 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 will 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 create object of string is by saying new string and you can give the round bracket 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 way 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 there now the reason we are creating a heap memory in a not a structure format is because there's no fixed size to it uh 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 105 so of course you will create a variable which is named here and then the address is 105 so there's a link here and of course in this particular box there's 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's print 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 you can pass this string whichever you want to example if I want to store naven that's the D I'm storing ignore this original this is given by the IDE but this is our value which is nven so inside the string Constructor you are passing the data which is nven right now if you try to print this you can see we got naine here okay so that means in this thing uh we are creating the data which is n 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 depend upon some calculation now what else we can do here so example let's say if I want to concate this naven with hello so let's say I want to print hello naven what you can do is uh of course you can write a refence statement to print hello and then you can print name but then string by default does not work with any other operators like supp action division but only there's one exception which is plus operator so you can use string with plus operator okay so let's say I want to print hello and N B so in this case you can print hello and then to concatenate to mix these 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 naen I hope that makes sense so we 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 uh 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 one here it will give me a character at index one so if you look at this navine it is n is index0 a in is index one then two then three then four and if you try to compile and and run this you can see we got a so yeah this is how you can basically use certain methods there which is character at or you can also concatenate two strings okay so let's say if you concatenate my name with I will give us space and ready so basically what I'm doing is I'm conting naven with ready here let's see if that works yeah it worked can see that we got naen 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 or let me 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 naven here even that works so behind the SC it will create object you don't have to worry about it so when you say string name equal to nine it will create the object for you you don't have to worry about object creation those stuff so most of the time you will see this intax hardly you will say uh string name new string and then a 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 assigned 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 na of course it will be getting stored in the in the HP memory but now what I want to do is before printing naen here let's say I want to append something so let's say I will say name 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 N Ready right so basically we are trying to change the value now if we talk about integer flow 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 naven is naen 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 naven to nain ready so if you're thinking there will be a variable here uh where you have naen you're making it naven ready uh no that's not the case that's not how string works okay so what happens so what I what I will do is I will 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 show you something let's say I create a string here which is S1 is equal to uh naven 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 naven 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 and 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 you 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 HEAP memory so this is your HEAP that's your jvm and that's your stack so inside your Hep you'll 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 naen here I cannot change it it's done it's n everywhere okay uh so you can't change the data so example what happens is every time you you try to use a variable here example if you execute the line number eight it checks okay we need no variable do we we already have naven in the string constant pool no we don't have it so let's create naven here so it will create a string variable naven it will have some address let's say 101 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 naen okay so it checks do we have naen with capital in the string pool no we don't have it so it will create naven here and let's say the address is 103 so it will store 103 here okay now that makes sense right and that's why it is referring to that particular location now what happens on line number 13 line number 13 says string S2 is equal to naven it will go to string constant pool again and check hey do we have naven with capital N yes we do have now it will not create a new object for you it will simply use this address 103 okay and that's the beauty you're that's how you're trying to save the memory otherwise you have you could have created multiple variable 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 nain by nain ready no that's not happening nain 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 naen ready it let's say it has the address which is 105 so on line number n basically what you're doing is you are changing this address from 101 to 105 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 nin 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 to 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 to remember it's immutable string okay so whatever data you're creting here it's 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 we'll discuss later but they both are similar and the both provide you a way to implement uh mutable string okay in this video we'll work with string buffer so what we have is we have a string and 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 it 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 uh so we can know the capacity of this string but first 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 naven here now naven is of five characters right this is how you basically create a string buffer in bracket you mention the string so it will create a string go object and the beauty is you can change this data okay now the thing is if you talk about capacity what it will be so capacity 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's no location available there's no space available in that case it has to relocate so to reduce the relocation of the string it gives you a buffer okay uh 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. length and length is available in normal string as well so if I compile and run you can see the length is five so length and capacity are two different things okay now can I upend data see the ultimate thing was can I change my data which is n 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 readyy here and of course I want to give a space between naen Na and ready so you can append and you can print the data so you can print SB as it is let's oh first we have to compile the code run and you can see we got N Ready that works next what if you want to assign string buffer data to string example if I do that uh you can see it will give you error it says type mismatch cannot convert from string buffer to string of course right STI is 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 will store in St if I run you can see it says Java of course you can add Java in between as well uh let's say I want to add after H location I think that should work after the space at six so let's try that compile run you can see naen 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 sub string let's say if you have a very big string you can specify uh 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 30 here it will save the data which is whatever you have no ready will be stored and all the other values will be null let's see so after N Ready it will add some extra data here okay so that you can replace that later cool we can do that we can also set uh minimum or maximum data I think there's 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 ensure capacity to 100 so it by minim 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 we'll understand once you talk about thread concept so at that point also we'll talk about difference between string buffer and string Builder but whatever methods you have in string buffer it's almost same as uh 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 his method is we know what is written type we know what his class but again problem gets stuck here in in studing 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 string so I say string brand uh and we can have a default value for it we can have int which is price and then um string Network okay and we can also make it for a specific brand as well but let's keep it simple uh 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 and then we got we also call them as smartphone okay the only thing yes you have to change it here as well obj2 obj2 and then I can print the values as well you know what I will do I will just create a method which will print everything I will say public void 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 call obj 1. show and obj2 do 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 you talk about this 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 memories 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 uh obj1 and one is obj2 and then of course they have the address as well so this is 101 103 and of course this will be 101 1 13 and you will be referring that right so this is effort here this is effort 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 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 sometime if I say obj1 do uh 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 see this is also pH now the reason is the static variable is actually shared by all the objects so basically we'll not be having individual static variable for every object we have a common variable which will be used by all the object imagine there's 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 all the objects okay now this is this is why we use static now the beauty is see why we have to refer brand with obj1 or prize 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. 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 to 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's no harm okay and uh 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 we can create static method and the question is can I use instance variables there okay we'll see that in the uh 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 a object member so so static this name here belongs to a class not to the object and that's why you were able to use this with the help of a class name okay uh and then you you're saving memory as well because not every object will have their own uh value for it or 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 variable so we 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 iniz 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 uh 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 the static variable inside a Constructor and there's no harm in that now the problem is every time 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 to to insues every time but why name has to insues every time I feel that the name variable which is static should be enzed 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 insise we don't want to do that now 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 the 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 Orit 2 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's 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 return return one extra okay I have removed the cut the backet from here so compile and run so you can see it is calling the static block only once and is calling Constructor two times there's one more issue why static block is called first even before the Constructor it is calling St block and that's weird is it the sequence let's try let me just put this after the Constructor let's see if this any make this make any sense anyway Java does not follow the sequence but just to uh 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 we there are two words we and we only focus on one word every time you get the object there are 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 this 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 class loading happens first and that's why it will call the static block first and then it will create the object and that's why it will call the Constructor after static 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 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 any object now so if you 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 will you do that now for that we have a special class inside Java which is called 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 it has lot of methods to it one of the method which we want to use here is for name now Ford 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 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 once we 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. forame will load your class if I go to forame you can see forame is a method and it's a St 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 in instantiate it compile run you can see 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 we start with 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 earli videos we have talked about static keyword static variable static methods and yeah that's about 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 is because we have not mentioned a static keyword now whenever you want to work with a nonstatic method you need to create an object of it of course if you want to call you have to create object of uh Mobile in fact you know actually we need those objects here and uh 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 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 uh mobile. show 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 nonstatic method that means you cannot call a nonstatic method with the help of a class name and that's why you need object here which you can see we're using obj1 obj2 but can I call show one from mobile let's try and 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 directly calling it with a class name now the question is this looks cool right static 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 see 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 are printing brand and price you're talking about obj one when you call show with obj2 you know that the brand and price will be of obj2 but then when you call mobile. show1 how do we know which Which object you're referring to so this brand belongs to obj 1 or it belongs to obj2 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 U this time I have added a directly keyword that that means indirectly is possible now how will you do it 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 obj 1. brand I mean obj brand and ob. price so basically we cannot directly use a nonstatic 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 1 not for obj2 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 it don't mention static that that means main becomes a nonstatic method which means if you want to call mean 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 mean we don't need object of demo problems solved right so yeah this is how we can solve this problem and this is what static methods are uh anything else I 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 op so when I say uh let's Implement objectoriented 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 your mind you have lot of different concepts or you know 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 uh I'm a big fan of pixel movie uh 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 naven or which names name is equal to and in the Box will'll be having a value for me it's naen uh 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 this variables right so let's say my name is naen 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 depending 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 uh 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 new human and once you got this object with this object you can fetch data right U of course you can set the values as well and for this particular object I can say obj do age is let's say 11 and obj do name is let's say naen I know that's not my age but at this point it's okay so we got this values and we got this object created here now what I do is what if someone ask me hey human or maybe someone is D to directly trying to fetch the data example if I say out and if I try to say obj do 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 issue so I will say compile and run you can see we are able to fet data and syntactically there's 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 V 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 but that's private to me right the same thing can be done for the name variable 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 repeat every time you create a instance variable make it private no one from the outside well 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 this variables that's right the reason why we're creating 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 an 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 that 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 naen and then from my methods which is my mouth or my by using my handwriting or by typing somewhere there's 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 Behavior I can share the information okay not like anyone can just come to me by they will break my head and say okay I got the data now that's not how how it should work so how do I comp how do I uh restrict it so what we can do is we can make it private we are restricting restricting it the only way to access them is with the help of methods so what I can do is temporary I will just comment these two sections and I will assign the value here itself and we can do that uh I will say name is naven 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 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 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 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 they are two different classes okay makes sense now now we can also do 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 want 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 do getet 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 do getage okay uh 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 naven and L so the point to 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 make some sense okay but there's 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 zero and uh 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're 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 a 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 comment to I want to set the age right so instead of setting the age to the variable I can say obj do 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 this will be assigned to age can we do the same thing for string let's try I will say public void set name and of of course we have to also accept the name but what's the format of format of name it is string name or will say n 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 set cut this part as well I will say set name and in the double codes I will pass the name and the name here is naven I I'll 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 can you see that we got ready and 30 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 their name name so get age 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 XY Z we can do that okay there's no harm in that the only thing is when you call these methods of course you have to change the name I will say this is XY Z so when you set the value you have to call XY Z 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 works now the problem with ABC and XYZ or any name is your method name should Define what is what is your intention because in 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 I say get age it 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 j 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 those are get letters and Setters uh in fact the beauty is if I delete the entire part okay let me just do that I know we have we need to type that once again actually you don't most of the idas have this amazing feature you can just right click and there's a option of source in fact it doesn't matter which ID you're using uh you will see the option of source or insert code something like this so you will say Source action what do you want you want to get you you have to say generate Getters and sets form click on that for both the variables and click on okay you can see 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 gets and sets and of course there's no compulsion to have both the methods you can you can only have get name you don't want to have set name that's your choice there's no compulsion that there should be both uh it depend upon you how you want to implement it for this example I want to assign value to age and name I want to get the values as well and that's why I'm using this two so only thing is for every variable you need two methods gets 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 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 if I run this code compile okay there is some problem with the variable names I think we are still calling XY Z so it should be set age and we still calling ABC it should be get AG 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 now the thing is when you talk about this variable like age and when you are 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 101 so we got obj and the address is one1 that's the first object we got and there's a link here which we canot cannot ignore and then uh we have some more variables you can see if you execute and of course this was my main stack uh now we are 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 101 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 30 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 U 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 30 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 age 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 age 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 uh stack we got 30 and if you see we are assigning age is equal to age so I'm EXP exp in 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 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 a 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 zero 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 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 a 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 uh human can we create object here itself obj1 equal to new human and then we can say obj 1. age 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't do that because we 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 we 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 a age equal to name equal to and what value you're assigning here you're assigning value which is 30 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 create 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 see 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 103 it will be 101 okay so this obj1 here will get the address 101 and we can simply say obj 1. and now we are not referring to this address we are referring to this address so we can change the age value 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 you differentiate between a instance variable and a local variable is by passing the object itself but there's one little problem here the problem is if you if you read this line it says obj do set age and then you're passing the obj obj object as well why we have to do this why you have to mention obj two times now Java 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 obj 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 Java says hey why do 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 represent 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 but first name here is this do name which is the object itself the current object I hope uh this all this thing have made sense this is what the disc keyword is in this video we'll talk about 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 this 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 not return 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 Heap 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 zero and it will have a variable called name and the value will be null right I will just I I 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 we'll discuss about string later in detail as of now just remember by default the the value for string is null and the default value for integer is zero okay that means if you don't assign this values there will be default values what I want here is of course there's 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 here or we can use something called a Constructor now how Constructor looks like so a Constructor looks like a me 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 brackets 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 change is in the Constructor you don't specify the written type okay so we can directly mention the Constructor 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 my 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 curly brackets are mentioned just I mean the for the opening brackets are just mentioned on the same line and here the cly brackets 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 out and here I'll 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 so Constructor is a special method that's why because 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 you don't don't have to mention in float or string 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 compilation done there's no problem when I run this Cod 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 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 uh 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 J 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 come in this section let's say I'm not assigning the values let's see what are default values compile run 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 uh statement like here if you want to perform some operation like assignment or calculation always doing the methods and Constructor is like method right so you can assign the values here there's 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 Advan let's say you are trying to connect with the data datase now in that scenario your application is trying to connect with our database or the network you need that connection right now where you will write the connection you can write the connections in the Constructor and that's why this 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 Constructor 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 value see 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 naen 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 naven here right so I can just go back here and age 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 this normal Constructor is also called a default Constructor now we'll discuss why this called 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 it will be assigned now you can use both syntax you can either create an object by not specifying any ameter 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 1 so basically we are printing both and let's see what the output is compile run you can see we got the first object drawn 12 the second object is not John and2 it is nv8 because you passing those values now what I will do is time and I will remove this part just to explain you something see when I mention that every time let me just come in 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 const 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 see we got two Constructors there's only one change the syntax which we have return 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 remember the 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 will say age is equal to 12 and the name is John so that's how we can basically use a default Constructor and a parse Constructor of course we can create some more Constructor here what if you want to just specify the name not the role 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 see we got a Constructor which only takes a name name now what happens to this typee 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 yourself you can say 12 you can say age because there's no confusion or you can also say this do age 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 this is a very super concept so let's focus so what I will do is I 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 a 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 extend a of course we are doing inhance here 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 got an object of B Class here and we can do that let me create a con Constructor of B here now how do we create a Constructor it's very simple you specify public and then you say B bracket 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 compilation 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 that's why you got in B that perfectly makes sense and we have talked about this from a long time the twist here is what if I have a Constructor in a as well and here I'm 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 construct 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 subass and super class both a is a super class here B A subass it will call the Constructor of both why we'll talk about it in sometime but it calls the constu 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 parameterized Constructor a Constructor which takes a value let's say this is n okay and here I'll print INB int because we have a Constructor which takes a integer parameter I'm just printing each 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're not passing any value here basically you are calling a default Constructor but if you pass a value let's say five 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 you can see it is printing in a and in B int okay that actually makes sense it will call the Constructor of a but what if I also have a parameterized Constructor in a as well which takes a n and it also prints in a in now since we were not having a parameterized 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 try let's see if this works oh no what is happening here it is calling the parameter conru of B that actually makes sense but why the default con 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 it 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 that's just that you can't see 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 it respect to which class it is it will have this statement super even if it 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.print in what it is saying is super now what is super now super method simply means call The Constructor of a super class so let me repeat super means means call The Constructor of a super class now what is there in Super class this is a super class it is calling the Constructor of a super class but but which one the parameterized one sorry the default one if you can see we are saying default we are not passing any parameter here so it is calling this particular Constructor and that's why 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 in and that's why we got in B int okay that means if I don't want to call this Constructor if I want to call the parameterise 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're passing to this super now what we are saying is hey Java I know you you're are going to execute this Superglass Constructor but I'm 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 B int 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 you will you have to mention so if you want to pass a parameterise 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 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 Constructor of B it will execute first it is calling this super that means it is calling this particular Constructor which is printing in a and then it is printing in B it will continue with this execution of conru 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 say five 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 super class Constructor by passing a value so which which conu 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 super class but when you say super for a what is a super class for a we we have not mentioned anything here right okay now that's why you have to introduce A New Concept here every class in Java let me 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 in object class also we have a Constructor this is what you're 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 and object both no B Class extends a a class extends object so this is multi-level inheritance okay there's no multiple still now okay so yeah that's how it works we have object class so 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 B 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 five so of course by doing that what we are doing is we are calling the Constructor which is the parameter wise one and we are calling super and that's why it's calling the super class 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 Constructor of a same class let me repeat this will execute the Constructor of a same class which is this one we are not passing any parameter so it will call the Constructor of a same class and then this Constructor is calling the constu of a super class let's try if this works compile run and you can see that we got in a in B in B int uh 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 B int so that's what is which is this keyword and Al so this method and super method so this method execute the construct of a 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 camel cas means or let's talk about in general how do we name a 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 capitator later example if you want to create 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 uh 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 all always start with small class Capital variable method small in fact we also have a concept of interfaces which will discuss later so if we talk about classes and interface we'll have to make it capital let's say interface is runable 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 you talk about a value of pi if you talk about a constant of brand so 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're 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 v this is a variable if you look at this you know this is a method and when you look at this this you know this this are con it makes your code much more readable and that's important 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 theming 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 marks because if you say show my marks it is actually bit difficult ult to read right so what you can do is every time you combine words make sure that the first letter 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 uh 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 works on Java try to follow camel casing and uh but again it will not give you error it perfectly Works underscore works in the names but that's what we follow we follow camel casing here not snake casing yeah uh if you have the class name which is again 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 age that's variable right okay uh 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 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 round bracket 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'll be having your stack memory and then you'll be having your HP memory as well now in this HP memory so if you look at line number 14 it will create an object uh let's say the address is 101 and then in this you'll be having a variable or methods uh we don't have a variable as of now so let's say we have a method which is 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 101 and it will refer here and then every time you want to call show basically you have to say obj do 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 why 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 H everyone hat this people right anyway so we have this variable so what we're doing is we are first of all declaring the variable and then we are assigning a value the same thing can be done for this thing right we 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 Hep 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's no object so this line will create an object doesn't matter what's the address let's say 51 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 int uh marks the moment you do that it will create a int variable called Marks here the value will be 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 stack 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's a reference in this tag so when you say obj there's a reference here for the anonymous object there's 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 of object only when you required so let's say if you want to call show you can actually call it okay so you can say new a do 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 whever you want to use a normal object you can use this of a objects now let's talk more about oops concept now oops basically stands for objectoriented programming now in objectoriented programming we have talked about object class encapsulation right now let's talk more Concepts now one of the most most important thing in the concept of oops 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 you talk about a computer now of course computer itself is abstract concept right now 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 a 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 a 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 has 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 is okay so example if you talk about a laptop so can we say a laptop is 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 we talk about 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 or you take you try to take the features of it now coming back to the inhance concept in real world example let's say uh if you talk about the human world you know when I was in my 10th standard so just for example okay nothing not that important but when I was in my 10th standard uh I did my 10th in 2005 at that point Nokia released 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 is6 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 yeah so it was 60 amazing phone if you can just search on Google uh Nokia you will see that phone now 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 phone 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 310 now I was not having any phone now this is my father's phone and we know the concept right father's phone is your phone in fact in in general will 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 or nepotism but yeah you get everything uh so I I used to say I have that phone okay so this is inheritance now how will you use this concept in programming now in Java what you have is you have different classes right example if you if you create a simple 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 uh 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 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 if someone comes to you and say hey you know you made a 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 advanc features as well so it's it is good to create a subit class okay so you can get a subit 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 calculator is a calculator and it will will get all the features of calculator okay uh it it will make much more sense on WE once we start coding but what we are doing here is we are doing inheritance so basically inhance 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 parent 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 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 subass we can also say this is a base class not yeah 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 sub class 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 inheritance right basically you can just use the features of a super class let's try to implement that in this code of course we 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 create a class called Cal and of course I can just create the 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'll say public in it can add two numbers it will accept two integers as well I'll say N1 comma N2 so we got two variables right and then I can just return N1 + 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 there will be a subtraction operation so we got two methods right now one thing to remember if you look at our 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 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 Explorer you can see we got demo. class and we also have cal. class now when you done this code if you say Java and if you say demo uh it will 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 let me do 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 cal. Java and 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 compiling the class. Java right but when I create an object of it if I create object of Cal obj equal to new Cal 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 Cal 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 cal. class as well or cal. Java as well you have to create object and that's how it compiles now once the compassion done you can Simply Save the value in R1 or you can say obj do add it will add two numb 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 three okay so we 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 I also print R2 so basically I'm just printing two values back to back and if I say compile again because we have made some changes and run you can see we got 9 and four so 4 + 5 is 9 7 - 3 is 4 and that's what we are getting here 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 naen 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 $10 now let's say if the same person come backs 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 $15 now of course I can add those two methods here or I can create a new class you know why is because if I edit the same file if I add more features here I can't sell calculator for $10 now I have to make a new product for $15 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 calor and I will say this is advanc calculator which has all the features Java and in this I will basically have two more methods which is public in 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 an error here it says the method add is undefined for type advanced calculator that means in this we don't have add and subtract 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 uh methods definition and classes the point is if I don't Define it here how will I access them that's where the inheritance concept comes into picture so inher says hey don't worry if you already have a class called Cal you can simply say extends Cal 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 that's a first first reason even if you don't have a code ready with you if you don't even if you don't have a ja 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 and suction working we can add some more variables here I can just copy this code and paste paste not on the 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 5 and three okay and then dividing the number let's say 15 by 4 let's do this and now when I perform this operation of course I 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 oh it works you can see that addition is nine uh subtraction is four multiplication is 15 if I'm not wrong yeah and division is is three so 15 divid by 4 will give you a quent of three okay cool this is working right so this is how we use inheritance so important thing here is this Advanced calc here is called a subass this calc here is called a super class and to inherit we use a keyword called extends okay and this is basically a relationship of e a a advanced calculator is a calculator and that's why we say is a important word there cool can I can I have multiple classes in in each other we can that's we'll see in the upcoming videos at this point we just went for a simple inheritance where we just use the feature of a super class you can say parent has all these two features child is just extending it or using it I can say this is NE nepotism so so basically we were able to do 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 operation so I will just say advanced calculator I'm very bad with name so I will just use this and this advanced calculator will have extra feature let's say it also has a feature of uh finding the power of a number okay so let's say public int I can simply say yeah Power now what you mean power let's say 3 is to two 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 basically we have a math class I can just simply use it Matt dot we 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 N2 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 return double value or you can just typ cast 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 where 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 subtraction 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 4 to two at least we know the output it should be 16 U and I also want to print R5 oh we are printing everything in one line great and you can see multi division not not working so if you want to make it work you can't just extend calculator you have to extend 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 0 because we are returning a double value this works so if you 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 inheritance so basically we have m m l inhance here because we have multiple level but in the ear example we were having just two right so we were having this two now when you say only two classes this is called single level inheritance and I know become I became a doctor there but you got the point right so if you have multiple layers that is multi- level inheritance and when 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 and advanced calculator is taking the feature from calculator so by default all the Cal stuff will come to very advanc 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 what is multi level so when you have the class which is getting inherited by someone else and then some else something else is getting ined by something else that is multi- level now can we have a different structure here uh 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 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 parent so we have a as parent and B as parent and then C becomes a child so C isting from A and B both now is it possible in Java let's try in fact it is possible in other languag like C++ 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 uh 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's no problem here but what if I want the C to extends 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 no 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 inheritance will not not work but the question is why so let's try to understand that here now let's say if you 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're 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 do ABC yes this will work but what happens you know when you say obj do 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 OBG 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 try to call Z this will work because in C itself we have the method if you say obj do 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 obia 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 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 show 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 in general 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 dox will this work now it will search in C in C 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 trying to say obj doy the problem is from C 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 parents 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 G giving an example here I'm not saying it's 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 ambiguity problem okay so ambiguity simply means we we have a confusion here which one to choose now this problem is there in multiple languages which supports multiple inheritant 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 doeses not work okay so in the it's a very famous interview question uh do we support multiple in in Java and the answer is no there is a indirect way not actually a way is just that it is possible by going all through steps but yeah that's how Java is implemented in new Fe 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 inheritance 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 overwriting 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 do 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 uh run and you can see it's 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 extends 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 here instead of a it will also work because that's how it 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 void 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 I can actually call both the methods not just one I can call config as well okay and if I compile and 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 show 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 in that scenario 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 show 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 uh 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 show should override the method 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 pattern 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 10th standard and my father had a phone which is310 now if you ask me at that point which phone I have I used to say 31 0 because that's my father's phone but after 2 years when I was in my 12th standard I got my own phone I got Motorola c168 again a not a smartphone but it was good it was a very slim phone so when now if you ask me which phone I have I was I will say I have motola c168 my father still had 310 so here if you can see if someone ask my father father will say I have 310 if you ask me I have a Cho chice of saying 310 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 run you can see it's it prints in B show now this will make much more sense when we say we are creating not a a but we are getting a calculator which has a method called let's say uh let me remove config for some time let's say 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 N1 comma int N2 and here we have done this before and that's why I'm writing this code very fast I can say return N1 + N2 okay so if your class which is instead of saying B I can say Advance Cal which is extending Cal 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 N2 + 1 I'm just adding extra one 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 Cal and if I create the object of advanced calculator by saying advanced calculator obj equal to new advanced calculator and if I try to call the add method by passing three and four of course three and four is seven 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'm passing three and four 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 Cal you are my plan class I'm searching for ad with you and yes it is available so basically we can just compile this code and one 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 add is overwriting the add method of calculator okay just remove this statement that's extra so this concept is called method overwriting so this method is overriding the method here I hope that makes sense 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 sad songs romantic songs and in fact in 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 will be having multiple files sometime 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 in 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 just to 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 uh you know you have an option here of creating a new folder that's one way or you can what you can do is 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 got a folder called database or maybe DB connections or DB classes or maybe you can see simply a 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 myql 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 gal and demo Advanced Cal and if you can see both are in a package now the only problem we 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 gal here so if I say Cal obj equal to new Cal which was working earlier if you remember our previous examples it's not working now you can see it says Cal cannot be resolved what's wrong the problem is demo and Cal 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 a indirect way uh yes we have an IND way so what you can do is if you if you see this bubble it says import Cal that's right if you want to use Cal in your particular class you have to say import Cal but then Cal belong where Cal belongs to tools. Cal 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 advanc calculator okay now you cannot access access this one you know why is because we have only imported Cal not advanc calculator how will you import this you're right just come back here and say import tools advanced calculator so whichever class you want to use you can just import them in fact Java internally has lot of classes example let's say one of the classes which we are going to see in future which is let's say least now when we use least which is a inbuild class or input interface what is interface we'll talk about that later or maybe at this point we can talk about let's say add list which is a class when we use add list and when you create object of it I will say list is equal to new aray list now the moment you do that it will also come from a package you can see uh it is now given be given by the IDE it is auto completing it but if I remove this line you can see it will give you error it says adist 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 do add list this is how to you have to import now you might be thinking why we have two dots here Java do util how it looks like so basically we have a Java fold inside Java folder we have a util folder so the it's the folder inside the folder and Java has multiple folders if you can see Java dot uh we have applet folder we have awt folder beans folder Lang folder IO folder and all these things so in general whatever class you use in Java belongs to a package let me repeat whichever class inbu class which you use in Java belongs to our package now you'll 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 we have never imported it so when I say system. println there's there was no need to import the package but does system belongs to a 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 now the thing is uh it's actually this not this line what is by default is this java. Lang dostar 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 also removed list we don't even need that ignore that so basically if you want to import all the files you can say star okay uh 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 this tools actually belongs to others so now if I move this you can see we have tools inside other now how it will look like when you talk about your Cal so you can see it says other do Cal so you have to if you want to go inside the package normally in folder structure we use slash here we use dot so it is others. tools do uh Cal 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 do star now is this two line same or can I remove this of course right when you say star it should import everything but no if you can see it is giving you error the reason is when you say others. star 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 name this as a. Java the moment you do that you can see this a class belongs to other package now you can easily use a but not Cal so if you say a obj2 is equal to new a this will work is because you are saying others. star it will import all the classes which are all the files in in the other folder not inside folder which is tools so if you want others I mean tools as well so you have to mention others. tools. star so that's how basically you use a package structure now there's one thing you know not important at this point but since we're what 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 are creating a library that's right what you do is when you create a project is not always for uh your use sometime 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 now librar simply means if you want to do something and if you don't want to write your the code entirely by by yourself you can use some libraries and most 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 driver 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 be 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 we 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 I will say com. gooogle do calculation now this is my package name okay so by making com. gooogle 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 reverse the 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 package 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 protect 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 we were trying to import the packages and of course we should do that right anyway we don't have to do that java. language 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 the class A is public okay and it ons 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 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 a marks let me go back to demo and in this I want to access it so I will just remove all these lines just want to work in fact fact tools is also not I need now let only work with other and in this let me create a object of a now can I create object of a yes we can because we can access it and then I'm just trying to say obj do marks now I trying to do that you can see it gives you an error it says the field a do marks is not visible I mean that's weird it's there right marks is there the thing is it is not public now when it is not public you cannot access it outside the p 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 uh let's say in the same package which of which is of demo I will create a file called b. Java and in b. Java let me just say because there 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 St 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 I access marks so you can see there's 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 variable should not be public but method should be public example if I say public void show here now show can show is accessible I can use show from anywhere example if I can come back here obj do show this will work because that's public but if I don't write public there it will not work now that's one so public means it can be accessed from anywhere okay now apart from public we also have uh 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 is a secondary part it can be used in the same pack uh 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 the same package right but now if I create a private variable here if I make this as a private variable which you which you should always Mark your variables as private the moment you make it private even if they 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 let me retweet when you talk about public it can be used from anywhere okay you can see this table uh 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 you talk about this a if you don't mention private something should be applicable right if it is not private it 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 is as simple as that it it can be accessed in the same package example if you 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 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 protector okay and then I'm going back to demo and in inside this demo somewhere I'm creating a class in the same package basically they 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 marks is not working so basically if you make it protected you can't use it outside the package before coming there in fact you know what 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 do 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 subass we have class C here and then if I try to create a method public w 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 can not access marks outside the package but you can access marks outside the package but it should be a subass if you can see C is a subass of a now if you see this image here this is how 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 subass 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 we 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 subass 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 keep it private protected or public in this video let's talk about polymorphism remember we have talked about method overriding and then we said there 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 it has two words one is poly second is morphism now poly simply means many and morphism means Behavior now what it means is there's 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 location 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 uh because the name also changes right at home I'm a I'm a son husband a father at at office I'm a colleague I'm a boss I'm an employee at my with 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 objectoriented 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 the instance 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 poph also called late binding but we have these two words compile time and runtime 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 time 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 get executed at the compile time itself it is compile time now which method will be executed will be decided at run time 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 uh which is ADD which takes two parameters int and int and then we have one more method here which is ADD and when you say int int int 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 to are two different classes and Class B extends Class A we have talked about this in method overwriting 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 two which one will get will get get executed it will be decided at runtime so that is called a runtime polymorphism in fact there is one more concept which we're going to disc 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 Class A okay and let's say we have class C and again we have discussed most of the topics 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 child there 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 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 let's remove this class just to remove the confusion let's go step by step so let's say we have these two classes Class B ex 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's 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's one more thing can I create a reference of a an object of B is it possible since b 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 to 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 how will you do it it's very simple you will say laptop let's say lap is equal to new laptop so let's say instead of saying uh lap 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 and 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 the 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 here it is possible to create a reference of a super class and the object of a subass okay and this will surely work let's compile there's no issue let's run and you can see it says in a show 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 a show or in B show as per method overwriting it should call in B 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 OB obj show what I will do is I will make one change here I can just come back here and I can say obj is equal to new B that's right we can assign a new object to the old variable able okay now if you want to understand this with a memory of course we have a stack here right in this stack we have different variables and then we have a HP memory here now initially when you say obj that's your object right and it will create object of a so one1 is object of a I'm 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 one 1 so I'm talking about line number 24 okay on 24 this is a link now when you say obj do show line number 25 obj do show at that point it will look okay so obj do show that means I have to call the 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 one3 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 one3 there's no 101 anymore that means this link this link is broken and we got a new link now on line number 29 when you say opj Dosh show it will call the 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 we call the B show let me compile and run you can see at first for this line it is printing in a show for this L is 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 say this is in 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 do 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 op. show is behaving differently with different objects polymorphism right so if you say op. show we are not even sure so when you compile this code at that point we are not sure op. 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 it's called runtime or polymorphism and all this concept is called Dynamic method dispatch because that's Dynamic which method will it will call we're not sure okay and that's how it works so point to remember here is irrespective of what type of object you have or what type of variable you create is it type it's of 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 creating a new class class D and uh you will say okay not demo 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 if you 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 the value for Num is 8 and of course when you make a variable of course you can change the value dator right so initially the value was eight and now you're 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 nine 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 uh I guess in C++ 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 one 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 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 why 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 variable 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 uh let's say calculator okay now I I'm making this class and in this or maybe let's keep it simple let's say a so I 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 calculator just to make just to make it much more sense and I will also say public void uh 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 Cal show Okay so let's say we have this class here which is Cal and then we have these two methods and if you want to make it work of course you have to get object of Cal you will say Cal obj equal to new Cal 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 four comma let's say five right now with this let me just compile this code and run compile no problem and run you can see there's no issue it is printing in Cal show and N now what if someone else is trying trying to create a class called advanced calculator and maybe you maybe they're trying to extend they're trying to inherit the features from your class which is galp okay and by default it's possible anyone can uh inherit your method or your classes right because it's available what I want to 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 subass 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 uh class for 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 Cal show or maybe I can say by naven okay so maybe this this Cala is by naven 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 addition because there's no credit 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 by 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 by 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 overwriting 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 a class to stop inheritance and you can you can use final keyword with method to stop method overriding so I hope fin makes 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 AIS of equals method then we got notify hash code uh notify all to string weight so many methods right now what they do so let's understand that so what I will do is in fact in of uh 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 in uh 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 uh I can say model as well so we got model and price right so if you talk about MacBook or Del XPS or Lenova laptops or 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 time in 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 OB uh okay so I will say obj do 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 do price is equal to let's say $1,000 okay so I got a laptop I am I got the values right what if I try to print the object what happens behind the 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 theate 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 hexadecimal numbers okay so these are exal format but what is it let's try to understand that now what if I say every time you try to print 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 object it will call two 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 class we don't have any method called two string but in the object class there is you can see we got a two string method which returns a string right that's what 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 adate symbol in between okay that also makes sense why we got adate there and then it prints something in hexa string that's why we got heximal 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 uh to know more about hash code 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 by default 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 super class but what if I create that method itself so the public method which returns a string the method name is two 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 he heximal values it's just trying to call two string which is this and it is wring hey it's not going for the super class here that is awesome now let me also try by deleting this thing okay let's not call to string by ourself 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 coloda in between and the price so that it will print that particular data compile and run you can see it says Lenova yoga ,000 okay so that's how we can basically work with the object class methods if you don't mention it will be uh coming automatically by the object class now there's 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 they same yes the values are same but do we have the objects which are same let's try how will you compare this so I what I can do is I can just create a Boolean variable here and I can say result is equal to how do I compare obj 1. obj2 or obj 1 is equal to equal to obj2 let's try I mean we can use double equal to between obj1 and obj2 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 Inu 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 uh 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 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 ourself 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 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 1 and if I try to compile and run you can see it's say is false because we have a different value cool okay in fact you know I can actually simplify this more why to even mention e Els 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 and it will return true or false I can do that in one line yeah this is 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 and equals click on that select all the variables which you want to compare and click on okay it will generate the equals method for you and you can see it does mult 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 it does multiple checks for you it's it's a good practice and that's why I say sometime you have to prefer your own IDE output and it also implements hash Cod you know this is how the hash is generated basically uh it takes your data and works and and applies some algorithm on your data to generate the result 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 I mean same model same price but different sale 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 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 this two extra annotations here in fact 3 this is actually we'll discuss that once we talk about annotations at this point this is 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 hashcode equals two string and yeah in this video we'll talk about what is downcasting and upcasting now before this we have to understand what is Type casting 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 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 will cannot be stored in integer so yes we can at least store four 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 a type mismatch cannot convert from double to it's because it says you know you might lose data and we can say hey that's okay for me I'm okay to lose data so what you can do is you can typ cast it so you can say hey I don't care what value you send just send that in the integer format I'm okay with it okay so this is type casting 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 stowing that integer we will lose data so we are losing 0. five but that's okay we at least we got four now this is called typ casting 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 in 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 and I'm just trying to keep things as simple as possible we could have gone with other other names as well like calculator 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 inheritance 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 these two classes I want to have some methods let's say in a I have a method which is show I maybe I will say show one and in B I can have a method which is public void show two 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 right right but the idea here is to understand the concept so 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 show one show two two different methods and I can call show one and you can see I can only call show one 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 show to 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 uh let let's try this compile and run you can see it says in as 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 method dispatch we can basically do this right now what we are trying to do here is we have an object of class subass and then we're trying to assign that to a pattern class now we can actually say hey you know what you can do please typ cast it so whatever object you're sending is of child class but typ cast it with a which is a parent class the same thing we have done for the integer and double right now when you are doing this we are basically going up right which is if you if you try to compare this A and B A is a a is a super Class B is a subass so we are trying to say refer I mean objectives of B but refer to a so we are going up which is upcasting so this concept here is called upcasting and of course this will work there will there's 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 even 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 downcasting first of all see using this even if the object is of B using this obj I can't call show two 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 to okay so to solve that problem what we can do is we can create a new reference let's say obj 1 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 type casting here we can say hey I know this is reference of a but the object is of B right so whenever you assign I want the type to be of B and that's how it works this is called downcasting 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 to okay so this is downcasting this is downcasting this is upcasting in general they are called typ casting okay uh so you don't have to actually remember this terms every time upcasting down casting at after some time you actually 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 let's talk about wer classes now we have talked about primitive variables right example if you want to create if you want to store a number like five of course you will store that in a int variable or maybe short maybe white that's your choice uh if if you want to work with a value which is point values let's say 6.5 or something like that you will be using double off float and then if you want to store a character you will use a car right so Java does provide you primitive data types and that's what makes Java uh 99.99% object oriented that's why Java is not purely object oriented because if when you talk about a language like object oriented programming language it means everything should be object but that's not the case with Java Java says okay we are still going to work with something which is not extending object as we know every class in Java extends object class but not the Primitive values if I talk about int float they are they are primitive types they are not object types right it's good because it helps Java to improve the performance because it directly works with the fixed site value right so it is not saved in a heap memory as an object which will which will be time consuming so what it says is if you want to store a value you can do that directly in Prem but then there are certain features or there are certain Frameworks in Java which only works with objects example let's say uh in future we going to talk about collection framework in collection if you want to store values so just to give you a heads up a collection is basically similar to array at this point just remember that and we can have multiple values in Array we can have the the Primitive types but not in collection you need to have object type and that's why Java says let me introduce something so in Java we got this concept called a wer class now what's a wer class for every primitive type we are going to have a class for it okay so if you want to work with int you can work with integer which is a class and this integer class basically extends the object class you have a c we have character as a class we have double we have a double as a class and Leist goes on maybe it is short then we have bite for every primitive type we have a class for it okay and it's quite good if you want to work with uh a framework only which only supports objects but yes if you want to improve the performance and there's no restrictions primitive still makes sense and that's why most of the examples we have used primitive type but again there are certain requirements where you have to use classes now how do we do that so let's go back here let's say I have a int variable int num is equal to 7 now how do I store this value into a class type or the object type so the class we have is called integer right so let me get a variable called num one so that that's my reference object now reference variable this is a primitive variable this is a reference variable now since this is a class integer we can say new integer and in this integer we can say uh whatever value you want to store let's say I want to store eight now you can see there's a line on this so whenever you see a line in any any of the IDE it simply means this particular syntax has been dicated depcat simply means it was there I don't use it now because in future release they might remove it or we have a better alternative for that but yeah this is one of the syntax we can use so we have new integer and you can pass a value which you want to store and of course we will see what is a better way of doing this apart from this replicated but at this point let's continue with that and let me print number one and let let's see if that works in the same way as we were doing okay you can see we got a warning there's no error we got a warning but let's run this code and you can see we got8 if this is not a good way of assigning a value what is a good way what we can actually do is instead of assigning like this we can directly assign a value called Eight here and this works you can see there's no uh no dicated warning and if I compile this code and run there's no warning as well now how this is happening first of all for every type we have a integer we have a class type right now what is happening here is example let's say if you want to store this int n variable which is a primitive type in this instead of saying eight let me just do that here I'll pass num now what we are doing is we are taking a primitive value and storing that in a object object now this concept is called Auto boxing so this concept is called boxing not Auto boxing this concept is called boxing because you are taking a primitive value and storing that in a primitive object or in a wer object in a class object that that is called a boxing now since this is so important what they done is you can directly assign num now so this boxing is happening automatically see behind the scene the num is a primitive type right this n this num one is an reference variable so of course it should be an object so this will be getting converted to object automatically and that's why we say that is autoboxing when you convert it automatically okay uh how do you assign the value to again A Primitive value let's say we have num two and I want to assign the value of num one to num two now in this case you can say uh num one and then this num one is an object type right you have to get the value so you can say get integer and you can pass the value which is number one or so you can pass you can simply say get integer okay get integer giving you an errow okay so not get integer we have to we have to use a different different method so I have to say dot what's the method name yeah so we got int value so the method name is int value so from this object which is of type integer you can see integer class which has lot of different methods if you scroll down for different feature you can use different methods here but then if you want to get the integer value you can say int value and then if you try to print num two again it will give you the same value compile and run and you can see we got seven right now the thing is if you do this you are saying unboxing because you are getting a value from the object type to A Primitive type right but what if you don't do it manually you directly assign num one will this work and you can see there's no problem now since this is happening automatically we call this Auto unboxing so what is autoboxing is when you store a primitive type in the object automatically and what is Auto unboxing is when you take out the value the Primitive value from the object so this is auto unboxing and as I mentioned before we have different classes example we can also use double even double is a class it so you can see it's a class and uh we can use character as well just write out now apart from this Auto boxing and boxing in integer provides you one more thing let's say I have a string data here which is string it's let's say St Str and the value in this is is actually 7even or maybe some other value let's say 12 now I got this 12 here which is a number but it is stored in a string format okay and then if I want to print this if I want to print s strr maybe I want to perform some operation on this maybe I want to uh double the value so can I say Str Str into into two this will not work right because St Str is a string you can't perform an operation there so what you can do is you can take this value into integer format you can say integer uh maybe num three and in this num three I want to store this 12 how do you get a integer from a string now for that there is a class called integer which we are using and this class has a method called pass int now pass int is a method which will take the string and it will convert that into integer and I can simply say s Str and it will take the value from the S Str convert that into integer and store it in the num three and you can perform num three into two okay so integer is great this is called a VAP class because it's a wer around A Primitive type but it provides you multiple features one of them is pass int let's see if that works compile and run and it worked can you see that we got 24 here so that's about the rapper classes as I mentioned we have multiple classes available try them out and if you directly assign A Primitive value to the object type it is called autoboxing and when you want to fetch the value automatically that is auto unboxing 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 inheritance 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 car you will Define some methods let's say if you talk about car a car can drive of course right uh a car can play music as well public void I mean that's what we do right we we play Al we also play music in the car 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 or maybe can we can add some more methods in the car maybe you can sleep in your card and other methods but let's say we have this two 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 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's a car right if you don't have a drive feature then why you will call it a car you can stay it's a uh home tater anyway now if I want to use this car I can simply create an object I can say obj car obj equal to new car and then I can call the methods I can call both the methods obj do drive and obj doplay music of course we have two methods so 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 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 synext 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 we talk about this car you are writing a method here which is drive when you're not implementing it why someone should be 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 the 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 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 I'm 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 cly 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 sometime 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 uh 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 Wagar car which some of them don't like not a big fan of it but okay so let's say we have this car Wagar extends car in fact I have a car which is smaller than Wagar uh which I love so we have this class Wagar extend car here and what what I want here is I want this wagonr to implement the drive feature see if you think car is just an abstract concept right no one actually buys a car what you buy is a real car uh like wagonr or BMW GLS or to Toyota Fortuner or Ford ecosport right so you buy a real car you don't buy a car 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 Wagar to be responsible to Define this method because I can just copy this and paste it here and here in of declaring it I will Define so what I want is I want this Wagar to implement it of course the other feature like play music can simply be extended but not drive drive should be implemented here and Wagar I mean the company who built Wagar 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're 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 drive feature or whatever object you're creating is a car you 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 keyword 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 card 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 it don't do that it will give you error example if I change the method name you can see it will give you error it says the type wear must implement the inhered abstract method car. 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 overwrite the method now when someone creates an object of a carard 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 can I create a 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 create object of not car but Wagar okay and then with this obj you can call drive you can call uh play music music and we have talked about Dynamic method dispatch and we know when you call drive it will be calling Drive of Wagar not of car anyway it 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 uh 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 me in the abstract class let's do that if you come in this section okay you can see 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 CL abstract class which can have both normal methods as well as the abstract method and you can create 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 other 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 find 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 flying okay and now with this you can call the methods you can say obj do 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 cre creating this class Wagar 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 wear must implement the inherited 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 U updated wagonr which can also fly and this will extend now which class you want me to extend the car or the Wagar of course Wagar right because you will get this drive feature from Wagar so you can simply extend it with Wagar 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're lazy to type you can come to this ER 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 Wagar the object will be of updated Wagar 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 creating object of abstract class we are creating object of the class which is not AB 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 read rate 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's no compulsion that abstract class should have abstract methods abstract class can have normal methods also I mean all the normal methods no abstract methods even that is possible next point to 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 subass which implements all the method 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 uh 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's 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 uh age and then we can have method as well which is public void show now we have these two things we have a variable which is age and then we have a method which is 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 create I created 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 do 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 obj1 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 refer B here you need to say a do 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 got a class file for demo that makes sense you can see we have demo. class uh you will get a. class as well because uh a is a class but we also got B is also class right it should have its own class file and that's right we got it we got B do class the only thing is B belongs to a so the dollar symbol separates this 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 uh a do 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 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 do 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 say it's very simple if you you want to call B if you want to call this show now show is a method right it's a nonstatic 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 nonstatic method okay so I'm focusing more on static here right can I create a static class uh yes we'll see that in some time but we got this object object Here and Now using this obj1 I can call uh 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 these methods and that will properly work you can see you got inow and in in config okay now we have also focused on a static class right you can also make this class as static and we all know when you refer it to a 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 dob now this will work only when the class is studing the inner class is static it will not work for nonstudy class now can we really make study class yes we can provided there it's an inner class if it is outer class you can't make OU class static you can see it will give you error it says uh illegal modifier for a 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 inner 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 obj do show let's see if this works compile and run it works you can see got in a show that perfect works now the question I have for you is very simple question what if I want to change the behavior of show method okay that means I want it 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 B basically we're trying to do method overwriting 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 it 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 reference is 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 of writing hold on hold your houses let's see if I compile this code and run it says in B show but now if you think about this class now first of 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 uh let's delete that you can see we got b. class as well now you will say what's the harm in that see the harm is 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 you 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 a 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 so 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 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'll 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 a inner class okay you will say hey how it's a class we are not creating a we are not using a class keyword right that's right we are not using a class 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 the 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 you create multiple classes you will have a different number it will say 2 3 4 and Le 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 once we talk about functional interface uh Lambda Expressions this is very famous concept so just get used to it now if I if you want me to type this code once again 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're 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 cly package 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 inner 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 thing 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 subject 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 seeing that I'm ring this line multiple times it's just that we have not talked about what is ADD override we we have a separate topic on that so at that point we'll talk discuss that so here I just want to say in B 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 see 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 vo in fact I 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 creating 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 in a class and this perfectly works so let me just try it out uh uh 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 method here which is public void let's say config we can create a method for config as well and since we 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 uh you can provide the implementation in the anonymous in class 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 uh 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 uh so they give you a laptop right right so that's that's the thing right if you want to work in a company let's say if you go to a 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 Dev 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 say da app you you develop application 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 uh 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 naven and then if you say new developer and with this object naven I can say na. da app right so I want na to 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 coding right in fact what laptop when you say code you it basically you code then you 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 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 why 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. 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 with 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 say laptop lap is equal to 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 will a lap 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 run so what you're doing is from the main method you're calling Dev app which is here and then you're 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 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 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 is faster than a laptop right so you can see when you're coding uh it says code compile run 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 I will say desk is equal to new desktop and they don't want to pass the laptop object so they when the company says hey naen 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 no will say I can't work you can see nain 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 when I was talking about abstract class and interface I told you about the design thing right so when you make an application you have to make it more flexible here we what you're 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 a laptop developer is dependent on a computer a computer can be anything a 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 give this empty because I don't want to WR 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 advantage is 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 new new desktop we can do that right we can create a reference of a parent class an object of a subass and that's what we're 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 desk 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're 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 see it says faster that's desktop but now if I pass a laptop let's say computer uh company decided to send you a laptop and now when you compile and run you can see it says code compile and run there's no faster here that means that's laptop code so that's how you you design the application right to make it Loosely coupled where your developer object is not dependent on a laptop or desktop it is dependent on a computer any computer will do a mobile phone also do because that's a computer now there's one change see if 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 sometime we don't mention Rose or lies or sunflower we simply say flowers right uh we don't say apartment or uh 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 here 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 uh 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 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 we'll talk about interfaces now before we start with interface let's talk about the abstract class now let's say I create abstract class here so we'll say 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 a 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's no compulsion to have non-abstract methods here or we call them as concrete methods there's 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 this are decided at design time so when you make an application at designing you specify things okay uh 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 that hierarchy in sometime 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 uh most of the time we use this interface in project development uh 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 anywh 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's no error because behind the scene public abstract is there now this is your interface so interface basically 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's no problem in creating a reference of it so that 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 the moment you 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 it's interface you can't instantiate it okay then what's the solution the solution is see first of all why do we create 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 ride 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 you design you have to literally take a walk I will not walk 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 your now I'm talking about class 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's 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 uh 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 this 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 see it works and now using this obj I can call show I can call uh what other 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 see it says in show in config okay what what else can I have uh so the way you create variables here Al so the way you create methods here 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 age 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 int age here and let's say we have string area so that's area name but you can see we got an error that means can I I cannot create variables that's weird I just said I we can right so the only thing is every variable in the interface we can create they 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 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 uh example if I want to print the value of name or area here I can simply say a DOT a is the interface a do 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 do area is equal to let's say if I want to change the area to HB uh 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 what whatever you instantiate is a class and you're not doing extent you're doing implements what you get in implements is only the methods right uh you don't get the variables there in implements so that's why it is static because we 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 of is a 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 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's 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 ER 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 EXN 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 uh 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 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 inance 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 run otherwise if you remove run from here if you come in this part you can see it will give you error it says uh the type B must implement the ined abstract method run okay but we implementing y right it's just that y extends X so we have to use run now Point remember here is whenever you have a class to class we say they 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 interface an 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 run method is if you can see in a we don't have run in fact what we will do is I will just remove this code for time being yeah and then if you want to implement run if you want to call run in that that case we need to create the reference of X which is obj1 is equal to new 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 in this video let's talk about enums now enums are named constants which we create so basically if you uh try to have some constant example you know when we talk about error 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 4 not4 error so basically you know we have this numbers for every type of uh 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 your yourself you can create a 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 uh so let's say this status can be of running state of fail State success pending so you can have all those things and of course you can create a simple class called state and you can create multiple objects right but just to keep it simple uh we can use enum where we can Define all the names constant and this this is pre-build okay uh 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 you can separate them with the help of comma uh 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 to return a status to the client uh that status can be running the status can be failed pending or success instead of WR returning a string format or a specific object you can simply return this status now how easy it can be it's 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 same as what you uh use normal variables example if you want to say int I and then you assign 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++ in C++ we directly create uh these status and we have all these constants and it treats enam in a different way but Java says oh everything is class here 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 these 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 uh status. running now that is my object which is be assigned to S now of course you can print this s here and it will print the running status so I can say compile and run you can see 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 ALS 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 B basically you get numbering for it maybe 1 2 3 4 so that you can pick the status based on numbers in Java it starts with zero so this is zero 1 2 3 and if you want to get that you can actually do it so you know you once you get a status you can use a method called ordinal so so different constants here have different numbers this is based on the numbers right the order of it so this is first second third fourth right but when we have zero 1 2 3 so when I try to get the ordinal of running of course it will print zero and that's what you want to see so you can see we got zero but 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 do success it will only give you one status but what if you want to get all so in that case in 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 the status is a class and you get some inbu methods now from where you're getting this methods we'll talk about it in some time or maybe maybe in next video but uh you got methods so you can see status. values will give you what okay so let me just jump to values just to see uh what it will give you so status. 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 are you can print all how you can just come back here you can print all let's try I'm not sure if we can get all here no compile time issue run or it is printing the address or something ignore that okay so what you can do is you can specify the index value I can say zero 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 enhance for Loop here and every time you iterate of course it will give you status right so what is this array this array is 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 see 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 I can say s do ordinal it will print yeah so it will also print the order of it 0 1 2 3 yeah so let's expand our example of enum with eels and switch now as I mentioned before these are constants right so basically when you have this constant can you 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 which I want to set is let's say uh pending okay initially the state will be 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 we have to compare right so you can say if the S is equal to equal to status. 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 uh let's say pending now in this case I can say please wait else of course you can also do if else else if here but we only are left with only one status so 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 because the St is pending but what if you got a status which is uh running state in that case it will print all good okay so this is how basically you can work with eils but what if you want to work with switch uh because see in switch there is a limitation which no a switch can support right uh 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 E I will just reuse the code but I will use a switch here and in this switch you can pass the status as it is there's a change compared to eils which is when you say case you know in eils you basically have to use the status as well but in case you don't have to do that because anywh the S is of type status right so it knows whatever you have passing is basically 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 uh switch we have to also put a break [Music] if it prints done yeah we got done this also works with uh switch so that's how we can use if Els and switch with enum if you remember we have talked about enum and I have also mentioned that enum is a class right so does that mean we can extend an any other class uh the thing is no that's the 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 con structors 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 just remember with enum we have used certain methods 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 this methods so by default enum in Java extends a enum class okay uh let me just prove that to you I will just print it here as dot get class do 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 I'm just trying to print the super class of our class inum and I will just compile and run you can see it is enum so enum in Java extends enum class and all these extra methods you get it from enum class 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 and 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 D XPS then we got surface not a big fan of surface but it's a surface uh the Microsoft laptops and we have ThinkPad I used to love ThinkPad when it was with IBM but anyway okay so we got this constants here 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 $2,000 uh XPS will cost you around let's say uh $2,200 XPS are bit costly and surface is let's say 1,500 I'm not sure the cost and let's say ThinkPad is $1,800 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 see we are trying to say that this is a price but how will your enum will know that you're talking about a price here now in this case what we can do is let's create a variable int price so that we can have a instance variable and then if You observe here don't you think this is a Constructor you can see it says that a Constructor of laptop that's right we can have a Constructor here so when you create the object by passing a value that's what we're doing here we 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's no error that means whatever value you passing here will be assigned to this prize 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 prints 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 do we 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 gets and sets 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 lap. 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 2,000 now what if you want to get all the laptops okay let's try so I will just commment these two sections first of all you need to get all the laptop that means you need arrays so it's a laptops 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 uh you can print the price let me print the price how do I get the price it's a lap. 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's 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's no Constructor here so you can say okay I know how to solve that you can create the 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 $500 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 can see 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 was created with the help of default Constructor and we are not printing anything there okay so that's how we use it 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 annotations simply means a supplement to the compiler or to the runtime or we also call it as a metadata so basically what happens you know when you write a code sometime you want to interact with a compiler by saying something of course it will not change the way your code will work is 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 overwriting 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 uh 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 B and then we have seen the this before right okay let's create the object of B just to understand this I will say b obj equal to new B and then I can call obj do show and we all know what will be the output uh so if I uh 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 show 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 sometime your method name can be big and yes okay I know this is exaggerated but sometime 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 would 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 and what do you think what will the output of course we have overd the method so it should call in B show 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 show 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 are simply logical problems where you are expecting something else and you got something else example you want to say 2+ 2 and the output is four 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 time in 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 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 will 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 in belongs here we have S in this method I don't have it and sometime you can miss it right and then when I'm calling it I'm passing s on purpose I was calling meod of a class this type of problems can arise when you write a code so what you have 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 override 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 show the intention with the help of annotation there are a lot of inbuilt annotations available in Java one of them is overdried so you can simply use atate 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's 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 uh at least you got some help and solving the problem at compile time is much better than solving the problem at run time 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 override is one of the way you can do that and now if you're trying to compile file and run you will get in B show now apart from this override we have different annotations as well uh which we normally use in terms of cod 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 hybernate 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 dicated now this is one of The annotation we have now what we are saying that we have this class A dicated simply means you can use it but don't use it it is replicated soon it will be removed from the Java language or there is a better alternative for this so you can mark your class as dicated so that if someone try to use your class they will get to know that this class class is duplicated now apart from this you can also use okay these are all class level annotations in fact we are going to see one more 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 save verogs for the variable arguments we have suppress warning if you want to hide the warnings once we start with hypernet framework we'll also say transient a function 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 retention 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 uh options there and of course this will make much more sense once we start start working with Frameworks so yeah that's about annotations and uh so we have different annotations and remember when we were doing method overriding and if you ask example let's say if I remove this part and if I ask my IDE to generate a override 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 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 uh functional interface and the third one is your marker interface now this functional interface is also called as Sam so why it is called Sam we discuss in sometime but let's talk about interface here now when we 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 see that later at this point we only know that in interface we can only declare methods so all the methods in 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 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 one we have is a marker interface now what is marker 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 calization 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 HP memory uh okay I will do that here so let's say in your HEAP memory you've 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 is 30 and then if you want to 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 this object is because what if you want to recreate this object later so let's say when you are playing a game and when you're playing this game you want to save the game now when you save the game let's say when you're playing this game the game takes around 4 GB or 5 GB 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 restart 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 more but you have a file in your hard drive and now you restarted the game the value for a BC will be 00 0 0 what you can do is you can load these values from here now this is called D calization so this is serialization and this is D serialization 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 Mark interface so we have a lot of Market interface in Java uh calization or serializable uh then when you talk about the advanced part also we have some interfaces but it's it's important okay so whenever you see see interface which is blank that's a market interface and maybe in the upcoming videos when you talk about some Frameworks we'll talk about different Market 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 uh try to understand this every every time you have a interface which has only one method uh we can use some extra features with it now let's talk about fun 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 uh 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 method 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 function interface and my intention was to have a function interface but if I if I write one more method here which is one okay there's no error here and we want errors right so if you have an intention of creating Only One S I mean single abstract method interface which is only one method and then by mistake you return two methods here you want compile time erors right so if you write it function interface you can see it will give you error it says invalid functional interface annotation a is not a functional interface so it it's good to specify The annotation here by specifying a function interface but then why we are giving so much importance to function 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 in of creting object of a I can get object of B which is possible right and then I can call obj do 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 come in this part and I can come back at by Say Hey I want to instantiate a by defining its own implementation here okay in fact if I just say enter back it'ss open close okay so you can do this and then then here itself you 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 in our class and we know how to use this right and then I can print a show here let's see if this works compile and run you can see we got in show now since it is a function interface uh now we are talking about Java 8 feature so till this point everything was uh good till Java 7 now in Java 8 they have done something awesome you know they made sure that Java is no more veros now what is veros means is specifying everything making a detailed code it's good most of the time to read the code but sometime 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 expression here so what we are doing is we have a function interface and then we have discussed about it function interface is an interface which only has one method and then we were able to instantiate a with the help of anonymous n 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 in a 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's 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 cly brackets 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 a class of interface here a class which implements the interface so basically when you say a obj this is something you can imagine right this 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 nothing it's a white type and it's also public by default so from here till here you know it right so if you know it even compiler should know this right why you as a programmer need to type this code what if Javas says hey don't worry I'm there you just don't have to type all those things you just say bracket 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 lambra 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 cly bracket here we have to remove this one as well so first let's remove that cly 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 a obj equal to it's your job to complete the sentence see behind the scene the compiler will do it for you it's there behind the scene new a uh 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 see if you have multiple statements you need cly brackets but if you have only one statement you can skip that as well in fact you can skip the 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 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 rewatch this video The Cutting part and try it out now let me can me do something else here what if the show method actually accepts a value which is let's say in 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 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 or any variable name 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 five 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 is again from here till here we don't need that and we can remove this cly packets as well the only thing you have to do is you have to put a arrow here we don't need the 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 in J as well and then here also you can accept in J just it's just that you have to pass two values let's say five and eight we are 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 now this 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 int why to mention it okay and one more thing if you have only one 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 old one let me just delete all the class files just to keep it simple delete and let's recompile the code here compile the moment you compile you can see it it is creating only two files a. class and demo. class of course behind the scene there's Anonymous class but it is not creating a class file for you so it sto 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 return 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 in 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 object I will say a obj equal to new a and then you can also go with creating a different class CL 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 int I comma in J and now you want to return I + 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 + J okay and then once you have this we can simply uh take the value in result is equal to obj do 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 cly 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 cly braet 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 cly brackets now now we can write everything in one line as you can see there 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 a WR type okay so when you have one statement which is return which is an expression you can see I plus J is an expression it becomes a written type a written value one more thing you can remove this int and int as well and you can see the code is so small now now and that's the beauty of Lambda expression and one more thing so let's say if you are calling a function or a 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 and 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 fun functional interface if you don't have functional interface it will not work because see if you have multiple methods let's say add and subtract both and when you write this type of line which method you 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 we'll talk about exceptions so before you understand exception you have to first understand the 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 these 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 here but even if you compile this code you will get a compile time error so it says error 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 uh of course when you compile this code you will not find any any eror 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 runtime what is runtime error now in fact let's talk about logical first and then we'll come back to R time now logical error means everything is working there's no compile time issue everything is working and then you what you did is you added two numbers let's say you added 2+ 2 and of course for this the output you were expecting is four and the output will be before 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 code where the output is not four output is five now it is working there's no compile time issue there's no runtime issue it's 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 errors are called logical errors also some 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 test in 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's no compile time issue there's no logical issues now runtime issues are 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 runtime errors your execution normally stops and that's an issue Let me Give 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 uni 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 were 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 where 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 $1 million 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 criticals example let's say if we talk about medical softwares uh you have all those scanning machines and then which where you go inside the machine and then it scans right uh it throws some raise 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 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 there 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 we have talked about what is exception so exceptions are basically runtime erors it's not like we are going to stop the runtime errors 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 exist if the file exists then you open try to open the file those are the things you can avoid runtime errors uh 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 exception runtime error 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 exception so 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 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 we will do is uh let's talk about statements see state stat can be of two types 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 situation 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 uh 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 panick right you will try try to say a kid is 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 kid 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 in I is equal to 9 now this statement is normal statement because we know what what may go wrong here if looks good right so this is a normal statement but what if you say int J is equal to I IDE by or maybe I can say uh 8/ 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 we will get some output instead of zero compile and run this works right but what if the value of I is not 9 it's zero 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 runtime error exception in thread meain 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 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 reason it 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 an 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 throwing 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 e 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 four in this case you can see there's no exception so let's try to execute this with the value four and if I run this code you can see it says four and buy there's no problem with the value we got four 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 zero 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 can you see that it says something went wrong now even if something goes wrong the execution is still happening the value of J is zero because before the execution of this try catch the value of J was indeed zero 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 and try inside Tri block and you can write multiple Tri blocks uh so one we have it here what if you have some few more statements here you can try catch thata as well that that works so I hope you got the idea what is try try will basically try to execute the statements if everything goes good it will execute the remaining statement if something goes wrong if there's exception it will execute the cat block so point to remember the cat block will be executed only in case of exception otherwise cat 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 Tri 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's 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 it is this if I create an array let's say the array is of type integer and let's say 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 zero of these nums and you can fetch the values let's say at this point I'm trying to print the value of nums of zero or one 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 of five what value you will get now first of all the size of this array is five 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 0o so 0 1 2 3 4 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 zero here or maybe let's let's keep it zero I will say compile and run of course you will get the error because uh the dividing number which is I is still zero let me just make it to see the problem is the moment you get exception here it will directly jump out of this triy block it will not even execute the remaining price statements okay and that's why you you 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 cat block but look at the exception the exception is aray 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 arithmetic exception we have array index outter 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 five here uh nums of five 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 a 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 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 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 error is still with the aray not with the denominator but if you run this code you can see it says cannot divide by zero now that's weird because I'm trying to fetch the value which is outside the limit and it says cannot divide zero that's wrong this is the error makes sense when the value of I is zero right so if I compile this code and run now this works you can see it says cannot by zero but then if you if the value of I is not zero in that case as well it is printing cannot divide by zero it's because the moment you get the exception in try it can be from this statement or this statement it will execute the same catch block and in the same catch 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 array index aut 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 here instead of saying exception I can say it is aritic 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's an aray 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 zero and let's see what happens again I will compile and run you can see it says can by zero that makes sense because we are able to handle the exception and you can see we are printing by 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 there's exception you can see it's a there's a cross symbol as well it says AR index Auto bound exception that means we are not handling it even if you mention that in the tri 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 catch you can mention array index out of bounds exception that's why 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 run you can see it says stay in your limit there's no abnormal exit of the code it it is executing everything and this time it is executing the cat block the second cat block not the first one but what if you make this zero then and again it goes in sequence okay so in Tri block it goes in sequence it will first execute this statement which may throw arithm exception and if that works well it will go for this one this might thr an exception which is ADD index aound exception so let's see if that works compile and run you can see it says cannot de to zero so you can basically have two different exceptions here now what if there's 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 this 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 inheritance 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 what went wrong so you can say something went wrong now if there's something some exception which you are not handling in that case it will execute this part to give an example let's say I create a string here string Str Str and let's say the value of Str Str is null as of now and by defa it 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 do 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 we'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 lot of exceptions and we'll understand that in the next video but then before that we have one more thing what should a sequence here the sequence is whenever you have a child and parent make sure that your parent is at the bottom so you can see exception is a top class right parent let me show you how it looks like so if I go to arithmetic exception now arithmetic exception is a class which extends runtime exception so runtime exception is a parent class for arithmetic but if I click on runtime exception you can see it extends exception here so exception is a parent of parent and in fact this exception actually extends throwable so there's one more class called thel which is like the topmost class and yeah so that's how 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 isue 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 two and that's why it says you know it says this exception cannot it's unreachable can you see 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 is a super class right and in that we have lot of sub classes 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 you will understand okay what class belongs where and which exceptions are compulsory 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 it 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 we have seen exception class extends throwable so that's on top one more trivia here most of the names which ends with able are actually interfaces okay uh runable civiliz clonable but coming to exception throwable 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 seen exception right so we have exception here and apart from this we 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 errors you can't okay when the errors 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 debt what if your thread dies example of this can be IO 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 erors 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 subass of exception is runtime exception and on the other hand we have lot of different exceptions here example of this could be SQL exception example of this could be iio exception and many more but we are just trying to understand the simple one so we have this two 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 arithmetic exception uh then we have aray index out of bonds exception and then we have nullo exception now all this comes on rtime exception this is 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 the 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 check 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 the all the classes here it's your choice if you want to handle them or not but whenever you compiler sees that there's 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. forame 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 throws yet but we'll talk about it so basically these are all checked and this are all unchecked here and there is 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 your throwable which is a parent class of exception and error in exception we have lot of different exceptions if you get SQ exception in your code you have to handle it compulsory IO exception compulsion apart from this if you have runtime exception it's your choice to handle 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 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 divid by 0 and let's put only one statement here let me also remove this one because not needed we only want two 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 two so everything is good and you know most of the time people will put cly brackets on the same line and it normally works and this is 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 uh compile this code and run everything is smooth you can see we are dividing the number 18 by 2 which is n 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 quotient here would be zero if you can see the value is zero 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 Z 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 / 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 error or something if someone enters a denominator zero 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 a 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 there's an 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 our 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 is a backup database you're 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 zero you want to print the default output is 18 maybe you want to 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're basically trying to handle the exception and if you don't have the exception it will not call the cat 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 zero okay because we don't want zero in this case if the value of J is zero you can basically call The Catch block but how do you call a catch block catch is not a simple function right where you can write catch by passing a massage no it's not a normal function or normal method what you have to do is you have to basically use a keyword called through 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 the this works compile no issue run oh can see 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 zero 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 if you try to compile this code and run yes it will say arthic exception but you can see is not printing the message I want the message as well remember it was printing slash by0 or div by Z 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 uh you can do that in the Constructor so you can see it says this is your message I don't want to PR zero so if you want to send the 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 throw keyword so just to reiterate throw keyword is used to throw the exception okay and then catch block will be catching it now basically we were able to throw the exception here right which is arithmetic now if you can throw the exception and what if you want to throw your own exception let's say I don't want to throw arithmetic exception I want to throw my own exception can we do that can I say my exception you can see 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 in of my exception I can say n exception you know what Navin 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 Nine 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 n exception which accepts a string St Str okay string string that works for me and still it's not working it says no exception of type no exception oh I have created a class right the class is there what's the problem I have created my own exception 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 see n exception exter 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 that's weird why something went wrong I want actually to print oh okay the thing is you are throwing exception but you're not catching n exception I want to print this one that's a default output so what you can do is instead of n arithmetic exception you can say no 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 no exception okay that's good where is this message I don't want to print zero the thing is the message which you have sent actually is received here but this output when you try to print e 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 can see that it says no 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 to reiterate n 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 runtime exception and if I clear this compile run it works 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 plal form of throw it's not like if you want to throw multiple exceptions you will be using throws a lot of people have this confusion throws has a different working so before you understand throws what I will do is uh 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 cat block as well so just try to imagine this in a coding perspective let's say we have a statement which is critical and then you try to handle that with the 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's 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 D method and E method of course in C let's say we have some more statements let's say we have method C and in 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 Cat by ourself 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 TR catch in each of the method can we just handle the try catch in C 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 Tri 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 TR 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 saying hey whatever problem arises in D and E they will not handle it they will just send the ex exeption to the method who is calling it sometime 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 C it will again create a problem for you because it will stop the execution now we have one more thing what if you're 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 TR catch in mean then B says okay even I will throw the exception c will say even I will throw the exception I will not handle the Exception 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 throws keyword now this makes sense for checked exceptions okay see for uncheck exceptions it's not compulsion for you to handle the exception but whenever you have a checked exception example if you talk about IO exception okay now in terms of IO exception which happens when you use uh 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 filing 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 was compulsory example remember we have worked with one of the thing which is called class. forame 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 compulsorily 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 Cal here and you know we don't have a cal 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 Cal 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 for exception which class Cal it's not there but if you do it for demo there's 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 show of course you have to get object of a you will say a obj and then you will say obj do show okay and now if you try to work with this when you say compile and run you can see there's no problem because anyway we are not printing anything we 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 cre block and I will print class loaded in fact you know 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 getting some output again the class load 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 Cal now in this case if you try to work with Cal it will give you an exception you can see it says not able to find the class which is Cal 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 statement 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 a 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 this same thing is happening here it says hey I'm not responsible to handle the exception talk to my manager which is main because main is calling show so it is Main's 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 throws class 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 jvm 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 show because we know who is calling show it's your main method so main will be handling it so don't try throws 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 tray 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. forame 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 throw 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 for 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 inheritance polymorphism but how do I take the input from the user because in between as well I mentioned that what if this value comes from the user and at 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 see 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.out.println right now you have to understand this first see print Len 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 an in 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 h on this print Len if I click on it you can see it gives you his definition it says print Len and print Ln method belongs to print Stream So print stream is a class and print Len is a method of print stream okay that means if you want to call Print Len 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 from 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's is static so we can use out with the help of system.out and once you have the access to the out object you can call Print Ln 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 input 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 a method we have to use now by any chance if you're coming from C C++ we use C out C in in C++ uh we use print F scanf in C right and it's straightforward right there are straightforward methods but in terms of java when you say system.out.print Len 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 there's 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. 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 a int value right it returns a int value that's great the only thing is it might throw an exception and if you can see which exception it's 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 TR catch here or you can be evil and you can say throws exception not a good idea but it works for this code since we Hadar so we can do that but when you are making an application don't do that try to use strike 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 enter a number I'm excited let's enter five and enter oh we got five but we also got three that's weird I don't want three there okay let's try another number let's see with three oh we got 51 what is happening here the thing is the system. in. read actually gives you the ask key 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 ask key value the same way for Capital a it is 65 for small a it is 97 I guess for zero it is 48 example if I enter the value zero it is 48 for Value 1 it is 49 so list goes on right so basically it is not giving you the exact value it is giving you the ask 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 not not 48 you subtract the number by 48 not by 48 minus num it is num minus 48 and now if you try to run this code 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 asky value we want whatever asky value you're getting we subtracting it right so if I enter six okay if I enter six 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 asky value what if you want to get a bigger number what if you want to get example if I give a bigger number here uh let's say 55 you can see this only gives you five 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. read we have a special option which is there's a concept called buffer reader now buffer reader is basically a class which works with IO okay and it belongs to a package java. iio so if you want to use buffer reader you need to import this package so IO exception buffer derer both belongs to IO package now whatever class we have used till now most of them belong to java. Lang which is autoimport 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 of buffer so we'll say buffera BF is equal to new Bufford 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 of buffer reader buffer reader Constructor says okay I will work but first you have to pass an object of a reader okay now which it is talking about so basically there's one more class which you have to work with I know this code will be bit lengthy because that's how Java has implemented it but let's use it in fact there's a better way as 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 creating here just pass it here your job is done but there's a problem you can see 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. 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 uh you have to get object of input stream reader then you can create object of buffer reader by passing the object of input stream now once you have it you can create our n variable and you can use Bufford 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. 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 see 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 sometime but the important thing is whenever you use buffer reader it is trying to access input stream reader and the beauty is buffer reader can take the input from anywhere not just from system keyboard it can also take input from the file from the network and and you can mention whever you want to take the input from you can mention it here you can mention the file you can mention all the other stuff now the thing is buffer reader whenever you 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 do close can you see that we have a close method so it is 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 resources okay otherwise you are leaking resources you're leaking 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 reader 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 dater 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 do in that's constant because we have to mention from where you're getting the input are you getting the input from the console are you getting the input from the file you have to mention that here and once you get the scanner object the beauty is in scanner you can say SC Dot and there are so many methods can you see that we got next line which will give you string uh you can say next int oh that means it automatically gives you integer value you can also say next shot uh it will it has so many methods but we we are going to use next int not next line because if you use next line you have to pass that into integer so let's use next int 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 buffera but yeah it was introduced later so people before that were using buffer so we have seen it in fact in the next video we'll try to understand more about how do you close resources 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 D or you can use a scanner class okay now in this video let's try to implement try with finally or try with 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 do an exception as of now we are not writing any code for that let me remove that as well and here what I want to do is let's try to understand try now if you 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 cat block the thing is is it compulsory to have a try with catch there's no compulsion you can tryy you can write try but you can see if I don't mention catch it gives you an eror 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 such scenario where you want to handle the exception you can write finally but then what's the use of try when you are not catching it of course right try is responsible to handle the exception and when it it will simply uh throws 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 catch block I will say exception e and then uh let me create a scenario which is in I is equal to0 I know you'll be thinking I'm 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 / 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 it will say uh something went wrong ignore that get lost so something went wrong right okay okay so if you get the exception it will call the catch block right now sometime 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 bu here in the tri 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 is because in the triy 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 directly jump out of the try block right so one of the solution you can have is you can just write this in cat block as well okay so that irres to if you get the exception or not it will print byy so you can say it is printing Buy in the catch block but if you don't have the exception it will print byy which is coming from the tri block okay so at least you're printing by now of course in the real project you will not be printing by in real project you will do something there are some set of statements which you want to do irrespective 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 irrespec you if you got the exception or not it will execute the finally 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 cat 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 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 reader of course we can use scanner here but let's use buffer reader so buffer reader BR equal to new buffer reader and we know if you want to work with buffer reader 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 reader basically we can say okay we can num is equal to br. read line and then we have to pass it right we have to say integer. pass in 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 throw okay this time it is throwing two exceptions number format exception and iio exception because we are passing it okay that's fine with us okay and let's say I'm printing the D value as well now after doing all those things we know one thing right whenever you open the resource example The Bu is 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 triy Block if you handling the exception should I do that in the catch block of course we have to write two statements right instead of that the finally block is normally used to close the resource so whenever you want to close the connection whenever you want to close the resource always use finally block it is meant for closing the resources okay the only thing is can you see that we are creating the reference inside the tri 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 finally 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 make 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 statement see ultimately we need object right and 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 more 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 final block that's good right but how will you do that so what this says in the try itself create a round bracket and then you create your declaration whatever object you are creating create them inside the try block I mean inside the brackets and you have to declare that as well buffer reader so you will not mention that here now so what I'm doing is I'm creating the object in the try itself now what's the advantage of this the advantage is once the try gets over this resource will be closed automatically it's because if you go to buffer reader buffer reader extends reader and if I go to reader you can see it says closable and if I go to closable it is autoc closable so whenever you have a class or the interface which is autoc 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 try with 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 sometime 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 uh what we normally have is we have a software which you build of course so let's say this is your software and this software basically runs on an OS of course on this OS you'll be having multiple softwares right and underlying there is a hardware right so this is where you have the physical set example in this uh you'll be having your CPU you'll be having your RAM this is what we use to run applications right now if you 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 2 + 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 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'll be having multiple uh softwares right and then sometime 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 the at the same time so on the CPU there's a concept of time sharing example let's say this software one there software to this software 3 uh let me just remove this part for time in yeah so let's say if you have three softwares here which is which want to run on the OS uh they will be going in time sharing so your Os 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 padly 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 types 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 the it 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 should 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 create 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 you have multiple players running at the same time the ball is moving at the same time uh the audience is cheering 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 mag 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 these software are right stream deck Zoom o Zoom uh Zoom software everything is running now all those are task and individual task if you can see we have threads there so the kardel is consuming highest number of threads 524 uh then we got Java because we are running Java behind the scene so Java is using 20 uh 20 42 threads right so in a task as well you have the smallest unit the beauty is they can run parall 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 no 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 gist what is threads and uh yeah just to redefine that's a smallest unit which you can work with and of course you can run multiple thre 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 is not like just because we have a concept of threads we we are going to use in every application what if I say most of the time you don't even create create threads by yourself see when you build applications we use certain Frameworks right now behind the scene these 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 handson 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 High 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 uh try to create a function here public void show and in this show I want to print high nothing fancy just High 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 = to 1 and I less than equal to 10 and i++ 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 High 10 times right now let's say I I just copy this code and I paste it here now what I'm trying 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 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 obj2 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. show and obj2 do show so basically 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 why it will not work uh we have done so many Java codes till now 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 Hells 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 Main 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 come complete this task this two task then it will go for the next statement which is calling the show method of uh a class right so it will say okay 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 the 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 the 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 meth is done with this work 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 1. 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 Behavior 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 this classes or this objects as threads just extend with a class called thread it's that simple the moment you extend with threads or with a thread 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 ex extend thread okay and then you can uh 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 this 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'm 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 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 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 create 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 method called start because when you are thinking that at this point I want to start a new thread you know we 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 we have to say start for both the threads okay see we are saying that those are obj 1 obj2 but they are threads now now the only thing is when you call start it should also say start right why it is show now thing 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 it's almost like you know when you are uh 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 their 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 the run method okay that's a 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 see 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 obj 1 and then we are assuming that obj1 will execute one high and parall 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 a thread 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 happen happen 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 Schuler okay so in your OS we have a concept of scheduler now schul will say okay if you want to execute something you have to come to me first now it doesn't matter how many threats have you have in your system of course was 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 524 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 multie CPUs so you have multiple CES 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 course I can execute four thread 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 you execute all so of course you can't execute all at the same time right so they go for a time sharing so Schuler in between so let's say this is your OS here okay so there's a concept of schul which is responsible to basically allow a 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 CES available so on your CPU basically you have only two CES right so SCH 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 the they execute they will allow This Thread and this thread to execute right so it is schedule job to allow which thread to go to execute on the OS okay uh and that's why so when when your schulle 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 what is happening here so basically we are running multiple threads at the same time so yeah that's how you create multiple threads with the help of the thread class okay now let's try to execute this things 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 hells right how do we do that so the thing is you cannot control your Schuler you can only suggest your Scher to give the priority so yeah we have a concept of threads priority as well example we have two threads right we have obj 1 obj2 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 obj1 dot there's a method called get priority which will give you the current priy 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 WR bracket and compile this code and run you can see it is printing all this thing and we got thread priority where yeah it's here so you can see we got five okay so since we are uh starting the threads after printing the priority you can see that's why the we got the value first but we got the priority as five now what is this priority what's the range basically so the 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 have has some constants which we can use example we have Max priority we got Min priority we got n priority 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 priority minus one that would be nine 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 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 schul will give you that priority schul is is see different SCH 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 SCH can say okay now I will see the priority or maybe SCH 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 schedule has a different algorithm to work with using priority you can only suggest what should be done okay that's one uh 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 High see what is happening is as a Schuler will give you some time to execute right so let's say SCH says Hey thread a you can execute for let's say 1 millisecond and we were assuming that in 1 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 1 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 sleep 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 do basically trying to do is once you print high it this particular thread the a thread will go into weight stage right waiting stage and then the B thread will execute hello and again once B thre done his work you can just put a sleep there as well now the only thing is this Sleep Method will throw as I remember we talked about checked exception so the Sleep Method basically throws a interrupted exception so what we have to do is we have to put a TR catch or we can say throws but let's put TR catch here so with this suggestion I can say surround with tri 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 sleep here as well so we got two threads and now I hope it will work let's compile and run you see that we are getting hi 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 schule 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 threats it all depend upon your schedule how to run run this and that's why when you play game sometime you know you can see some of the character ctors 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 2 milliseconds so after so I I just want both both of them to start with a gap in between of 2 milliseconds so compile and run and if you can see I hope there will not be any High lello which got two time print you can see now it it's all once it's because there's a gap in between hi and hello who reaches to the uh schedule at the same time so yeah that's how you can optimize again as I mentioned we just trying to optimize it there's no guarantee that you will get this output every time oh we got it can you see that we got two hell 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 in your machine so we have understood two methods or two things from this video one is the priority so you can set the different priority for the threads Min priority Max priority and normal priority and you can set any number between 1 to 10 and by doing this you only suggesting to your scheduler to give the current thread maximum priority 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 ways to create a thread one thing we have seen which is of thread vising 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 Fe 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 uh subass 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 a thre 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 thread If You observe thread is a class which implements runable okay and if you go to runable you can see we have the method called run now this is important right if you remember in the code which we have return we have went with a class name or we have went with a method name called run right that actually means that if I Implement a runable here instead of using a thread even this should work right because it has a n 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 runable okay so both the side now I have a choice of extending a class as well if you have some other class because uh 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 how can you work with this the moment you go down first of all let me just remove this we don't need this code in fact you know before continue I just want to make some more changes to the code the first code the first thing I going to change instead of having uh let's say 100 iteration let's go for five iteration 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 runable interface we don't have a start method the start method before 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 runable 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 T2 this is your second thread right so we got two threads here and instead of saying obj 1. start we have to say t1. start because we know know that start method belongs to a thread and problem solve 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 the 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 obg1 obj2 they have no idea about these two classes so in that case what we do is if you go to thread Constructor you can see thread has multiple Constructor one of the Constructor takes a runable object that means obj1 here is actually a runable object you know let me prove that so instead of using a can I use runable 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 obj 1 and here we can pass obj2 so we are passing the renable 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 say clear compile run oh it worked can you see that we got hi hello hi hello and it is working so basically whenever you want to uh create a thread we have two choice you can extend a thread class or you can Implement a enable interface the only thing is this reenable interface will not have the thread methods in that case you need to create a separate thread object and then we can use this feature Fe es okay now if you remember we have also learned about Lambda expression right so let 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 creating this class we are creating this class is because we want to implement runable right okay that makes sense how about if I remove this class so of course this renable will not work example let's say if I comment the entire section here okay on this part if I comment the entire section you can say this will give you an error so I want to create an rable obj1 but I can't create a because we don't have a so can I say new runable 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 there in runable 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 at 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 reenable here itself right okay that works let me just try if this works before continue so I will say compile and run it worked can you 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 runable is actually a functional interface which means I can use lambra here and in lambra 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 we can we just 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 hi 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 will be used only once 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 exp 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 uh because till this point what we are doing is we are trying to convert the anonymous class into Lambda expression which is easy but typing the Lambda in the first go 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 two two options we have creating with the help of with a thread and by implementing the interface runnable in this video we'll talk about raise 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 uh threads will be useful now sometime we create two threads sometime we go for multiple threads maybe eight threads 10 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 you 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 them right example if you have int I so let's say if I say int I is equal to 5 and then later on I'm changing this value of I I'm saying I ++ maybe after sometime I'm assign the value for I to six I can change this value right and it is actually useful to mutate a value and of course that's what we do with data processing right so when I say you got data you want to process it most of the time you might want to U 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 we have mentioned that strings are immutable right so there there are some Concepts which which makes them immutable so which is good mutation or immutability uh at this point if you want if you can change something mutation looks good right so yeah so to this point mutation looks good but then let's try to discuss something more what about threads are 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 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 7,000 7,000 so in total you're trying to withdraw 14,000 rupees the balance is only 10,000 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 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 save 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 who going to call show T1 and T2 so only one of them can be able to get into show at one time so if T1 is ex show T2 canot execute show 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 1,000 and maybe I don't want to print something I don't want to apply a Sleep Method let's 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 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 void 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 zero because that's an instance variable and then inside this increment I'm just saying count 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+ and of course if I call increment once the value for count 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 me multiple times so before we even use that let me create the object of counter I will say counter uh C is equal to new counter and then from this two threads what we have we can simply say C do increment so basically I'm going to call increment multiple times now you tell me how many times you 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 increment increment for thousand iterations right now when I say thousand and th000 in total this increment will be called for 2,000 right okay let me just reiterate we have two threads each is calling increment thousand times so in total the increment will be called 2,000 times right so if I go back 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 would be the current value of count so if I compile and run now when I run this code I want this to be 2,000 right let's say what happens oh it's only 116 the weird thing is every time you 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 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 2,000 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 their work and then main will print count but that's not the case anyway this two we have two threads right T1 T2 and they are running any anyway now when when these two threads are running in any case now T1 and T2 they're running right main is doing nothing so main says okay my job is done I have started this 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 2,000 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. join now join is 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 this two threads you can use join there okay but the only thing is join May through an exception so I can uh simply say add throws declaration you can see we got throw inter exception anyway we have done with the join and now I think it should be good we should be able to see 2,000 so if I compile and run oh okay we were expecting 2,000 but we got okay now we got 2,000 now we got 1746 and that's why you know threats are sometime be unpredictable because they have a different behavior and unfortunately you 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+ 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 + 1 because that's how count Plus+ works right so there 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 0o then 0 + 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 1 + 1 is equal to 2 now again T1 goes back T1 says okay new value is two I will say 2 + 1 is equal to 3 and that's how the ration 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 the count at the same time they both got the value which is three they both said okay 3 + 1 is equal to 4 we got two iterations I mean we we called increment two times but the incremented happens only once okay that's what happens when you have two threads working with this the same variable or shared variable oh okay so mutation and threats 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 2,000 here is because maybe your threads are not going to the increment the same time okay so that's not the case in fact if you go with a bigger number if I go with 10,000 and 10,000 you can see it will lose lot of different uh bits in between compile and run you you can see 7,000 we are losing 7,000 values we got 9 8,000 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 s works with that at one time that's what you can do right so example when 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 synchronized you just use this one keyword uh your Java 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 uh so if I compile this code now and run you can see we got 20,000 now it doesn't matter how many times you run this you will get 20,000 because now both the threads are not going to the that 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 the this condition now in this video we'll talk about different states of a thread 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 join method so basically we were basically playing with the thread States right where the thread was running and then we ask this right to wait come back right so how do we what are difference States available let's 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 runable state okay so we have uh runnable 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 state is we have 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 thre example in the code if you remember when we said thread T1 equal to new thread at that point it becomes into a new the new state now the moment you say start you know we have this method which is start so when you say start it goes to a rable state so maybe we can say start so when you say start it says it goes into renable State now there's a difference between renable 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 Schuler that is into runable 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 wait which can make it go into waiting state right so when your thread is running State you can also say wait now waight 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 runnable State now whenever the Schuler is assigning you the task or whenever SCH says it's your turn at that point it will go into running State uh in fact if if your thre is running and if you want to keep it there you can also say stop there's also stop me available but that is dicated actually don't don't stop threads okay it will create instability in your software but yeah once the thread work is over it will automatically go into stop State uh you can also send a thread from the runable to dead even with the help of stop even you can do that uh what else we have here okay so basically we have this option so whenever you create a 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 C oo 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 wait 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 state it will it will go to the runnable state right so it goes from the waiting to runable from running to waiting waiting to runable that's the cycle it follows and then again from runnable to 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 C which is a collection API so let me just write out somewhere uh so we have a concept called collection API uh then we have a collection and we have collections now this is the only thing I I think it's confusing in this topic now when I talk about collection AP 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 like 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 5 and I'm 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 five size which is five now this is your AR right and it perfectly works okay then why we are talking about this this collection 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 right data structures are very important the way you fetch data the way you the way you store data is very very important now sometime example let's say if you store data in a box here now sometime 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 like a normal cube right example if you want to take a ticket you go you stand in a q right in the same way this concept which I'm talking about here is q the concept which I'm talking about here is stack right now apart from this there are multiple data structures which you want to work with right and we can actually Implement all these data structures with the help of aray as well there's no harm in that so as a programmer you need to do it right and it's good you know it's fun the only thing is when you work on a project you don't want to actually work with an array where you create the 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 uh the collection API where you can actually work with all these data structures apart from this we also have link list right so you can work with all these things by using some inbu classes so it makes your work much much easier and also when you want to fret data when you want an expandable array that's right the thing is the array which we have worked with it is fixed size example if I create an array of size five it will be five right uh you can't extend it what if you have a SC Ario 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 ARR 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 area let's say nums 2 and you can say new int and you can mention seven here so you got a new aray of size seven 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 sometime it becomes complex to do all those things what if you can just get a class or the feature where you can create an array which is dynamic right so it's quite exciting to work with collection 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 array two arrays one where you have all the keys one you have all the values okay so and of course as I mentioned before in Array 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 aray makes sense in most of the cases when you know that the length will be fixed so you will be using aray now whenever you have a specific requirement of different algorithms and structures that's what you'll 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 in 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.il package as we have seen before most of the classes which we have used belongs to java. Lang package and that's why we we have not imported them but if we talk about this collection interface it belongs to a java.util package in fact uh 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 INB 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 the interface which we are using now there are certain more interfaces which actually implements The Collection one of them is list uh we have q uh we have uh set so all these Bas all these classes or all these interfaces basically implements or basically extends The Collection interface and individually this interface faes have their own class implementation example for list one of the most famous one is ADD list so we can use this for Q also we have different classes for set we have a very famous class which is called haset so we have different classes implementation example for Q we have something called DQ which we can use uh so for list we have at list apart from at list we also have link list which we can use if you have worked with data structures before we have a concept of Link list as well well and then in most of the languages we Implement them by ourself right but then in collection API we get the implementation in fact in set also we have something called a link haset but then the most famous one or simpler one is to use at list and has set for the 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 set list here so what I will do is instead of saying new collection I can say new array list and you can see this is a class ignore this angular packet I know it is coming for multiple times but ignore that time bin so we got this collection nums equal to new at theist now in this nums basically we can add values okay uh now how do we add values it's 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 see 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 two array 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 add I can add values so let's say I want to add value which is six 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 five uh this is 8 and this is two so basically we got this value right now ignore this uh yellow zigzag lines here we'll talk about that later okay but yeah there's an important concept which is coming up but at this point I I got this collection right and I want to print this now if you have an array of course you can also create array with these 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 warning in sometime but let's run this code 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 uh you can use a loop here so I can say for uh we can use index values let's let's see do we have index option here so I can say nums dot uh if I say get oh 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 for loop with the loop so I can say int n 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 gray this yellow line here see 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 int nums right so you know that in this nums 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 you 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 say 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 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 did done in 1.2 1.3 1.4 but they realize 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 mention the type of element you want to work with so we are working with integers so integer is basically a class uh which is a weer class for the int type because int is primitive inte is a class and collection normally work with objects not the Primitive values so if you want to store integer numbers which you have to use a class integer and you can see now there's no problem there's 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 see we got the values now one more thing why this type here is so important see it also helps you to 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 enhanced F 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'm converting this object into integer so we can do that by typec casting it here so I can just typ 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's no problem but what if by mistake you doing the code and you have added a number but that to in double codes let's say five now what will happen Okay so let's see if I compile this code there's 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 genx here and if I mention integer here as well now if you can see we will get the compile time error not runtime error and that's why if you compile this code okay you can see we got a compile time eror 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 add list 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 adist as I mentioned in the diagram as well add list 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 and values so which one to use should I use a collection here or a list see 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 fet the second index value so I can say nums do get 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 uh it will give you eight which is an 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 I can say nums dot add was there 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 will say nums dot index of I want to print the index of two right so two 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 you work with list and list now to this point we have talked about List Right Now list is one of the collection API concept it it supports index values right uh 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 it's great uh it does support multiple values example let's say and that's a feature 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're 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 are not printing all the values is it no we not printing all the values so I will just remove these 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 sometime 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 adist okay because see the thing is if you 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 uh we also specify the type here the generic type which is integer and okay let me remove the list UT 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'm working with integers um let me remove this and I also want to print the value num that's it okay now when you have this 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 see six and six we only got one six here the second thing is it is not following the sequence remember the if you can see the sequence is 6582 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 sorted I will say compile and run if you can see it's not actually sorted it's 82 21 54 62 okay it is not sorted and it is not even going for a sequence and that's how this set Works set will not give you value in this sorted format okay uh it basically uses uh and it also doesn't have a index value example if I say nums dot for 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 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 it does it does not even have a index value is it not mentioned anywhere anyway so you can't have index value and remember when I mentioned about collection API it was introduced in 1.2 and also it is also called collection framework okay instead of saying API you can also say it's 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 has 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 three 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 three set makes sense but if you want to uh want to have a set which will have only unique values there's no need for auditing you can go for has set there one more thing instead of saying set here we can also say collection even that works because anyway uh the tree set Implement set and set exchange 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 iterable and if you go to itable we have a option called iterator so that even that is one option you can use a 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 do iterator right so we got the iterator now this iterator will return will give you iterator back the iterator object I will say this iterator is integer I will import the package and here I can say 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 you 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 print this next element but the problem is it will only give you one element how will I use a loop here because see if I use a loop I 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 do 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 uh we can also use iterator that's also an option and that's why we got the topmost interface which is iterator so that's how we use set iterator and reset we can also try it with uh uh 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 a famous one now there's one more which is is called map Now map is not an implementation or it does not extend collection interface but it's a part of collection concept 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 uh 23 67 and 92 now we have this values right and let's say uh if it if you work with ADD list here if you work with list basically for this values if you want to work with this values you need to work with the indexes right we have to say 0 1 2 3 right but sometime it's better to have a name or a different kind of index instead of having zero and one to three uh example let's say if we 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 naen uh this marks belongs to let's say h uh this marks is belong to sushil and we got Kiran so instead of having index value can we have a name to it okay instead of having index value 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 they are values and the key for this is naven the key for this is H the key for this is social and the key for this is Kon right and we can achieve these two things in one data type or data structure which is called a map so what is map map is basically a collection of not mad 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 this Imports now so let's create a map so way you can create a map is map and okay let's import the package for this again java. and we can say this is students that's map of students and now now map itself is an interface if you can see and it supports key and a value pair now if you remember when we talked about adist it was having e there 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 K this K and this V can be replaced with data types like string integer integer float or we can have student string whatever the 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 student string that's your choice uh what type of data you're entering but we got this map here now once you 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 and a value and as for our diagram we have seen the key I want to insert is naven and the value is what 56 and likewise we can have all the values here copy paste paste paste and this is hsh and the value here is 23 this say suil 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 the 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 naen 56 we got s 67 we got K 92 and h 23 now one thing to observe even this is not following a sequence you can see we got naen first then we got sual we got Kiran we got hsh okay 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 students 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 hsh 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 hsh 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 doing 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 correct it right of course if you got extra marks then you don't go there but let's say h 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 lot of mistakes in the paper so let's say we got 45 here now tell me will it add a new student as hsh 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 Hersh we only got one hsh here which is 45 we don't have two Hersh that means the key cannot be repeated keys are unique the values can be example I can have two student with the same number and of course we do get uh same marks right if you copyed anyway so we cannot have keys repeated so can we say this keys are actually a set that's right and the values are least here so we have a set and a least combination to get to get a map here okay so what I 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 forloop 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 right it's a set uh so I'm printing all the key set now so if I compile this code and run oh okay see that we got all the keys if key set gives me all the keys uh if I run a loop on this if I say for what's the type of key 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 I 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 oh can see that we got key and a value there so that's how you can print all the values from uh map now why we are using our put method here is because when you 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 that'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 you we can also get values all the values the way you have keys 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 uh please explore those methods to understand this more 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 has 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 hashtable 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 hashmap 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 H hashmap works so that's how basically you can use map and 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 at a list right and then uh we can import this okay now one thing to observe when you talk about this type here on the left hand side there's no compulsion to add that on right hand side okay at the start when gener 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 on the left hand side on the right hand side you can simply keep empty bracket that completely works okay so in this add let me add some values I will say nums do add I'll say four and likewise let me add some elements here okay so let's say we have these values here and what I want to do is of course if I print this values 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 ar list 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 this values and let's say the output if I run this code you can see we got 3 47 9 so sorting is actually very easy in terms of collection here right now the 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 4 then I want 72 then 43 then 29 so this one first then this two then this three and then this nine how do I change a logic here see 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 comparator 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 uh class here okay this comparator works with integer and I can specify that here as well which is integer in terms of of anonymous class it is compos to specify on the right 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 uh so integer integer okay so I will say public int compare and we have to specify two values basically one is your U I will say in I comma in 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 2 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 I will compare these two values if the first value is greater than second value I will swap so after swap it will be 43 sorry 3 4 1 6 then I will compare this two values uh then it will be 3 1 4 6 then I will compare this two values so this will be 3 1 six so 4 six because the first value is smaller than second value no swap in this case we have swapped it because it was uh bigger value now once you got one 3 1 4 6 we'll do this situation multiple times again you will compare this two this will be 1 3 4 6 then this two it will be 1 3 4 6 now we don't have to do that we don't have to compare the last two because in the first situation you know the biggest value comes at the end so we got we don't we got that right so we have to do that only three times and again for the next itation we can say 1 three I know this is sorted but still we have to continue the iteration first compare first two 1 3 4 6 and you're done right so this is how basically you sort the elements the the aim here is when you sort elements this is one of the algorithm for sorting basically we have to compare two values and swap that's basic basic rule right so here as well when you say you are sorting you have to just have to specify when to swap when 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 is that simple okay so what I will do is I I will check and I want to check the last number right so I will check if how do we 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 minus one 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 Comm object here itself so I will say Comm and okay there's still an error I don't see any reason to get the error did I forgot any colon or something I think not 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 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 name here right because collection works with the wer classes and now I will try to compile this code and okay compile two times and run you can see we got the SED values we got one 2 3 9 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 creation of comparator we 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 the string array with the help of the length of the string that will that will be awesome right try that this is how you sort the value based on the comparator so what is comparator basically so comparator is a concept or a interface using which you can specify your own Logic for sorting okay that makes sense right now I will do something LCM 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 AG for a student and string name and I also want to have a two string me so that I can print the object as it is so a 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 SCE 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 a 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 this are studs I have to replace this with studs oh maybe let's have numbers what's an issue and then here instead of add 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 specifying age right let's say age 21 and name is let's say naven then let me just create some more objects here so let's say age 12 name is let's say John and then age we got here is 18 let's say parle and we got age 20 let's say this is Kon okay so we got this four student here with different age and different names now what I do is I want to sort them okay time let me just come in the section I want to print the 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 nums I know we should have used studs there what I can do is I will replace nums with studs okay this should solve the isue 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 this values based on their age how would 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 see that with the sort method 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 student so you have to say student now and both this object so again we have to compare two students right so we need two student 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 their age right so we have to say I doh is greater than J do age then swap it's so simple right since we have worked with the integers it should work normally and if I this code can see that we got all the students with the ascending out 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 why is not why it's not sorting it's because the 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 implements 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 comparable interface has a method called compareed to okay so we have to Define that method so I can just come back here and say implement this compare to Method this is a signature okay and we are comparing this with a student itself and let me say this is that okay the the the name of the variable is that will I I will tell you why I've said that but now what I'm doing is I'm adding a method called compareed to and I'm implementing something called a comparable now and then this comparable I 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 comparable 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 it's is not sorted and if I'm not using compar to now I'm not using comparator what I'm using is I'm using compared to of comparable so whatever logic we have return 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 do AG the current object the current student we are comparing the current student with the other student it's something like uh when we don't have a compartible and if we 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 you can see that we got 128 20 21 so this is how basically we can use comparable and comparator so you have two choice either you can make your class implements comparable if you don't want to do this you can also use comparator in fact even if if you mention comparable you can still override the logic by comparator that's your choice okay uh one more thing before we close this let's say I want to use comparator here if I pass comparator and I want to you I want to reduce this code remember 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 cly backets as well one more thing uh instead of using eils we can also use a t operator here so the same logic I can just comment this part I can say return I doh is greater than J.H return one otherwise return minus one so instead of using IF Els I'm using turn 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 cly 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 function 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 comparable 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 comparable if you want to give a power to the class itself to compare itself or to compare its object to itself uh we can use uh comparable 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 understand that first so what I will do is first of all I 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 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 add list here and in this add list 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 want to do instead of adding values like this there's another way is using which you can add values in the list uh which is there's a class called arrays and arrays do 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 uh this is much simpler right now once you got this list what if you want to print this values of course I can say s out and I can say nums and of by doing this we can simply uh 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 out 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 uh odd numbers I only want to focus on even numbers example we have four and then we have two uh ignore this do do a basically it represents an array uh so we got this four and two I want to filter them and whatever filter whatever value you get after filtering I want to double those values so example four and two becomes 8 and four and at the end I want to reduce it so I want to say okay 8 + 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 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 after doubling it I want to add even numbers basically so we can check if my n mod 2 is equal to equal to zero that means that's a even number if that's a even number I will first of all double the value so 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 to a to a sum let's say I will create an INT sum 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 six 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 example I want to fetch this four uh then this two then this six I want to double each value so example eight four becomes 8 uh 2 becomes 4 and 6 becomes 12 so this is 8 + 4 is your 12 12 + 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 sometime 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 AP Pi 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. you. stream package and if I click on stream you can see stream 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 these methods later so if you go down you have sorted in fact there's also a method called reduce we are going to use that as well so there's reduce as well okay we'll see that later so basically stream provides a lot of methods to work with this now how how exactly to use stream that we'll see in the uh next video but before that I want to show you one thing so let uh so let's say we got we we were able to do this right at this point I I will just commend 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 printing 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 Z and int I 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++ so we have basically we have to use normal for Loop and then we can print each value I can say num. 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 this is one way uh 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 for Loop what it does is it basically goes to the loop or it basically goes to the list by saying Hey I want to fetch the first element I want to fet the second element right on the other hand if you 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 two looks CT but then we have a better way of doing it so I can just come in 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 e each method Now using this for each 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 least 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 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 each 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 for Loop 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 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 for each I need to create object of consumer okay so the the moment you say you have to create object I can simply say consumer and you can see consumer is a interface uh which belongs to a package java.util do 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 will say consumer integer and then I will come back here let me say uh 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 void let me just check once consumer has a method called accept which is of type void okay which returns void and the method name is accept I will say that and then it takes the integer value I mean whatever type you mention 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 on 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 4 each what it does is it gives you one value at a time okay now whatever value you are getting here or for each is giving you that goes into this consumer object and the consumer object has only one method which is accept Okay now this accept will accept a number that's right the forish 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 this screen compile run and you can see it is working you you are able to print all the values okay and 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 function 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 cly brackets and I hope till this point you have you 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 cly brackets I will remove that as well and I can write everything in one line that looks cool right and then 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 whever 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 earlier 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 n you can print it you can add it you can save that 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 Leist right where 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 you 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 that it was introduced later but we got this method called stream now what this stream 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 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 the stream if you uh 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 sometime 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 uh 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 do for each mind you again in the Stream of S1 we got all the values which are there in the list and let me just try if 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 one 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 s nums dot for each 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's no issue run time it will work once the second time it will say uh the stream has already been operated upon or closed 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 earlier example where we were removing all all the uh 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 z 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 do 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 do 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 see what we are doing here we'll have a separate video on this what 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 got this stream I can create another stream where I 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 the 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 reduce will do it again don't focus on the logic now just focus on my reduce this reduce will not give me a new stream this reduce will give me a value of type whatever type you mentioned with the inte with the stream which is integer so it will give me a int value say in result equal to s3. reduce and instead of using a for each now why we can't use for each 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 we have a function like filter map reduce now there's 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 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 you got 24 and this is the same logic which you have done here can 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 come in 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 I can say dot I can apply a filter and this filter can be uh we can be any logic as I mentioned we are going for even numbers zero and on the same stream because see nums do stream nums is the 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+ e or C comma e which will be giving me C+ e and that's it this reduce will give you one value right and that one value you can store in a result so this nums do stream let me just give a tab here yeah so this basically nums do stream will give you a stream on that stream you're applying a filter on that stream it you're applying a map on that stream you 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 New Comers this will be difficult but once you get used to it you'll always prefer to use stream okay and one of my favorite uh trainer says W suum 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 clear in fact it's much more readable right on a stream you're applying a filter on that filter applied you applying a map and then you're 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 for each it takes the object of consumer but I'm not sure what it will do here so what I will do is just to simplify the code I will remove everything and also let me remove all this things so that it will look cool you know simple code okay so let's go step by step if you 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 predicate here so if I say predicate and you can see it belongs to a package java. do 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 bullion value it will say okay it can be either true or false if from a string the value t or whatever value you want to have let's say the value is n not I don't like TQ T later let's go with n so if n is mod n mod 2 is equal toal to Z in that case return true I can give a tab and I can say else return false our problem solve 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 return 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 see 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 2al Z because see ultimately we need a Boolean value right it can be true or false so we can do this also the predicate itself is a function 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 cly bracket we can put a arrow here and then since we only have one statement you don't even need cly brackets you don't even need a new key return 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 and 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 n 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 it 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 we just do that quickly now we have mastered Lambda and we have only one statement which is return return so we can remove that we can remove this cly brackets we can remove this integer we can remove this brackets and this is what we got and now we know whatever 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 is a method called apply okay let me just simplify this for you know this is a lot of implementation there let me simplify this thing now when you talk about reduce how do you reduce a value example let's say if I ask you to add three numbers let's say 1 2 3 how will you add them now in your mind you're doing this so if I say 1 2 3 and let's say four also how will you add them of course you cannot add all the value 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 three and three which will give you six and then you will add 6 and four which will give you 10 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 + 1 is 1 then 1 + 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 zero that is your that is the 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 six is a carry this 10 is a 10 is the output of course but this 1 3 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 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 uh 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 the stream to be sorted so I will say integer and I will say sorted values but I don't want to apply reduce because reduce 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 forage let's try that compile and run oh it's you we have to use forage okay so I will say sorted values dot for each and you can say whatever 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's one more thing what if you want to do the filtering but with multiple threads by default you have only one thread right what if you want to use multiple threads in that case there's 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 now this is the biggest update for someone who is a Java learner or a Java trainer that's right normally for a trainer you know you don't see lot of updates coming up and then as a trainer You Don't See a new update by saying okay I like it but then as a Java programmer you get lot of different updates right in fact in every 6 months now we getting new updates in Java and Java 21 is coming in September now what are the features in Java 21 there are multiple features but then one feature which is very important for Learners and for trainers and that is this when you want to teach or when you want to learn Java what's the first code you write in fact it's it's true for any language Let say C C++ python JavaScript doesn't matter what you do is you write hello world and if you talk about python this is how you do it it's so simple right if you talk about C language this is how you do it but if you want to teach Java or if you're learning Java this is the first hello world code which you see and that's where you feel Java is difficult in fact Java is one of the easiest and most structured language available and that's why most of the companies they are using it because it's it's easy it is structured and it has multiple features the thing is I have been training in Java from last 12 years and then every time I go there to teach about Java I have to be mentally prepared you know because it's easy to talk about the advanced part but the moment I talk about the basic program how do you do hello world in Java of course before we do that we motivate them we say Java is easy but then the moment you show this code they don't feel comfortable because you're focusing on hello world but then for hello world you have to write all these keywords public class class name public public static void main then in bracket something weird comes into picture string args with the square bracket and then you write your code there and you end it now when you are explaining this concept of course it's easy to mention main because that's how you can say it's a starting point you easy to mention white because it says we are not rning anything it's very difficult to convince them about why class it's difficult to convince them about public static it's difficult to convince them it's very difficult actually to talk about that string args and that's where as a trainer what we do is we say don't focus on those things we will see them later now even if you tell your learners that we will talk we'll see that later it's very difficult for them to keep keep it out from their brain because you they can see it right and every time they see it they they will have these questions in the mind of course we talk about those things later we talk about public we talk about class we talk about static we talk about the array as well but then that's later right when they are starting they will find it difficult how do you simplify this in fact from last few years the Java team is trying to make Java easy for the Learners and that's why we have different tools available I will show you those tools but then this is a new update and what's the new update this thing what if you can write your first Java code like this not this one right this is so simple right you simply have void main easy to explain you have system.out.print oh they have to work on this I think in future they are going to simplify this line as well but we have void main bracket no string rjs no class no static and you you will get your output that's right this is the new update coming up and if you want to try out you can just download the JK 21 uh the early release or early access and you can try it out uh today when I'm recording this video is still not available the feature came two days back and I think the build will be coming in this week and once the build is available I will make a video and show you the output but the build is not available but that is coming up if you don't want to wait for the September and if you want to try it now you can right after a few days but this is a new update and for Learners and for trainers now this becomes easy it's not like when you're are building a big project you're going to use this syntax no this is only for learning purpose this is only for training purpose if you are learning for the first time and if you want to make it easy you can use this syntax but if you want to make a big application and that's where we have to follow the older syntax now thing is now when you're building a project of course you know Java then all this thing makes sense in fact as a jav developer this things is something is there in my muscle memory you can anytime you can say hey write a Java code I will start typing this line and then think about okay what to do next right so this is for Learning and training purpose and this is for development in fact if You observe this there's no class and of course right Java is objectoriented so where is the class what's the class name so that's where they have introduced unnamed class and uh instance main method because there's no static keyword there how exactly this will work and it will follow up I will we can only talk about that once uh they have released 21 because I'm yet to look at the documentation because it's not released yet so that's one now as I mentioned this is not the first time Java is doing a major update right in fact they did that before as well example let's say in Python if you want to print hello world you simply write print hello world right it works in fact in Java as well they have introduced something called jell in the earlier versions and using jell if you want to print one line you can simply say system. out. print or if you want to create a variable you can say int I equal to 9 you got your variable there right so it's not like Java team is not trying to make Java easy yeah to start with okay it's a easy language once you learn the concepts but then to start with sometime people find it find it difficult one more let's say if you have a Java code here and if you want to run this you have to follow two steps first you have to compile the code right we say Java C uh demo. Java and then you have to run it so you have to follow two steps right now they have introduced something called a source code Launcher not a new feature it was there in the earlier versions as well last two versions but now what you can do is you can say Java and you can mention the file name now this only works with single file that doesn't work with multiple files but as a learner when you are learning the language for the first time you don't have to write two steps write one step mention Java and the file name and it works okay so this is the new update and let me know in the comment section your thoughts on this new update and I've checked it with some people a few people are happy with this few people are saying okay it was okay you know when we talk about uh the this syntax here and uh when we say okay we'll look we'll look at static later and we'll look at class later they feel that students are happy with it but I don't you know it's very difficult to convince people to ignore something if it is there in front of you I hope you enjoyed make sure that you share this video with your friends who wants to know about this update and do comment your thoughts welcome back aliens my name is the ready and in this video we'll talk about lvti now don't go for a short form here the full form is actually easy to understand which is local variable type inference now type inference in Java is not new it's there from from java 8 as well in Java 8 we use Lambda expression where we specify the variables without specifying a type of it now in Java 10 we got something new which is using local variables without declaring with a data type I know that sounds weird right because we always say Java is a statically type programming language which means if you want to use a variable first you have to declare the variable with the help of data type it can be int string a class name doesn't matter but you have to declare a variable and that to with the help of a type now in Java 10 it has changed for the local variables it is optional now to specify the type instead of that we can use a Vare keyword that sounds weird right because in Java from the start itself we are we got this habit of using type and now we are using where why in this world we need a where in Java now think about this oh I know you are waiting for the code we'll do that in some time but understand why do we need this where keyword now if you talk about any software or building a project yes of course we need to write a good code right but then if you really think about writing a code actually reading code is more important than writing code because writing will be done only once that same code and same project will be read by multiple people at multiple instances you know what anyone can write a code which a machine will understand but it takes a great program to write a code which a human will understand so you have to make sure that you write a code which is much more readable which is much more maintainable now coming back to Java what happens is if you use class name example if you want to declare a variable and if I go back to my code so let's say we have a very big class name so what you normally do is let's say the class name is at a list so what you do is you say at a list and then you specify uh the object here and then you say new at least now say this is important because this is what is creating object right but then this line here is actually difficult to read so basically we are trying to create an object of at list why you have to read in this format you know Java is known for its verbosity where reading is very easy but this is one thing which we don't like right sometime we have a class name which is way bigger than what I'm talking about now and that's why it says what if you can just remove this part and make it rare and yes you can do that in Java 10 so instead of using the type at the start we can mention where the variable name of the object reference name and then you can do whatever you want on this side now to understand this in a in a basic way let's start with creating normal variables now this thing is actually applicable only for local variables and if you can see the title itself it talks about lvti which is local variable type inference so that means it is applicable only for the local variable now why only local variable why not instance variable is because in programming when you create variable inside a class which is a which is your instance variable they are meant for storage right and then when you create a variable inside a method it is for the processing purpose we don't actually store the local variables right we store the instance variable and the local variables are basically there for the processing of the function or the method so here I'm writing something inside a main method that means it's a local variable I can create a simple local variable here which is in I or let me say int a is equal to 9 now this is a you can create a variable right uh we can do the same thing we can create an integer variable with the help of where keyword so we can say where B is equal to 8 so you can see we got two variables and both of these are integer type so yes we are used to this part because we are specifying a type of it but this where is New introduction to Java 10 now you might be thinking this is T type language makes sense this looks like more of a dynamical type language not exactly since it is the type is assigned at compile time anyway right compile will do it for you so even if you do you don't mention the type compiler will say okay I got a variable B here which is assigned eight that means the type is int okay so it is by default converting that for you behind the scenes that's why it is still a statically type language cool so instead of using int we can use a where here and which makes much more sense now the only caveat here is if you can create a integer variable like C and if you don't assign a value to it if you don't have a initializer on the right hand side that's okay because now at least we know the type of C of course you can't use it it will give you error but at least it will not give you compile time issue it will say okay there's a c which has a integer value let me just run this code once to verify so what I will do is uh I will just introduce this bit later let me go back to my terminal here and let me compile this code so we can say Java C the file name is demo is it demo. Java no it is still abc. Java okay let me verif I there's something wrong with my okay it is abc. Java anyway let's do that so we'll say Java C abc. Java enter and you can see we got no error even the V keyword works here right and then we can run this code by saying Java demo and you can see okay there's no output because we are not printing anything but important is it is working cool now let me introduce with this int C and let me do it for v d now what do you think will it work you can see this and this two lines have introduced and let me just compile this code once again and you can see we got an error it says infert type cannot infert type for local variable d uh cannot use V on a local variable without initializer which means if you want to use a we keyword it is compulsory for us to assign the value you need to initializer there so if you said 10 then it will work okay so you have to assign the value so save go back and compile you can see there's no error now okay so you got the idea how do we use this here but what happens if you use it on at the instance level so let's say I have a number here and I want to say where num is equal to 10 I'm also giving the value there will this work let's try so let me go back to my terminal and say run you can see it says where is not allowed here so that's one thing to remember cool so we can't use it here so let me just also right hit here this will give you error okay cool now since we are using bad here is it a reserved variable or reserved keyword let's try can I create a variable of integer type or may be a string type variable and can I say a variable name as well and if I say this n let's try you can see okay there's an error oh so still I'm working with solidity programming okay go back and run and you can see there's no error so basically you can use v as your variable name no issue so you can use it as a variable name you can use it as a keyword as well so that works but one thing if you create a class don't make a class name as W this will give you error so this will not work so if I don't go back and compile you can see it says where not allowed here so class where not allowed where is restricted type name and cannot be used for type declaration so remember this thing V keyword cannot be used with a class name okay this looks good anything else we can do here how about an array so normally example if you want to create an array of numbers so we say integer int then we say nums and then we give a square bracket because that's what you that's how we mention the aray and then you say new keyword again you say int and then you specify a type here right which are the number here now this perfectly works right but what if you want to do that with v now one of the issue with this syntax is if you are saying that it's an array on the right hand side why you have to use a square bracket here and that's why V says you can create the array like this you don't need to mention that square backet even if you do that it will give you error so you have to say new int 10 so this perfectly works so it only works with this type of Declaration if you use static values if you assign the values by default uh it will not work so remember this thing if you are using if you're creating an array with W do not put this square bracket and let's see what happens if you put a square bracket so if I come back here and put square bracket compile you can see we got an error it says where is not allowed as an element type for an array okay so let me remove this square bracket and compile and you can see okay this time we have a different error we still have this class remove and run you can see there's no error cool so this is something you have to remember this is how it works you can use a Val keyword there uh in fact you can also do that for another class object so let's say if I if I have a class which is alien and if I want to create object of alien here can I do that uh yes you can do that and that to so normally when you create object you do that in this way right you say where obj equal to sorry alien obj equal to new alien what you can also do is you can also write Val W here even that works so you can see I'm creating a reference of type alien and compile even this works so yeah that's the beauty of this W again it was introduced in Java 10 so if you are doing this coding before any version below 10 it will give you error this will not work so make sure that you have jdk 10 or more installed in this machine I have jdk 17 and it is working perfectly so that's about the local variable type inference welcome back aliens my name is z ready and let's continue with the series on new features in Java so basically we are showing up all the amazing new updates coming to Java here and today we are to talk about sealed classes that's right a very different word now basically we have talked about record classes and we already know about abstract class and final class now just to give you a quick walk through between abstract class and final class see it's all about how how you model and design your application so when you're building a Java project of course you have to build a project which is which follows a proper model so that it will be easier for you to maintain it is easier for you to update the application now for that what you do is you create a proper design by using all doops concept now when it comes to abstract class basically we create a base class which may have some abstract methods and also some defined methods and of course the actual implementation will be done by the concrete classes which will have all the implementation now basically what you're saying is abstract class is meant to be inherited right on the other hand we have final which means no class can inherit this is the final implementation of this type of classes you can't inherit it right now it provides you a good way of doing stuff but what if you want something in between you want to have inheritance but not every class can do it there should be a limited class which you want it to be inherited can we do that can we restrict it and one way in fact there was way before but then in new Java version we got this update where you can use seal classes which you can mention which are the sub classes or which are the sub interfaces which can inherit from this particular class and interface and we can do that with the help of sealed keyword that's a new keyword right so to understand that let's head back to our code so what we're going to do is we are going to create a simple code which you can see here which is a bare minimum main method and what I want to do is I want to create a class so let's say class A now if you want this class to be inherited of course you can make it abstract and you can say hey don't create object of this class these sub classes like Class B which will inh Class A should be able to create the object so we can make it abstract but if you don't want it to be have a subass you can make it final but now we are looking for a way so what I want is I want to have class B here and let's say Class C now can we do this that only these two classes should be able to inherit a no other class should be allowed example if I have a class D here so class D should not be allowed to inherit a is it really possible can we do that in Java let's try so basically I want to get a compile time error the moment D try to inherit a see one way you can do that is by making this final but again it will make sure that no other classes will be able to inherit so even if you do that example if you say extends a here it will give you error let me show you that by running this code okay so let's compile this codes we'll say Java demo. Java and when you say enter you can see we got an error it says cannot inherit from final a so basically this is a final class so you can't do that so if you want to restrict this what you can do is you can make it as a sealed class now sealed is a new keyword in Java so just just mentioned seal here and you're done and let's try now again you are getting an error now of course even final gives you error seal gives you error so what's the solution see basically when you say sealed it normally permits some of the classes to inherit so we want to allow b and c not D so where do I mention that so you can do that in here so just after your class name or whatever uh extends or implements you want to mention just after that say permits and you can mention the class name which you want to permit so I want to give the permission to a and soorry not a and b b a is the same class we'll say B and C so what I'm doing is I'm allowing the a class to be inherited by B and C it's that simple and now if you run this code so there is still an error it says invalid permit Clause now we have to make some changes here uh which is if you are permitting B and C this subass which is your sealed subass it can be B or C we have to make sure we can use three keywords here either this specific class should be sealed okay either you can use sealed or you can say this is nonsealed okay what are the types we have or this can be final so at least this class need to have one thing it should be final or sealed or non-sealed okay so now if I make it final you can see there is C okay for c as well let's keep it final as of now that's what my Java was saying Class C is not extending it so we'll say Class C as well Class A okay so these are the things you have to remember so you know sometime we have to learn from the other it says if you are permitting a class those classes need to be either final sealed or non-sealed and then we are when you are doing a permit here you have to also make sure those classes are extending this class which is a okay and now let's compile and you can see this compilation done okay finally we Sol the eror and we can also run this of course we have not return any code so we'll not run this as of now we just compiling it so basically the idea is if you want to restrict the sub classes for a particular class you can make it sealed again this is not useful for every use case this is specifically for designing an application when you design an application if you think hey this is a class I want this specific sub classes to be allowed to do that example we can set a payment gateways right uh so let's say if you have if your payment Gateway you want to accept only three to four payment gateways you can you can permit that that's one thing if you talk about a computer class if you want you want only laptop desktop and mobile should be able to inherit the computer class not other classes so you can do that here as well so you can give a permit but what happens if D try to extend a in this case so D is extending a do you think it will work let's try let's compile and you can see we got the error it says class is not allowed to extend a sealed class now why is not allowed is because it is not mentioned in the permit okay but there's one more thing you can make it final right now when you make it final the class B cannot be inited again so no other class can init B irrespective which what class we are talking about no class can do it but if you make it sealed and you can give a further permission here so you can say okay so B is giving permission to D that's one way you can do that and D will be extending B now but again when you whenever you extend a sealed class the subass should be either final or it can be non-sealed or it can be sealed okay that's your choice so I'm making it final and now D should be able to do that you can see there's no error we can add one more Clause here which is I can make what if I want to extend so what if D want to extends B and I don't want to mention b as a sealed class I don't want even want to give a permission I'm just saying hey you know anyone can access B Class anyone can extend b class but a class should be extended only by B and C but then B says okay I don't have that criteria anyone can extend my class in that case you can make it non-sealed so yeah you can make the sub classes non-sealed which can be extended by extended by someone else or some other classes and you can see we have not got any error so basically the sub classes of a sealed class it can be final sealed or non-sealed okay so we are making b as non-sealed we are making c as final that means no one can extend is and B is extending D is extending B which is this class here remember when I mentioned you can use permits now before permits also the class A itself can extend any class maybe let's say I want to do do that with thread so I'm making a as thread it doesn't make sense but let's say we have a class A which extends thread so if you want to extend any other class you can do that here you can say extends thread and then you can write permits in fact you can also Implement some other method here uh we can uh interface you can Implement let's say what interface I want to run let let's say clonable of course you can you can Implement any interface but let's say we are saying clonable here so we are extending Earth s and we are implementing an interface so if you want to do all those things just mention these two things and then you can mention permit remember permit should be the last thing after extends and implements now can I use permits with interface let's try so what I will do now is I will just go back here and say sealed I can create an interface which is interface and I can say interface X so you can see I'm saying a interface X which is a sealed and now I can mention which other interfaces it can extends or implements so I can say permits let's say y interface y should be able to extend it so we can say interface y extends because in the in between interfaces we say extends X right the same syntax which we did for classes and it should not let's see if it is giving me any error okay we got an error it says the interface okay so the interface which is extending the sealed interface it should be either sealed or non-sealed so now we don't have an option of final because interface can never be final okay so that's why it's only two option sealed and non-sealed but for the classes we have three options sealed final and nonsealed so remember that that part yeah so this is how you can actually restrict what are the classes you can you want to extend and Implement of course it's not a compulsion uh feature to use in your project so it depends upon your project how you're designing it so if you have a restriction use seal classes so I hope this makes sense let me know in the comment section if this is a good idea in Java and what are your thoughts on seed classes welcome back aliens my name is D ready and in this video we'll talk about record classes now Java is amazing but there are some complaints and one of the complaint is Java is very verbos now of course it makes Java more readable in fact you can understand the well- written Java code just by reading it without documentation but the thing is there are certain places where you think hey this should be smaller of course you are doing a small thing but they have to write a lot of lines of code just to achieve that and one of the example is data carrier classes see what happens is once you start working on Java projects there are certain scenarios where you create classes which are actually functional basically you will do processing there it will be a service classes utility class or a class which is doing basic basically something and then you will be having some classes which are only used for data storage or maybe for data carrying purpose example let's say you have a server here and you have a client now client says hey I want data now if that server is a Java server of course in Java everything is object so even if you want to represent that data which represents some object or something it will be an object format let's say alien or the product or a laptop or a desktop now this is an object which will go from the server side from into a c side so the only purpose of a class which is defining an object like this is to hold data of course we are not going to change that data that class is only used for data transfer or the object is only used for data transfer so from the server side you will convert that into Json that Json goes to the client and everyone is happy or maybe you want to get that data from a database of course client needs a data so client will ask for the server server will say okay I will give it to you I will get it from the database now from database it will be a row which will coming into a Java as an object and then from the object we will get the Json what we are focusing more on is that object now that object is immutable because we don't want to change it second that object is only used for data storage we not using extra methods here we can but then that's not the intention of that now if you want to create that particular object in Java or for the class let's try to do that in this code and then we'll move towards why record classes came into picture so what I will do now is I have a very simple simple code here and in this let me create a simple very simple class here which is called an alien normally for me alien means programmers so I'm talking about you if you're watching this so for me you are alien but again that's a different story but let's say we have a class here which is alien and then I want to have certain variables in this now this variables which hold data we call them as States okay so let's say we got int ID so every alien will have an ID and let's go for simple two simple variables here which is ID and name so we'll have two things and normally what you do is with whenever you have a class which is used to carry data this is not a functional class we're not going to do any processing here it is just to work with these variables and if you want to work with these variables we need some set some extra methods maybe you want to assign values uh so one way to assign the values here is of course uh we want to make it private because encapsulation so make your variables private and also we'll make it final because this is not going to change so it's better to keep your variable final when you don't want to change it okay and then I want to assign some values as well now one of the way you can assign the value is you can use a Constructor here so let me right click and say generate I want a Constructor using which I can assign the value for both the variables and that's done and sometime if you want to fetch this data not sometime but then of course you want to fetch this data right so even if you want to convert that into Json you need to have the methods which will access it so what we can do is we can say generate and I want getter so normally we use get get methods for both this variable okay that's done so basically we need a Constructor we need two methods for fetching the values in fact imagine if you have 10 variables you need 20 methods to get the values sorry 10 methods to get the values and then I will be okay now there are two extra methods here we'll talk about it in sometime but we got this right now if I want to create object we can simply go back to the main method here and say alien and I want my first object let's say A1 is equal to new alien and then you have to pass the values here so let's say the first alien is one and we got a name and okay let's go work with only one object as of now and then with this object you can actually fetch the values uh let's say I want to print A1 do get name we can do that right so I can right click and run now we are able to Fest this data is because we have a get name method but what if you um you want to print the entire object something like this I want to print A1 now what I'm expecting when it when I'm printing A1 is I want to print the values of the state or the variables in this case which is one and naen but unfortunately if you run this code you will get some hash value here or exact code we don't want this we want to get the values in order to do that what you can do is you have to override one of the methods which is called two string and then of course we have done that a lot in our in our projects so let's generate a two string method which will print the value so the moment you have this method which is two string and if you this code now uh you can see it will print the values so I'm printing A1 but it is printing this values okay that's cool now what if you have two objects let's say alien. A2 now we got different object here and if I say new alien and if I say two comma let's say hsh now you can say we have these two different objects and what if you have the same object so when I say same object I'm talking about data so let's say we have two different objects but the values are same right and now if you want to compare two objects let's say I want to compare A1 and A2 of course in the memory there are two different objects but they have the same value so ultimately I can say they are same not with the memory location but with the values and the way you can compare them is by saying A1 do so I'm assuming you know what is two string here uh so two string basically prints the values equals just compare the values instead of comparing the memory location and stuff so you can see how many things we have so just to create a class which will only hold data we are creating so many methods now do we have a solution for this uh the answer is yes what if I can just commend the entire section how many lines we have here uh okay can we count the number of lines it is starting from Seven it is ending at 45 approximately 40 Lin can we just do that in two three or one line let's try so in Java if you want to represent a class which only holds data basically a data Carrier class we can create something called a record okay it's a record class and just mention the class name which is alien record record name which is alien and in the bracket I know that sounds weird in a bit but maybe if you know cotlin language it looks more familiar now you can give a round bracket and you can mention int ID comma string name you can open the brackets and close the brackets done 40 lines of code and one line of code which is better of course right this is better so this is what makes Java more readable of course now you can also read it right so when you say record it simply means it will have this component so yeah that they in record we can say variables instance variables as components and these are basically your state variables we can say State description State variables and yeah this is how you do it it's so simple right now behind the scene you got all the object you can see in the mean I have not done any changes and there's no error you got a Constructor with two parameters so what we are defining here is also a Constructor right look at this alien and the parameter with brackets it's behind the scene it is creating a Constructor for you which is a parameterized Constructor now since this Constructor is based on the value you mentioned here it is also called a canonical Constructor so yeah this is how you can do it it's it's it's so simple right and now what about equals let's try let me just run this code and you can see it says true okay so by default behind the scene equals has been implemented how about if I print the object itself I want to print A1 will it print the hexa code because you can see I have not written any two string method here but what happens if I do that run you got the values so even two string method it's already defined in this record it's cool right okay now this looks awesome in fact there's one more Advantage I will show you now what if you want to change the behavior of a Constructor can we do that and can we use a default Constructor let me try I will say alien A3 is equal to new alien and if I don't specify any parameter here it will give me an error the reason is you are saying we have two State uh two State description here so we have to mention both so by default we don't have a default Constructor inside a record you can create if you want because see behind the scene anyway you have a Constructor there right so even if you don't mention you implicitly get a Constructor which is alien which takes two parameters int ID and string name and it is happening behind the scene okay you don't have to mention this do ID is equal to ID and this do name is equal to name so this Constructor is already there you're just rewriting it okay it's not needed it's there already but if you want a default Constructor you can actually write it here not recommended but if you create one which is alien and you can give a round bracket here but you can see uh if you do that it will give you error by saying okay give me a hint it says non-canonical record Constructor must delegate to another Constructor so what it says is if you want this to work you need to call the another Constructor this Constructor and the way you can do that is by passing you can call this method by passing some default values I can say zero and empty string again not a recommended way because it will if you create object like this the default values you will get is zero and empty string let me prove it to you I will say A3 and run you can see we got this output which is which is not a good way because the the amazing thing is all the variables here in the record is by default final what it means is you can't change it how do we change it we need to set up methods right I can say A1 dot you can see there's no set method now since it is immutable you can't change it so you will say what's the use of a variable which you can't change and that's the idea right because this particular object is created just to carry data so why you have to change it once you create the object just sent it right and that's what we're doing here but yes if you have if you have that requirement create a class okay for you class is there this is just for some use cases or most of the use cases where you don't want to change data so by default your variables are final so it doesn't make sense to use your own Constructor but then you will say hey why do we have to recreate this Constructor when it is giving you that there might be one reason the value which you are sending here let me just remove this particular object here and let's make it one again what if you want to send one and naen that should work but if someone is sending zero I don't want anyone's ID to be zero so we can check that so what we can do here is we can do a some PreCheck so we can check if ID is equal to equal to zero in this case just throw the exception so we'll say Throw illegal argument exception and I can say ID cannot be zero so you can throw the exception just like that okay there's an error here oh I forgot the new keyword now let's run this code now since I'm creating an object by passing zero and you can see we got the error this is what we wanted right ID cannot be zero so yes if you want to do some extra checking before creating the object uh you can do that in the Constructor just like that now since we are saying that we should you know this should be more optimized can we do this there is one more way so this this Constructor is actually called a canonical Constructor because it has same parameters as defined in this state description here so we can create something called a compact canonical Constructor I know the words sounds weird but you will get used to it so what you can do is you can actually say don't mention the variables here anyway those variables are coming from here right why do you want mention that and don't need to mention this assignment as well assignment will be done automatically you do you do your checks that's it let me just try it out first let's verify with normal entry let's say this is two this time and run and it works you can see we got two naen if you say zero it should give you the error and we got the error right so it's so simple right we are just replacing all these lines just by writing whatever is required even if you don't need you can just remove this checking as well okay now certain things to remember first all the variables here are actually private and final why final is because it's immutable data so you don't have to mention that it is by default private and final and the thing is this record is actually a class so behind the scene it's a class basically and this class cannot extend any other class so even if you have another class let's say a book or a pen or a human this class cannot extend that because the record class extends a record class so there's in build class available but yes you can implement the interface as well that's completely compl fine example uh if you can if you want to implement you can Implement an interface let's say clonable or maybe any other interface which you love so you can inter implement the interfaces as many as you want okay and of course you can Define the methods as well so you can actually create multiple methods here uh public void show and if you want to do that you can actually write multiple methods you can create normal methods you can create static methods and you can create static variables as well but what if you want to create an instance variable can we do that so first let me create a static variable so I will say static int num that works but if you say int age now this will give you an error so if you want to create a instance variable do that here itself in this declaration not there okay so that's one thing you have to remember so when it comes to record by default every field is private final and if you want to access them you can do that with the help of I mean you can assign the value with the help of Constructor or uh you can get the values with the help of methods in fact there's one more thing uh let me just remove everything here what if you want to fetch the value because in the earli example when we used a class we were able to get use get name how about now so if I say A1 dot so we don't have a get name method what you have is a name method so whatever variable name you have just use that and use a round bracket since we don't have a Setter so there's no confusion between get set its name which will give you the value so yeah that's about this record class I hope you enjoyed let me know in the comment section and let me know if you want to see the more features on uh Java 17 hi my name is hayar Abbas I welcome you to one of the most potent course when it comes to unit testing with junit 5 testing framework I'm here to make you proficient developer with a strong skill set of junit 5 which is important for Java developers before we get into using junit 5 testing framework let me give you brief overview about this course to start with in order to understand unit testing you should have knowledge of java I'm not expecting you to be Java expert however from beginner to ediate level of knowledge is required in order to proceed with this course now you know Java you will be in a position to understand junit 5 which is the latest testing framework for unit testing you will learn both theoretically as well as practical and of course the more Focus will be on the Practical aspect of using junit 5 testing framework however we will also understand some theoretical part to have a strong Foundation about unit testing you need not to have any prer knowledge of unit testing or testing as we are going to start this from absolute scratch if you could able to watch all the lectures which are part of this course at the end you'll have stronghold of how to use J unit 5 you can also call it as junit Jupiter testing framework for unit testing which will make you industry ready if you want to complete this even more earlier you can also watch the entire Series in 1.25 xeed or 1.5x speed having said that let's get started hello and welcome back in this video we will try to understand about unit testing and how unit testing is different from usual test testing of an application whenever you hear testing in the software development ecosystem testing referring to something which is done post the development that means testing of an application which you have developed and usually done by a separate dedicated team of testers or you can say test Engineers different companies different designations we call them qas software development in testing test Engineers or or tester unit testing is also talking about testing the application testing the functionality of an application only however this is something which is done by software developer itself that means the one who is developing the code the one who is writing the code he will only perform unit testing exactly how different they are we will try to have an idea about this to start with whenever we have any requirement in front of us any problem in front of us for which we are trying to give Solution by developing an application usually we go through with software development life cycle sdlc and I know want to get into the entire development life cycle but typically what we do is based on the problem we have in front of us first we go for the designing before we develop the solution before we write the code first we design and based on the design then we develop application that means then we write a code that means coding is being done here and once coding is done once the development is done then we will perform the testing testing of an application whether it is working fine as per our expect no expectation now whatever testing is talking here it is different from the unit testing this testing is is usually done by a separate dedicated team of test engineers in a project which they are involved in and this is done post the development that means maybe you have developed maybe you have developed the web application so many classes so many files are involved there and you'll create a VAR and then you'll deploy once it is done then you'll go to the specific feature specific functionality of an application manually and you'll perform the testing whether it is working fine or not not and if there is any problem then they'll rise a bug and then developer will try to resolve which usually developers don't like it but however this testing is totally a different and this is the traditional way once we develop and that will be tested later by a separate dedicated team of test Engineers now what unit testing is talking is this is done when you are developing the application while you are writing the code and this is done by a software engineer itself now what do you mean by that let me just open this code file here now in this code maybe whenever you develop an application there is a possibility of having so many classes and in those classes we may have so many methods and one unit is referring to one method of those many classes example one unit one method I'm writing usually we write methods to write some Behavior to exec giv some behavior and this one method I can call it as one unit of an application it need not to be only one method sometimes in a bigger application a group of methods can be called as one unit a group of classes can be called as one unit of an application example we may have 100 classes in our application and that 100 classes you can divide maybe in uh 20 units so maybe five classes in one unit so sometimes unit can be referred as group of some classes based on the behavior and feature they are trying to develop or they are trying to execute or maybe it could be sometimes a group of some methods but typically generally we refer unit means one method because inside a method some Behavior will be there some functionality will be there some task you might have written and testing this one task is all talking about unit testing and this will be done by developer at the time of developing so what we usually do is first we write the test based on the test we develop a specific unit and then we will run the test whether it is passing or not if it is successfully passing that means this unit you have developed perfectly if it is not passing then this unit you have not developed at that instance itself you can perform the modification if your you know if your test what whatever test you have written if that is failing and you can modify that unit at that instance itself and whatever test you are writing it is not just for that specific time that will be there for the future also example I have written the test right based on the test I have developed this specific unit fine and now it is passing you know successfully that means this unit is working fine now later maybe I develop few more functionalities I will add few more units that means I'm writing few more methods maybe 3 4 5 6 seven methods I have added and for everything I have written the test at that instance also again the same test which you had written for this particular unit that also you can execute that means if I add some other methods if I add some other features is this old feature this old method which I have written is it getting affected by the new methods which I have added or not you can cross check by writing the same test which you have written earlier f or maybe by chance all of a sudden someone comes here and try to modify this particular method which you had developed and then if you run the test it will fail making sense so what I'm trying to say is this test you are writing for a specific unit it will have impact at that particular State also at the particular moment also also at the future that means the same unit the same test which you have written can be use and again and again to test your specific unit of an application so unit testing basically talking about testing your application in units so one method one unit I'll develop a test and I will write the code based on the test and then I will run my test whether this particular unit functionality is working fine or not and this is totally different from usual traditional approach of testing an application which is done after developing the whole application if it is a web application so many classes I have then I'll create a VAR of that then I'll deploy and then that will be tested by software test Engineers that means testers but here it is something which you as a developer are going to do it I'm going to write a test based on that I will write the functionality then I will check and how exactly it will happen how can we use junit that we will understand in upcoming videos to get more clarity on this hello and welcome back consider the application which you can see on your screen wherein I have developed one basic application which consist of two units two methods and I want to test these methods or these units and testing this one unit is only called as unit testing in order to test this particular method or a unit in Industry we use one of the most popular testing framework in order to perform unit testing which happens to be J unit 5 however can I perform unit testing without having to use junit 5 or any other testing framework yes you can do that and that's what I'm going to do here suppose whatever method I have developed here I want to test it in order to test that first you need to prepare how can I invoke this method what type of data I may have to pass in this method and whatever is required in order to invoke this method you may have to make an Arrangement let me write the test here in order to check this development which I have done here I would go to the main class H first I have just created the instance of that Kelsey class because the method that unit is uh instance method non-static method if you want to invoke that I may have to create the object this is also part of the preparation then I will pass the input to that particular unit or to the particular method so I'll use the reference here and then I would like to call that method and later I would like to pass some data maybe I'll pass some input here 10 and maybe five fine now if I run my test this method will get executed and it would return me some result that result I would like to collect it here maybe integer type result and this method would generate the actual result that means whenever you have developed a unit and when you test that that method would give you some result and whatever result that method is generating that can be considered as the actual result which method is generating and now whatever actual result I have got from the method or a unit which I have developed I would check with the expected result that means what what is my expectation by keeping what in my mind I have developed that particular unit so I will just verify it in other words I will perform the assertion here assertion means I'm just trying to verify is it matching is the actual result which is generated by that unit is it matching with my expected result and based on that later I would report the result to the developer whether the test case has been passed or not that means is if it is not matching if it is not generating the result which I'm expecting I may have to alert the developer that your test cases failed you may have to again work on that particular unit let me do that here first I will say here if in case if in case result is equal to equal to 5 maybe 2 10 by 5 or maybe two if it is two if it is two then I would alert my developer then I will alert my developer by telling whatever test case you have developed that is past I'll see here test case is past and else else if it is not then I will say here I would alert my developer that test case is failed test cases fail fine that means this I'm verifying and B based on the result which I have got I'm just alerting my developer whether whatever unit he has developed whether it is passing or not so let me run this application so this test case is passed now I will check with multiple types of input that means multiple test cases you may have to write again I would like to write one more test case here based on a different type of input and maybe this time I would like to give here zero and maybe it is result two or test case two and now I will check whatever test case to that means if I run that particular method it would give me some result and based on that I would collect it here I'm checking is it matching with my expectation is it matching with my expectation if it is not mapping that means the test case is failed and this is how we perform unit testing manually without having to use the testing framework I hope you got one basic idea about it so let me just brief over it these are the steps which we have followed for unit testing however out of all these steps some of the steps would be common example running the test performing the assertion alerting the developer based on the test case whether it is passed or not would be common however for every unit test test all these steps are necessary but they may differ based on the type of unit you are working on you're performing testing on example if you have a unit we may have to prepare differently for a different unit maybe one unit is a normal instance method if you want to invoke that we may have to create first instance of a class then we have to invoke example we may have another unit which is a static method in order to invoke that static method object or instance of a class is not needed directly with the class name we can invoke so what I'm trying to say here is every unit we may have to prepare differently we have to do preparation but in a different way for different units and we may have to provide the input to the unit differently for different units every method may expect different type of input based on what it is expecting you may have to provide that based on what type of oper you are performing inside that unit based on that you will be giving the testing input so in general providing the testing input is common but the type of data which you will give as a input May differ for different units and of course based on the activity which that particular unit or a particular method is performing you may expect a different result in general expecting a result or output is common but the type of the data which you are expecting from the method May differ from one unit to the another unit but however running the test case is common irrespective of the type of the unit or irrespective of the type of the operation which you are performing inside the unit performing the assertion that means verifying the actual result and the expected result this is something which is common irrespective of the type of the activity which you are doing inside a unit inside that method you may have to verify based on what type of input you have given what is the method generating what result method is generating that is the actual result and we will again have to provide the expected output what I'm expecting and based on the actual result and expected result I may have to verify and this is common irrespective of the type of operation that method is performing and later reporting the test result that means alerting the developer and manually we have seen we have used Buns of system.out.print in order to alert the developer whether the test case is passed or failed however in these cases testing running the test performing I mean verifying the result and alerting the developer these are common steps irrespective of type of operation which a particular unit is performing and here is where J unit comes into picture in place of manually testing unit if you use testing framework which happens to be J unit 5 you may just have to prepare and based on that you may just have to provide the input to a method and you may just have to provide the expected output J unit will take care of running the test case J unit only will perform the assertion J unit only will verify is the actual test matching with the actual result map matching with the expected result and J unit its in own way will alert the develop Vel ER whether test Cas is passed or not you as a developer need not to worry on that you as a developer need not to write extra lines of code in your test you just have to give the input you just have to give the expected output rest of the things J unit five will take care of it and to perform the assertion we have lot of apis it's not just you know it's more than performing the you know equal operation whatever we have done right now in the code manually to perform we were just checking whether this is equal to this or not it's more than that we have lot of apis while performing the assertions with junit 5 will take care of this and once we start with this testing framework J unit 5 you will have more idea about how exactly we may have to go here if you use testing framework which is junit 5 it will make your life easy whenever you want to perform unit testing and how it will help you eventually you will see in upcoming videos when we practically use junit 5 hello and welcome back in this video we will try to understand J unit 5 architecture and this could be the last video wherein we discuss about junit in theoretical way and in the upcoming videos in this series we will get J unit 5 in use practically and before I discuss junit 5 architecture one point I would like to add here junit 5 is not junit 4 plus new features unlike the other technology for an instance we have Java 8 Java 8 is Java 7 plus new features similarly we have spring six spring six is spring 5 plus spring 6 and all this new feature supports backward compatibility that means if you are using Java 8 then Java 7 whatever you have done till Java 7 that would not get affected so it is Java 7 plus a new feature and we have a new version called as Java 8 that is not the case with respect to junit 5 it is not the existing junit 4 and they have added the new feature and they have made junit 5 that is not the case with respect to junit 5 lot of developers when they started to realize the importance of unit testing when they develop an application they came to an conclusion we should have have a better testing framework though junit 4 was extensively being used but whatever the problems which were associated with junit 5 and know junit 4 they wanted to resolve that and they wanted to have a better testing framework in order to perform unit testing so lot many companies involved so many individual developers they tried to contribute it and finally they came up with a whole new thing which is junit 5 and what do we have here we will try to understand here the first thing which you need to understand here is about in the architecture is the underlying thing which is platform platform is something very fundamental or foundation of the entire J unit 5 one of the important aspect which provides the environment to run your test which consist of you know you can say test Runners here is this platform is what responsible in order to run your test cases this is the core of the entire J unit 5 architecture so if you want anything to happen it has to be done by platform so as a developer you not be directly interacting with the platform you'll not be using the platform though platform is the one which is a core of the entire architecture which consist of the know execution context test Runners it is a one because of which you will be able to run your test whatever you have written so what you would be using is you'll be using one of the apis and the API which is being used for junit 5 is Jupiter API and this API as a developer you would be using it which consist of all the methods for the assertions which consist of lot of annotations which you are going to use in order to write the test and then platform would be able to run the test which you have written fine and maybe if you have already written test and you have used junit 4 and now you want to use junit 5 so junit 5 does not provide backward compatibility as I have told it's a whole new thing what to do in that case in that case junit 5 provides one API which is vintage and you have to use this API in order to run the existing test which you have written and but you want that to be running in junit 5 you want to use junit 5 to run the test which you have already written earlier and maybe that you have used for the junit 4 so of course junit 5 directly you cannot use and if you want the existing test which you have written and if you want that to work with junit 5 then you have to use the library or API which is vintage that option is also available with respect to junit 5 of course direct backward compatibility we do not have in junit 5 with respect to junit 4 however we have an option we have an API which you may have to use apart from that we also have extension extension it's a whole new thing I mean it will just enhance the testing capability of junit 5 what do you mean by that so if you it's like you can have your own API and you can make platform work for you without you having to use Jupiter so usually we use Jupiter library Jupiter API whenever we are working with junit 5 and all the annotations all the methods for assertion which are associated in order to write the test all of them are part of Jupiter and if you want that to work if you want to use junit basically we add the Jupiter dependencies into our project we ask our Maven please add this Jupiter and then we use all the anotations or all the options which we have within that API how what if if you don't want to use a Jupiter can we have our own API maybe something third party apis apart from the Jupiter and I want to work with the J unit 5 environment J unit 5 architecture which is by using the underline the testing know underlying the test Runner which we have in you know uh in the form of platform I want to use platform without having to use Jupiter API can I do that yes you can do that with the help of extensions that option is also available with respect to junit 5 however usually if you you know if we write test for J 5 we use Jupiter API and suppose if you are using any of the Ides almost all the IDS Let It Be eclipse or it could be intell or it could be anything there is automatically all these IDs are integrated with the platform that means the one who is responsible to run your test cases so if you're using any of these IDs directly you have an option option there and if you just right click you have option to run junit 5 test so if you run automatically behind the scene it is integrated with the platform and then platform would be able to run your test cases and you may get the desired results so you need not to do much if you're using any of these IDs and according to me as far as I know almost all the IDS are internally integrated with the uh platform that means you can use junit 5 the entire architecture is integrated with all the IDS which we use so you can easily run the test cases which probably we'll see in the upcoming videos I hope you're able to understand this and let's get into junit 5 in use practically now it's time to get into junit and create our first mavin project for which I have opened Eclipse IDE you can choose any IDE of your choice but make sure you should be in a position to create Maven project why mavin project because it will provide a specific structure wherein using which you can easily write test by using junit it expect us to use no follow one specific structure and Maven will provide to us and however in the industry also we are going to use such build tools even if you don't know I would be guiding you how to create a maven project now I have opened the Eclipse IDE here and if you have opened Eclipse IDE in the the Java's perspective this is how you have to create it file new you'll not have any option of mavin project directly here if you have opened the eclipse in Java e perspective directly you'll have an option of create new Maven project since I have opened in Java's perspective I do not have any direct option I'll go to the others and in there Maven project next and create simple project next group ID some entries it is expecting us to pass we may have to enter that group ID is similar to what we give the convention for the package whenever we create a simple Java project and we create packages within which we write the class something similar here I'll give the package name as com dot maybe telescope dot learning whatever you would like to give and artifact ID the name which would like to give here I'll give here junit 01 finish now our Maven project is ready I'll just scroll down you can see here and now you can see Source /m/ Java here is where we actually write our project that means for our application whatever classes are needed whatever files are needed we are going to create here here is where we develop our application however the test which we are going to write is in the source SL test/ Java and this is the beauty of mavin Maven will give you this structure so I know want to mix my test files into my project file so project file should be independent of whatever test I'm writing here so I'm going to create the test here in the source SL test/ Java and actual project actual classes which are involved in my project I'm going to create here this is how we use mavin here so let me just create and for that I'll go to the pom.xml and since I'm going to use junit I'm going to ask my mavin please add junit libraries to my project if you don't use mavin then you have to do it manually manually you have to download all the libraries or jar files and then that jar files you have to add into your project by your own since I'm using a build tool mavin I'll make my mavin to do that for me so I'll just go here and add one tag called as dependencies tag and within the dependencies tag I'll go to the maven repository I'll browse to the uh Google I mean the browser of your own choice and once I have browsed here you can just search mavin junit repository you'll be open know mavin repository ww. mavin repository. comom and there you can see testing Frameworks and tools if you click there you'll have so many option the first option J unit I'll click that and any of the version I'll click this and you can see the dependency it's like the way we are informing the mavin to add the dependency required jar files into our project or required libraries and the moment I save this you can see Maven dependency that means all the required Library files mavin has added to our project we need not to do something explicitly this way we just informing mavin that please do this need for us I want to use junit could you please add junit library to my project mavin is like okay I'll add and you can notice here mavin dependencies fine and scope by default it is test scope that means whatever test I'm going to write I don't want that to be added in the deployment that means once I develop my project I deploy I don't want this test also to be deployed it is just for me when I develop just to check my units I'm writing the test not for the deploying know purpose not to deploy the all the test which I'm writing for the applications so what I'm doing here is like I'm just specifying scope that means it is just for the testing purpose for me as a developer I want to check my units only for that however anyway the dependencies I have added now I'm going to go and create a new project whatever project maybe simple project just to show you this I'll see here maybe caly and the package Let It Be com dot telescope Dot and maybe I'll say here learning or junit whatever name you'd like to give and I have a class here and I'm going to develop a unit within this I'm going to specify your public integer type whatever method you would like to give maybe I'll say here divide and within this integer type Maybe number one comma integer type number two and here I will say a return by dividing number one by number two it could be anything else we are going to explore so many units we are going to explore so many projects to start with one basic unit we have in my application and I would like to test it fine now how do we write the test as I have told test you need not to write in the actual project actual application you have to write inside this so I'll right click new you can have an option of junit test case junit test case and whatever is the name the name you know actually whatever the class name where the method is the unit is resding that name it's a convention to give test however before doing that I'll say class under test I will not specify directly here I'll say here class under test browse and I would like to specify here Kelsey and click this I'll say you're okay oops let me just um caly no problem prowse it and okay give the name as caly test and the package must be same as that you have given there the other way the easier way to do that also there by using the eclipse ID I'm doing it manually I'm just creating the test manually but usually we don't do that we have an ID which will help us to create the test I'll give the package here the same package which we have given for this class and the class name followed by test the test name you can give anything in fact you can give your any name of your own choice but it is recommended which unit inside which class that unit is present followed by test it's a convention whatever name you want you can give but it's good to follow the structure of naming the test that's it so after which I'll say here finish now my test is ready fine so let me run the test I'll see here right click run unit test you can notice we are getting the red it's failing that's fine we will see how to rectify that but however we are able to run the test so what I'll do here is fail pass we will see that later so I would like to just uh run one message here maybe first junit run okay maybe first unit junit run one message I would like to give into my console and now I would like to run that that means we are able to run our first test which we have written I would like to also show you how do we create this without we having to create separately so what I'll do here is you can see here maybe I would like to delete this okay done we can just click on the class inside a unit is residing and I want this unit to be tested I'll just say right click new you have an option of junit test case I'm using the IDE in order to do that now see everything is filled here new junit 4 test I know we have to learn junit 5 however in initially I'm just trying to show you how to use junit testing framework for unit testing of course we will also see how do we actually create test for junit 5 using junit 5 testing framework for now just using the junit simple so next see the test is ready you need not to do that explicitly like we have done previously just click on the crash new junit it will only create it and of course it is not running the way we are expecting we will Rectify this in upcoming videos this is the setup for the first J unit now the required setup is ready let's just run the first test let's use junit for the first time in order to check whatever unit I have develop whether it is executing as per my expectations or not I would just go through the unit one basic method I have here which will just divide two numbers so if I execute this method whatever output whatever result I'll get will be considered as actual result that means if I execute this particular unit this would give me some result and whatever it is giving me that is an actual result and there is something called as expected result that means as a developer what I'm expecting so maybe if I have to divide 10 by 5 my expectation is two and if I run this method if it is giving me two that means my expectation is matching with reality my expected result is matching with actual result in that case the test will be passed suppose I'm expecting two and if this method gives me maybe four then the expected result is not matching with the actual result which this method is generating then the test case has to be failed for this basic unit let me just create here so you you just have to use one method here that means you're just going through the assertion assertion means you're just trying to verify the result so what I'll do here is I have to use one of the method I'll say here assert equals and within this maybe I have to pass first the expected result and then what the actual result it is giving me so for an instance if I have to call this method let me just before I know actually do with respect to my unit I would like to give one basic method here maybe expected is Java and the method is actually which is generating is Java now consider this as an expected result this is what as a developer I'm expecting and this is the result which my method is generating if that is a case what would happen let me just run this test case I'll say you run unit test you can notice the green bar that means test is passing so similarly I would like to do know do with whatever unit I have developed so I have to invoke this method if you want to invoke this is a non-static method not a static method I have to instantiate that class first so what I do here is within this I would like to instantiate the class caly type maybe C equals to new caly instantiation is done and later I would like to invoke that method divide right and I would like to pass some data maybe 10 and assume I'm passing just maybe five it would give me some result that I'll collect it here in integer type maybe actual value whatever method when you run the method whatever it would give you that will be considered as an actual value and what is my expectation let me run this I'll say here expected result equals to maybe I would say here two now I will check here is my expected result matching with it's the API method which I'm using so let me just tell is it matching with my actual result or not so let me just save this and run run the unit and you can notice green bar that means my test is running successful sucessfully whatever unit I have developed it is working properly it is this simple to write a test and run a test of course we will consider many example many complex examples and we will Explore More on this to start with now we are able to run our first junit test let's create a test using junit without having to create mavin project that means by creating simple Java project let me create on my eclipse new simple Java project I would like to give the name as junit 2 I'll say finish I know what module as you can see simple Java project is ready in front of you now within the source folder usually we create all the classes all the class files which we want to be part of our application that means within this folder we will be creating multiple package will be creating multiple classes which would be part of our entire application that means actual code of our application would be present here as I have told earlier that test file should not be part of your application that means I have to create one more folder in order to create or in order to write the test though here itself alongside your application class you can also include your test files whatever test you are writing however it is not recommended so I'll be creating a new folder here right click I'll say here build path new source folder and name I would like to give here as test finish you can see here test folder also have been included now within this Source I'll be developing one logic within one unit so I'll be creating a new class here new class and whatever activity may be I'll write a logic with with a unit which will reverse a string object so I'll give the class name as reverse string as you can see on your screen I have developed one unit one method which would reverse a string whenever you invoke a method and pass a string object the logic is responsible to reverse that string object that means if I just pass the input here as Java and it should reverse this object and give me as this this is a simple logic which which I have developed here and let's test this particular unit using the junit testing framework so now I'll go to the test folder I'll see a right click new junit test case and let it be new jit 4 test case normal and I would like to specify here the class name that is a convention which we have to follow I'll say here reverse string test and it should be in the same package so I'll give the same package name also so I'll say here com dot now let me just say here finish it is asking add junit 4 library to the build path I'll say okay now you can see my test is ready fail usually we use in order to explicitly fail the test and I don't want to fail the test here rather I would like to write the assertion methods and verify whether my unit is working or not for which I may have to invoke this particular method and if you want to invoke this particular method then you have to create the instance of this class because this particular unit is a non-static directly with the class name you cannot invoke so first I would like to just create this class name I mean create the object within this method and maybe I'll give the name as reverse equals to new reverse now the object is ready using the reference I would like to invoke the method which is responsible to reverse a string object and within this I would pass my string object I have passed my string object as Java now this method would take this input and perform the activity to result to reverse the string and whatever it will reverse that object it will return return I'll be collecting here with an object called as string type maybe I'll give it as actual object that that means if I invoke the method whatever actual object whatever activity it is doing based on that whatever actual result it will give you that is what I'm collecting here what is my expectation that also I would like to specify here within a variable called as what is expected I'll give it as maybe expected and my expectation is here this so if I invoke and if I pass Java it should be the expectation now now by invoking the assets I'll be verifying it asset equal and if you just check here as uh control space you can see all the methods which you can use here and we would be exploring many of them but let me just write here the expected is this and actually what this particular method is generating me let me just run this test and check if it is solving let me just say here right click run unit test I'll say okay and you can see green bar that means the test is running successfully now I would write more shortcut here this is not recommended to write multiple lines so I'll make it more simple instead of calling this method storing that in a different variable and then passing that here and expected also directly you can mention here itself that means you can simplify this code even more so I'll go here right click I'll say here refactor and there is an option of in line if you just click here I'll say okay and you can see that line has been added here directly so you can do the same here also and this particular line will be added here so I'll say here right click I'll say refactor in line where that particular local variable is used exactly there it will add automatically so you need not to again use those lines this is simple logic building but however using assert equals method we would be we are now able to run our test and verify whether whatever unit we have developed is it giving us a desired result as per our expectation now we have done this using simple Java Project without having to create mavin project now let's write the test using junit 5 testing framework which can also be called as junit Jupiter and I would do that using simple Java project we would also see how do we do with respect to mavin project now let me just create a simple Java project new Java project I'll give the project name as junit 03 I'll say here finish you can notice my project is ready now within the source folder we would be developing actual classes all the files which are required for our application my units would be there if you want you can also write the test in the same folder but that is not recommended I know want my test to be part of my entire application I'm testing so that the development is good so for which I would be creating a new testing folder here so that I can perform I can write all my test within that folder I'll go to my project right click I'll say build path within the build path you have an option new source folder I'll give the folder name as test finish you can see a test folder is ready wherein you can write your test now let me just create a new application here new class wherein I'll be developing the unit I would like to use the same program which we have developed in the previous video so let me develop that unit now now you can see I have developed one unit here if you invoke it should reverse a string to this particular unit or a method if I pass this as a input this should be the result this particular unit or method should generate for which I would like to write a test using junit Jupiter so for which I'll go for my test folder wherein I would like to actually write all my test right click I'll say here new junit test case you can see it is asking the package package name must be same name as the name you have mentioned for the files and it is recommended to give the file name also same name as the class name reverse string followed by test you can give some other name but this is the recommendation package let me give it as com dot telescope dot learning now you can see new junit Jupiter test junit Jupiter test is referring to junit 5 like for J unit 4 and three we have numbers for J unit 5 we call it as Jupiter as we have seen in the architecture Jupiter AP is what as a developer directly using it so let me say here finish add J file library to the build path I'll say here okay you can see my test is ready fail method will explicitly fail the test I don't want that to happen I want to invoke that particular method for which I have to create the instance of a method because this is a non-static method like the way we have done in the last example let me just create the object of this I'll give the method name as reference equals to new and this is the object I have created after which I would like to invoke the method dot a method reverse string within this I would like to pass my string whatever object you would like to pass I'll pass it as Java now it this method would take this as an input and it will do the all the activity required activity or behavior it will execute in order to reverse a string and it would return me the result which it has generated after executing this method whatever result it will generate if you want you can store it in a variable called as string type actual data this variable name can be any anything since it is executing and giving me the result and that is what I have given the name as actual that means actually what this particular method is generating let me set my expectations here I'll see here string type what is expected out of this method and as for me it should give me this output let's see if my method can do that for which I have to use assertion API I'll be calling all the methods of assertion I'll say here assert equals method you can see so many options are there however I would just use some of the method here and I like to specify expected actual maybe the spelling so I'll say here expected whatever name you want you can give here however these two statements which you have written here you can directly write here itself if you don't want to mention it separately for the good readability purpose you can write in the same way or else you can just write it in this way also so so let me just right click here where this actual local variable is used I want this entire statement to be added there directly so I'll say here refactor in line so wherever this was using that local variable there itself the entire logic have been added same thing I would like to do here I'll say wherever this expected is used can you please add the entire logic whatever statement I have written the right hand side to this particular statement so let me just go here refactor I'll see here in line you can see we are able to do that so let me just write few more cases here and this time maybe I'll pass it as some other string alien and my expected result would be n e i l a let's see if we are getting it so let me run this test I'll say right click run as unit test you can see green bar that means my test is passing successfully it is similar to junit whatever we have done previously and this is how we create junit 5 test and we run it we will see more on this in upcoming videos now let's understand about this test annotation which we have used over the test method now to start with this test annotation we usually apply over the method to mark method as test that means this way we are informing the test engine the platform which we have seen in the junit 5 architecture that this is a method wherein we have written our test this method you need to execute so here we are just informing by applying this test method out of all methods which method the platform have to execute and this test annotation which we are able to use is present inside a package called as ar. jun. jupiter. API and Jupiter API came into picture from junit 5 junit Jupiter not in junit 4 or three there this annotation was present however in a different package now as I have discussed earlier that junit 5 which is junit Jupiter is a different architecture Al together now whenever you apply this test annotation to a method that method need not to be public unlike for junit 4 and three it could be public producted and default example you have applied this to a method called as test and the test method need not to be public it could be default Also let's check that by opening the code which we have already written as you can see this example junit 03 I have tried to use junit 5 which is junit Jupiter and this method it was generating the moment I have asked my Eclipse to create a new junit test for me and you can see here it is not public it is not protected it is default and let me open wherein I have not used junit 5 I have used J unit 4 let me open my test you can see here it is public when I have asked my Eclipse to create a new junit test case for me it was generating a test method and we have brought here something called as public that means this test annotated method was public or must be public however in J unit 5 you can mention you can see here it could be public it need not to be public it could be public also protected also it could be default also and by applying this annotation to this method we are informing the engine that this is where you have to run the test this is where we have written the test which you need to execute and and let me just run this and show you if I just run this particular test I'll say run as junit test and we are getting green bar and also let me add this point here this green bar indicates that the test is successful however one point I would like to add here usually here in the J unit it will test for whether the you know it will test whether it is failed or not it will check for what whatever logic you have written it is failing or not it will not check for success and if it is not failing by default it will consider that this is a success so the default Behavior will be success for an instance I would not be writing this let me just remove this from here nothing is there still if I just run this test let me run this test now so I would like to run my genu test you can notice we are getting the green bar it is not checking for the success success is the default of this junit the default Behavior it will check for failure that means within this annotated method is there anything which is failing it will just only you know it will check only for the failure and if it is not failing by default it will consider that this is success I hope it is making sense now for an instance this is the method I have applied maybe alien now I'll say here this is what I'm passing and this is what I'm expecting of course let me run this test again if I run my test it should fail you can notice it is failing and you can see the detail it will just whenever you run the test it will check for the method with annotation test like the way we write main method to inform jvm what is the start point of our Java application whenever we write Java PR programs we have to specify main method and through main method we are informing the jvm from where it has to begin the execution of java program we are specifying this starting point of the execution similarly through this test annotation we are informing the junit engine which is platform that where the test is present which the platform needs to run now let's talk about assertion which simply means verification between expect result and the actual result expectation versus reality suppose I have a expectation that I should be developing a unit or a method wherein if I give this as input this should be the output that is my expectation I should be developing such a method wherein if I pass this as an input I should be getting the result as this for which I have develop a unit after developing a unit when I run my program and pass a particular data what this particular unit or method is giving me as a result what this method giving me the output that is the actual output or you can call it as reality this was my expectation I have developed some code and whatever code I have developed that is giving me some result whatever result that particular code is giving me that we call it as reality or you can call it as actual result assertion is all about uh comparing what is your expectation and what is the reality let me talk more on this as you can see here it's all about expectation versus reality if expectation is matching with the reality whatever code we have developed and whatever know output it has generated if it is matching with my expectation then if you're using the J unit you'll be getting the green bar that means your test is pass suppose whatever is your expectation you have given and then after running a particular code whatever result it will give you the actual code that that is a reality if it is not matching if you are using the junit testing framework which we have done so far you'll be getting a red bar that shows that whatever test you have developed that is failed and in order to do that we have used certain methods asserts equal one of the method like that we have so many other methods which are there in the assertion class which is a part of which is a part of your junit Jupiter API and all those methods are static method so let me like the way we have used assets equal and similar to that asset array equals asset to asset for so many methods are there we will try to explore you know more uh going forward in this uh entire series and basically it is present inside a package called as org. jun. jupiter. assertion class and Jupiter if Jupiter is coming to picture I'm talking about junit 5 or you can say junit Jupiter of course if you have to use junit 4 some methods are present but they are in the different package because that follows a different architect and junit 5 it's all about junit Jupiter we use Jupiter API here so all the assertion methods if you want to verify expectation versus reality actual output then you may have to use all the methods which are part of assertion class which is there inside Jupiter API and after know junit 5 or you can say after it has introduced junit Jupiter it supports all those methods also supports Java 8 features such as Lambda expression stream API which was not there earlier in case of junit 4 now we have used one of the method assets equal in our previous examples now going forward we'll try to explore more methods for the assertion now let's try to write more test in the process will understand how efficiently we can write test whenever we have multiple units and how do we run for which I would be creating a new Java project let it be simple Java project new Java project and I would give the project name maybe junit 4 and let me finish this I know what module we have a project in front of us now let me just within the source I would like to add my files for my application whatever I have done in the previous project I would be taking the same I'd be taking from here and I would just paste it now we have got the same project here which you can see in the same package we have this project and the same unit we will try to understand no we will try to run it by writing multiple cases and how do we do that so first of all you know if you want you can write the test within the same Source folder that is not recommended I'd be creating a new folder where I can write my test I'll say here build path new source folder I'll give the name as test because this is for the testing I'll say here finish and within this I would be writing the test I'll right click I'll say a new junit test case that's a beauty of an ID which we use so I'll be giving the package it must be same as that of the package wherein your application files that means the class under test which whichever unit you are testing and that unit is present inside some class right that class we call it as class under test so wherever the class is present the package you have to give same I'll give the same package because this is what we have given there and the recommendation the convention to write the names for the test is whatever is the class name class name under test followed by test no I would like to give something else no problem but this is the convention which we follow the moment I say it's a new junit Jupiter that means junit 5 I'll say here finish it is asking me to add junit 5 library to the build path I'll say okay it has added that and we have got this and as I've told unless if you explicitly F fail you know the test which you have written you have to invoke this fail if not it will just check with respect to the assertion methods I don't want to explicitly fail this so now it need not to be test only the method wherein you're testing that must be annotated with the test method name could be something different also let me uh know explain that to you I want to test this method reverse string inside this reverse string we have a method called as this unit called as reversa string and this method this unit is present inside this class and since it is a non-static I may have to create the instance whichever method I want to test and whichever you know that method is present inside whichever class class under test that instance I have to create I'll be creating that so I'll just create this particular instance for which I'll just take this and uh I would have typ but anyway I'll say your reverse equals to new and this is what I have created done now instance is ready using the reference using the reference I can invoke the method and whatever is is the input You' like to give you can pass here input we need so I'll pass this as my input which we have done and later it would generate me uh you know the result which I can consider as an actual result like we have seen in the previous lectures what I'll do here is directly I'll call the method called as asserts equal and within this I would be passing the actual result and the expected result this is my actual and I would be passing the expected result and that expected result I'll be passing here maybe this is what I'm expecting and this is the input I'm giving and definitely it will run let me run this I'll right click I'll say here run gunit and it is running successful wonderful no problem in that so what I'm trying to say is what I'll do here is based on the type maybe I am just reversing one word I'll give the name as test reverse string followed by underscore I'll say here one word test reverse string one word so what I'll do here is this is the method I'm writing and I'm just trying to reverse only one word fine maybe I want to reverse more than multiple words more than one word maybe one entire sentence for which I'll be writing a different test so what I'll do here is I'll write here void maybe test reverse string reverse string for what type of data I'll say here multiple words multiple words and this method cannot be considered by the platform by the test engine unless you annotate it so I'll be annotating it with The annotation called as test now this method will be considered by the test engine here is where I'll be writing the same logic wherein different case so let me run here and this time I want to pass this as an input maybe I'll say here Java is easy more than one words are involved and what is my expectation let me just show you the expectation I'll say here y s e sorry a e and Si and it has to be fine so let's let's uh cross check if it is working fine and I would like to check here I would like to run my test run as junit test you can see both the tests are passing we are getting the green bar for both the test and also let me just show you one more thing maybe I don't want to you know test this particular whatever test I have written here I don't want to run that I want to run only this test so can we do that yes we can do that you can just you know select whichever test you know you want to run I have selected here I'll right click and there's an option of run as junit test and you can see it is testing only this reverse string multiple words you're not testing this so you can test you can run the individual test also or you can run all the test which you have written and also it need not to be just one name test whenever we have multiple units you have developed you know a big project usually we test with the precise names to avoid ambiguity to avoid know to avoid the confusion once as in uh we'll write more modules more classes the project will be know will get bigger and bigger in that case maintaining the test which you have written would get difficult and this way you can be precise for what purpose this particular test you have written you can be precise with the name itself test reverse string only for one word testing a reverse string method only for know for the multiple words and one more thing I can do here is I can just create the instance of a class under test directly at the class level and here I need not to write this once again now this will become more efficient you can see here it has become more efficient because this is the class under test all the units which are there in this class is what I'm testing for which I'm writing the test I'm trying to run so now let me just run let me just uh change this this is my expected result and I'm passing this input the no definitely this case this particular test will not succeed let me run it if I'm running see it is failing now let me give back here and I would like to run all the test which I have written here so for which I'll go here entire file itself I I will test junit and I'm testing so it is pass you can see you can cross check both the tests are there I hope you have seen how efficiently we can write multiple test and I'm creating the object or instance of a class under test directly at the class level so that because all the test which I'm writing here it is for the same units which are present inside this particular class we will see more in upcoming videos of this series write test then Code test driven development methodology which is one of the pattern or one of the methodology which we follow in order to develop an application and whenever we follow test driven development very often we use junit testing framework in order to write our test now let's see how do we write test first and then develop a application in this video so let me just open the eclipse ID and here you can notice I have created one Java project simple Java project with the name junit 5 which we have done earlier and all of you know very well how to create this project and after which I have created two packages or two folder Source wherein I'll be writing all my application all my classes files code which is involved for my application development and I've also created one package or folder which is test where I'll be writing all my test for this particular application so within this Source I have created one package com. telesco do learning and within this I have created one class called as shapes so the application which I want to develop here is maybe which will calculate the area of certain shapes maybe area of square area of rectangle area of circle such shapes area it should calculate for which I have to develop some units here so what I'll do is first let me develop one unit you may say here first you writing the code first we may have to write the test I'm not writing the entire logic here just the method signature because in order to write the test first some method is required right so I can invoke that method wherever I'm writing the test for that I'm just writing the method signature without the actual logic public maybe I'll say here double and maybe I'll say here area of square I'll say area of or maybe I'll say here compute or calculate compute square area and I'll accept some input here maybe I'll say here double type the length double type length and based on this I have to return some value for now for now I'll be just telling here return zero what logic to calculate that we will develop that later because first we have to write the test the test will be failed then in order to pass the test we have to develop the logic that's the approach we have to follow here basic method signature I have developed actual logic which is responsible to compute the area of square I have not written here in the test driven development we don't do that we have to first write the test so I'll go here in my test folder or package I'll be creating one unit jit test case and let it be junit Jupiter and the package which I want is dot tore do learning and where in my class under test is present whichever class I would like to test that is present so I'll give the name I have given the name as shapes and by following the standards I'll give it as shapes test I'll say here finish please add that to my build path I've added now you can see it is ready now as you as we have seen earlier we can give the method name maybe test area maybe I'll say here test compute compute square area method I want to test this method so only I have given the name choosing the you know names for methods is quite difficult sometimes but anyway I have given this name and now within this I'll be writing my test since the method which I have written here it's a non-static method I may have to have the instance of this class whichever class is under test I have to create the instance of that class I'll be creating it I'll say here shapes type maybe shape equal to new shapes the instance that I have instantiated that class and now using the reference I would like to invoke that before I invoked directly I'll call the assertion method and this time also I'm going to use assert equals you can see we have lot many options here asserts equal so I'll be just using that here and here I'll be giving the expectation and here the actual with this method generate so I'll say here I'll call that method shapes dot I think I have given the name shape shape Dot and I'll compute the area square and maybe I'll give it as 24 and if we calculate what should be the expected result I'll say here 576 should be the expected result fine this value earlier I had calculated 24 into 24 and then it should generate this value fine so this is my expect expectation and if I invoke this method what result it would give me what would be my actual value which will be generated by this method fine so let me run the test here I have developed the test first of course it will fail because I have not WR any logic there so let me go and just uh run my unit case run junit test and it will fail you can see here it is failing fine so now I'll be developing that I'll go back to shapes and to pass my test I'll be developing what logic is required maybe I'll say here double type result equals to length into length and if you calculate length into length that is the result and this result I would written instead of this you can directly write the program in this style maybe you can say here return length into length fine now it will just take it and it will calculate of course this is very basic uh application just I'm showing the approach how do we follow the uh know methodology of test driven development here so I have developed this unit now again I'll run my test let me just run this test I'll say here run junit test and this time it is passing now let me have one more unit here one more unit I would like to develop and that unit maybe would calculate the area of compute circle area and for Circle you need radius and the formula is pi r² so what is the value of pi I'll say here 3.14 into r² so I'll say here radius into radius radius into radius I have developed but before we develop actually in the test driven development we should not be developing this so let me just erase this and I'll just say return zero return zero what I have done I've just given the method signature so that I can call this what logic is actually involved to compute the area of circle that I should not be doing it I have just given the method signature what I have to do what I'm going to do it but what actually the code which is responsible to do the certain task that I have not specified yet first I'll be writing the test let me write the test here and I'll write one more test so I'll use The annotation test and within this I'll write void maybe test compute circle area done and within this I'll do one thing I'll create the instance of this test under class directly at the class level so that I can use for all my cases and now within this I'll do the assertion assert equals and within this I'll pass my expected before that I'll say shape dot compute Circle so radius maybe I'll pass the radius as maybe five because this value have already noted down so that I can show you and if it is sp the result I should getting is 78.5 this is my expect result and uh let me run this particular test case now if I run this test case it should fail as you can notice it is failing so first we have to write the test it should fail and then we have to develop the logic then we have to develop what logic is involved in order to calculate the area of circle later I'll be developing so and also i' would like to show you in the asset equals method you can also pass the third parameter if if it is failing whatever message you want that can be displayed I'll say here yet maybe I'll say here area of circle calculation is wrong one message I have given so let me run this of course I have not yet developed the logic if I run this it should fail let me just run this and uh we'll see run junit test and if I run oh oops yes it is fail you can notice here fail Trace you can see entire Trace is also giving so what was expected 78.5 and what we have actually got 0.0 making sense area of circle calculation is wrong that message whatever we had given here that is also displaying it now let me develop the logic here I'll go back to my unit and here I'll develop the logic and now I have actually run the coverage instead of running test no problem I'll say here return return 3.14 into radius into that means later I'm developing the logic so I've saved this now I'll go back and run my test let me just run this test again enter I'm running as a whole so all the test which I have written everything will be running now you can see both the test cases are no passed and I'm not getting the message which I had given here it will be getting only you know when the test is failed now we have seen how to test you know how to run the individual test how to run all the test which you have written and how actually what structure we may have to follow that also we have seen and with basic application with basic units we have also seen how do we approach test driven development so writing first test then developing does not mean that you're not going to write anything you may have to at least write the method signature or the unit signature so that you can invoke that when you are writing the test and it should fail first and later you have to develop the actual logic you have to write actual logic which is responsible to give the desired result which you are expecting and that again after developing that again you have to run the test and check if it is passing or not now let's create the Maven project and use junit 5 or junit Jupiter in order to create the test we have done that using simple Java project however we will be now creating the mavin project because most of the projects in the industry we use any of the build tool mavin or gdel so we have done junit 4 with mavin now it's time to create a mavin project for junit 5 so let me create a new project similar the way we have created earlier I'll say go create others because it's a maven project and new Maven project next create simple project and group ID I'll give it as com do telescope and the artific ID I'll say here junit 5 and uh it is 06 project for MAV let me just finish you can notice our project is ready it is the maven project and the structure like we have seen before it is same all the test we may have to write within Source SL test/ Java and whatever the classes which are part of our application those classes we are going to write within Source /m/ Java our actual application will be developed here which we have discussed now in order to proceed ahead we may have to add the dependency here in the pom.xml file so that we can use junit 5 or junit Jupiter so I'll be just showing you what properties are dependencies you may have to add here and this particular file I would be sharing you with the GitHub link or I will attach this entire file you can also use while you are doing it so first of all when you have to add this dependencies and the version which I'm using here J jupter version you can notice it is a latest version currently 5.1.0 this is the latest version for junit 5 that is what I have used here and it is junit Jupiter and the group ID is that means package is or. jun. jupyter here is where this API is present and all the methods and the annotations which are part of this API is what we are going to use so since it is junit Jupiter that means I'm asking my Maven to add junit 5 let me add this dependencies first I would would like to add these dependencies now I have assigned this version in a property I would like to add that as well all these properties and one more point you can notice here in the properties I have given mavin compiler do Source 14 that means I want to use Java 14 here because whenever we create a mavin project by default it will take Java 1.5 that is a default behavior however junit 5 will support eight version are higher than eight maybe 11 14 17 but not lower than 8 so I have to add whichever version of java you want you can give you can give 1.8 11 14 it's up to you and the latest version of Maven is what I have added here and let me just add the uh certain more plugins here this plugins also would like to add and this file anyway I'll attach you need not to worry you need not to work much on this now you can see mavin compiler plugin which is required know to compile this entire mavin project we have many more plugins we will see we have a sh fire which can be used to run the test cases uh using filters and we can use for the reports to generating the reports so many things we will see that in the upcoming entire series however these dependencies have been added let me save this after saving you can notice now it's still 1.5 but I need 1.8 are higher so what I'll do is I'll go to my project right click and you can see here there's an option of Maven and then update project I'll click on update project and I'll say here okay now you can see Java 114 has been added so let me save this now all the required dependencies have been added now as we have discussed earlier also we may have to create the test within Source SL test/ Java within this I'll be creating a new test I'll say a new junit test case if not you can create your own class and within that class you can just write a method which is responsible write a test with annotation that method must be annotated with The annotation test so let me create using the eclipse ID itself and the package maybe I'll give it as com do learning do I mean maybe I'll say aom dot telescope do learning and the name I'll give it as uh test trial I'll say here finish you your package as we have discussed you have to give for whichever class under test wherever the class is present the package must be same it is New junit Jupiter test that means J unit 5 so let me just finish and you can notice here our test is ready I don't want to explicitly fail it if you want to explicitly fail you have to use this fail method I want to use the assert equals which we have used earlier and just for the demo purpose now within this maybe I'm expecting the result to be six and I'll say here what is the actual result which my unit May generate maybe I'll say here 3 + three something some activity or maybe I'll give it as here also six doesn't matter so this is the expected and this is the sorry this is the actual result and this is the expectations and both of them are matching so of course the test must run so let me run the test here right click I'll say here run as junit test you can notice our test is running successfully green bar that means test is passed and this is how we may have to created and if you want to add all the projects as we have discussed in J unit 4 M project structure example I want to test this so let me just add this entire thing and if it is in the maven you have to add in Source SL main know/ Java here is where we have to develop actual applications I have taken the application which we have developed in the previous example wherein it is calculating the area of shapes let's try to you know write a test for these units so let me go here and just let it be there if you want to write one more test I would like to create a method called as void and void test maybe I'll say here compute compute which method we want which unit compute square square area this is the method I want to compute and as you can notice here whenever we are using junit 5 it is not it is giving us default methods because junit 4 it has to be public all the methods which you annotating with the test has to be public but from junit 5 it need not to be public it could be public there is no problem it could be default also now unless and until I use one annotation test it will not be considered now test that means junit engine will consider this also in order to run now here what I'll do here is I'll say here assert equals and uh let me see my expectation what is your expectation I'll say here maybe 576 and uh I'll call this method for this instance is needed so first I'll create the instance of shapes I'll say here shapes type shape equals to new shapes because it is a non-static method and using the instance I'll call that method I'll say here shape Dot and compute square area and here I'll pass 24 now 24 Square means length into length 24 into 24 will give you 576 let's run this particular test and see if it is working right click I'll say run as junit test run as gunit test you can notice test is passing now let me give some other result here and this is my expectation and this method or unit would generate a different result let me check and run this particular test which I have written and you can notice test is failed so with this we have been able to use junit 5 J Jupiter both in normal Java project or in the maven project depends on your need from upcoming series and lectures you can use either Maven you can create either Maven project or Java Project based on your need as we have seen so far if you would like to run the test which we have written here we just have to right click click and run as directly we an option as junit test and we could able to easily run the test with just some clicks and this is happening because of eclipse Eclipse IDE internally integrated with the test Runners of junit framework for which we need not to do something explicitly here and easily we could able to run the test as a unit or maybe whole test how many cases you have written all of that at once if you want to just and there also we have option you know run as junit test easily we could able to run all the tests which we have written here what if I want to run the test without having to use the facility which eclipse is giving to us in Eclipse it is easy right click run as junit test and this facility is from the eclipse and I want to do that independent of eclipse what if maybe my project is going in cicd there build process is not going to install the clips and right click and run as junit test case in that case I want to run as a whole project Maybe by using the facility of mavin test for which we may have to integrate one of the important plug-in which is sh fire plug-in how to do that integration is what we are going to see here and most of the projects and most of the companies will be looking for it so I just have to add one of the plug-in and as we have seen within the build after the dependen I have already added one plugin and here I just have to attach sh fire plug-in so let me just attach that here and this entire file I would be sharing with you so let me just add this particular plugin and as you can see I'm able to add this Shore Fire plug-in now what I can do is I will not need to use the facility if you are comfortable with the eclipse maybe right click run the junit test there is no problem no harm and uh I'll have now one more option if I just click on my Maven project right click and you can see run as I have an option of Maven test I'll just click here test and as you can notice on the console so many options I'm getting for the first time you can see here and you can just scroll down and maybe let's see all the required things for the first time it is just installing and now you can see test every time all these things will not come for the first time when you are you know integrating this sh fire plugin you'll be getting this and now you can see I could able to run the test here I'm not getting that green bar or red bar which was the facility which is given to us from the eclipse ID I'm able to run this as a independent without having to use that and you can see here there are two test cases zero failures and what is the result zero failures zero errors zero skipt and both of them our success build success and this is how we could able to perform mavin test our project test without having to use the facility of Eclipse IDE and for which we just have to attach this important plug-in here maybe let's change the version of this Shire plugin which could be the latest one being 3.2.1 and let me just see if could able to run this and I'll just go here I'll say here run as mavin test and if you're not getting this you can go to the mavin build there you can mention the goal as test and you can run it so let me run mavin test again since I have changed the version and all the required files it is just installing it is downloading and you could be able to see now we will be in a position to run the test without having to use the facility of eclipse see test and as you can see build success that means both of our test cases which is a part of our test are success zero failures and this is how we could able to integrate Shore Fire and able to run our test without having to use the facility of Eclipse IDE now let's understand more on asset equals method as you have seen so far in all the test cases we have written we have used asset equal method and very often we have passed two arguments here two parameters number one happens to be the expectation and second the reality What that particular unit would actually generate and also in the previous example where we have seen we had passed one more argument here which is a string type message and that would be executed only if or that would be displayed only if your test case is failing that means if expectation is not matching with the reality and your test case fail and whatever string message you are giving here that would be displayed for an instance I would like to run this test here right click run as junit test and if I just run this test you can notice it is passing and we are not getting any message now maybe I'll give my expectation is 76.8 and this method would generate me something else that is actual result and now definitely expectations and reality will not match and the test has to fail I'll just right click run the unit test and you can notice Red Bar and also one more important Point whatever string message you had given that is getting displayed here same thing I can do using one more approach here what is that approach if you ask me by implementing supplier interface instead of directly giving the string value I would give the same message but by implementing supplier functional inter which is Java's functional interface and by implementing that you can do that and what will be the difference if you do so and how do we do that we will understand here and let me just reuse the same test case and I'll just change it here I'll give here a circle maybe I'm going to implement now supplier and now supplier is a functional interface if you want you can Implement in your own way but since it is a functional interface I would Implement using the Lambda expression so parameters required and then I'll use the Lambda operator and the actual implementation so this time I'm giving the message here using the implementation of supplier interface direct string message I'm not giving here so now let me first pass the test here and I would like to run this particular test which I have written so we are getting the result no problem well now I would like to fail this test maybe I'll say here 74.5 and this would give me something different and the actual and expectations will not match and you can see right click run unit test and if I run we are getting this let me just rerun and I should be getting the message as well so I'll run junit test case and see you can notice we are getting the message whatever message we had given here so what is the difference both the methods are behaving same what is the difference between this and this here you're directly passing the string message and if you pass the string message directly this assert equals method is evaluating that evaluation is happening that string message is being considered and by chance if test is failing it is displaying that message if test is passing that message is not being displayed but but that message the string object is getting evaluated in this case whatever you have given using the implementation of this supplier interface it will be executed only if the test case is failing what do we mean by that by chance if the expectations and reality if both of them are matching that means test case is passed and if test case is passing this part will not even be evaluated by asset equals method that will not be considered and only if the test case is failing expectations and the reality is not matching only then the evaluation is happening and whatever message you have given here in the form of string that is getting displayed so the only you know difference here is this is the efficient way of writing or giving the message to developer just in case if the test case is failing and here also you can do it there is no problem but that will be considered only if it is failing that display you know Asser equals method is displaying that if is not failing if the test case is passed display of that message is not happening but that has been considered that has been evaluated here evaluation only will not happen if the test case is passing if the expectations and the reality is matching evaluation only will not occur only fails it will evaluate and whatever message you have given that will be displayed there so implementing the supplier functional interface and giving the message would be more efficient now let's explore assert not equals method in order to perform assertion for which I have considered the same application which we have developed wherein I have two units one is responsible to compute the square area one which is responsible to compute the circle area based on the inputs we have received here for which I have written one test class in a different folder and I have written two test cases number one here I'm going to perform the assertion using a assert not equals not equals by the method name itself you should understand it will do exactly the opposite of what asset equals method do that means in asset equals method if expectation is matching with the reality test case would pass if not test case will fail in this case if expectation is matching with the reality test case will fail if both of them are not matching test case would pass for an instance I'll give here some expectations maybe five maybe I'll say here 706 and this is my expectations if I invoke a method which is there inside shapes class dot compute area of square area I'm going to pass here 24 and 24 into 24 if you calculate this particular unit will calculate and the result would be 576 now the actual result which I'm going to get from this unit will match with the expected result and if both of them are matching according to me it should fail let me run this particular unit here I'll right click run as J unit test case you can notice test is failing why because expectation met with the reality now let me fail intentionally maybe I'll I'll make sure that it is not matching so some random data I'll give here this is my expectation and let me see what my unit would generate if both of them are not matching in this case it will not match but my test case would pass so let me run this and if I just run my unit case you can notice it is passing if expectation is not equal to reality only then the test case would pass if not the test case will fail which is exactly the opposite of Asser equals me method and this is also overloaded like the asset equals so let me run this again here and in this case I would like to give some message also and directly string message also you can pass here maybe the message would be message to Dev if test case fails so that we can see the output now let me just give the proper you know result 576 and uh if you just run this unit it would generate 576 that means the actual result would be 576 and the expected result is 576 in this case expectation is matching with reality and we are using asset not equals in this case if expectation is matching with the reality test case should fail and if test case is failing whatever message I have given here that that would also be displayed let me run this particular unit here right click I'll say run as junit test and you can see here so let me just run once again and we are also getting the message message to Dev if test case fails whatever message you would like to give based on the requirement of a particular unit just to Showcase this and similar to what we have seen in the assert equals we can also give the message not by giving direct string object rather by using the supplier functional interface here also this facility is available and we can implement the same thing using the supplier functional interface and the advantage as we have seen earlier it would do the lazy evaluation that means this will evaluated only if the test case is failing if test case is passing this will not even be evaluated fine so now here lazy you know execution is happening but in case of the string case this particular whatever string message you have given here that would be considered that would be evaluated by assert not equals method irrespective of test is passing or not passing however if test is not passing that message would be displayed if test is passing message would not be displayed but that has been evaluated that is not the case with respect to the implementation of supplier function interface if you do so lazy evaluation will happen that means only if the test case fails this will be evaluated and the message would be displayed in this particular case the test would pass then the message would not be there here so I mean of course it will not asset not equal to I'm telling so test would fail and let me see the message of this particular case which I'm running oops it should give me the message let me just rerun this run as you know test case and we are getting message to developer if test case failed whatever we have given using the supplier functional interface and if you pass this message also would not be displayed but the evaluation also would not happen maybe I'll give something else here and definitely the reality is not matching with my expectation and if that is a case in case of asset not equals to this particular test case would pass and if it is passing whatever message you have given that will not be displayed in fact since you're doing that using the supplier function interface it will not even be evaluated so let me run this particular unit and if I run test case is sp so basically we have seen this it is exactly the opposite of assert equals method now let's understand few more assertion methods which are important which happens to be assert true and assert false so if you want to perform assertion these methods again plays important roles based on the need of a unit so let me use here assert true and this assert true method would check whatever value you are passing here whether it is true or false example if I just pass here true maybe let me just pass this value here and if I say true so whatever value I'm supplying it is true that means this assertion method would pass the test case which you have written here so let me run the test case right click I'll see here run as junit test case and you can notice the test is passed we are getting the green bar that means our test case is pass if I just give here false that means it is checking for whatever you have supplied here whether it is true or false if it is false it would result in fail so let me make it more easy I'll create a string object here string Type S Str and um I would like to pass junit fine and now I will say here St Str dot maybe equals I will pass your junit maybe I'll use here junit so whatever object we have with Str Str string reference variable is it matching with this of course not so this entire method would result me in false so whatever value I'm passing if it is false this assert true method would fail the test case so let me just run this unit test case which I have written and if you just run you can notice the test case is fail and if I just pass here J unit now of course whatever we have within St Str referred by St that object is junit and whatever I'm comparing with it's also junit so this entire equals method of string object would result in true and whatever value I have supplied to assert true method if it is resulting in true in that case it should pass the tou case let me just run here right click run as junit test you can notice test Cas is pass now let's understand more on this for which I would be I have actually developed one project here wherein I have two folders com. learning. Dao and also I have com. telesco do service so here this entire application which I have developed is based on purchasing the courses we may have three options here one is uh Java course then we have spring boot course then we have the junit course based on the need that particular course should be executed for an instance these are the courses I have spring boot junit course Java course based on what course is being chosen that particular course method should be executed and whatever message you have here that message should be displayed Java course is purchased if my user has chosen this course fine a basic you know application and also please notice all three classes are implementing course interface so this method is the implementation of abstract method present inside this interface so in that context if you know the fundamentals of java also can we use Co type reference to invoke or to refer Java course object yes that is possible because of the concept of polymorphism so I can create reference of course and I can invoke the methods which are present inside Java course which are present inside spring boot which are present inside junit course because this would act like a parent so an you know interface type reference can invoke the methods implemented methods which are present inside implementing classes keep this in mind let me open the Service class you can notice here so I would be invoking this method and I would pass some object here and based on the object pass that course do purchase I'll invoke so if Java object is pass year course purchased method of java class would be invoked if junit object is pass year then course purchase Method of junit class should be invoked similarly if spring the spring boot course is supplied here course purchase because I'm using the same reference course do course purchase so course purchased method of spring boot class should be invoked so we have that method here course purchased so based on whatever type of object you're going to pass here it will invoke the method of that particular class and whatever method you invoke here all these methods after executing it is returning true that means if it is able to execute this it is returning true you can notice here there is also returning true and if you invoke those methods successfully that will return you true and if it is is returning you true that means whoever is going to invoke this method for them also true will be return what do you mean by that let me simplify here let me go back to my test I would like to create a test here right click I'll say here J Test case and I'll give here test maybe I'll say here purchase course this is the class under test and I'll say here finish now I would like to write my test cases here I'll say here test which method I would like to test this particular unit I want to test the unit name is proceed with course this is the unit I would like to test so I'll say here test proceed with course fine now if I want to invoke this method I should be having the instance of this class so I should be creating that so this purchase course instance I needed in order to invoke the method I have created this maybe purchase course equals to new purchase course instance is ready and now let me just import it I have imported now I'll say purchase course Dot and I'm going to invoke this proceed with the course and it is expecting me to pass the object which can course variable refer now course is a reference of course that means course is an interface so you can pass any object which is implementing this particular interface if I just pass here maybe I'll pass new Java course I'm going to pass here and if I pass new Java object let me import here if you're are passing here that will be received here please see the control flow I'm going to receive so it is equivalent to you creating the object in this style course equals to new Java course I have kept in mind you know course Java properly so it's equivalent to this and now if you say course course has the reference variable of java course object now if you say course doour purchase so course purchase Method of java course would be in invoked so whatever method we have here that would be invoked this message would be printed and you're returning true so whatever true you're returning you're not collecting in any other variable directly you are returning to its caller so whoever is going to call proceed with course that true will be returned to them so that true will be returned here so of course in this case I'm you know injecting a proper object it should result in test case pass let me run this particular test and and if I just say run this focus is needed here see here we are passing this and also getting the message Java course purchased now let me use assert true method and perform the assertion over this logic so first of all what this method would return return me I'm going to collect in a variable called as Boolean type status and then I'm going to apply assert true on this stat so if I just run this particular test run as junit test now test case is pass after performing the assertion so this is yeah now what is happening here I'm invoking this method using this purchase course reference variable and injecting Java object that means from here the matter is going there and here Java object is being invoked or injected and it is equivalent to you creating the you know object as course type course equals to new Java course and if you say course doour purchase that means method which is present inside Java course object that will be invoked so in the Java course this method would be invoked and this message is getting printed and then you're returning true and whatever true you're returning here that again you're returning back to its scholar so whoever is going to call that method is returning here and now I'm checking whether it is passing or not yes I'm able to pass here so basically assert true method whatever value you pass here if it is true the test case is pass if it is not true the test case is not pass and it will result in fail now let's understand assert false method which does exactly what assert true method do exactly opposite of that for an instance if you want to perform the assertions and verify whatever input you're going to pass here or whatever value you're going to insert if that is resulting in false then your test case would pass for an instance in order to perform the assertion I'm going to use a method called as assert false and whatever input or the value I'm going to pass here if that is resulting in false then my test case should pass let me run my test case here run as junit test case you can see it is passing and if it is resulting in true whatever I'm going to pass here then the test case should fail let me run the test case here and you can notice it is failing so exactly the opposite of asset true for an instance maybe I have a string value here I'll say a string type St Str equals to I'll say here Java or junit junit 5 and and this is my string and now I'm going to test here string dot maybe length is it it is returning me the integer value I'm going to check here is it less than five it is less than five so 1 2 3 4 5 6 so it is not less than five it is more than five so this entire expression whatever I'm passing here it is resulting in false and if it is resulting in false then this expression would give me me you know it will pass the test case so let me just run the test case now and if I'm running the test case you can notice test case is passing because whatever length I have passed here it is more than five and I'm just checking is it less than five no so it will result in false and if it is resulting in false then my test case would pass so exactly the opposite of what we have seen with respect to the asset true method now if I have to just go through the project which we have seen earli here so this test case wherein we have tried to understand this and we were injecting the Java object here to the proceed with course method which is there here which is there in the purchase course and now we used to receive here Java object and if I say course do course purchased so the course purchase Method of java course would be called here so maybe course is purchased and also I would like to give a message here maybe one message I would like to give here I'll say here it is not actually connecting to DB to DB hence results in false just a message it is since I'm not really performing the activity related to DB I will not written true I will written here false and if I'm written here false whoever is going to invoke this course purchase Method for them false would be returned so if I have to just go back to the course purchase and here is there I'm invoking this and it is resulting in false that false again I'm directly returning to the caller of proceed with course that means the false would get back here wherein I'm invoking this method so the false would come here and I'm checking this time maybe let me just comment this and this time I would like to check with respect to assert false so if I just have to check the status here assert false I have to import it done so now this is resulting in false so of course this would no pass the test let me just run this I'll right click and if I say run my J unit test and you can notice test is passing because it is resulting in false and we got this it is not actually connecting so only it is resulting in false but however we we have seen asset true and asset false we use it in order to check whatever value or unit we are checking whether that is resulting in true or false and also like the way we have seen for assert equals and assert not equals apart from this asset false and asset two both of them are again are overloaded you can also pass this string message like the way we have done there and this string message whatever you're going to pass this will be executed only if your test case is failing and only then this message would execute maybe I'll say here it is failed because unit is results in maybe I'll say here true of course for asset false if the unit is resulting in true it will fail the case so let me just import this so I will just like to import I'll just hover here and you can notice I have an option of import it fine now right now whatever I'm going to pass here it is result in false and whatever message is there here it will be evaluated like the way we have seen but it will it will not be displayed so let me run here and if I just run the unit case you can notice it is passing and the message will not be there and this message would display only if this is resulting or this entire test case is failing but it is passing this message will not be there and similarly in place of this string message you can also pass the implementation of supplier functional interface by using the Lambda expression like the way we have done in the assert equals same thing you can do here also however usually we don't that here we will not be giving any of the string message or we will not be giving any of the implementation of supplier because if you just wherever whichever test you're running if you just go here and just click it will just direct you which test is passing and which test is you know failing exactly it will take you to whichever test you are you know you are running and and whether it is passing or failing so that doesn't matter if you want you can give this message and what is the difference if you give string message and what will happen if you give the implementation of supplier functional interface that we have already seen it but that facility is available here also so this way we can use both this assert true and assert false method in order to perform the assertion or you can say verification now let's understand one more important method in order to perform the assertion which happens to be assert array equal by the method it name itself you can understand it is to perform the assertions over the arrays and if you want to pass the test Case by using Asser equals method then actual array which is generated by a unit and the expected array it should match if both of them are matching then it will result in test case pass and in order to match that you have to make sure that the number of elements which are there in both the arrays should be equal it should be same number if one array is having five elements the second array it should also have five elements not just that it should also have the same data example the first array has 2 4 five and the second array should also have 2 4 five only the data also it will check the elements which are part of that array also will be checked here apart from that the order of those elements has to be same if all of them all of these requirements are matching only then the test case would pass if not the test case would result in Fai now let us check that practically in order to get a better Clarity here you can see I have developed one unit very I'm going to test my you know array so what I'll do here is I'll have an array here by the name maybe this is my expectations expected array and here I'm going to pass some data Maybe I have some even numbers here 2 comma 4 comma 6 comma 8 fine this is one of array and one more array I'll have integer type maybe this is the actual array which I'm getting from a unit and now here I would be writing in this style 4 comma 8 comma 6 comma 2 fine now both of them have same number of elements four and four correct and both of them have a same elements here also we have a four here also we have four 8 8 6 6 and two and two but order they are in is not same so let us check here I'm going to use one method called as assert array equals array equals in order to perform the assertions and I'm going to pass here the expectation what is the expected and what is the actual that my unit is generating now you can see here it should fail let me check if I have to run this see here it is failing if you open here you can get here where exactly what is happening you can also cross check you can see array content differ at index zero in the index zero itself I was expecting two but I found four and this was my expectation but I could not able to find that exact message also junit is able to display to you what exactly the problem with respect to this test now I'll do one thing here I'm going to perform some of the operations over my actual array I'll say here arrays dot sort arrays is a class utility class in order to perform the operations so let me give here aray do sort method aray do sort and within this of course I may have to import that and here I'm going to pass act if you sort this particular array then it'll become 2 4 6 8 and after after I sort it of course the return return type of sort method is void you cannot print and check it but it will sort the array and this array if you sort then it should result in 2 comma 4 comma 6 comma 8 and now tell me both the arrays number of elements are same yes the expected array and the actual array both of them have same number of elements and the Order of the element and the elements both of them are same it is also two two 4 4 6 6 8 and 8 order and what is the element all of that is satisfying here all three conditions are satisfying in that case this particular test case should pass let me just run this test case and see and you can see test case is passing because both of them are matching here now in place of assert array equals if I use the normal assert equals method and I'll pass here what is my expected and I would also pass here the actual now let me just import it and if I have to just run it all right now I would like to run this right click and run as J test and if I run you can notice it is failing why because it will check the reference variables if you just know the basics of Java on your HEAP memory we will have two array objects one array object is referred by expected and one more array object will be there which is referred by actual now both of these reference variable are referring to two different arrays so it is checking the reference variable it is not checking the data which is there inside those arrays and if you want to perform the assertions or verification with respect to the elements which are there inside the array then you should not use asset equals method you have to use assert array equals and assert array equals will check for the data which is part of the array that means what data whether it is in the proper order and the elements the data is same or not all that it will check and then it will result now both of them having the same alongside the order elements are also same order of the elements also same and number of elements also same it is not just with respect to the integer array you can perform with respect to the character array or string type of array now array could be of any type the type of the data should not be concerned here type of data not a concern I'm referring to both of that should be same example if I have an array which is having a string array and one more array is integer array then the type is different here the elements are different here in that case it will fail so as suppose both of them are string array and now both of the array have same number of elements same type of element and same element also alongside the order of the element in that is the case if that is the case then the test case would pass if not the test case would fail fail and this is how we can use assert array equals method in order to perform assertions over the arrays now let's understand about asserting with exceptions that means testing the exceptions whether a particular unit is generating the exceptions or not for an instance here in this class I have developed a unit or a method which is responsible to receive an array and sort that array and return that sorted array to the caller of the method so whenever you want to sort any array you can invoke this method and you can pass this array and this method would accept that array perform the sorting and after sorting the sorted array it will return back to the caller so here if you are passing an array which has some data it will take and it will sort of course it is an integer type of data but however if you pass an array which has null no data maybe an array with a null reference and that array if you pass here if this particular unit would generate a null pointer exception we will try to know execute this and check whether this unit is generating an exception or not so here I have developed a method or a test case and here what I'll do is I'll be invoking that method in order to invoke this method first I need the instance of a class where my unit or a method is present so I'll go here and create the instance of a Class Type maybe array equals to new and the instance is ready I'll say here array dot we can see here we have a method sorted array and here we have to pass an array so I'll be creating one array here integer type unsorted array one unsorted array I would be creating and if I pass some data there is no problem and this I'll be passing here maybe I'll give it as unsorted the reference I'll pass here and this method would accept this array perform the sorting and then it will return back the array to me so I can collect it back after I pass the array I can collect it back and I'll say here maybe integer type I'll say here sorted array integer type sorted array and now you can iterate over it using the loop and you can check for and I'll say here integer type integer type maybe element of an array and what is the name of an array sorted array this is a enhanced Loop to iterate over the array and to F the values now I'd like to print system.out.print Ln and I would like to print whatever data I have inside that array so let me just run this and let me see if I'm getting the result first of all see here 1 2 3 so it was not sorted earlier 214 now after invoking that method this method is able to sort and I'm getting the result 1 2 4 now I'll be I would like to give system.out.println I will say here exception generated now if we are getting the result if you are getting the result whatever is present inside the catch block that means that particular unit is generating an exception object and we know very well in Java cat block will get executed only if there is an exception object and if exception is not generated whatever you write inside the catch block that will not get executed now let me run this here if if I have to run this particular unit run as J unit and you can see exception generated so this exception generated message is present inside the catch block and catch block will get executed only if there is an exception that means whichever method we are invoking that method is generating an exception and test case is passed here now you know very well that the line where exception occurs lines below that till the catch block will not get executed by chance if there is any exception here what do you mean by that maybe in the line number 16 you are invoking array do sorting array and if this particular method is generating an exception whatever statements you write here those statements would not be executed maybe here I'll say here statements below exception it will not get executed by chance if there is an exception here so let me run this here I'll say here run it see I'm not getting that message now however I would like to pass this that means maybe I would like to pass some data here I'll say 2 comma 4 comma 5 now if I'm passing here there is no exception whatever message you have given inside the catch block that will not be executed and this line also will be executed this line also will be part of our execution because everything apart from the catch block will get executed if there is no exception now I'm invoking the method and passing the proper data here that method can accept it and sort and give you back the array the sorted array which you are collecting here so there is no problem here so let me just run here right click I'll say here run unit you can see here statements below exception also we are getting it I'm not since I'm not iterating so I did not get that however we are getting this so let me just iate over and show you I would like to run this particular test case once again we are getting the sorted array also then we are getting statements below exception but the test case is passing here I want to fail this test Case by chance if there is no exception now right now exception is not generated what do you mean by that exception is not generated because whatever message we had given inside the cat block is not executing if cat block is not executing that means there is no exception and if exception is not generated test case should fail and if exception is generating the test case should pass so what I'll do here is after this statement I'm going to fail explicitly this will be probably the only place wherein you are failing the test case explicitly now my dear friends please see very carefully if this method which I'm invoking if it is generating exception lines below this this should not be executed until the cash block if exception is generating here directly it is a catch block which gets executed it is a concept of java and lines below this will not get executed and if it is not executing that means test case is pass because the fail explicit failing I doing before the cash block after the you know exception generating statement so this statement should not be executing if it is not executing fail also would not get executed and test case will not fail and test case will pass now if exception is generating now i' would like to generate the exception nulla I'm passing so if I invoke this method and pass null here that method will throw an exception and exception would be generated at this statement if exception is generated at this statement all this will not get executed directly it is the catch block so let me run here if I just have to run this test case is passed exception also generated by chance if exception is not generating the test case should fail so if I have to give here maybe a null value maybe some array I'll give if you pass a normal array then in that case test case exception would not be generated if exception is not generating I have to fail the test so explicitly I'm failing cash block will not executed all other statements will be executed so let me just run this right click I'll say here run unit test and you can notice I'm getting the fail that means I'm failing the test because there is no exception so basically here test case should pass if there is an exception and test case should fail if there is no exception now test case is failing why because there is no exception whatever unit I have developed that is doing task it is not generating any exception if it is not generating exception test case should fail and I am failing this this is one of the way and there is other way also if you don't want to you know use this so what I'll do is I'll come into all of these lines all these statements I would like to comment and now I have created the instance and there is a new method in the new API assert throws and you can see here it is accepting the type of exception and Implement you know implementation for executable both it is expecting so assert throw and I'm expecting null pointer exception null pointer exception I'm expecting so reference I'll give it as e I'll say here maybe dot class I have to give the reference with the dot class so dot class and now I'm going to invoke that method here what is the reference variable array Dot and the method name is sorting array and whatever the statement is there I would like to give here right so I'm going to pass here unsorted array and this you have to do by implementing the executable that means you have to implement using the Lambda expression the easiest implementation I'm implementing lambdas by implementing the lambdas I'm invoking this method now if this method is generating null pointer exception then the test case is passing if this method which I'm invoking if this unit is not generating pointer exception this would fail right now I'm passing the proper data and if you are passing the proper data if you're passing the proper data then this method which I'm invoking it would not generate any exception and whatever test case we have that should you know fail so let me just run this and you can notice it is failing why because it is not generating I'm not giving null here I'm giving a proper array and this method if you give proper array it will take that it will sort and give you the sorted array so of course it will not generate any exception now what I would do is I'll give here as null so whatever array I'm going to pass here whatever array I'm going to pass here you can notice that does not have any data any element it is with a null reference so of course this method would generate an exception called as null pointer exception now if it is generating null pointer exception whatever exception I have know specified here that means my expectation is matching with the reality and if it is generating my test case should pass so let me run my test case here and you can see the test case is passed that means that particular unit is generating exception and this way without using assert throws also we have seen how do we test Exception by asserting through the method assert throws also Al we have seen how do we check whether a particular unit is generating exceptions or not now let's understand about testing the performance of a unit whenever we have a longer running task or longer running unit maybe a unit wherein we have lot many task and it might take lot of time in order to perform we may have to test the performance of the particular unit and how do we do we have to understand here for an instance I have a unit which is develop in front of you you can see here and here I'm trying to sort an array and arrays do sort method you can notice here I have kept inside and loop and I want this to Loop for maybe 10 lakh times that means this sort method would be called for 10 lakh times now again somewhat I can say here it might have a longer running task and I want to check the performance of a unit and if you want to do so how we have to do now please see here I have developed one test I have written one test here and wherein I have created the instance of the class under test and whatever array I would like to sort that array I have created here now in order to test the performance of that unit I can use one method if you are using junit 5 which happens to be assert time out and if I just check here assert timeout you can see we have a assert timeout static method and it is an overloaded method and have you can notice here it is expecting me to pass the duration you'll understand and also it is expecting me to pass the implementation of executable or implementation of the supplier you can see here two things I have to pass here number one I can say here how do you check maybe I'll say duration dot in Millie see duration of Millie and I'll say here maybe whatever unit I'm testing that has to execute within 10 milliseconds that means 10 lakh times invoking the sort method should be finished within 10 milliseconds if it is not able to do that my test case would fail and if it is able to finish calling the invoking the sort method for 1 million time or you can say 10 lakh times then my test case would pass now I want to check the performance that whatever unit I'm going to test here that has to be completed within 10 milliseconds now let me give the implementation here with respect to the implementation of usually we do the implementation of functional interface using the Lambda expression that is what we have seen here I hope you know that much of java so I'm going to implement now I will say here array dot that method I would like to invoke array Dot and you can see here sorting array and I'm going to pass here unsorted array now it will accept this and that unit is executing the sort method for 10 lakh times now I would like to just run this particular test case and check if my method or unit is performing well you can notice test case is failing wow now I'll do one thing here maybe I'll give little more time and I'll check here with the respect to maybe 100 milliseconds and let's test right click I'll say here run unit test test Cas is passed now that means whatever unit I had developed it is able to execute within 100 milliseconds and if I check with respect to the 10 milliseconds that unit is not able to execute and the test case would fail so let me just execute this unit in this case I want that unit to execute within 10 milliseconds oh it is happening let me give the time as 1 millisecond now I'll right click and I would like to run it is a red bar now that means my test case is failing and it says that you cannot you know you can consider that this particular unit cannot complete the task within 1 millisecond so with this method assert timeout method we can check the performance of a particular unit and you can see off mes off minutes you can give any method here let me just show you once again I'll say here duration you can see dot you have all the options I have G I have gone with respect to milles you can go with Nanos minutes and now I have checked with respect to 10 millisecs and this method would able to execute and able to call the sort method of Aras you know class for 10 lakh times within 10 milliseconds and if it is happening the 10 test case would pass and you can notice here and this is how we can use assert timeout method in order to test the performance of a unit however if you are using J unit 4 we do not have this option you may have to within The annotation you may have to specify time out directly here and you may have to give maybe 100 milliseconds now it is not working here because I'm using Jupiter API this option is not available for the junit 5 and it is there maybe whatever projects we have created earlier with junit 5 let me open that and here with this annotation I'm going to check whatever unit I'm going to execute whether that is executing within the time frame so I'll give here timeout attribute and I'm going to say here maybe 100 milliseconds equals to 100 milliseconds now see it is working fine so let me run this test I'll say a right click run unit test and you can notice the test case is passing so if you are using junit 4 you can just specify in The annotation test timeout and if you are using the latest junit 5 you may have to use assert timeout method in order to test the performance of a unit you must be knowing the typical life cycle of a normal Java class class whenever we have a class if there is any static initialization block that will get executed first and then if we have any static method using the class name you can invoke that that would be executed next and then we will be creating the instance of a class object creation and during the object Creation The Constructor would be invoked and after Constructor and if we have any non-static method that means instance method using the reference of the object or an instance you'll be invoking that instance method non-static method and that is how the execution will happen at the end the object would be destroyed and that is how a normal Java text class a normal you know class of a Java would be executed now what if the case what is the case with respect to the class wherein you writing the test cases what would happen there what will be executed first what will be executed next in that case we need to understand here in other words we will be understanding this by understanding important annotations we have here which is before each and after each for which you can see one application I have and two units we have here and we will write the test cases for these units and understand those annotations now you can see I have written one test case here and to invoke these method first I have to create the instance of the class under test because that is a non-static method which I'm going to test here same thing goes here so basically I have written two test cases fine now what happen happens here is in order to invoke this method of course whichever method is annotated with test internally test Runner would be executing that and this is a non-static method so in order to invoke these methods internally you need to create the object of this you know test before after whatever class wherein you're writing the test so internally testing framework J unit will take care of that so first what will happen it will create the object or instance of a class very you writing the test and when you're creating the object that means Constructor is getting called so it is Constructor first and then using the reference you're invoking internally the methods which are annotated with test that would get invoked and that will be executed here now maybe I want to execute some of the code some task which is common for all the test cases I'm writing here so what do we mean by that in this context creation of this object I'm doing in to invoke these methods here also I'm doing the same because this is the non-static method you have to do that by chance if you're writing one more test case if you're invoking any of the method of the same class then again you have to create the instance that means duplication of case is happening duplication of the code here so what I do here is I'll be creating one method normal method please understand this maybe I'll give the name as init method you can give some other name no problem I have given it as init method and here I'll be creating the object before that I'll create one instance variable shapes type shape and within this init method I would be creating object or instance of the class under test and to this I'll be creating reference by the name shape fine and uh just to demonstrate and see the control flow I'd be writing one sis out system.out.print maybe I'll give the name as before test fine now to this method I'll be annotating with one annotation called as before each before each and I'll be importing that so it is been imported now I'm able to apply by the name of The annotation you can understand before each that means before each test this would be executed so if I have five test cases before running five test cases this would be executed that means in it method would be called internally by the framework for five times because you have written five test cases right now I have two test so how many times in it method would be executed two times so now anyway I have already created the object here so I need not to write this again again this duplication is not required because even before executing this particular test it will internally first call the method which is annotated bit before each then it will call the method with annotated with the test annotation and again before executing this first it will execute in it and then again whatever you will be writing here so let me just also write one thing here system. out. print and maybe I'll say here actual test actual test running and same thing I do here just to see the console on the console control flow and now I have saved this and I would like to just run my test case so let me run it ignore whatever we have seen there now you can see before test actual test running before test actual test running that means before every test before every test this init method is getting called it is not init method any method name you can give there is no problem but whichever method is annotated with before each before each annotation it is there in Jupiter junit 5 if you're going with the junit 4 then you just have to use before just before you should not write you know each only before it will work for the you know uh junit 4 and now in junit 5 it is enhanced so of course it is giving the precise name but the name itself you can understand this particular method has to get executed before every test usually we do whenever we have to initialize any instance variable or maybe uh some DB connections or any activity which which is common for all the test cases because this would get executed before every test if you have 10 test cases 10 times the init method would be invoked so if something is common for all the know 10 test cases you need not to write that inside those test 10 times duplication of the same thing again again instead you can just write within this method which you can annotate with before each so if we have before each we have after each and now I guess you can you know get to know what is after each so I'll write one method called as desktoy and here I would be writing uh just you know just to see the control flow after test and this method I'll be annotating and you can understand The annotation by the name itself after each so let me import this I have imported and it is coming from the Jupiter API that is J unit 5 and you can understand I think by this you have already sense after every test case this would be executed and usually we do this whenever we have a cleanup or closing of the resources maybe some of the resources you're using inside this test cases and after executing that after running that particular test you want to close the resource which is common and that part you can write within a method which you can annotate with after each so if if you are aware of you know exception handling try cat finally usually whatever the cleanup closing of the resources or cleanup code we usually write within the finally block something similar here so whichever method we annotate with after each that will get executed after every test case so let me run here I would like to execute this particular uh unit case and you can see here run this and you can see let me just show you the console before test actually test running after test clean after test that means clean up is happening before test whatever test you have return that is running after that means whichever method you annotate with before each that method would get executed after you know before every test case you are writing if you have 50 test cases that method which you're annotating with before each that will get executed 50 times and same goes with respect to after each after every test that part would be executed usually uh to close the resources maybe we are establishing we are doing some DB related activity or we working with the files and once the test is over we want to close those resources and that we can do here because anyway this method will get executed after every test case and if you're going with the J unit 4 similarly you need not to use each you just have to use after and that will get executed after every test case in unit five they have just given the precise names before each after each so what is the life cycle here so object or instance of this class is getting executed that means Constructor is getting called and after that before invoking the actual test it is invoking a method with before each annotation and then it is invoking destroy after every test case we'll understand more in upcoming videos now let's understand about The annotation before all and and after all what will happen if we apply to these methods so you can see here before each after each we have seen that will get executed before every test case now I would be writing one method here and maybe I'll give the method name as void before all and this is a method name I have given whatever name you want you can give here also it need not to be in it it could be anything but this method now I'm going to annotate with The annotation called as before all and one more Point here you able to apply but if you run you may get exception at the run time so you have to apply this annotation for a static method usually we use this annotation for a static method so I'll make this method as static and if it is a static method that means it is independent of the instance it is not dependent on the object without the object with the class name itself you can invoke that keep that in mind and also I would like to print one message just to trace the control what is happening which is getting executed first I'll say here before all tests fine so now if I just have to run this particular test I'll say here run my test and you can notice the output before all test so before test which is present within before each annotated method is executing before every test case wherein before all test you can see it is getting executed only once that means before all the test only once this will be executed and this usually we have to apply to a static method static method means you need not to create the instance of a class to invoke this so what is happening with respect to the test cycle here the life cycle if you see before even creating the object of a class where you writing the test case that means before the Constructor execution it is this method which is getting called so this is the first method which is getting called here then the object is getting created that means Constructor is getting created and then before each test case if you are writing a method with before each annotation that will get executed if you're not writing here it will be just this test case which you are writing so basically if something is common for all my test cases maybe all the test cases I'm writing I'm testing some of the methods or units which is doing some DB related activity that means database related activity from your Java application and establishing the connection to the database is common for all these methods which I can specify in a method in a static method which I'm annotating with before all because this method would get executed would get called only once before executing all the test cases if you have 100 test cases will this method be invoked 100 times no it is only one time and this is the method which is getting called first even before the instance of this class is getting created internally so before know even before this it's a method which is annotated with before all is getting invoked so that also you have to keep in mind maybe if something common is there for all the test cases which you are writing and which you want to execute only once before even you run the test cases then you can write a static method with annotation before all so if you have before that means annotation which is after all and I'll give here as after all and the method name whatever you want you can give I'll give the method name as after all I'll say here after all the test and this also would be invoked only once after all the test cases are running are executing so let me just run this particular test right click I'll say here run my unit test and you can notice test all test all test it is coming only once whatever message you have given here it is coming only once that means it is getting invoked only once after all the test cases are executed so this also you may have to apply to a static method itself fine make sure that these methods are independent of object independent of instance and it is doing exactly what before before class and after class annotation used to do in J unit 4 so whatever is happening here just in case if you are unaware of J unit 4 we had an annotation called as before class after class same thing is happening here with The annotation before all they have just given the precise name before all test cases and this will be get executed only once before each test case 100 test cases you have whichever method you're annotating with before each that will be invoked 100 times But whichever method you're annotating with before all that will get executed only once before executing all the test case and whenever you want to close the resources a common resources you want to close and in that case you have to use after all annotation to a method and at the end of everything if all the test cases are executed that time it will be invoked and I cannot show you the same example which we have done here by creating the object I cannot create the instance here because static method it is indep dependent of instance so you cannot have something about instance here because this method would be called even before creation of the instance even before creation of the object fine now one more thing you need to understand here now these methods wherein I'm writing my actual test the method which is annotated with the test annotation here is where the test we are writing now suppose I'm writing two test here and if it has to be invoked internally the platform the test Runners would create the instance of this class and using the reference it would be invoked and also I would like to tell you that no default behavior in which it is working is how much ever test cases we have that many instances would be created that means in order to invoke this test case first it will create a instance of test before all after all this class and then it will invoke this test case and again in order to invoke this one more object would be created so if I have five you know test cases I have written with a method with with test annotation then five times the object of a class wherein you are writing this test case would be created what do you mean by that so let me just execute the Constructor that means whenever we create an instance or create an object Constructor would be invoked uh just to demonstrate this I would be creating one Constructor here and uh within this Constructor I would be giving one message system.out.println maybe one message I'll say that test object maybe I'll say your text object is created before test method fine so let me just run this I'll right click I'll say I run as unit test so one thing you can notice here test object is created before test method test object is created before test method that means this message is getting displayed two times and this message was present within a Constructor and Constructor gets called whenever we create the instance so right now I had two test which I have written here so in order to invoke those methods two times the object is created so five test cases if you have written here that means five methods with annotated with the test annotation then five objects of a instance would be created and I don't want that to happen I want it to create only ones the instance of a class only once and then using the same reference I want it to invoke all these methods which are part of this particular class if that is a case then we have to use one annotation in order to change the behavior of test instance so The annotation which I have to use here is test instance so annotation is test instance and this annotation let me just import it and this is coming from the Jupiter API that means it is there with the junit 5 and here one enum I have to pass I'll say here test test instance test instance dot I'll say here life cycle Dot and you can see per class per method so per method if you say this is a default Behavior so even if you don't say this the default behavior of this class is permitted that means for every test method the instance is getting created so if we have 100 test methods 100 times the instance of this class would be created and I don't want that to happen so in place of per method you may have to go with DOT per class now if this is the case the instance should be created only once and also whenever you're creating the instance only once multiple instances are not creating so there is no meaning of having a static method static method usually we write whenever we have a common activity which has to be executed independent of the objects so we used to write here before all to a static method so that doesn't matter how many test cases we have and you know how many objects internally the test Runner would be creating in order to invoke this it will be executed only once so right now I have changed the behavior right I have just told here that please create only one instance so only one time the instances are getting created and if you are changing the behavior here in that case you need not to write the static method you can apply before all to a non-static methods also because there is no meaning at the end of the day because anyway we have only one instance one time only this method would be invoked so if the behavior is per method you have to apply before all after all to a static method only but however if the behavior is per class and if you're changing the behavior only one instance will be created and using the reference all these methods would be called here and in that case this before all and after all you can apply to a non-static method also and let me just run this and show you that we will be executing Constructor only once so if I just have to run this you can notice we are getting test object is created before test method only once test object is getting created then it is invoking all the methods before all test that is getting that means before all annotation method is getting called then before each annotated method is getting called then the actual test is running fine I hope you have understood how to change the behavior of an instance the way it behaves behind the scene what are data structures the reason we are talking about this is because if you look at the internet everyone says if you want to get into this company or that company they will ask your question Based on data structures and algorithms so first of all why such a high and what exactly data structures are so before we get into data structures let's talk about data see the entire software industry is based on data when you do a course on it which we call information technology it's not about technology it's about information because we work with information so data is everything so it doesn't matter what technology you learn you basically learn or you work on it so that you can work with data example think about programming languages why do we use programming languages to process the data why do we use database to store the data why do we use AI to generate data or to understand data right I mean just to bring it down everything is data now then question arise what is data structures now if you talk about any programming language we have something called primitive data types if you want to store a number we store that in integer if you want to store a a text you store that in a string if you want to store a character you store that in a character of course depend upon different languages the term or the way you store it changes but ultimately you have some types to it where you store the data but what if you have bunch of data and if you want to store them it's not just about storing data anyone can do that it's about how do you store data efficiently and you can also save some memory the thing is if you simply dumb the data you are expanding the memory and if you want to search something from the data will be difficult and that's why storing that data efficiently is very important and that's where data structures comes into picture so data structure is a way to organize and store data efficiently okay so when you say efficiently it means two things first in terms of performance and also in terms of the memory now what about performance now think about this if you if you talk about any software application which we use it can be a normal calculator or it can be an application like Amazon website or any application which we use a banking application as well now what we do in that application is we use some features using which you can compute something you can process something example on calculator you calculate on the banking website you transfer money so what you're doing is you are basically building an application which will do some processing and the way you build an application is through algorithms now what are algorithms set of instructions let's say if I want to uh add two numbers it's very simple you what you do is you say take two values from the user then perform the operation and give the value back to the user now those are the steps right those are instructions now what I have mentioned the instructions those are called pseudo code because we're not actually typing a code here we're not being specific to a language let's say C C++ Java python JavaScript what we are simply saying is these are the steps you have to follow and that's your algorithm right but yes when you want to make it work you have to convert that into a code which will run on the computer right that's your actual software code and you can use any language which doesn't matter right but the Pudo code will remain same now when it comes to processing of data for any task it's important for us to make it fast and also save memory now most of the companies are focusing on this concept of data data structures is because they want to give a good experience to the user of course right if I'm using some application I want it to be fast now you will say okay to to make the system faster you can increase the CPU speed you can increase the amount of ram you can do that but what if with the same amount of memory same amount of CPU power you can still make it more faster right and that can be done with the help of data structures now if you know how do you store that data efficiently in a proper structure and by doing that you can make your application faster because in data structures we have different type of data structures example let's say if you have bunch of data you can store that in an array but apart from array we have other types as well we have Set uh we have link list so when to use what it's not that this is best or that is best it's it's all about when to use what and to understand when to use what you have to first understand what those things are right how do you decide that this time you have to use array this time you have to use set and that's where understanding these concepts are very important now these are not the only options we have we have tree we have graph when to use them so when you understand those Concepts then we can think about okay for this situation we will use this and this is why companies are preferring candidate who knows DSA it will help them in multiple ways first it helps them to reduce the cost is because every computation see I know you you might be thinking uh most of the application which we use are free right think about Instagram now when we use Instagram of course we are not paying for it but then companies are paying for it right so the meta is paying for for you to use Instagram so because those computations will be happening somewhere maybe meta is using some cloud service let's say Amazon is in this case So Meta is basically Bally paying to Amazon for every computation which you do okay of course they earn from ads but then they are paying for it so what they will do is they will try to optimize it they will say okay if one query takes let's say $1 or maybe half a dollar can we just reduce it more can we just make it 10 cents so that's the thing they are trying to do and the way you can do that is by making sure you use a proper algorithm with a proper data structure so why companies are doing it to reduce reduce the cost second to give a better customer experience so that it will run faster and if I search something it should be faster for me as well so as a customer next when companies want to hire people they have so many candidates right how will they filter those candidates now data structures algorithm becomes one of the way to filter the candidates because if you know data structures that means you have worked a lot on that particular language and you understand how how a particular system works on the other hand data structures are not the only only thing you need to know if you want to be a good developer if you want to get hired there are multiple things needed example you need to have a good hold on a particular language or technology you should have worked on few projects understanding the entire ecosystem not just one language working with databases working with networking but then DSA becomes one of the important thing there so just to summarize what are data structures so data structures is basically a way to organize and store your data in the efficient way and there are multiple data structures option available there and you will understand that in the upcoming sessions we're going to talk about in the upcoming videos you will we will talk about what are the types of data structures we have how to use them when to use what and what are the algorithms available there so I hope you got some idea regarding data structures so of course entire series is important to understand that properly welcome back aliens my name is z ready and in this video we'll talk about a DT which is abstract data type now before we move towards ADT let's talk about data here of course in the previous video when we talked about what are data structures we have talked about data and we also mentioned that in the software industry everything is about data so whatever you do you doing it for data right now with this data basically you get the data from the user you process data you also give the output to the user or maybe you want to store this data in the database for the permanent storage but the thing is it's all about data and whenever you use any language doesn't matter which language you use what you do is you store that data in your code somewhere and to store that data we use something called a variable now imagine variable as a box and you are keeping your data inside that box the problem is every box need to have a type of course there are languages which are dynamically typed language where you don't actually mention the type of the variable but in general this box will have a type so that means if you want to store the data you need a box which is your variable and this variable also needs a type to it or this box needs a type to it now example let's say if you want to store a number so we can use something called an integer or if you have any other point values you can also use float or double now it depends on the languages how they work but in most of the languages we have something in common we have integer for the normal numbers we have a float for the point values and what if you want to store a text that's why we have string and in few languages we don't have all these types we have limited types and and in other languages we have many but that doesn't matter right in general we'll be having a variable and a type to it now all these type are called data types and they are system defined data types or you can also call them as primitive types is because it's there in the language itself you can directly use them but sometime you want to use some complex data type and we can create that with the help of some other Concepts example let's say if you want to represent a human or if you want to represent a phone now I love phone so I'll be using that example example so if we talk about this phone here now this phone will have a name to it right so it will have a name it will have a brand of course name is model number then brand then the configuration the amount of CPU Ram all this are your data right so if you want to represent any physical thing in the virtual world we use objects there right now in some languages which are object ored programming we do that with the help of objects example in C we use something called structures so we can define a structure name let's say phone and inside that you can specify what are the properties there in the same way in the object uned languages we use something called class in this class you create different variables with primitive type and then the Box itself or the class itself is your complex data type or you can say user defined data type user defined because system is not giving you we are defining our own type right so that's about data and data types but let's say if you want to store a bunch of data how will you work with that and when it comes to data structure how will you store this data in a proper way see what happens is when you talk about primitive types as well every type will have a way to store data of course that's why we creating them right so example if you are creating a variable called num now let's say this num is of type integer and then you are storing a value to it now with this variable num you can perform operations as well now what are the options you can do with integer you can add two values you can subtract two values you can divide multiply so you can can perform those operations on the number but let's say you have a string there now with string of course you will not be adding two string that doesn't make sense right why you will add two names like say nav and K you will get a new name okay a lot of parents are doing that for their kids but that's not the idea here right we can't add string we cannot subtract strings but yes with string you can do something else maybe you want to see the size of a string maybe you want to concatenate two strings maybe you want to cut two strings so string are different operations in the same way if you are creating your own type this will have data of course but also you have to you have to mention the operations which you're going to perform so we have two things right we have data and we have operations now let's say in data structures you want to store a bunch of data as we mentioned and you want to store so there's a concept of array so if you have multiple data instead of showing them in multiple variables let's say you have five numbers uh 2 6 12 21 and so on now if you have all these values you basically store that in five different variables all or you can create a single array and you can store all the values there the advantage would be you have just have to use one variable name there so instead of saying a b c d e you can simply say nums you can use any variable doesn't matter so what's important is you can use array here now when we use array we are not basically using a primitive type here we are creating our own type here own data type which is an array now in this array basically you should be also able to perform some operation example example let's say what if you want to read some value you want to fetch a particular element you want to search the array maybe you want to add a element maybe you want to delete an element so this operation should be possible on that type so in the concept way when you have a type where you can perform some operation we call them as abstract is because the implementation for array changes from language to language and we don't just have array right so let's say if you want to store a bunch of values we can use list we can use set Q now when you talk about a q here let's say now inside Q we have I mean we have one more option for that which is tack they follow different concept so let's say if you want to add element in the queue which follows last in first out which means you have to insert from the end and you will get it from the first of course you can reverse it you can insert from the start and you can take it from the end the thing is you have you will insert from one end and you will remove from the other end so that is first in first out in stack it is reverse you do last in first out example let's say U to an example let's say for Q we can say a queue which you follow right example if you want to buy a coffee from a cafe shop of course there's a queue there you have to stand in a queue and then when your number comes you will get the uh coffee so basically the first person who went there first will be getting the coffee first right in terms of Stack it is different so when you keep multiple books the first book which you can take out is the last book which you have kept there right which is last in first out that is your stack now we have different way of implementing in different languages the concept remains same right so when you have a concept and the associated operations to it and of course with data which we call them as abstract data type we also have map but we'll talk about those things later at this point point to remember is we can create our own types and which is a concept and if you want to have data inside that and also you want to specify what operations you can perform that is your abstract data type of course in the subscri videos we'll talk about how how do you create them how do you work with them and it will be fun in this video let's talk about arrays so let's say you have bunch of values let's say five values now instead of storing them in five different variables we can store that in one particular sequence right or one variable you can say now in this array you can store fire values so let's say we have this array here and then this will have a name to it now of course this data will be stored in a memory right and every memory will have something called a memory location so let's say we are storing this data in at a location 101 now what happens is every time you store a normal variable let's say A Primitive type let's say int and depend upon how much size it Tak so every type will have a different size to it depend upon a language but let's say we have two bytes now inside your memory let's say if you create a normal variable which is int AAL to 5 now this particular a variable will take some space in the memory of course let's say two bytes and it will also have an address to it right so every time you in your programming language you say hey I want a value for a the program will jump to the mem location by searching for the address and it will get the value but the problem is when it comes to array we have multiple values right and we only have one address now that's tricky of course each element here will take the same size if you say in take two bytes the complete array will take 10 bytes is because every element will take two bytes how will you allocate memory now that's an issue so what you do is the array will have a memory but that will that is only pointing to the first location right so if you say the the memory for this is 101 the first element is 101 what about the second element now that's where we have to use something called a index values so let's say the first element is look is at one 1 so it will have a index value which is zero the second element is one then two then three then four right now for five values the index value starts from zero and ends at four now why zero is because that we have a memory allocated right 101 which represent to the first element so we don't have to give one to it so what you do is the next element is + one that's why 1 2 3 and then you can just get it right so one 1 let's say if you want to get the third element you have to say plus two that's how you do it so you got the values you got the index and you also got a memory address to it now with this array as we already talked about ad which is abstract data type we should be able to perform some operations now what operation you can perform on this array now we can perform something called a read operation so what is read so let's say you want to get the value of a index number three now what what you do in this case is you say okay my array name is nums and then for this nums I want the value at index three so in different languages we have different representation for this but let's say this is common one so nums bracket of three now what your computer will do is it will directly jump to the memory address it's very simple for for that right so you will say one1 plus three you will get the value so that's how you do it now reading is very easy and computer takes way less time to jump to that particular area is because the computer knows the memory right because you you are mentioning the index value reading is good it's fast also what about searching now this is tricky because when you're searching you're not searching for the index you are basically searching for a value now so let's say from this are I want to search where is 17 now in this case your computer has no idea where 17 is because computer only knows about the memory address of course the values are there but computer has no idea in which location we have that value and do we even have that value so what a computer will do is it will start from the first location so basically you have to write a code to search from the first location you have to check is the first one 17 if yes good we can exit no then you have to jump to the next location is it matching if yes good if not next matching yes no next so basically you have to search each element right and let's say the element is at the end you are searching you are basically tracking to all the different locations so this is time consuming right what if you have a array of let's say thousand values to to move between these values it will take some time what about inserting now this will be tricky is because see if you want to insert a element what do you think will it will it take a lot of time see if you want to insert the element at the end that's very simple you can get the size of the array because we also have to have that option of getting a size of the are right so let's say you have five values you you will simply jump to the location which is index 5 and then you will say Okay I want to add a value here let's say the value value is 21 and you can add the value it's it's very fast but what if you want to add a value in between so let's say you want to add that value at the second location so after the first you want to add the new value now what you will do is you don't have a space there right you can't simply create a new space you can create space at the end so what you do is you basically have to move all the elements and you it's not like you can simply move the elements what you do is create a new block so move the second last element now to the last block then again you have to move every element one by one then you can add your value to the second location so if you are inserting at the end that's fine but if you're inserting in between that will take a lot of time and that depends upon the number of elements after the position which which you're adding what about deleting see deleting from the from the end is always welcome is because you're not affecting your array in total but what if you want to delete a particular element from between now you can't simply delete from the between right you have delete the end block but how will you move so again when you have to when you want to delete this just replace all the values right so that's that's tricky so it will take a lot of time depend upon how many elements you have after that index value now while we talking about all this thing array was simple right we can perform the operations the important thing here is time taken for each operation and it's not about CPU time if you're thinking uh my computer is super fast it will be happening very fast there see in the world we have different computers and different computer have different CPU power different Ram Power different configuration and that's why let's not calculate a speed of speed of a code or the algorithm by the actual runtime important is the number of steps it takes example let's say if you want to insert the element at the end that's good if you want to insert the element in between that will take a lot of steps is because you want to move the elements right now in the upcoming videos we we're going to also talk about time complexity which is a very important concept something called Big Big O notation but yeah we'll talk about that in detail later I know you're excited to hear that but important thing is when you write a code think about the number of steps your code takes because that defines your time complexity and if you say my algorithm is good it should be time efficient okay so for the same particular operation you can write two different codes example on the screen if you want to print let's say 10 numbers from 1 to 10 and maybe you just want to print the even numbers we have two options there which one is good of course you'll let me know in the comment sections but the code which is not trating between all the values is good right so that's how you define how your algorithm is good now we can also expand the other example let's say if you have a sorted array so let's say you have an array and this is sorted by default so every time you insert the element it gets sorted by by default you don't have to mention where you want to insert this example let's say you have a bunch of values here all are sorted and now you want to insert element which is 11 now if you want to insert 11 there where it will get inserted of course it will search for the location let's say we have a number nine it will get inserted after 9 now the question is how do your code know is where is nine so you have to basically search you have to match is it greater than is it greater than is it greater than and then you have to insert so that will take a lot of time but what if you're inserting in between you have to shift all the elements so that's about the array and a basic introduction of time complexity we'll discuss that in detail later in this video we'll talk about time complexity see the thing is in the previous video we have talked about algorithms right now the thing is if you want to build an application basically we are trying to solve a problem right and every problem will have multiple Solutions so the steps which we write to solve that problem that's what we call algorithm example let's say if you want to cook something you follow some steps right now those steps are your algorithm example if I want to record a video we do multiple things we prepare the content we set up this camera mic and then we start recording in then we edit the video so there are multiple things involved in the same way if you want to solve any problem we have to follow some steps which we call algorithms and the thing is for one particular problem we don't have one solution we have multiple Solutions and we have to pick the best one now question arise how will you choose the best one so when you say best one what exactly it means so let's say when I on this application I want my application to use less memory or maybe I want when I run this application I want it should take less time or maybe both and that's why we have this term called algorithm analysis so basically you have to analyze your algorithm so that you can make it more efficient right of course as a developer first focus is first the product should work and then you think about optimizing it but when you say optimizing you have to you can optimize in two ways one think about the space complexity and second is the time complexity space complexity simply means can we use an algorithm which will take less memory and time complexity means it will it take less time but there's one more problem here and the problem is how will you calculate the time or how do you specify the exact time because every machine will have a different output right so if you are testing the algorithm in your machine it might give you different time and then the moment you test this in your office machine it will give you a different time so that's not the best thing to calculate the number of seconds it takes but what you can do is there's a way you can calculate with the help of steps but before we go there let's focus on two algorithms here and then let's say I want to search 12 okay I want to search this element so maybe there is a target value which I want to search in this case the target value is 12 so I want to search 12 right and then this is an array so what we do in the linear search is you go one by one so what you do is in fact we have talked about this in the previous video as well so we'll make it bit faster so what you do is whatever your target value is you will compare that with the first element if it is matching grade otherwise you will move to the next element otherwise you will move to the next element otherwise you will move to the next element and I mean of of course at this point we don't have to go to the next element so here we basically got the value and once you got the value you can return return the value right now there's a problem here the problem is what if the element is not there in the array so of course you have to reach till the end okay what could be the best way scenario here so let's say you are searching for element let's say element is five and now we know element five at the start itself so when you have the element at the start it will take only one step but what if you have element at the end so it will take the steps depend upon the length of the array so let's say if you have five values it will take five steps when you have 10 values it will take 10 steps when you have 1 million values okay that's huge let's talk about thousand so let's say when you have 1,000 values it will take 1,000 steps that's tricky right now the problem is not it is taking 1,000 step the problem is as your size increases it is also increasing the number of steps to search okay that's one parameter you have to remember so that's the right now let's understand that with the help of a pseudo code here so you can see I'm writing a pseudo code why Pudo code why not some programming language syntax here is because we wanted to make it generic so that you can doesn't matter in which language you work maybe C C++ Java JavaScript python maybe cobal your choice this could IM right now this can be function procedure doesn't matter so what I'm doing is doing here is we have a procedure right it can be a function and the name of the function here is the linear search okay now we are accepting a list of items now this is your array the name of the array is a okay and then you are also passing a Target what you're searching so in this case we were searching for 12 but doesn't matter so you have an array with multiple values it can be one value 10 values thousand values doesn't matter and then you're also passing a Target what you want to search now you will go inside this and then for the first thing you will do is you will want to find the length of the array because of course you should know right till then you have to search so you got a length which is stored in the n and then we are going to basically going from 0o to n minus one because we when you start with zero you have to end with one less right so if you have 10 values you will go till 0 to 9 so you will go from the first value to the last value and then you will search if the target which you are searching is equal to the current element where you are searching so when you searching for the first element that's the first element here Target is matching if yes you're good you can simply return the value but what if you're not match it's not matching it will go for the next situation next value is it matching yes good return what if it's not matching next value so basically you have to search each value till you're not finding it so that's basically a pseudo code here and what if there's no element example you have an you have five values and you're searching for a value which is not there you can return minus one by specifying hey the value is not there this is simple linear search the code is small but then the time complexity which is the amount of time it takes is huge okay now let's go for the second approach which is your binary search now what happens in binary search is it's very simple basically it's not that simple but let's say so what you do is you have a array again a sorted array and let's say we have multiple values here I'm not sure how many lines I'm going to draw so let's count it later so let's say we got five here again 6 8 9 11 13 17 okay so I maybe I love odd numbers so doesn't matter what values you have there uh so we got uh 1 2 3 4 5 6 7 so we got seven values right now what you do is in binary search now since this is sorted if you're searching for a particular element let's say I'm searching for8 here okay and we know8 is here we know it but computer has no idea where eight is so how how it is going to search now in linear search what we were doing is we were jumping basically from value to Value searching checking this checking this and then goes on so now why why this call this binary is because you divide your array into two parts that's the binary is now how you do it so basically what you do is you give a starting point and then you give a ending point and of course every element here will have a index value so in total here we got uh seven values right which goes from 0 to six now what you do is you first divide your array into two parts and to do that we have to find a mid value okay so even before you divide you find a mid value so how do you find a mid Val so mid value is equal to the starting position plus ending position / by two okay now this is 6 / 2 which is three okay so your your mid now is three and this is your mid okay now basically you check with the mid okay so you always check with the mid is the value which you searching for which is here is it matching with the mid value which is nine no it's not matching but is it less than or greater than if the value which you're searching for is less than the Val which you got as a mid value then the values after the M mid value is of no importance right so this values here has no importance you can directly skip them so you you can divide your array into two parts so you got the first section here which is 5 6 8 9 and then you got the second section here which is 11 13 17 so what you can do is you can basically skip this part or maybe I will just do that with a color now we can skip this part we don't need this value the value which you want to focus on is 5 6 8 9 so what you do now is you make your mid as the end okay because we have shifted to the left part of the array now when of of course when you shift to the right part of part of the array in case then you shift your starting value now here you got S and E again and then what you do is you again check your ending you you will find your new new MID value so again s + e / 2 now s is 0 0 + 3 is 3 by 2 which is 1 uh of course you can say 1.5 but let's say this is 1 equal to 1 we can apply a float function to get the nearest integer okay so we got one here right so what you do is you check so this is your mid again you check is it matching with the value no it's not matching so eight is not matching with six then what you do then you again divide your array into two parts but then which which one you have to skip now now now we know that the mid is six so we have to jump to the next value I mean we have to look at the right side value so 6 is less than 8 right so have to look at the right side so now what we're going to do is let's we have divided into two parts right so now we can skip this part and focus on the right side now so your starting position has been moved here now now this is your s and you got e so again you will find a new MID which is s + e / 2 which is 1 + 3 is 4 4 / 2 is equal to 2 so your mid now is is 8 now is it matching yes okay and that's how you got the value now you might be thinking we have done so many steps right not exactly basically at the start itself we have remove half of the array and which is very important right so let's say if you have array size of 1,000 at the start of the first operation you have removed 500 values for the next operation when you do it you remove 250 values so the number of operation which you're doing with B search is less right and that's how you know that this is better than the linear search of course in linear search if the value which you're searching for is a first value it's very fast right it will take a lot of time for binary search but in general binary search works faster but how do you do that so with this Theory let's talk about the Practical implementation here so let's look at the uh code here very fast because we have already done the theory here so what you do is you basically write a function let's say binary search and then you passed a list of values right and then you also pass a targets same thing we have done in the linear search and here then you go with the left and right so basically we have mentioned the start and the end here in this we are taking left and right left starts with zero as we mentioned the starting start with zero ending ends at the last value okay we have done that and then you run a loop till you find the value of course then what you do is you check the mid value so this is the mid value we have done that mid is the the starting point ending point divide by two and then you check is the miror matching with the target if yes you're done you can simply return the value otherwise we have to do something more then you check if the value which you got which is a mid value and the target if the mid value is less than the Target in this case you will focus on the right hand side okay so basically you have to shift to the right hand side so your left your starting point will shift and we have discussed that here right your starting point will shift and then what if the mid value is greater than the target value in that case you will shift to the right the left position okay so that's what we have doing here and then every time you do that you run the loop again you find a new MID value and that's what we have done multiple times right we found the mid value three times in the same way you you're doing you're going to do here and then at the end you will get a value if it is not get if if is not there you will simply return minus one as we have done before so basically by doing this what we doing is we we have seen two algorithms right now we have to find the time complexity now what exactly time complexity is it's not about number of seconds it takes to run your code it's all about this so time comparity simply means the measure of how the running time of an algorithm increases with the size of input data so basically let's say when you are testing a particular algorithm you're testing for let's say 10 values or 20 values but what if the values goes up what if you have thousand values how will it will affect your time will it increase itas a time in linear section let's say if it takes 5 seconds for 10 values will it take 10 seconds for 20 values or will it take more or will it take less okay or let's say you have 1 million records will it take 1 million seconds that's the question we have to answer okay now how will you do that and that's where we have something called Big O notation now what happens in big or notation is we use this particular concept to understand your time complexity of your algorithm if someone says hey your algorithm is not fast enough you have to first find the Big O notation of it uh so why Big O is because it is represented with the help of O the Big O in bracket you mention the order of n or something so basically it is also called order of or Big O notation and anything will do so how do you represent that you represent that with something like this so we have o off and then the value which is one will keep changing so there are multi things here you can say we have O big O of log n we got Big O of n we got Big O of n log n we say Big O of n² which is quadratic time we got 2 to n we got n factorial okay so we got all this notation but how do you find this okay now this is tricky so of course in this particular video it's not you will not understand everything but let's start with the basic now let's talk about the linear search now if you go back to linear search what we were doing there now in linear search when you have five values you take five steps when you have 10 values you take 10 steps you when you have 100 values you take 100 steps right but let's say that is for searching right what about reading if you want to read a particular element from the linear search or from the array it's very easy right you specify the index value let's say the index value is 0 1 2 3 4 and then you specify hey I want the value at index two so let's say you say nums which is the name of the array and when you say two basically what you get is nine it's very fast so it doesn't matter what is the size of your array it can be 1 million records the time taken to get a particular element with the help of index value is constant right because computer knows where that memory is so in that case I can say the Big O the time complexity for this is one because it is constant okay now see it's not about one step if you're thinking just because I mentioned that it is only one step it can be three steps as well so let's say if you are reading a particular element and maybe behind this thing it takes three steps doesn't matter it's constant right it can be one step three step 10 steps it should be constant if it is constant you can say o of one but what if you are searching element so if you have an array size of let's say 10 it takes 10 steps now you'll be saying hey you know what if your element is at the start itself of course it will take one step but we have to go for the worst case scenario here the worst case is what if the element is at the end what if the element is not even there it will take 10 steps and that's why we can say the order the Big O notation here will be n what is n here the number of elements in the array as it increases the time also increases the time complexity I'm not talking about the actual seconds okay so that's your linear search now coming back to the binary search what could be the Big O notation for ban search see the thing is as the number increases it will not directly increase the number of steps reason for that is let's say we have we have seven values how many T steps it takes let's say three steps because we have to find the mid value three times right what if the size increases to double so let's say from Seven we got 14 so what do you think how many steps will increase what if I say only one reason being even if you have let's say 14 values here let's say we have so many values at the start itself it will be half right you will get seven values after one step and then you basically incre increase one more step to find the maer so that means when your value increases it will also increase the number of steps taken but not exactly n because in the linear search if your number of values increases by 10 it will increase 10 steps here as the number of value increases it will not increase the steps in the same sequence so what we can say is we can say it is something between constant it's not exactly constant but it is between the O of one and O of n so it is between this two and what comes between these two is O of log n now what is log n basically so log n basically means we have a base two here by default okay so number of steps it takes so what exactly this n here so let's say we have eight values here so when you say log two of 8 it will give the value which is three okay now why is three is because uh log of 8 is simply means it will take two multiply itself three times to get eight okay so as the number increases let's say now instead of eight values we got 16 values it will give you four because it will take two to multiply itself four times to get 16 right uh so it will give you four that means as your number of value increases from 8 to 16 it will only increase one step right and that's why the binary search is log n so your linear search is O of N and your binary search is O of log n so which will take less time the log of n okay and you can see the graph here as well so this is elements the number of number of elements you have in the array and this is the number of operation it takes okay so number of steps so if your algorithm is following the constant time it means it's here it doesn't matter how many values you increase it will be constant time log n is similar to it's almost same as constant not exactly same as constant but it will take less time so you can see log n is mentioned here it's there the the wave is there you can't see it it's there uh so what if you go with Big O of n so as your value increases your time also increases right but there can be some worse scenarios as well there can be some other algorithms which you implement which might take n log of n which is worse than o of n there might be some algorithm which will take o of n² which is worse than the o n log n so basically if you want to build an algorithm try to make sure that you are here if you are here you're good okay if you are here it's okay if you are here try to make it better if you are here that's the worst scenario that means you're building an application which is not scalable the moment you have five users on your server your server says I'm good the moment you have have 1,000 users it will struggle okay so make sure that you whenever whichever algorithm you write you follow this of course in the upcoming videos we'll work on some more algorithms we'll see which one matches there but you got the idea right so the time complexity simply means this definition the measure of how the runtime of the algorithm increases with the size of the input data and to represent that we use Big O notation in this video we'll talk about the Practical implementation of linear search and B search to understand the time complexity which we can represent with the help of Big O notation now basically we'll write two examples here in fact we will also do binary search in two ways just to understand how thing works and then how will you find the amount of time it takes not in seconds of course but the number of steps right so we have discussed about Big O notation which is the Big O of one big O of n Big O of login right but we also have multiple options but then at this point let's focus only on two with the help of two algorithms linear search which is Big O of N and B search which is Big O of log n okay so what I will do is I will write this code in Java and of course we'll not be doing very complex stuff in Java if you know any the language you can follow this so I'm using an IDE and again you can use any IDE I'm using intell idea so in this I have a project I will simply create a file here and we'll name this file as demo so we got a file called demo. Java and this is where basically we'll also have our main method so if you're are coming from different languages you know we also have main there so we have a main now let's start with the actual work imagine this is just a template for to run the code we'll write our code here now what we want to achieve now basically we will be having a list of elements and we want to search a element out of it okay now the size can be anything it can be five it can be 10 it can be 20 100 doesn't matter initially we'll go for less number of values and then we'll also see how do you work with thousands of values and how do you find element and then how much steps it takes to search a particular element there so what I will do is first of all I will create a simple array here and we'll say this a array name as nums so let's say we got an array and then I will also specify the initial values to it and since we are going for a sorted array of course when you talk about the linear search we don't have to specify a sorted array it can be applicable on any kind of array but when it comes to the binary search you need to pass a sorted element array Or List you can say so here I will simply assign some values let's say we got five values here and then I I just want to keep it simple with those values of course you can have any value doesn't matter I'm just trying to keep it simple or maybe if you want I can say 7 9 11 13 that's my love for odd numbers okay and I want to search a value so whatever value I want to search we can also specify here now we can use any variable name but we have already used Target as a variable right when we were discussing the theory so we'll say Target equal to and we'll mention the target let's say I want to search a which is 11 okay so this is what I want to search from this particular list we can do this with multiple algorithms we can use linear search binary search or whatever you think about but let's go for linear search initially and then we'll look at the binary search now how do we do that so what we can do is we can say I want to get the result okay U I want to find this 11 in this list and give me the index number I don't want anything else just the index number where it belongs to okay so who will give me that so what I can do is I can assign this start of someone else let's say linear search now this is another method which will give the value to us so I don't want to populate everything in this particular main main will simply say Hey you know I got these two things I got the Leist I got the T Target value you tell me which Index this value belongs to or if you don't have this value in the list also let me know so linear search will say Okay um accepted the challenge it will accept two things first nums and Target why you have to pass nums of course linear search will say I will search but tell me where to search and Target will say this is a value you want to search now the thing is we don't have this method yet so what I will do is I will just use my IDE to generate okay so I will say more actions create this method and you can see we got this method I don't want this to be private I want this to be public and that's it what you're doing is you are calling another method at this point since we are returning a value what we can say we can say return minus one okay nothing fancy I just want this to work and that's why I'm ring minus one okay and what if you get the result of course uh if there's no if this value is not there in the list it will return minus one otherwise we can return the index value so whatever value you get you can simply print here and you can say element found at index and then you can specify the index value here so let me just give a space so I can specify the result here so whatever index is there okay and let's run this code just to see I just want to make sure that everything is working let me just run this code and you can see we got the answer and also I want to move this to right hand side okay so it looks good so you can see the output says element found at index minus one that's weird it's just that we are ring minus one that's why it's printing minus one what we should be doing is we should be checking if the result is not equal to 1 or not equal to minus one so this statement it should print only when the result is not minus one in case it is minus one that means the element was not there so I can simply print element not found okay and now if on this code it will print element not found because we are not even searching right but we'll search now so how do you search it's very simple so what you can do is you can use a simple Loop here okay nothing fancy again simple Loop and in this you're going to iterate so we have seen the linear search right so if remember the theory we have talked about this uh so basically we'll search from the first element right so we are going to start from five and then we'll look for is it matching is it matching is it matching of course the values are different but we'll basically search for each element in the same way in linear search if you want want to move you have to use a loop so it will start from zero and will go till the nums of length so basically if you if the value number of values you have five you have to reach four okay so that's why we have saying less than and then I ++ now that's your for Loop right so in this for Loop you don't have to do much you simply have to check so whatever element you got the recent the current element which you're focusing on so in the list we have five values if you're focusing on five we'll check if five which is our first element is it matching with the target if it is matching with the target our job is done we can simply return the index the current index so let's say if you're searching for five and then you got five return zero your job is done that's what you're doing here okay and that's it okay it should work that's what we expecting linear search is so simple if you run this code it says the element found at index 3 that's right so this is 0 1 2 3 and our job is done okay uh what if you searching for let's say five in this case it will print at index zero and that's what we got so this is working but what if you're searching for something let's say 77 which is not there there in the list if you run this code it will say element not found so linear search is working right now we can do the same thing with the help of binary search now let's see how binary search works so what I will do is I will just write a code here which is public in fact I will just copy paste the code not a copy pasting but reusing the code I can simply copy this code and and paste it here now if you're coming from java and if you're getting confused why I'm getting a starting method why not non- starting methods it's because I don't want to deal with objects here okay just want to keep it simple so we got a static method and a binary search the thing is only method name will change the parameter will remain same in B search also you pass a list and you pass a Target the algorithm will change of course it will return minus one that's not that is not something which is changing but what we will do inside this now if you go to the board here what we have done is if you have a list of values if you can see we have a list of values here now initially you have to specify the starting point and then you have to specify the ending point as well right so starting and ending you have to specify in this example we'll say left and right okay because that's how that makes much more sense right left and right starting point is left ending point is right and then you will find a mid every time you find a mid if it is matching that's great otherwise you have to uh basically skip the number of elements which is in the other section okay so how do we do that here so what I can simply do here is in fact I will just for my reference I will also have those values here so I will say 5 7 9 11 and 13 okay and the value let's say which I'm searching for this time let's have the value so let's say I'm searching for 11 okay so what I will do is if when you're searching for 11 first of all you have to specify the starting point so I will say left in fact left int left is equal to now left is a starting point right which is zero then int right which is the ending point which could be four but then why to hardcode it what you can do is you can say nums do length minus one because nums do length will give you five you have to say minus one which will give you four so now we got the starting point ending point and then Our Journey Begins right so what are we going to do here so we'll use a y Loop here now inside this while loop what I'm going to do is I'm going to check if my left is less than equal to right we'll check if left is less than right because that's what we want right left should be on the left hand side which is the which is smaller values and then here what we'll do is we'll find the mid okay that's the journey we are going to start so mid is basically your starting point right which is left plus right and we want this to be in the bracket so that we can divide so left plus right divide by two okay that's how you find the mid right now once you got your mid you can basically check if the nums of mid is is it matching with Target if it is matching that's great we can simply return the index which is mid of course right mid ises representing a index here if it is not matching then we have to change the value of left or right depending upon what you got in the mid so what we'll do is we'll say if else if now how do you check so we can check if nums of mid is less than the target so let me take this up as well yeah so if nums of mid is less than Target so let's say when you talk talk about this elements and then we are searching for 11 right the mid which we got is nine if the nine is less than the 11 we have to shift our left right so this left which was representing five will shift here now I mean not even nine we don't have to even consider nine right so what we can do is we can say left will shift to Mid + one okay so basically now we have two values 11 and 13 which we are going to focus in the next iteration but what if you're searching for something else which is on in this side let's say we were searching for seven in this case we have to shift our right so right is mid minus one so basically let's say if you're searching for seven here you got a mid as nine what you will do is you will say Okay so the value is less than the mid so we have to we have to skip this two the focus will be only on five and seven that's what we have done here if you're searching for seven okay I think uh this looks cool we our job is done let's verify if things are working out the only thing if we do is instead of searching for a linear search this time we'll go for a binary search let's run it works you can see it says found index at three and what if you are searching for let's say 17 which is not there run it says not found let's search for five and it says it works you can say index zero so that means you can use linear search or B search both works but what if you want to really see how many steps it is getting so what I will do is let me just say this is result two which is Ban search and let me also say Result One of course I'm not going to use both the things I'm just I just want to call both the methods okay I'm just going to use result one because if linear search is able to find it P search will also able to find it okay let's print result one here so I just want to call both the methods that's the reason I'm I'm using result two but result two we are not using it anywhere else okay so let's do this with both the algorithms and you can see both will print the same thing oh we are not printing the second part okay doesn't matter both will give the same result but what I'm really concerned about is the number of steps so what I'm going to do here is let's calculate the number of steps it takes so I will say for linear search steps is equal to zero initially and every time the loop iterates okay I will simply say steps number of steps taken so steps Plus+ and then I'm going to print just so just before return I'm going to print steps taken by linear colon and I will print steps the same thing I want to print here as well what if there's no element found so this will not never be executed right okay we have to put this into a curly brackets cool so we are printing the steps here the same thing I think we should do for bunny search as well so here I will say int steps is equal to zero and every time you iterate in this Loop you will say steps Plus+ and then basically here you will print the same statement so I will just copy this so we'll print before returning the value but here we'll say binary and we also print this before returning just to cover all the cases okay let's see what it does run this code and you can see for five elements linear search is searching in one okay reason being we are searching for five right I think we searching for five let's search for 11 okay in some cases linear search works well run and you can see the linear search takes four steps the binary search Takes Two Steps that's crazy run again of course you will get the same output but what if the values are higher let's say I have more values here let me add 10 me add one 2 3 so now we have more values right I don't know how many values we have but let's say for this code the linear search takes eight steps binary search takes three steps okay but what if you have th000 values now how do you check th values now that will be tricky so what I'm going to do is let's add some random values here let me create a array of size th000 I don't want to specify my own values let's say thand and the value which I searching for is let's say 500 okay somewhere between maybe I will say 900 let's see what happens so I have a value which is 1 to th000 I'm searching for I mean I have th000 values in the array and I'm searching for 900 but by default the value will be zero okay uh example if you search this it will say not found but look at the number of steps lar search takes th ban takes 10 and what if you have more values here run linear search takes 10,000 ban takes four and look at the size example let's say if the size is eight and by default all the values on the array is zero so anyway it is sorted by default and you can see if I run this code uh which says eight for linear four for binary but the moment I double the values look at the steps taken by binary is only one extra linear all is double if I double the value again 32 so every time you double the value binary search will take only one extra step and that's why we say it's a big or notation for this is log n okay so in this case the binary search works better than the linear search now the same code you can write with the help of ban search as well example instead of using a loop here I can simply commment the entire section and we can replace this with a recursive function the only thing is in recursive that the changes you will be doing is in recursive you're going to call the same function again again by changing the values let's say you have this big array and then you done your first search you find you found the mid now the moment you found this section let's say this part or this part video is matching you will remove the other half and again you will run the same B search on this list and then again you will break it down down in two parts you will remove one and you will run the B search on on the other one so in B search basically if you want to make it recursive you have to pass two more things which is int left and okay not int left you to specify the left and right so will say zero and nums do length minus one so basically you have to specify the start and the end and every time you call binary you will simply accept those two things you you will say left and right so that you don't have to specify them here in fact you know instead of removing it maybe you want to refer this later I'll just come in this section now since this is a recursive you don't need a loop here right what you can do is you can check if the same step left is less than equal to right if this is a scenario what you will do is of course you have to find the mid I will just copy this code this is where you finding mid and after finding the mid you can basically also uh check the same logic this is a l you're going to use the changes is we we going to change in the else part I mean the if else part let me uncommon the entire section the changes you're going to do let let's not print the steps now we know the steps right how many steps it takes but I'm just trying to convert that into a recursive function so it will you will return mid here there's no problem with this the changes happens in the Els if so in the else if instead of doing this we are going to basically call the binary search Once Again by passing the same nums and then you will change the target value now so you will pass Target value so you have to pass nums and Target value then you have to pass the left value now what is left so in this case we have to change the left right left will become mid + one and right will remain same right will remain right but here again we have to make the changes else part instead of this left will remain left and right becomes mid minus one so basically what you're doing is you are changing the values like this if you have this list of values you break it down into two parts again after checking and then you're change you're again calling the same B search with a new values okay that's what we're doing here so let me remove the extra cly brackets just to make the code look good or short basically and that's it this is your B search let's see if this works let's re rest I mean rerun and it says not found because yeah of course it's not found because we don't have that value but yeah this is a code for the uh ban search with the help of of recursive now there's one more thing uh you can try with different examples what you can do is you can take a array of thousand values and add random values so in Java we have this random class you can add the values and see how much time it takes in this video we'll talk about the Sorting techniques now basically we have talked about the Big O notation right and now we know that if you want to judge the algorithm which you are writing you have to make sure that you are using an algorithm which is efficient in terms of time and space as well we are focusing more on the time complexity where as your data increases it should not increase your time exponentially so we have seen different levels of uh time complexity and we want to be into that lower Zone not on the upper zone right now when when it comes to understanding different algorithms we have to go for some INB algorithms right or some old algorithm which normally we use so sorting so we have talked about searching sorting is one of the way you can learn more about algorithms and how do you make it more efficient also in the real world we do use sorting a lot so let's say when you when you say you want to search something on the Internet or maybe you want you want to buy something on the Amazon you do that uh sort by pricee sort by reviews right so you do that multiple times so sorting is majorly used so there are a lot of different sorting techniques available so we'll try to understand them not everything or not every algorithm but let's try to understand of them now if we talk about these algorithms some algorithms are easy or simple to understand but they are not time efficient and then there are some algorithms which are good in terms of time so they are time efficient but they're not simple to understand okay uh so we have to do a trade-off here now which is the best one that depends if you have less number of elements and if you're learning for the education purpose or to understand how sorting works then we can talk about those algorithms like bubble sort selection sort and then there are some algorithms which are bit difficult and they are very efficient when you have huge amount of data now what ALG I'm talking about so when you talk about sorting there are different options available so we have bubble sort selection sort interesting sort merge sort quick sort counting sort uh reic hip sort bucket sort and there are multiple sorting techniques the famous One are bubble sort quick sort selection sort insertion and then M sort if I'm not repeated that multiple times so let's start with the first one which is bubble sort now bubble sort is not efficient but it is simple to understand so that gives us a starting point when you talk about bubble sort why do we use it so let's say if you want to sort different elements so let's say we have this list in front of you the numbers are 8 6 9 2 4 5 now if you want to basically sort them how will you do it now of course we don't have a magical way where you can find hey this is the first one this is second one so what you do is you basically try to use some algorithm like bubble sort to sort it in this what you do is you basically create a bubble now what exactly this bubble is take one element put that into bubble and shift it to the end okay now that sounds weird right so let's let's do step by step now when you have this value let's say you have six value in front of you you can put them in an array like this and then you have provided the index values now if you want to do this sorting on this of course we want a ascending sorting here so basically we want two at the start then four uh then five then six then eight and then nine that's a sequence we want but how will you get it so what you do is you compare two values at a time so let's say if you put six people in front of you and they want to sort them according to their height so what you do of course you will compare two people at a time right that's how you sort and then you will try to swap them so how it exactly it works so let's say we have these two values here8 and six and then what you do is you want to make sure that the biggest element from this particular array in the first iteration goes to the end end okay so the way you do that is first you compare the first two values now these are the first two values when you're comparing it if the first value is greater than the second value you basically need to swap okay swapping is very important here so you have to first compare the first two values if the first value is greater than second value then you have to swap and then you basically shift your pointer to the next two values okay done we are done with the six now let's go for the next two the next two is eight and nine if the first value is greater than the second value you have to swap in this case they are not there are so the first value is less than the second value so what you do is you simply say skip because there's no swapping needed then you go for the next two values the first value is great than second value so of course you have to swap and then again you shift your values next two values again you will compare the first value is greater than second swap then again you will compare the next two value the first value is get than second swap so basically after the first iteration you got the biggest value at the end but is it sorted unfortunately not you need to do this operation one more time actually not one more time multiple times till you get this sorted now this also depends upon the size of your elements or array now in this case the first five values are still unsorted the last one is done so we don't have to touch the last element so in the first iteration if you're going for six times the second iteration you can go for five times the third iteration you can go for four times so let's see how that works now after the first situation we got the nine value at the end now what you do is again you repeat the same steps compare the first two values is it require to swap in this case no we can skip it next two values is the first value greater than second value yes swap it then again you go for this next two values is 8 greater than four yes swap it is then again next two values 8 greater than five yes swap it now in this case what happened is after the two iterations you got the two biggest values which is a and 9 at the end now we know that those two values are sorted but what about other values those are not sorted so let's sort them again you have to repeat the same steps so you have to say 6 2 which is 6 is greater than two again we have to swap it 6 and 4 greater than 6 is greater than four swap it 6 and five again swap it so now after this iteration you got three values 6 8 9 which is sorted but what about other values they're still unsorted I mean we know they are sorted now but your algorithm has no idea if they're sorted okay now this is tricky because your algorithm goes from start to end okay it will not check for do the other values are sorted or not so it will keep doing that so it will compare uh the first two values not swap not needed next two values swap not needed done we have also confirm five still there's a confusion for two and four your algorithm has no idea those two are already sorted but still it will try to sort them it will compare the two values they are compared swapping not needed so by doing that four is confirmed and since you only have one element now that itself is sorted and by doing this you got the entire sorted areay so yes even if you it is sorted it will still check we are reducing One Step which is swapping but still you are checking and that's why bubble sort is not an efficient algorithm and the problem is the time complexity for this is O of n² because you have to use two Loops one for the iterations and the inner loop for each iteration you have to compare the values so that's your n squar not a good way of sorting the elements now think about six elements so that's good six square is not a big number it is actually but what about if you have a very big array or very big list let's say 20 values 30 values imagine the number of steps it will take so not an efficient algorithm but easy to understand now it's it's time to convert this into a code okay now let's try to implement the concept of bubble sort in the code so we are going to use Java as a language so if if you want to sort of course you have to store the values somewhere first so what I will do is I will create an array of integers and let me just name this as nums and then I will assign some values of course I can go for this new array and then assign the value one by one or we can give them the static values so here let's say we go with values 6 comma 5 comma 2 comma 8 comma 9 comma 4 I don't remember the sequence in the previous video which we talked about the theory but let's go with this values and I want to sort them okay so what I will do is even before sorting let me print all the values the way you can print the values of an array in Java is you of course you can use a normal Loop or we can use enhanced for Loop so let me just use enhanced for Loop here so I will say for in Num in nums and then we can print the values right so we have I'm printing num here so I will say this is before sorting and then I will I will do the same thing I will execute the same code but after sorting here so let's say this is after sorting okay now if I run this code of course you will get the same value two times so if I and one more thing I will not be I want to print this on the same line because Ellen will print on a new line I don't want it and maybe after every printing I want to give some space as well to differentiate the values and let me do that here as well so that it will it will print on the same line with spaces right click here and say run and you can see uh we got before sorting after sorting I think uh this should be printed on a new line so what I can do is just before this I'll say out and run okay so this is before sorting and this is after sorting at this point is not getting sorted of course right we have not written any logic here now this is where in between you have to write the logic for sorting now in bubble sort what you do is you compare the values two at the time and then you swap so you compare these two values if the first one is greater than second one just Swap and then likewise you will do for all the elements now for sure if you want to move to the iterations now we have to do two Loops here the nested Loop basically the outer loop is responsible for the number of iterations or the number of passes and the inner loop is actually responsible for swapping so what I mean by that is I need a first variable I which start from zero and where are we going to end it so of course can end it at less than six so you can say six or you can say nums do length Okay so basically we can use the added length you know the better way would be to store this value somewhere so I can say size nums dot length so I'm just storing that value in a variable called size it will be easier to work with so I will simply say size and then I will say I ++ so it will go from uh start to end so this is only for for passing right we have to do this multiple times you have seen that in the animations but now let's do the inner loop which will do the actual swapping so here I will say int J is equal to Z because we going to start from zero the real question is where are we going to end it at this point I would say size and let's see what happens if you do do with size okay so both the loop the outer loop and the inner loop are starting from zero and ending get size the size of the array and now we have to basically compare two values now how will you compare two values uh and how do we even swap them so we will check if the first value now how do you know the first value so nums of six or nums of Z basically six is nums of zero so nums of zero compared with nums of one that's how you compare again you can compare nums of one with nums of two then nums of two with nums of three then nums of three with nums of four that means the current number when you say 0 1 2 it is represented by J so I can say nums of J but this will be compared with the next value and how do I know the next value which is your nums of J + one as simple as that so we are comparing two values now how we are comparing them which operator we are going to use here so we are going to use greater than if the first value is greater than the second value then you have to swap simple technique right now how do you swap there are different ways of which you can swap the values uh I'm going to use a traditional way let me use a temp variable uh in fact you can can also declare a temp temp variable here so I can say int you know it's better to have all the variables on top so I will say int temp equal to 0 inally and then using the third variable I can simply swap them so I can say temp is equal to nums of J and then nums of J is equal to nums of J + 1 and then nums of J + 1 is equal to time so basically what we doing is with this three lines we are just swapping them nothing complex just swapping the two values okay and that's what we have done here so every time you find this the first value greater than the second value just swap them and you do this multiple times because we are keeping this in a loop and that's it the Sorting is done uh since it will be happening multiple times it will make sure that you keep the values at the end the bigger value at the end after each iteration so if you run this okay we got an error it says is out of bound now there's one problem here don't you think when we are going for J + 1 what about the last value so we need to make sure that we don't go to the last value so it should end minus one right uh because when you're comparing the last two values the value for J will be here and this will be J + 1 so we have to make sure that your J ends before the last value that was the error and run and you can see sorting done and you got the sorted values this this is how basically you sort them again we have seen this explanation in theory how things are working out but here we just try to sort it with the help of this logic now there's one problem here uh every time you do this in the animation if you remember every after every pass we don't have to go for all the values because the last value is sorted in the next pass two values are sorted in the next pass three values are sorted so we don't have to sort them again or even check them again so here J should be not going to size minus one in fact it should go size minus i - one is because after every ration so let's say we have the first situation where I is zero it will go till the second second last value because we're saying minus one in this second iteration where the I value will be one it will go till the second last position or third last position because the last value is sorted you don't have to check that again it will not affect the output but it will affect the speed because we are saving some time by not checking the same sorted value again and again okay that's how you basically you do it in fact if you want what we can also do is we can print this in every iteration so I can just copy this and after every iteration of the outer loop we can print the values and see what is happening there right so in fact I will also print a new line here so that it will come on new line let's run this so this this is what basically is happening initially you have this value and then this is where the Sorting starts if you can see after the first iteration the biggest value nine just went till the end then after the next for the next iteration the biggest value which is uh eight is at the end or second biggest value third iteration we got three value sorted fourth iteration we got four value sorted fifth iteration we got four value sorted or five value sorted and last iteration we got all the value sorted so that's how basically it does of course here too we got at the start itself we don't have to sort them again but that's algorithm works right how will you even check if the array is sorted even before you compare all the values so that's the thing about the bubble sort where you compare the values swap them and you're done in this video we'll talk about selection sort so basically we'll talk about the theory in this and then in the next video we'll talk about the Practical of it the thing is when you talk about sorting we have different techniques available in the previous video we have talked about bubble sort and it was working right but also the amount of time it takes B the time complexity of the bubble sort is n squ now that's not the main issue of course that's a main issue but then in this video we are not going to solve that quadratic uh time complexity but at least we can reduce the number of steps now in bubble sort we are going from start to end multiple times that's not the big issue the big issue is swapping because every time in the Inner Loop we were doing this swapping multiple times and it consumes a lot of time and memory so what if we can reduce that and that's where selection sort comes into picture now what we do in selection sort is we don't actually swap every time when you compare to values but what you do is from the entire array or the least you find the minimum or the maximum value depending upon how you want to implement it so let's say we got we are going for the minimum value so what you do is from the entire array you find the minimum value and then you make sure that the minimum value stays at the start now once that is done now that is your sorted part so from the entire list you create two sections the sorted section and the unsorted section so once you find two that goes into the sorted section which is at the start and then you scan more values now let's say when you scan you find hey we have three which is the minimum value in the unsorted array then what you do is you pick up that three and replace it with the second location now what happens to the value of the second location now that will be swapped where the value of three was so what you're doing here is you ask swapping by selecting the minimum value So when you say selecting that's your selection sort now what it does is you're not basically swapping value inside the inner loop you are doing that in the outer loop which means the number of swapping in a code will reduce now to understand this more let's look an example so let's say we have six values here which is 65 2 8 37 and we want to sort them so how will you do it first of all going from all the values now of course as I mentioned before you can go for the minimum value or you can go for the maximum value so let's go for maximum value okay so ultimately you will get the same output but the way you do do the code will be different so let's say we are going for a maximum value now from all these values we have to find the maximum value now of course at the start we don't have any idea which value is a maximum so let's say you make six as the maximum value because at this point we don't know right so let's assume that six is the biggest value from the from all these values and then you start comparing six with all the other values so you will compare six with five if six is still bigger no issue go with the next value if six is still bigger no issue go with the next value now at this point we are comparing six with eight and we know now 8 is the biggest value so now what you do is you change your variable or your you change your biggest value from six to 8 and then you start comparing eight with the other remaining value which is three and seven so you compare with three eight is still the big biggest you compare it with seven seven eight is still the biggest now once you go through all the values now you know the eight is the biggest value so what you do is you make sure that eight goes at the end now if you're doing this with for the minium value what you do is you take the value two which is a minimum value and you put that at the start but since we are going for a different approach where you're finding the biggest value you will make sure that eight goes at the end now when you move eight to the end what happens to seven now seven will say okay I don't have a place so eight will say don't worry I will come to your place you come to my place that is swapping of the values but if you remember we are not doing it in every iteration of the inner loop we are doing that in the outer loop so once you complete one pass that's where you do the swapping so only once for the entire iteration okay now by doing this we have made sure that you have the biggest value at the end and you got a sorted at the end which is the eight and then the first five values are still unsorted so what you do is you repeat the steps again start with six now we'll say Okay six is the biggest value let's assume that and then you start comparing six with five if it is a biggest Val six is still bigger no need to change the value of biggest value then you compare it with two no need to change but then when when seven comes to picture seven says hey you know I'm the big I'm bigger than you so six says okay you're the biggest value so now the current value will be seven the biggest 7 now again we have to compare seven with three now seven is still the biggest so we have to make sure that after all this situation we move seven at the place of three and that's what we are going to do now by doing the second iteration so we have completed this second iteration in which you got two values seven and eight at the end and the first four values are same so again you do the same thing next iteration so you have to do this till the till you complete all the Sorting so you again go with six and say hey you know I'm the biggest now let's compare six with five so still the biggest compareed with two still the biggest compareed with three still the biggest now once you complete that unsorted iteration you will make sure that you move your six to the location of three because that's where the six should be so it will swap with three and then the last three values 6 7 8 are sorted the first three values are not sorted so again if You observe what we are doing is we are making sure that you got two different section of sorted values and unsorted values now goes for the first three values so three let's assume three is the biggest and then compare it with five now we know that five is the biggest of all or at least bigger than three and then you compare you you make the bigger value as five now three is gone then you compare five with two still five is biggest and now we know that five need to go at the location of two so you have to swap it now comes the first two values again the same thing you will keep doing this till you reach to the end of your array and you move to three and since we only have one value don't need to sort it that's already sorted and by doing this you basically got a sorted array now again compared to the bubble sort we are still going from start to end you have a outer loop you have a inner loop but we are doing swapping only once in every big iteration or in every part so that's the advantage of using selection sort over the bubble sort about the overall all the the time complexity is still o of n² but at least it is better than the bubble sort in terms of swapping now it's time to implement selection sort in Java so in the previous video we have talked about the bubble sort right and this is how it works now one thing to remember in the Inner Loop can you see this Loop in this Loop we are basically doing this swapping so it increases the time as well so in selection what you do is you basically Swap this of course you do that swapping but outside the inner loop somewhere here so it reduces the number of swap you're doing how will you do it so what I will do is I will just remove the entire code from here because this is the bubble sort I don't need this and what we have here is we have the array which is which has six elements we got a size variable where we are storing the length of an array and then we also have a temp variable for swapping then we are printing the array before sorting then and we are printing the array after sorting okay so if you run this you will get the values before sorting after sorting but at this point they are same is because the main logic of sorting is missing here so let's write the logic for selection sort here the thing is in the theory we have said that we'll go for the maximum value the biggest value to swap or we'll put the biggest value at the end let's say in the Practical let's try to implement with the smallest value and we we have seen both the approach then so what we're going to do is we'll look at the entire array and then we'll find the smallest value which is two of course you have to iterate between all the values and then once you got the smallest value you will swap this six with two because two should be the first value and that's how we can start and for doing that we have to use two Loops the outer loop and inner loop we'll start outer loop with zero and then we'll say I less than uh size right okay so we'll say I ++ but also we can make it more efficient by saying that size minus one is because if you remember when we were doing the sorting and once once you got all the values sorted except the first value or the last value you don't basically sort the one value right so you can reduce the number of equations you go for so we can say size minus one and we'll see if that works or not and if it works then we are happy we'll also go for the inner loop now for the inner loop will go for J equal to now where do are going to start J so basically if you're putting the starting the biggest value or the smallest value at the start what it means is after for every iteration the inner loop we going we can skip the first value right so for the for the second iteration we can skip the first value for the third iteration we can skip the first two values because they are sorted right so we have two sections right sorted array and unsorted array so here we can start with J equal to I yeah in fact uh when your I value is zero J should start from zero so we can say J equal to I and J less than size and j++ so basically we have to end we have to go till the end okay so that's the for we have now what you do inside this so we have to find the minimum value to start with we can take Min value variable here so I can say int Min is equal to zero in fact we want the minan index not the minan value because we have to work with the index right and we'll say the index is by default minus one and here to start with we'll start with Min uh index equal to I and then what you do you basically check if the are which is nums of mean index now we want this to be smallest value right so I assume that this is the smallest but what if this is not the smallest compared to the value which we are considering the value which is considering here is nums of J so example if this six is the smallest which we are thinking in fact you know we should be saying I + 1 the reason being when you are saying that 6 is the minimum value we are assuming that and then we are comparing with five now you will get five when you say J starts with i + 1 because in the first ation the value of I will be zero and this five is one right so we'll compare these two values okay if six is smaller than five then everything works but what if six is greater than five in that case we have to swap now okay we don't have to swap we basically have to change the minan index to J because the new index you will get is J so the new index here is one okay uh so that's the main main index so what we doing by doing this is after the entire iteration you will get the minimum value from this array okay so that means after this sitation you will you will know that two is the minimum value now once you know the minimum value what you do is you swap two with six is that simple how do you swap two and six how do you know we have to swap six because I is referring to six and the mid index is referring to two so we can simply swap them so we can use a temp variable here and we can put nums of uh minan index and we can say nums of Min index is equal to nums of I and nums of I is equal to 10 so basically we just have to swap them so what we are doing is in the Inner Loop we are just finding the minimum value and then once you find it you simply swap it with the Min value which you're considering which is the first value and as the I value goes up first you will compare with this then you compare with the five and you compare then you make two as a minimum after of course after the swiping and what what I will do is I will also print this values after each iteration so I can just copy this and paste it here and we also print a new line to get it on the new line so now if you done this let's see what happens so you can see if this is okay uh I will after printing this also I will just say new line or maybe I will just SK this new line before the for Loop printing okay let's run R and you can see this is before swapping and then and this is after swapping so let's compare the value is it properly sorted uh yes 2 4 5 6 8 9 those are the values we have and then see the steps in the first situation before swapping uh so this is the first situation what you do is you know that two is the minimum value so you swap six with two and that's done then in the second iteration you find the minimum value so the minimum value from this is four so you swap four four with five and that is done here then you consider six then you say you find the minimum value from this which is five you will swap five with six so in this you'll say uh six is the minum so you will swap it with eight comparing this two this is 8 and 9 so you'll swap eight with nine and in last value you don't have to even sort it because it's already sorted and that's what we are doing here and that's why we say it size minus one we don't even sort here in fact if you don't put this minus one what will happen is if you run this code you will get one more iteration and there's no change there it doesn't matter how many values you have you're not going to have any change in the lastar iteration so you simply save it by saying minus one here so that's how we use selection sort basically find the minimum value and Swip it with the location where it should be so we have talked about different sorting techniques we have talked about bubble sort and selection sort in this video we'll talk about insertion sort so what you do in this technique is basically you take the elements and then you put the elements at the right position that is your insertion so you have to put at the right location now in this you don't normally do swapping yes we do swapping but we don't use word swapping what you use is something called shifting okay so example if you're playing uh if you're playing cards so if you if you have a random cards on your table so basically in this example we not going to use cards we are going to use this uh characters I I found these characters in my uh in my drawer I think I got it from my it's birthday but it doesn't matter so I got this and we'll try to sort this values here so let's say if you randomly get this values and if you want to sort them uh what you do is you pick up the value and then you you know imagine these are cards playing cards so what you do is you take the first value you take the second value and then you keep it like this I don't know if how many of you play cards I used to love them not for money uh but I used to love them then you take a next card and then you put it before this then you take a card then you know that this card goes between a and o you put it here so so this is called insertion but how exactly you're going to do that that will see with the example on this table and with this with this cards so now let's sort this values now these are not values but characters so let's try so what we have here is we'll start with the first location as you can see on the screen we have different values here and I'll pull this down a bit so that we can have some place to skip this so the first value is O now I'm assuming the the O goes here the first location because it's always is sorted and then you take the next value which is e let's say now we know that e comes before o so what we need to do is this need to go at the start so you have to move this to the next location so that you will have some space for e now the point to remember here is it's all about shifting okay so we don't use a word swapping here you'll understand that in some time why don't we say swapping uh let's go for the next one which is s now we know that s is greater than so we'll compare s with all the values so we'll compare s with e okay it goes after that it goes after o so s goes here okay I'm not promoting any camera model here but that's the sequence then you go for the next one which is D now d goes at the start so what you do is you basically have to move all the elements right so if you compare with s here D is less than S so you compare so you have to shift s you have to make some space for D then you take o and you move o here so that you can make some space for D then you compare D with E and you have to make some space of course you have to swap you have to move this you'll make some space for D and D goes here right so now it is in sequence then what about the last value which is a now a we all know we have to I mean of course it goes at the start but we have to compare from start or last so this goes here which is s if it is less than that we'll shift this to make space for a but then we have to also check with o this also have to shift we check with e this also have to check shift We compare with d this also has to shift and then a goes at the start by doing this what you're doing is you're basically inserting value at their location so this is how you do the insertion sort now let's say this was randomly we were picking values right but what if you you have things already in the array so what we'll do is we'll keep it here we'll keep this here we'll keep this here this goes here and let's say this goes here in fact I will also move this okay I'm not trying to make any city name but let's say we have this sequence we have this five values and now you want to sort them so what you do is you don't start from the first value you start with the second value and then because we assuming that the first value is already sorted okay so what doing this technique is basically you divide your list or array into two segments the sorted and unsorted at this point all are unsorted Al so this is unsorted aray but when you start you have to assume that your s is already sorted then you start with E then you see hey do do we have e at the right position the way you do that is you compare e with s and this and you say Hey you know e is not at the right position so you will take e out uh in programming you what you do is you save the value of E in the temporary location okay so exactly you're not moving e you're just making a copy of it maybe there will be a copy of e here as well but you have extra copy with you saved in a temporary variable so let's say this is a temporary space then you move your s here so that you can make space for e and that's sorted now now if you see this two it looks sorted now what you do is you check E before do we have any value so of course we don't have any value before e now that is sorted then you go for the next value so we started here in the next iteration you basically start with o now o will compared with the previous value which is s now is it the right position no so we have to copy o in the temporary variable and move your s so that you can make some space for o and O of course you can move o here you have to also check if o really goes here you have to compare o with the previous value which is e and we know that o is greater than e so you can SK o here so that's how you basically sorted the third element then you go for the next element which is D so what you do is you compare D with s here and then you know that D is smaller than S or S is greater than D so what you do is you have to move so you take into temporary variable move s here of course you don't move D directly there you have to compare D with the other other elements which is O is it smaller than that yes we have to move this here we have to make space for d right again the before that also we have one more element which is e so you move e here it's all about shifting values and then your D goes here and if you compare uh it it looks sorted right compared to the other other values so it looks sorted but then we have to complete one more iteration so you take a you compare a with s now a here is compared with s so again we have to shift so what you do is you take a in the temporary location then you move your s you will check a with o again you have to move O then you check a with e again you have to move e so remember you have to only move when you know that a will not be at the right position and then you compare a with d again D has to move so that you can make some space with for a so this is how basically you do this sorting technique now question arise how will you do in programming so what you need is you need two Loops one for the number of passes you are doing right moving from different element and you need a inner loop which will responsible to find the key and uh shift the values and also you need a temporary variable so that you can store it somewhere so you have to you need a key which will go into a temporary variable on something now how do we do this that's Implement in the code so let's try to implement insertion sort using Java now basically even before we Implement that before we write the code let's write the algorithm on the board and then we'll try to convert that into code so what we'll do is first of all let's create some let's get some values okay so we also need a array so let's say I will name this as array itself ARR and then it will have some values so let's let me create a array here and we'll take few values let's say we got uh five values of course you can go for more values here and then let me just say this values are 3 6 2 1 5 okay so we got this five values now I want some variables as well so as I mentioned before we don't swap here right we do shifting of the values so of course to achieve that we need Inner Loop and the outer loop so let's say we got the outer loop which will will go for for Loop so let's say we have a for Loop here and then we have to take a variable which is I for the outer loop counter and then inside this we are going to have a while loop which will have let's say some condition of course you can also go for four and four but then when you talk about insertion using while loop inside makes much more sense is because we want to B we want to run this based on the condition not on the number of itations okay so this is your outer loop let's finish it here this is your inner loop let's finish it here and let's write the code there now we need two variables as I mentioned of course in the while loop we are not going to create a variable we are going to use one variable but let's also go for J so we need a i variable let me just write I here we need a j variable and we need a key as well so that you can store this value somewhere so at one point you will compare and you will save it now from where we are going to start this now if you want to understand what what we going going to do is it's very simple so initially you will take your six as a key so when you write this variable we'll say key is initially six okay now how you're going to get six so let's say we take a variable I here and then we'll keep the J here okay so we're not going to start I from the first one we're going to start I from the second one and then J will be the first value now I will represent the outer loop and J will change in the inner loop as well okay so that's why we going for I and J we could have gone for different variable name but we got used to I and J for the Outer Loop and inner loop okay so now uh what you're going to do so the idea is very simple once you got a key which is six in this case now how you got six maybe we can do something like this we'll say a r r of I whatever I represents we'll save that in the variable K or key and then we're going to start J by saying J is equal to i - 1 so I is starting from 1 so J is i - 1 that means the value for J initially will be zero and I is 1 now what you're going to do with this it's very simple you will simply compare the value of J which is this the first value so you'll compare J with the key if this value is greater than the key then you will do the shifting part at this point we don't need that right because the value of J where the J represents is less than the key then you don't have to do any shifting part so this is is already sorted okay but let's go for the next situation now when you say next situation it's very simple what you simply do is so you shift your value of I here and you shift your value of J here because we know that the three and six if you compare this two they are sorted let's go for the next one now in this what you will do is you will check again you will change certain things right if you can see this will change because now a r r of I is not six a r r of I is basically two okay so key is two now what about the value of r even I is changed I is now two so index is two so let me also write index so that it will make much more sense right so that those are the index value so I value is two now what about J so J is i - one so J will start from 1 that's done and now you will apply a condition so what basically condition we are checking for so of course if you write the code here so I is initially starting with one we are not starting with zero and then we'll go till I less than n if the value if the length of this array is n and then we'll say I ++ okay and then we'll assign the value okay we let's not write the actual code let's write some dummy code so let's say we got a key variable the value for key is basically a RR which is the array of I and that's what we are doing here that's one thing next we also need J variable so we'll say J is equal to IUS 1 okay that's how we going to start now what you're going to compare in the while loop so in the while loop it's very simple you will check for the a RR so a r R of J let me just say complete this here so this if this is greater than the key because the shifting part will come only when whatever is represented by J is greater than the key so if you can see the key is two here and in this case yes it is greater so 6 is greater than two so what you do is you basically perform some operation now what operation I'm going to perform so basically first of all I will take this key somewhere of course we are storing that key two in the key variable and then you do the shift shifting shifting of what so you will first compare this two with six and now we know that we have to shift six so you will shift six here then that's one done right can you put two here we can actually we can put two here but the problem is we also know that we have to compare two with the previous value which is three in this case so you will not shift two there but will shift three here but then to shift three here what we have to also do is we have to first shift our J here on the first location that means we are also going to perform a minus minus right so what I'm saying is the first option you're going to perform is Shifting the values right so how do you shift you simply take the next value so initially J was here right so this value six is going here so that means what we can do is we can say a r of J + 1 which is in this case two is a r r of J by doing this what you're doing is you're shifting so the J initially was here which is one so index of J was one or the value of J was one then you're saying [Music] so it will become J J becomes minus one so we have to stop it here once the J becomes less than zero we have to stop but don't you think we have to also add that condition in the while loop so we'll do that we'll say and J should be greater than equal to zero okay this is one condition we have to add if this is false if J is not less than or equal to zero then you will do the last last step the last step is very simple you move your two here and the way you can do that is by saying a r r of J + 1 because J became minus one we don't want minus one we want zero is equal K is equal to key so basically this is where the value goes first now by doing this we were able to sort the first three values now what next of course you will increment the value of I so when you say increment that means the value of the I variable goes here and again for the next situation J goes IUS one right because of this basically for the outer loop the I value is incrementing but the J value is just before I and again you you do the same things what are the same things you first check if the key okay we have to update the key as well now how do you update the key now the key value is a of I which is in this case should be one so the new key is one and j i is what I is three and J is 2 okay new key value is 1 you will compare if the a r r of J this is the condition we're checking now if a r of J which is in this case is six is greater than the key in this case yes you perform this operation what is the operation it's very simple let's keep this value somewhere you start shifting six here six goes there and then your J comes back J comes here right because of this step here right and then you you again check if a r of J is greater than key yes three is greater than one again you will shift three here then you shift J here then again you shift two here then you shift J here and then now J becomes minus one Let's Stop the Loop and shift one here you can do the same thing for five let's do that quickly so what you will do is you will shift the value of I here which means certain things are going to change now this I becomes 4 uh this J becomes three the key value now is five right again you do the same things uh you keep your J here because J is just IUS one then you compare if the a RR of J which is six in this case is great than the key yes let's do the shifting now how do we do that let's move that here and then we have to shift six here J goes here now again you check is the a of J is greater than key no it's not there so you will just simply come out of the loop and then you will keep five here and if you can see it's all sorted so this is the logic let's implement this logic in the code now so you have the logic in your in front of you what you will do is you you got the same values in fact we can go with the values which we have so let's say okay I now I just re returned the values let's go for this values and let's see if this works instead of array we are going for nums that perfectly works I don't want a Time variable so we can remove this portion here this is basically the older code which we had for selection sort if I'm not wrong and this is the before sorting elements this logic is going to change for sure okay so let's write the logic it's very simple you create a array so let's say this array is a RR is equal to let's have some default values to it let's say 5 6 2 3 1 this time we're going for only five values and this is an array okay what next now basically as we have WR in the board let's take a for Loop now the for Loop is going to start with i i is equal to 1 and we can do that quickly now it will finish at n but we don't know what n is so we can say add. length and we can say i++ right that's one done and now okay let me just remove some space in between okay now what are the extra variables we need basically we need two variables right we need key the value for key will be a r r of I which we have done and then we also need J and we know J is J minus one you know this this is what happens when you know the algorithm on the on the board right I have a I have a board with me so okay those are the things we need okay not J minus one it should be IUS one and then you do a while loop because this is where you do the iteration we have to make sure that J is greater than zero that's one thing and we need to also check if a r r of J is greater than key in that case you will execute the while loop so you will do certain things here now what you are going to do inside this while loop so basically you have to do the shifting right if it goes inside you will say a r of J + 1 is equal to a r r of J right and also you have to reduce the value of J so we will say J we can even say J minus minus syta that works and for the outer loop you will assign the value for key for that particular empty location and the empty location is defined by J itself so J + 1 is is equal to key and I think we are done so the code is so short uh let me check if that works what I will do is I will just print the values here so I will take a for Loop and I will say int num from array and let's print the values let's print num I don't want the new line I would just want a space here and let's see if that works right click run and boom you can see we got the output and the values are sorted it doesn't matter what type of values you add here let's say a four and run you can see this is sorted yeah so that's your insertion sort in this video let's talk about another sorting algorithm which is called a quick sort so the thing is when you talk about sorting algorithms the idea here is to explore different algorithm and we'll see how efficient it is now when we talked about the other algorithms which we talked about till now bubble sort selection sort they have a big or notation of n squ which we don't want right we want to make make it faster or efficient now in that case we have to explore some other algorithms and one of them is quick sort now in the best case the quick sort goes for n log n which is much better compared to the n n squ but in the worst case also it goes for n Square so in the worst case yes it is almost there but uh on average it is n log and or you can say the best case so at least we are happy in the best case and in the average case now let's let's see what is quick sort so actually it uses multiple things so let's talk about that first the first thing is divide and conquer so basically till this point when we talked about bubble sort or selection sort we were doing the algorithm on the entire collection so let's say if you have a list of values which is six values so what you do is you perform the operation of bubble sort and all the Sorting techniques which we have done before applied on the entire list but what quick sort says let's do divide and conquer which simply means divide your into multiple things and solve the problem or sub problems individually so what you're doing is you're dividing it you're solving it and at the end you will combine it so that's your divide and conquer so in this case if you have six values let's divide the values into multiple sub problems and then sort it and then combine it so that is your divide and conquer next it it uses something called a recursion of course you can also use recursion in the other other algorithms but here it works with recursion now basically what is recursion so when you call the same same function by itself so example let's say the function name or the method name is show and if you're calling show inside show that's your recursion right and next we have to understand the concept of pivot now basically pivot is something called a central point of a problem okay so let's say you have this entire list here or the array now you have to find a pivot and then based on that you will perform the operation of course it will make much more sense when we start but remember we have to work with the pivot so those are the things we have to remember divide conquer recursion and pay there's one more thing which is the tree now basically when you say you are dividing your entire list into subsections or sub problems what you're doing is you are creating a tree structure okay so once we start with the diagrams it will make it will make much more sense but these are the things we have to remember so let's see how your quick sort works and then we'll try to convert that into algorithm and then we'll see how to write a code for it now B the basic idea here is let's say you have a list of values so we are discussing about quick sort uh so let's say we have a list of values so let's go for six values here and I will say this is five this is Three 6 1 4 and 2 so let's say we have the six values I'm not sure will it really make sense to use this example but let's get started now with this six values what you normally do is you do divide and conquer here so the basic idea is break the entire list into sub problems okay something like this maybe can we can create two two different that is here we'll be having three uh I mean 5 3 6 here you can have 1 4 2 and then you can again divide this maybe you can have one and four here and divide this into two here also we can have five and then we can have another array which is three and six so we got this two arrays from the big array and then you create the shorter format of it so at the end what you're doing is you're creating the small chunks now this is what your tree structure is so we have d we talked about divide so we are dividing it we'll sort the array and then we'll combine it and then since we are performing the Sorting on each section in sub problem that is your recursion but is it the real thing no so basically we talked about the idea but how do you divide the array randomly I just divided the array but that's not the main idea but we have to divide but not this way so how we are going to divide this that's the question let's remove this and let's go with the values here the idea is you want to divide for sure you will divide this into two parts but you need to find a point from where you will divide this and that point is basically your pivot now how do you find a pivot now basically uh the reason we get this parti or pivot to do the partitions of the array right now should we pick of one now that depends so even before you divide you have to do one more thing so I'm saying pivot but then that pivot should be a point which is at the right position let me repeat the pivot is a point in this array which is at the right location now if you see this after this sorting what the values you will get so you will get values like let's say I will just write it in shorter format now so you will get 1 2 3 4 5 six this is the output I want okay I just went for the sequence of numbers here but this is the output so this is the output which we want from this input which which we have the bigger array here or the big size which I mentioned okay so if you talk about the value one here so one should be at the first position two should be at the second position three on the third position fourth fourth position Fifth Fifth and six right so they should be at that position now since we had taken those values and that's why it might be confusing but you got the point right doesn't matter what your values you have it should be in the ascending order so the first value the smallest value should be first and then least goes on okay now we have to make sure that at least you you find one point from here or one value which is at the right position example if I say uh let let me take four now four is it at the right position the answer is no four should be here right so instead of one it should be four then we can say four as at the right position now if you want to understand this with an example so let's say you in a classroom and then you have multiple students there okay and then you want to sort them based on their height there's one way as a trainer I can be there and I can say okay you go there you go there and that's what we have done in the bubble sort right I was controlling it but what if students can sort themsel so they can compare each other and they say okay I should be here or I should be here and they they will move now one way is one person can sort them but then we don't want it so what we want is what if the students themselves can get into the right position that that will solve the problem right okay but how we are going to do here how they will reach to a right position now what you do is initially we don't know the position right so you can take any value normally we can we can find the pivot so for the first operation we what we are trying to do is we want to make sure at least one value is at the right position so that we can divide the array now which value should we take technically if you take the average value or if you take the middle value example if you if you go from 1 to six the mid value is three or four right so if I can take three as my Pivot it will be much easier for me to divide the array so what will happen is if I take three as my Pivot then three should be coming here right so three this is the right position for three right but it's not if you start with the are this is not the right position but if you get to the right position we can divide into two parts right okay so that's the main idea how will you get that there so normally what you do is if you can find a good pivot and that's very difficult to find because how do you know the mid value since we know the values here I can find the mid value but as a program or as a software they don't they don't have any idea what are those values are so what we do is we randomly pick the pivot maybe you can pick up the pay it let's say one in between somewhere anywhere if you want so you can pick up this one you can pick up this six or you can pick up the first value or you can pick up the last value or what you can do is you can you can just add all these values and divide by six whatever value you will get so try to find the average of it that's what we we can do but again that's a lengthy process normally which I have seen is they go for the big the last value not the biggest value last value because we don't have any idea what the biggest value is so we will go for the last value okay so this is this becomes your pivot that means we need multiple variables here now what variables we need so I will just write variables here so the first variable we need is a pivot variable so we'll say Pi I okay and then uh you need few more variables you need a variable I for the positioning again we'll discuss what this positioning is uh then we'll need a j which will track which will go through each element and we have done that in the Sorting right so when when you jump from each value that will be done by J and then of course we also need array here so let's say this is a r r this is your are okay and let's have the index Valu so we say this is 0 1 2 3 4 5 those are the index values we have apart from this we also need a low and a high variable okay so we'll say l and H so let's say this is L and this is H now normally we can say h is the last value or we can say it's a length minus one so we can say that so we have a low and we have a high so low starts from the first and H is the last value now how do you per perform the operation it's very simple first you find the pivot now we know that we are going for pivot which is two in this case so what you will do is let's take this two as the pivot so I will say this is p so this is referencing this is uh referring this to so Pi I variable at this point is referring to so I will say this is two in this case and then we need two variables so let's take variable I now by default the value for I will be the lowest value minus one so let's say the I value is here at this point you know what I will do let me just remove this portion at this point because let's keep this state clean so we need a i variable and then we need a j variable so J will be here to start with so I will be here at the minus one position J will be here at the zeroth position and then what you do is you basically run a loop where of course the I will also increment but the main Loop here we want is for J okay uh so we'll take a for Loop for J okay so let me WR the code here so what we want is of course the rough code so I will say for J will be starting with low okay that's what we are doing here and then J should go till n but then we are not using n variable right we are using variable high so let's say J less than high and j++ now what you are going to do in this inside this Loop so basically you will first make sure that you get the PT value right so you will I mean of course you have a P value here which is two and then you also got I I is still minus one now inside this Loop what you will do is you will check if the a r r which is in this case we have an array if a r of J if it is less than the pivot so pivit we have a variable here if it is less than pivot you will perform some operation now if you compare the values here do you think the a r r of J which is five in this case uh is it less than the pivot because pivot value is two no it is not less than pivot so we will not do anything but we have to find a value Which is less than two right so if the value of five is less than two you will perform some operation since it is not true we are not going to perform any operation here so basically you're going to shift your J here okay now how while we are doing it of course we are running a loop right so J starts with low and then we have to increment so J goes there it will compare itself with the pivot is it small no so again you will shift your J then you compare 6 with two then is it smaller no then you shift your J here then you check is it smaller yes what if it is smaller then what you will do in this case if it is smaller you perform some operation first thing you do is you increment the value of I basically what you're doing is you are shifting your IE here okay that's done next we have to make sure that you swap the value of I and J okay so whatever value you have with I and J you basically swap them so what I'm saying is take this value here move this value five here and move this one here okay now how do we do that of course we have used it before so we can use a temp variable or maybe at this point I will simply say Swap and then we'll write the logic later so I want to swap basically the value of a r r of I with a RR of J okay so we need to do the swapping that's it this are the thing we are doing inside the if condition now once that is done of course you have to move your J again because we are in a loop so J will go here now again you will compare if the four is less than two no and that's it J will move here because if it is not less then we not doing any operation then your J goes to the last value because your J ends at high right so J reaches here and of course the pi and J is same then you don't you don't have to I mean of course it is false the condition this condition will be false which is a r out of J is not less than PT so in this case it will not swap but what happens after that even if you complete the entire Loop do you think two is at the right position no two should be here right so this three need to go there so what you do is you perform one more operation after the loop so after the loop you what you will do is you will basically swap the a RR of I + 1 with the ARR of high okay now this is the operation you have to perform now what will be happening here so what is the value of I value of I is z which is index you're picking the next value which is three so you have to swap this three with this two because that's what your ARR of high is so you will move here and you will move this side now by doing this we make sure that the pivot value which you choosed is at the right position can you confirm is it the right position yes it's at right position now what you do is you divide I mean if you if you observe the array if you look at the pi which is pit here now if you see the left hand side of it this is always lower than two and this value is always greater than the two so yes two is the right position now what's the next step the next step is very simple you divide this array based on the PIV value so what you do is you create another array here and in this you'll be having only one value which is coming from here right in fact we can also draw an arrow there so this is coming here two will remain there itself we can write two here we don't have to work with two because it's already sorted and then you take this particular section into another array or not another array but let's say logical partition we are doing here so we'll get another array here so we are doing divide and conquer so here you're going to have six 5 4 3 If You observe they're not sorted but at least we got the partition right now what you do is you again perform the same operation which we have done here in the first example now you help me should we start with this yes we can start but don't you think we only have one value and whenever you have one value it is already sorted let's work with this so what you do is you again take the same thing so you take the low you take the high and then you of course you again have this P all this values here so all these variables are still exit because you are going to apply quick sort again on this particular values so let's get started let's take pivot which pivot we are choosing of course we are choosing always the last value so this is your Pi I pivot then you got I here you got J here then what you do is you start running your algorithm which is first you compare the value of J which is six in this case is six smaller than three look at the algorithm which we have done is six smaller than the pivot the answer is no so you will simply so you will simply move your J here again check is it uh small no shift is it small no is it then again goes here is it small no then what you do is you have not you have not done any swapping but at the end we got one more logic right so once you complete this for Loop you will do something with this which is swapping the I + 1 so I is at minus one so you have to basically swap the six with the array of high which is three so this goes here and this goes here now if you look at the pivot this is sorted right so now what you do is you write three here which is your pivot value and then you divide your array into two parts but since the pivot was the first value what you will do do is you will get the new array now from this which is 5 4 6 and then you start performing operation here now what operation the same operation which we have done again you will apply quick sort so this is L this is H so Pi is refering six here and then you start so you got I here you got J then you start comparing is the first value the the value of J is it smaller than the PT yes now when you say yes what you will simply do is you will increment the value of I first if you look at the code that's that's what the logic we have and then you swap the value of I and J now in this case I and J are referring to the same value so swapping doesn't make sense of course they will get the same value then you increment the value of J J goes here check is 4 less than 6 yes in that case you will move I here and then you will swap the value of I and J again it remains same so nothing to do here then you shift your J here of course the array of J is not less than P because they the same value then this Loops end the for Loop ends then you perform this operation which is a r r which is + one this will be replaced by high and that remains same right so there's no swapping I know it all depend upon the value type of values you're getting so at this point there's no swapping happened so this remain as it is six but then you will get a new array here which only has five and four and then you start up applying the same operation let's get started in fact I will just write this bit up so that I will have some space and this is coming from this particular section then what you do is you will take the same value you say and if You observe we are doing recursion right the same steps we are repeating multiple times so LH we got Pi I here we got the variable I referring to minus one position or in fact notus one position it is one less than the low so in this case the low is this position right now one what is this position this is in fact we should have maintained the index value right so this is zero this is 1 this is 2 3 4 5 then we got three here which is this one this is 3 4 5 what we got here is 3 4 so these are the index values for them okay so we got I referring to the index value two and J is here then what you do is you simply compare the same steps we have which we have done so the array of J is it less than four no so all this operation this swapping is not needed you will simply move your J here again do we need to swap of course not because the array of J is not less than pivot so it will remain same but then you will perform the last operation which is this one what it says it says Swap this value with the high value so this will be swapped so five goes here this goes here and this goes here now if You observe we divided and we conquered so if you merge this in fact when not merging physically we are merging logically right because nowh we we were breaking the array but if you see if you try to merge logically what you will get so you will get this one first which is one goes here then two goes here then by sequence three goes here then by sequence this 4 five goes here and then this six goes at the end so if I want to write this this will go here so let me write one then this goes here that's two then three goes goes here that's three then if you can see in the binary tree we always goes from the left to right so if you see the left because six is there on the right hand side you will say four and five and at the end you will get six now this is sorted so after all this explanation this is how basically you sort them now this logic which we have written here is actually a part of a partition method so this is a partition method so the entire thing goes there but we also need to return the p right so which is the private value so we return I + 1 because that's what the pivate is but this particular function will be called by whom so we have a concept of quick sort here so quick sort is going to call this partition how that we're going to see in the code itself so we got the idea basically you divide sort each values and you will get the output now this will make much more sense once you start coding and I hope it will get clear so what I will do is I will just remove the entire part here we got this aray and we are going to perform the operation now of course you can write the entire logic here but then remember quick sort goes for recursion So when you say recursion you have to create a function or a method which will get called so what I will do is I will create a function here and let's call them so we'll say a quick sort as a function name or the method name which will take three parameters now what are the three parameters the first one is the array itself on which you want to perform the operation next is the starting point and the ending point of each section example you start it will be the entire array but remember when we have to break it down so that is your each section okay so we have to specify the low and then you have to specify the high now in this case what is low so low is zero and the high is basically the array do length minus one right that's the value you have to send now basically we don't have this function so we can just right click here and say okay I want the IDE to create this method for me May is using some in methods okay let's create our own so what I will do here is I will create a method called public I don't want to create the object I will say static void quick sort and it will take these three values so first it will take a int array in fact one of the syntax of array you can write it on this side because it will make much more sense you got array and then you got int low then you got int high so these are the variables you need and then you can start perform the operation so basically from the main you're calling quick sort only once but quick sort will be calling itself okay but based on what now basically I want quick start to call itself only till when your low is less than high we don't want it right so that's what it makes sense right low should be low high should be high and if it is true then keep calling the quick sort so I I will keep calling the quick sort but the question is what values have to pass now think about this if you have one big array and then you are saying that we'll be breaking this into two parts so don't you think you have to call quick sort two times for two different arrays that's right so you have to call two times but the question is what are the values for sure the first variable will be array because that's what we are sending right we have to sort the array the question is what will be the low what will be the high because when you divide your array into two parts what should be the starting point of the second array and the ending point of the second array that's what you have to mentioned but how will you know the starting point it is easy actually here the starting is always low for the first array and the ending is always high for the second array okay even that is solved the problem is with the ending of the first array and the starting of the second array that's what we need to find now how do you find this for this we have to run a partition okay so this is the logic which we have to refer to so what I will do is I will just reduce the I size so that I can see those values I mean this logic so this is what we have to implement in the partition so basically to get this value the pivot value what you will do is you will say in pi equal to and you will create a method called Partition in which you will pass three values the same three values array low and high so basically in the logic I have not mentioned it but we want this value as well okay okay so we need to create this method so what I will do is I will just go back here more actions create a method okay so we got the method let's perform operation on this method we can make it private because we're not going to use partition outside this class so private works but what should be the logic here and we have to return something remember we have to return the pi value okay so we can write the same logic which we have return return here and once you get this Pi you can basically pass the pi and we don't want to include the pi so we can say Pi minus one and here we have to pass Pi + one because every partition when you create you don't consider the PI right Pi stays there you create two parts and then those are your two partitions but we have to find this Pi that's the main task and for that we have to write this logic which is mentioned here so we need some variables the first one is temporary pivot variable and we have to assume pivot for the first time so the pivot here is always high so that's the algorithm we are using but we can pick up the starting value or randomly any pick any value that works so let's say we got pivot high and then we also need one more variable which is I remember we have to use this I which will so I is basically your low minus one remember if the if the array starts with zero the I will be at minus one next we need a loop so we can get the entire loop from here or maybe we can type so we can say for INT J is equal to zero in fact not zero right it should be always low because when you create different partitions not every partition will start with zero but they will start with low so J less than equal to high and j++ I'm just referring to my algorithm which I mentioned here and then the logic we have to write is if a r r of J if it is less than pivot then you start doing some operation what operation first you increment the value of I and then you swap but how will you swap we don't have a swap function actually we can create one but instead of that what we can also do is we can write the logic so I can say int temp is equal to a r r of I then a r r of I will be replaced by a RR of J and then a RR of J will be replaced by the temp okay that's the values we have to swap and that's it this will run and you will do the partitioning part but once you complete the for loop again we have to swap the value of I + 1 with high so you can actually use the same logic here or same statements so I'm not copy pasting I'm just reusing the code sounds better right so here we can say this will be replaced by I + 1 and I +1 will replaced by high and this high is going to replace by temp spping done and at the end we need to return I + 1 because that's what is referring to the pivot okay uh looks like a right logic we just converted that into here so by doing the partition we are saying replace one in fact once you get this algorithm also try the same thing which I've done on the board okay then it will make much more sense okay I hope this will work what do you think let's run and let's see run and yes looks like sorted 1 2 3 4 5 6 8 let's change some values let's say this is 81 this is 62 and this is 11 one one let's try with this values and let's see if they sorted so 2 3 4 5 62 81111 so it works so those this is your quick sort now why we are saying this is n log of n is because we only have one Loop here which is for Loop plus we are doing the partitions of it right so they can run parall yeah so this is n log n but in the worst case it can go for n squ but better than the other sorting techniques so yeah that's it from quick sort we have written the code in Java in this video we'll talk about merge sort now basically let's talk about the theory first and then we'll focus on the implementation now when we talk about sorting techniques we have worked with multiple sorting technique and when we talked about the quick sort which was going for the time complexity of n log n now we have one more there which is called a merge sort which also has a Time complexity of n log n now both quick sort and merge sort follows a technique called divide and conquer so basically what you do is you break down the problem into sub problems and apply the algorith when I say important I'm also I'm also saying it is complex part the easier part is to break down this into small parts merging will be timec consuming so let's understand this so when you have this values here what you will do is first of all you will break down into two parts now the question is how will you break it down in different sorting algorithm we have a different way of breaking the entire list in fact in quick sort we use a pivot but here let's make it very simple we use something called a median now what is a median or maybe a midpoint so here what we can do is as we know that in if you talk about this particular array here the mid is here right we can see and we can tell that but how an algorithm will know that this is a mid and that's why we will do some calculation now what kind of calculation it's very easy so what we can do is let's say this is your left and this is your right so what we will do is we'll simply use index values so this the index values for this is 0 1 2 3 4 5 so that depends upon the values you have and then if you want to find a median so basically L represents the first index value and R represents the last index value so you have to find a median and the formula for median is very simple so what you will simply do is you will say median is equal to left plus right divide by two that's how you get the median right now in this case the median is what is l l is zero R is 5 and this ID by 2 is equal to 5 by 2 now since in different languages we get different different outputs so let's say this is two in this case uh because in most of the languages we go for the floor value so we got two here now two is your median so this is your median so whatever is there on this side will go into a different array and whatever is there in this side will go into different array so when I say array I'm talking about the list so what we can do is we can divide into two parts and then we can create a different section so we got a section here which has three values we got a section here which get three values and then the values will be 8 5 9 here and 1 6 7 here okay and again the job is not done again you have to divide into two parts now how we going to do that it's very simple the same steps which you have followed again you will take L and R here L and R here let's find the median of the first point here or the first array now how do you get the median again you will say m is equal to l + r / 2 so now we can do it quickly so what is l in this case so if you go with the index value index value is 0 1 2 in this case and 3 4 5 now if you talk about the first array the L is 0er R is 2 so you will say 0 + 2 is 2 and 2/ by 2 is 1 so M for this is one so m is here now if you want to calculate for the next array or the next values so again the same value so what is l l is three R is 5 so 3 + 5 is 8 8 / 2 is 4 so four is here here so median is here of course when you look at the values you will find the median just by looking at it but let's talk about the algorithm and we don't have a limited set of values what if you have 500 values you will not simply search for the median right so that's how we can get the median again you will divide into two parts so we can divide something like this you will get two sections here for this and two sections for this we can say the first section will have two values which is 8 and five the second section will have a value which is nine here the first section since we only have uh one value here nine we got nine but let's say here we got one and six and here we got seven so basically that's how you divide again the job is not done so you have to go till the end so what you're going to do again you you will divide the first one so can we divide the second one the nine we don't we don't need to divide this part right because when you get one value you can you cannot divide that so let's divide the first part here so if you divide this you will get again two values which is 8 and five and again you will divide this you will get two values which is 1 1 and six so that's how you can divide and at the end you will get the individual values now still we can persist the index value because we are not physically dividing them we are just virtually dividing it right so index value for this is 0 1 2 3 4 five the index number remains same and if you compare with the actual list we got the index value and the values are same right so till this point we have not done any sorting part we just did The Divide part and now it's time for the the conquer now when you say conquer it simply means that you need to sort this value so whatever value you got at the end sort them but if You observe they are already sorted because each element if you compare if you say eight this list is sorted when you have only one value It Is by default sorted okay so sorting is done so conquering is done basically the next step is actually is to merge them and the actual fund starts here now when you when you say merge what it means so basically what you do is you start with the first two values and then you merge them now merging is important so when you say we are merging 8 and five of course you will get a least and you can again say eight and five but no this is not how you will merge it so the way you will merge it is this so you will take these two values and merge them into another block you will get two values but when you are merging it you will compare these two values so which is less here so if five is less you will put five here and then you'll put eight here because that's the only remaining element then you you think about merging so you will you will merge this two now when you merging this two of course you will get three values right so you will take it take three values here and this is coming from here so these two values and this one value now how will you merge now since we have two different list or two different values here or two different ARS we can say when you want to merge that's where the actual algorithm starts because when you when you merging two values you can simply compare smaller or get greater but now we have three values in first we have two in second we have only one so what you do here is you basically compare these values before adding them so what should be the first value now we know that the previous array is sorted right and then in fact both the values are sorted both the arrays are sorted when you combine them what you can do is you can compare the first two values only the first two values not eight only five and nine so which is smaller five so you will put five here now since five is done you can simply say okay I'm done with five now let me focus on8 and nine so which is smaller eight and then the only thing remaining here is N9 so we'll put nine here now that's done now let's combine this three which is 1 6 and 7 now first you have to com combine this two so you will get another box here which is 1 and six so we will compare so it is one and six because they are sorted so now you'll combine the next part you will get three values here now when you get three values what we are doing is we are basically combining this section and this section same steps you will compare the first two values so one compared with seven so one will come here this is done then compare six and seven six will come here this is done then seven will come here okay and then now the major work starts we have to combine this two now when you're combining this two you will get a bigger array of six elements so 1 2 3 4 5 6 so we got six box now what you will do is you you have you got two arrays and we know that those two arrays are actually sorted when you combine them again you will start with the first two values so when I say first two values let's let's compare five and one so what you will do is you will compare okay five and one which will come here so it is one so this is done then you compare six and five because five is still remaining right so five and six it is five then 8 and six which is six so five done six done then 8 and seven so which is 7 so seven done then eight again now we don't have anything comp to compare from the next array so you can simply take these two values as it is here this is merge sort okay so you get the sorted value I hope yeah you got the sorted value so this is how basically the merge sort works that's how you basically divide and you combine but if you want to do this in programming how will you do it and we'll keep this as a reference so we can divide we can write the section here so we can compare with this now if you want to write an algorithm for this for first of all uh you will get an array of course now in this array you will have some values and we can take the same values which we have here right so let's say we have an array with some values here the same values which we have and then the next step is you basically need to create a function let's say merge sort and this is where you will do this sorting part so when I say merge sort the first thing you will do is you will say divide now when you say divide you have to pass two three things first you have to pass the array then you have to pass the L value r value right and then if You observe we are dividing again you're dividing again you're dividing so don't you think this is a concept of recursion when you do the same thing multiple times so we have to do recursion here so basically here we have to pass three values right so you will pass an array you will pass the left value and right value for each array now as this will be called multiple times in recursion every time you have to pass the array the left and the right remember one thing we are not dividing the array physically we are dividing it logically so even if you divide this array ultimately the values will remain same okay now when I say divide what it means the first thing you will do is you have to find the median or the mid value so I can say mid but then since this okay so let's find mid here so we got mid is equal to we know the formula r l + r / 2 now once we got the median what we're going to do is once we got the median you will break down into two parts and you will apply the merge sort in each section so what I'm saying is when when I'm pass when I'm calling this for the first time I'm passing the entire array with the L value as zero and R value as five which is the last index but again I'm going to call the merge sort by passing different values so I'm going to pass a RR next I need to pass the first array uh index value so the first array here starts from 0 to two which is the median so starting is L ending is median or M middle value or mid value we can say again I will call merge sort for this second array again I have to pass ARR but this time you have to pass if you look at the values we have to pass three and five now where do we have three we don't have three anywhere so we can say mid plus one so mid is two so mid + 1 is three and the ending is right okay so that's how you call the merge sort we have to remember one one more thing after doing merge sort which will break it we have to merge them as well so when you say merge you have to pass four things here first the are the left value the mid value and you have to also pass the right value again we'll see the logic later but this is the merge you have to pass but if you call the function by itself you can you can see merge start is calling itself but don't you think there should be a limit when it should end so of course there should be a condition as well when your left is less than right then only you do this otherwise stop okay this is your merge sort so by doing this what you will get is you will get the values divided but where is the concept of merge we are calling merge but we are not defined the merge function yet now this is where things gets complicated not exactly complicated but it's a lengthy process so what we need to do is when you say merge of course you have to pass all these values here now in this merge what we are going to do now basically the first thing here is you need to create two arrays I will not write the logic exact logic here but let me just draw something what we need is we we basically need two arrays here when you say merge at one time you need two arrays example so when we are combining this two we got one array so at one point you need two arrays to combine so that you will get one big array same goes here when you got two arrays here we need to combine them to get a big array so that means every time I call merge I need two arrays we'll call them let's say left array and right array okay so we got two arrays here the next thing we need to do is we need to copy the elements of the left array and the right array this is empty at this point so we need to copy these values from the actual array okay now so we need to write a for Loop which will copy data from the array so basically it will copy data from the array and put them in this list okay so maybe I just I will just write copy values again we'll see the actual logic in the code itself okay next thing we have to do is once we have done with the copying part you will simply use these two arrays to combine to get a bigger array now how will you do this so of course there should be partition here there should be some values so what we will is let's compare let's work with the last values here so we got the last value as 5 8 9 and 1 6 7 so the logic of combining this is you will need multiple variables here you need a variable I you need a variable J now why we need this two variables one to represents this particular array and one to represent this particular array to it trate then we also need a k which will use which will be used for the big array which you're going to to create so out of from this three values or three three values you're going to create an array of six right so K will be handling this part now every time you copy one element so let's say when you compare five and one and you know that you're going to copy one here so basically the J value will shift here right because this is done the same thing goes here so once you got one next time you will get five here then you have to increment this here so I goes here and of course every time you copy element in the bigger array you will increment the value of K does a logic okay so this is how you are going to complete the merge sort now how exactly you have to write the code that we'll see in the Practical video so I hope merge sort makes sense where you basically break down the entire array into small chunks and then you combine them to get the sorted values so now let's implement this logic with the help of java now the thing is first we need a array right so let's create an array here so I will say int AR RR and this will have certain values so let's have some values here we'll say 3 5 1 4 6 2 so we got this values here and let's apply the Sorting techniques on this so before sorting I will print some values I'll say for in n in Array and let's print this values so I will say print n but I will also make sure that it will put a space I will not print on new line and once it is done I'll print on print a new line line and then after sorting also so I will set this I will just write it here after sorting so here let's let's do the same thing and let's say after sorting it should print some value but at this point if you run this code let's run this and you can see the output they both are unsorted so because that's why we have to do some sorting here right so let's apply the Sorting logic so what I will do is I will simply call a merge sort method which will basically accept three values the ARR the L now L here is zero and then you have to also pass the array do length minus one basically you have to pass the R value as well remember we have to pass three variables now since we don't have this method I will simply say more action create this method and you can see we got this method here and let me just put this method not here but on top now this is where the actual logic is going to happen so you can see we got two variables this should be L and this should be R so left and right okay now as for the logic if you can see in our logic we have already mentioned this logic so basically what we have to do is we have to first check if the L value is less than R then only you do certain things and what are things you have to do basically you have to find the middle value so I will say int mid is equal to l + r / 2 and then you have to call much sort two times again I'm just going with the logic which I written in the code or in the board here so you have to pass ARR and then you have to pass pass the L and then you have to pass mid that's how you create two different arrays so mid + one and R okay and then you will simply say merge of course right after doing the Sorting after breaking it down you have to also merge but while doing merge you have to pass three values ARR uh you have to pass the left mid and right you have to pass the three values so this is the only logic you have to write inside merge sort method but then we have to also complete merging because dividing is very easy this is how basically you are dividing the values the difficult part is merging them so let's Implement merge here so I will just go back here more actions uh create a method merge yes so I got a method at this point there are private methods is because I'm going to call from the same class but depending upon your requirement you can just change them and now the actual logic goes here the merge okay now as I mentioned you need to create two arrays here right so if you can see the logic we need two arrays let's name this as lrr which is the left array and this will be an array but the question is what will be size of it because when I create this are I have to mention the size I don't know what is the size here now based on when it is getting called it will be different size but the question is how we are going to know the size and that's why this l m all this thing comes or l r comes into picture so let's use that and let's get the size so if you want the size of the the first array the first section basically you can simply say mid minus L because for the first array it starts with L if you can see here it starts with L and ends with mid so if you say mid minus L you will get the length right but the problem is since this is zero indexing we have to also add plus one because mid is not representing the size of the array it represents the index value so you have to say plus one then you will get the second AR length with the help of R minus made so I'll just put some space here so that it will be much more visible okay so you can see we got these two values and this is the size you have to mention here the first array will be of size N1 the second array will be of size N2 that's done the next step is actually to copy the values if you can see we are mentioning the copy values here right in the in the noes now how do you copy value we can use a for Loop and let's use two different variables I will say X and Y this time x is equal to zero and depending on the size of the array so so for the first array the size is N1 and x++ not used to using X and Y inside a for Loop but let's try so how do you copy so a r r this values which is X now how will you get the values so from a r r from the main array we have to start from the left so you have to say left plus X that's how you get the values so whatever value of the L is example if you look at here uh for the first array the index is 012 but let's say if you're merging the second array this one the index value is 3 4 5 right so whatever value of L is which is the left which is three in this case it is 3 + x that's how you will copy and thenone so this is for the first one let's do the same thing but your different uh values so I will just copy this for the second array okay we can also use x here no problem we can say N2 and this will go into the right array but the question is what will be the value here now any guess so when you say on the right hand side it is your mid + 1 because we are not considering the mid value for the next example if you go up the mid here was two but we started with three so you have to say 2 + 1 mid + one so that you will get three okay so we copied what the next step and now the actual work starts of merging and already mentioned we need two variables one is I equal to Zer uh we also need J for the second array so first aray will be counted with I and second array will be handling will be handled by J but we also need one more counter which will represents the main array right and this value will start with L because we have to start from the left hand side now once we get these three values let's start merging them so we have to do multiple times right when you say merge you have to take my value from here example when we were doing this we would compare the first two values compare the next two values that's how we were doing right so when to end this if your I value is less than N1 and not this and and J value is less than N2 we'll we'll continue till this point if they are not true then we'll have to stop because that means we have to we have actually reached the end but now question arise how will you merge and that's where you have to start comparing values now when you say compare what it means compare five and one so basically the first element of this array and the first element of this array let's compare them how will you do that so let's simply check if the L array of I because that's what is representing the L array which is the I variable if it is less than and we can also say equal to what if they're equal then we have to compare this with the r array of J if the left one is smaller in this case the R1 is smaller not the left one but let's say if the left left one is smaller in that case you will put that into the main array so array K is equal to l array not length L of I now since we have used so let's say this was a smaller value and once we have used it we have to cancel this right I mean this is done so we can shift our pointer to the next next element how do you shift your not the pointer but the reference you will simply say i++ here in this case but what if it is not same or not is what if it is not true in that case you will go to else part you will do the same thing but not with the left array you will this time you will do with the right array and then the J variable and you will say j++ and every time you run this Loop because see after one of this Loop basically you will get the first value of the array right main array you will simply say k++ because you have to also increment this so once I is done because K is here once once I is done you have to move K here okay and that's why you're doing k++ and that's it this is your merge sort let's see if that works let's refresh this or this rerun and we got an error something went wrong you can see everything was going well but this four is not at the right place we got one there because sometime what may happen is when you copying two arrays what if the values are left in one of the array example if you look at our example when we were copying this remember when we were completing the entire task these two element were remaining from the first array which goes at the end so that thing is not covered yet so what you do is for the remaining elements of the particular array you will again run a while loop here so you will say if I is still not equal or still not less than N1 you will simply copy the remaining elements what is left because after this loop at least one array will be ended okay so we have to work for the second of the remaining element of one of the array maybe left hand side or right hand side first we're trying to understand left hand side let's see what happens on the left hand side simply copy all the values so you will say AR of K is equal to we have to copy from the left array and let's use I here now every time you do this you you will increment both the elements I and K both so this is for the left hand side left out left out values the same thing should be done for for the right hand side so this time I will say J is less than N2 in that case you will copy but not from the right left hand side you will do it from the right hand side the variable is J and the increment you'll do for j++ okay and let's run this and you can see now it is sorted and now it doesn't matter what values you have let's say let's take the values which we have returned on the board which was 8 what was the value 85 9 1 6 7 and if you run this you can see we got sorted values let's add some more values here just to remove the confusion with different values let's say 111 this is 57 and now let's see 6 7 8 9 57 75111 so this is your merge sort so the steps basically you have to first do the breakdown of you have to break the array down into small chunks and then you have to also merge them the only thing you have to remember is when we were drawing this we were going in two sections right something like this we were saying okay we will create two different section and then first section will break into two parts and second section will break into two parts no that's not how it is working If You observe what it will do is it will break down the entire section into two parts yes but then first only you will get the first part then you you're not going to the second part okay you're not looking at here this is not there yet you're still into the first part then this will divide into a section you will get this 85 then you're dividing into two sections which you will get 8 five and then you will merge them then you will start break breaking down the second part which is nine okay nine was still not there then once this is also merched once you get this it will start working on the next part then it will work on this it will work on this this merge and merge I mean sorry break down then you get seven then you merge this two and then once you have merged then it will go for the next two so it's not exactly breaking down the entire section equally it's basically going from left to right so yeah that's how things are working out and this is your merge sort with the help of java code and I hope this makes sense let me know in the comment section and in this video we'll talk about link list in the last video we have talked about arrays and what are the drawbacks in Array now one of the drawback which we have seen is once you define the size of an array you cannot expand it or you can you cannot even shrink it but in case of Link list we can do that now in link list what we can do is we can create a collection of elements and every element will be linked with each other okay so let's let's make it simple now so what I will do is let's take an example here I will take four values let's say I want I want to take values like 12 the next value is let's say six and then we have 8 and then we have three so I want to store this four values so I want to say 12 6 8 and three I want to store it as a collection not individual because if you want to work with individual values you could have created three four variables and you can assign those values but if you want to keep it as one collection in normal way we can create an array but the only problem is you cannot expand it so in link list what we do is we use a the concept of a data and a reference now what it means now first of all to store these values I will not go for any any continuous memory what I will do is I will create this four four boxes so the first box is will have a value 12 the second box maybe anywhere so you have a sequential memory right you can have your data anywhere but just to make it simple you know just to represent in a format I'm doing in sway so we have one more value here which is six and then let me just draw one more here which which is 8 and the last one which is three so we have we have these four values we have 12 6 8 and 3 now what I want to do is I want to say this is a collection but then it is possible that they are not in a continuous location they might be some elements in between so let's say we have 12 here after that we have some element here maybe a b uh then you'll be having some some more more than maybe five to six elements in between maybe 15 20 elements in between so they're not continuous right so when I say have these elements they're not in sequence example if I want to say hey I want to fetch the third value how can you mention third value because you you don't have in sequence if you want to make them in sequence what you have to do is first of all you have to make the first element as a head so you have to make this as a head so once you have made this head that means this is a first element so that is simple but how about the second element how you do how do you link the first one and the second one now to make it work we will call each element here here as nodes so the elements which we are using here are nodes so this is a node this is also a node the eight is also a node and three is also a node and of course all these are elements so node value is 12 node value is six node value is 8 and node value is three or you can call them as value you can call them as info now how do you link this 12 and six the way you link is by creating one more memory here or one more box now this node will have two things the first one is the info so this 12 here is info and the next one will be the address of the next node now every node will have an address of course right so let's say the address for this node is 512 that's the address for this node the address for this node is let's say 326 the address for this node is let's say 101 and the address for this node is let's say 202 so we have all this address here now in this box this will have the address of the next node so in our example six is our next no right if you want to have eight as a next node you can write the address of eight here but in this case six is our next node as you can see the sequence here I want to maintain the sequence you can say 3 2 6 so this is the address of the next node that means we have a line between this so we have a line between the first node and the second node so this node one this is node two node three and node four now as you guessed it right in six you'll be having the address of three address of eight which is 101 so this is 101 here and this one will have two 02 so we have a reference so this is a link and even this is a link and that's quite simple right but the problem is what about the last element now of course we don't have any element after that even that even there we will be having an address but then address of what so it will be null now since we don't have anything after that it is null so we have the first node here which is 12 this is the first node second node third node and fourth node and fourth node will be null because we don't have anything after that this is how you create a link list so we have a list of values and they all are linked and that's why they say linked because they all are linked and a least because they this it's a least so once we have all these values assigned this is a least now in if you remember in the start of the session we thought we said this is expandable so when I say it is expandable how do I start the first one I mean how do I add the element anywhere so maybe I will start by adding the element at the end maybe I want to add the element here after three so let's say in the Le Le we have one more value here let's say seven and how do we add seven at the end here so what you can do is as this this very simple step just create node here whatever node you want and the the value for this node will be seven so this is the value right what about the address now this is as this is the last one you can simply put here null so you can say this is null because this is the last one but then we have mentioned three as null right but then now three is not the last element the last element is seven so after three also we have seven and this seven will also have an address uh let's say the address for this one is uh 1 2 6 this is the address if you want to link this you have to mention after three we have seven so what you do is you have to change this value so now you cannot have null here now you have to make it 1 2 6 after three we have seven right so you have to mention the address here and then we can we can give an arrow so this is the seven is the last element here after three we have seven that's how you can simply add the value is that simple uh can we add the value at the start yeah it's quite possible uh the only thing you have to do is again create a node so we got a next node here and let's say the value for so after before 12 as well I want to assign a value five I want to have a value five here so what you can do is you can make the value five here and how do I refer so now there there's one more change you cannot refer the 12 as the head element now so 12 is no more the head element now the head element is five and of course this will have an address so let's say five is at address uh 306 that's the address now what you will do is in five after five the element is 12 right so we have to mention the address of 12 here which is one 51 2 so 51 so after five we have 12 so that means we have an arrow so five is the first element now because that is head and then after that we have 12 after that we have six after that we have eight so that's how we can add the value at the start now can we add at the uh the value in the in somewhere in middle and the answer is yes it's quite possible what you can do is you can create one more box here let me make a box and that box will have a value so what I want to add so between six and 8 I want to add let's say nine so I want to add nine between 6 and 8 so you can create a nine you can have a nine value here and then it will have it own address let's say 56 I hope that add will not C here so we have 506 here I know if you can observe I'm giving random values or random addresses because all these values will be stored somewhere else you you don't even know right they're not in sequence I mean you will know about the address but then they're not in sequence so if you don't have this links it will you cannot fetch them in sequence okay so now in nine so we have to mention nine comes before 8 so nine will have an address of 8 which is 1 0 1 so now nine is linking to8 that means we have to remove I mean we have to of course remove this link but then if you remove this link what should I put here so six so inside six will be not so the address will not be of eight now it will be of of nine so we have to say 5 06 and we have to give an arrow so of course we are not using this Arrow anymore so after six we have nine after nine we have eight so that's how you can add values in between so it's that comfortable because we don't we are not consuming data in a continuous location so we can create as many notes as possible provided you have enough memory in your system it will create number of notes and all these noes will be connected with a link list now this typ of Link list which we have created here is called as a singly link list because it is single link list uh that means we we also have a dou link list or double link list yes we also have a double link list we'll see that in the in the in the one of the videos in future but now this is a single link list so this is better than add is right so before we have talked about arrays and now we are talking about link list so now you tell me which is better is it array or link list now of course uh both have their advantage and drawback but what is the advantage so the advantage of using link list is it is expandable you can increase the size of the list you can reduce the size of the list right so you can say the advantage here is expandable but the problem in link list is it is slow so when I say drawbacks uh it is slow in in when when you compare with array because in Array it works with index numbers now since everything is stored in a sequence it works with index numbers and you can fetch the value randomly so the benefit of array is you can fetch the value randomly right so that's the positivity of it so let's say this is Arrow positive value and the so linklist drawback is it's slow we have to also mention the drawback of added right in fact we have talked about in the in the previous video it's not expandable so yes linkl is slow because if you want to fetch a particular value let's say if you want to fetch eight you cannot directly go to eight you have to go from five so first you have to go to uh five then you can so you can go first you have to go to five then you have to go to 12 so they have to you have to follow that sequence and then you can reach to eight in fact we also have a concept of big Big O notation again we have not discussed that yet so we'll say Big O notation would be uh big big o n so if you want to search for the value you have to say Big O N again we'll talk about it later remember that link list to for search it uses big o n so it is timec consuming compared to link compared to AR in in terms of searching but in but if you want to fetch if you want to insert value in between link this is the best one now this can be implemented using any language which you like you can implement this with C you can Implement that with C++ you can Implement with Java or maybe python in this video we'll see the implementation of Link list in the earlier videos we have talked about why do we need link list and what is link list so let's try to implement that with the help of java language again you can Implement that concept with any language which you love in this video we'll focus on Java we have two sections on this screen we have uh on the left hand side we have Eclipse IDE again you can use any ID which you prefer you can use intellig you can use net beans I'm I will be using Eclipse here on the right hand side I have a paint so that if if I want to show you anything any diagrammatical representation of that whatever we are doing you can see on the right hand side so what exactly we need so just to make a quick recap what we have learned till now is when you talk about link list we have representation like this we have noes now these are our nodes right so let's create a first node here so this is our first node and let's say I want to store values like 5 uh 12 6 9 I don't know why I love these numbers and let's say we have eight so I want to store this five values that's it nothing fancy just five values in a link list so in link list what we do is we create individual nodes so all these things are called as nodes right so this is a node and then we'll create another node so this node will have a value which is five and then we'll be having another node here which is let's say value 12 and this this node will have some address and we'll be having one more node here let's say six so this is six uh we have one more node here which is nine again I'm doing it purposefully creating the objects anywhere and then let's say we have eight here so we have all these nodes here so this node which is of five will will be linked with 12 we we have to find a way to link these two nodes and then we have to find a way to link these two nodes and then and this node and this node this is what we are going to do so once we can link this node in future if you want to add some new elements you can do that very easily every node will have two things a value so this is your value and this is your address now this is an address of the next node now in C programming we can use pointers but in Java what we do is we create a reference of the next object Java is all about objects right so of course this node is also object so this is the first object so you can say this is the first object this is a second object so the first object will have a reference of the second object the second object will have a reference of the third object now again we don't number them but then every object will have a reference of some other object the last object here will have nothing it will have null right so we don't have any other object after that so we'll be having null maybe after some time we'll add one more node the reference of that node will be assigned here so let's try to implement this so what are things we need here so let's see what are things we need in Java Java perspective so of course we need we need to create objects right so before getting the object we need to we need to use a class so the first class we need is a node class because a node class will have two things a node class will have a data and a node class will have the next node so data will be of type int again you can have any data you want you can have a string data you can have any object maybe alien object maybe accounts object maybe a laptop object your your wish and then the next node will be of type node itself because this will refer to the next node a node class so this is a class which has two things we have a data and a node then what other the class we need we need a link list class right because link list class will have certain features because when you talk about link list it has certain methods example we are going to look at insert so we look at insert now insert will add data at the end so let's say if I specify data as five it will add that at the end of the at the Leist what if you want to add in between so you will say insert and you will say insert at ations and then oh we also want a show method right so show method will display all the elements so if you want to display all the elements you can do that so this is where this is what you'll be doing here so in total we need three classes here so this is the first class we want to work with this second class we want to work with and the third class so let's create these three classes in our Java code so let's do that so I will say right click and let me create a Java class here I will say this class name as node because the first thing which we are going to do is node now this node will have two things as we have discussed the first thing it will have is the index value I guess the font is not visible let me just do that so now I would say this node will have the first thing which is the uh int data and as I mentioned you can use any data you want you can go for in float uh double alien Type U data any any complex type if you want and the next thing is the node itself so the node object which is next so this node is a reference so this next is a reference for the next node quite simple right so we have public class node and we have these two things nothing fancy here we got just two things you can make it more efficient you can write you can make it this private private variables and you can use geta Setters but then just to keep it simple I'm doing a I'm doing this code the next thing we need is uh a class which is called as link list but what if okay let's create a main class first I will say the main class and the main class name will be Runner again you can have any name doesn't matter we have Runner class which has a main method now what if or maybe I don't want to create my own list I want to use the inbu feature so in fact in collection API you do have a link list as a class so you can say link list I will say list equal to uh New Link list in fact you can use the internal one as well or you can simply use this class and you can say least dot we have a add method so using add you can uh specify Which object you want to add maybe I want to say I want to add five so this same as the thing which we are going to do we are going to use insert yeah and then we'll be having list. add you can specify the index value where you want to add let's say I want to add the index at four and you can add the element as let's say 12 so the same thing which we are doing here maybe index value three which we're talking about and then you can also delete element so you can say least. delete okay we have remove so we have remove we can specify the index value the method which I'm going to use is delete and then do we have a show method we don't have a show method here but we have size okay we are we are missing size there we'll be doing that later and okay we don't have the show method here but then we have iterator using which you can fetch all the values in fact you can print the entire list list as it is that will work this is how you work with list but then I don't want to use the internal implementation I want to go for my own implementation so of course we need a class so I would say the class name is link list itself again don't get confused with the name which we are using and which is provided by Java I'm going for the same name but since they're in different package uh we can have our own class names now this class need to have certain methods right even before creating a method here what I'm going to do is every list will have one thing and that one thing is okay so how do you know that this is a first node now if you remember we use a concept of a head node so the first node will be referred as head node and where do we Define it so in the list itself I can say I have a node which is which I will call as head node so this will refer to the first node okay that's it so we got that node there let's let's implement the functions first of all this code is difficult to understand in the first go what I would say is watch the entire video and then uh try it by yourself so maybe you'll be watching this video two to three times so make sure that you also practice this that's where you will get this concept okay so let's let's start with the first method so the first method we want to go for is insert so let's do that so I would say public void insert that's the first meth I want to go for and this will accept a integer value I would say integer a data s so this is what I'm I'm expecting from the user now this data I want to assign to a node so whenever you call a insert what we have to do is we have to first create a node so what I will do is I will just clear this up so let's use this area here on this side so what we are going to do is every time you say insert you will be creating a new node so let's get a node here and this node will have a value which is which is whatever value you're passing so what we are going to do here is let's create a new node and then the way you create a new node is by saying node and let's name that node as node itself and we'll say new node this is how you create object right so we got a node there the moment you say node node equal to new node this is what you will be getting in your memory the next thing we have to mention is two values of it so we'll say node. data the data which is coming from the user so whatever user is passing let's say if user pass Five the node will get the value five just to make it more realistic what I will do is I will also create the object of list uh the link list here here in the main so I would say link list uh link list I would name this as list itself equal to New Link list and in this link list I I have only one method right which is which is insert this insert will take a value let's say let's say I'm passing five so this method will call five will it will pass Five and that five will be assigned to a node which means this node here it will be having a value which is five that's simple right and by default the value of this one will be null so we got our first node but then it is possible that this is our first node because what we are expecting is when you say insert it will add the data at the end so if you already have a list with you it will simply add data at the end but what if you don't have a data itself I mean you don't have a list itself this is your first node in that scenario you have to check because if if if this is your first node then the head would be head would be null right because by default the value for this head is null so if your if it is your first object this is the first object we are inserting so head is null so we'll verify if the head is equal to equal to null in this case I would say the node itself is a head so we'll say head equal to node so the first node itself is a head but what if that that is not a scenario what if you do this is not your first node we already have some nodes there in that case you will go for the else part so the first case we are checking is what if your head is null so then when you say head is null it means you don't have any nodes in the list so whatever node you are adding is a first node in that case we got the node by default the next value is null in fact you can if you want to make it more readable you can you can assign that to null even if you don't do that that's fine because by default the value for the object is null but then this looks more readable right so you can do that so if it is a first object that's become your head but what if this is not the first object in that case you will create a node and you will make it as head okay now why I'm doing this it's because let's imagine you already have a list so let's say if you have multiple nodes here uh we got uh we got six and in fact let's go back to the original uh original diagram now you can see if I'm adding a node in between or somewhere here so let's say I'm adding a new node which is which is let's say okay the the value which I'm passing here is not let's say not five it is it is let's say 18 I'm passing 18 now so we got a new node and of course this is not the first node which which we are adding so head is pointing to five so in this case what it will do is it will create it will assign the value here here which is 18 right we got 18 value here but then this is getting added at the end right but that means after8 what we have to do is we have to first check which is the last element how do I know which is the last element and that's where you have to start with the first location so from the first location you will you will travel from first location to the last location now that is tricky right how do we travel is it that easy um no it's not that easy but just so I understand so from the first one we'll go to head first and using head will jump to the next node so from the second node you will jump to the third node from the second third node you can jump to the fourth node and then fifth node and then but how do we stop where do we stop so we have to stop the moment you get null as the next value okay now this is this looks simple so to Traverse we have to use you have to use a node right a temporary node which will hold the data and that is let's say n here so we'll say while n do next since we have to Traverse between different nodes so we'll use a while loop so we will travel till you got a null value the moment you get a null value the value will stop so how do we travel so what we are doing is we are saying n. next equal to null so we have a temporary we have a temporary node here let's let's imagine I'm doing that node I'm creating that node here this is temporary node and I will call this node as n now this node will first refer to the Head node so it will refer to the Head node and then head node will check do we have a null value here no we don't have a null value we have the address of the second uh node in this case a reference of second node so it is not null okay so then we have to jump from here to here now how do we jump so to jump what we will do is we will change earlier this was referring to this node right now we have to refer to this node now how will you get this node it's very easy you will say n is equal to n. nextt so now you are jumping between nodes so if it is not null let's jump to Second node if this is not null let's jump to third node and using Y Loop you can do that now this will stop the moment you get null at the end now once you got the null what you will do is once you got this null here you have to change this value you have to change from null here so you this is not not null anymore so this will refer to the new node which we have created now how will you do that so to do that we have to say n. next is equal to the new node which you have created that means the value of this node because the current n value is referring to the this this node right again this is the tricky part in in Java you have to you have to work with objects so imagine when the loop gets over so at this location when the loop gets over you are here at this node yeah you have you here this node now you have what you do is you have to just change this null to the next node whichever you have so the next this is a new node right so this is a node in in our case so the value of node will be assigned to the current node. next which is this value so this will be replaced by the node reference so you don't have a null anymore and this is null by default now if you can see the code here we have a null value here right okay this looks cool now this is how you insert value can be verify now even if you can verify we don't have a way to print all the values right so I will I will insert the data and I I will also try to print it now how do we print it we can use a show method to print all the values so I will say public void show to print all the values and how do I print the values so we will use some methods to we will use some statements to print here now how do we print the values so same steps you have to travel from between all these nodes now how will you travel between all these nodes is you have to say node n again you can say node uh node node equal to head so initially you have to start with head and then we'll be using a while loop we have to travel right so when want to travel you will use a while loop so you will say n do uh node. next is no not equal to null this is how you travel and then so while traveling you will print the data of the node so you will say system.out.print Len and you will print the data of the node so you will say node. data that's it this is what you are going to do and every time you print it you just have to shift to the next node and the way you can shift to next node is by saying node equal to node. next this is how you shift now see this Loop the same Loop so same vile Loop the only thing is which the only thing which are doing here is we are printing it that's it okay I hope this this will work but let's try so what I will do is I will insert not one value I will insert three to four values so I will say least do insert and I will insert 45 as well and I will say least do insert I would say 12 okay let's add three values and at the end I will say least. show I hope this will work now so let's run this code right cck and say run as Java application and we got oh we got the output but with one mistake you are getting 18 and 45 that's perfect we are not getting 12 so what's wrong is it not getting added or is it not getting printed uh I think it's not getting printed it's because you are traversing till null right the moment you get null you are stopping it in this case we only have three nodes so in this case we only have three nodes let me just draw that here so we got the first node as uh 18 the second node as 45 and the third node as 12 so you got these three noes right so this note is referring to this note that means this is not null value so this is not null even this is not null but this okay this is referring to this object right so this is null so this here is null so it will stop as soon as you get null oh okay that's weird that means we are not printing this one but we are reaching to this point right so when you say it will print data it will not print data which has a object which is null in that case let's let's do it manually so I say system. print in and here I will say no dot node. daa so for the last element you have to print it outside the loop because it will not getting printed in the loop itself now let's run the loop run the code and you can see we got those three beautiful values so just just to make a quick recap what we have done we have created two methods here one is insert and one is show in insert what we are doing is we are creating a node because that's what we do every time you say insert it will create a new node and that node will have a value which is so whatever data you are passing will be assigned to that node now if you want me to go for a quick run through of this uh we can use a debugger so what we will do is we'll use a debugger here and let's apply a break point this is where I want to apply a break and then I will run this in debug mode I will say run as okay where's the debug mode it's here and let's jump to the locations so we'll say F5 and F5 will take to the next thing so what we are doing is the moment you got a link list uh so I will just draw it here so the moment you got a link list it will create the first element So when you say insert so insert what is it is doing is it is saying it will create a new for you so here it will create a new node okay we got the node there and then we are saying uh okay it will go to node class it will take those two values and then it will assign the data so data is 18 can you see that it's 18 and we got 18 yeah and the ID for this is 22 so let me write 22 somewhere so we got 22 so the value is 18 and the value ID is 22 and then it is saying null so the next one is null here and then uh it is saying if it is null uh is the head element null yes by default is null so head is null here right Head is by default null in this case this becomes your head so head is head will point to this location now and then it will go it will not go to L spot because if got executed it will jump back to the second value which is 45 so in this case it will try to insert 45 so the same step it will create a node for you if I jump into this uh you can see it is getting a new node and with those two beautiful values which is data and next it is assign data which is 45 so we got 45 here and then the next value is so this is null now when you say null it will jump to the next location is it is it head null no head is not null right because head is pointing to the first node here so if will not get executed it will execute the else part and that's that's working now it will drive us between the elements right so first it will jump to head and check if so it will Traverse the first element itself is null so it will directly jump to the location here and you will say the N the this this dot next so this value will be replaced by the reference of the next element which is 20 uh okay what is the ID for this the ID for this this not is 23 so this will refer 23 now and then it will jump back to the insert the third block and will we have we got the object here with the value 12 the same steps will happen now it will again go back it will create the object assign the data 12 and then this will be null so the text will be null here and then it will jump to next it will jump to head is it so head is null no head is pointing to the 18th location so it will not execute the if if block it will execute the else Block in else block it will Travers from the head block to the end block uh so is is the first block is null you can say uh no it's not null so it will execute it will say jump to the next location so now n is referring here now is it null yes it is null you can see that is null so the moment you get null it will execute it will say the then do next next is this one so this will be replaced by the ID 24 so this the ID for this object is 24 this will be 24 here and then you got a link it's that simple the end of the code and then when the moment you say show what we are doing in show is we are jumping from one location another location so while so it will start with the head location and it will check is it null no it's not null so let's print the value so it will print the value of it which is uh which is 18 so we got 18 there you can see the output console window here we got 18 and then when you say next uh you can see it is printing the next node now so it is referring to the node 23 is it yeah so now 23 and then it will print the value data of that which is 45 and now it so the moment I go back you can see the node ID is 24 now it is referring to 24 and it will print the data which is 12 so this is the beauty of Link list now you can add values anywhere again this is a lengthy video so what I will do is the remaining part we'll see in the next video where we'll talk about the other methods which we have discussed we'll talk about insert at insert uh at start delete and show so the next one will be faster because we have seen the difficult part so I hope you understood something about link list again you may want to rewatch this video again so let me know in the comment section if you it was helpful so now let's see how do we add element at the start so we have seen how to insert the element at the end now we'll see how to add at the element to the start so what we're going to do here is let's create another method and we'll name this method as public uh void insert and then we'll say insert at start okay so we just have to pass a value there so I would say the value would be int data that's the value we we are going to pass now let's try to add the value now how do we add the value so if you think about this same thing which we have drawn before let me just take it take you there now if you remember this image now I want to add a element the start so I would say a new element and this element will go at the start so let's say the element is maybe 25 so I want to add 25 and this will go at the start now now when you say start it simply means that you just need to change your head location from the current current object to a new object so this is the uh new head location and the value or the address for this one would be the earlier first element which is 18 so the ID is 22 so I want this 22 to be here so that it will refer to this element now so now the new element is at the start so the head will become the new element and it will have the address of the previous first element so let's see how do we Implement that so same steps we have to get a node so I can you know I can simply copy paste the code now uh what I can do is I can say okay the same thing will this this thing will remain same so I will copy and paste it here again we cannot say copy paste I can say code reuse so I would say paste here and once I got the value the only thing you have to do now is the node so we have to say head node is equal to node the current node okay so when you say uh when you say you have to say head equal to node so the the current node will become the head but even before doing that I just need to verif I just need to mention that the next node for this so this so the current node next node should be the first node and it is there in the head right so head is the first node initially so I would say before assign the node to head you will say node do next I want to say head so whatever value was having with the head will be the next for this one so let's say this node is maybe uh let's name it as n6 so this node will have an address of this node initially this node was head so we can simply say node. next is equal to head is that simple let's verify it is working just to verify that what I'm going to do is let's go back to Runner after printing after adding 12 let's say insert at start I will say insert at start and the element I want to insert is 25 so now 25 becomes the first element let's run this code and you can see we got 25 the start so this is simple now we can add the element as start itself at start what we are doing is we creating a node and just saying that the node next element would be the the previous head node and now head will be changed to the node so head will be pointing to the first the node which you have created just now that is how you insert at start the next method we are going to do is insert at any location so I would say insert at and then we have to specify two things the first thing you have to mention is the index value and then you'll specify the actual data now when you specify index value uh maybe I want to insert a data here so I would say list do insert and I would say insert at at position two now index number normally starts with zero and we'll say data is let's say 55 so I want to add 55 at location 2 so when I say location 2 it is zero it is zero location so this is is my zero object now and this is the first this is the second one and this is third one so when I say I want to insert this 55 at okay I want to add 55 object at index two which means here so we got a new object and this will be 55 and this will have some address so this object the second first object will refer to the new object and the new object will refer to this one so now this becomes three and this becomes four in fact we don't have to number that but then this is how you push you have to push this one to the next location so what you will do is you will just mention this node will change to this address and the new node which you have created will be will refer to the next object okay now how do we do that and we are why are we getting why we getting error there so I will go back to my link list I guess I've not saved it now how do we mention that so to achieve that what we'll do is we'll say node node equal to new node again the same steps in fact we could have copied it so we say paste yeah so this thing is sure it there with every insert now in this case we have to Traverse right because we have specified index value we have to Traverse from that location to the to the to the index value and to travel we have to again start with the node the head node so I will say n equal to head so remember whenever you want to Traverse you have to always say n node I mean node n equal to head now how do we Traverse now since we know the index value so whenever you know the value it's always better to start with a loop so with that Trace what we have to go to this point so if your index value is two you have to reach till one so if index value is two you have to reach till one because in one you have to make the changes right so if I send X value two you have to make the changes here I guess this thing is not properly visible let me just rewrite again as I'm very bad with writing andw writing so just let me just do that once again so I got the first object here so we got the first object which is 18 and which is referring to the next object which is 45 which is referring to the next object let's say this is uh 12 and uh oh we before that we got 25 right because 25 is the first element now so 25 referring to 18 18 referring to 45 45 referring to this one now when I say you want to count when I say index value two it means this is zero I have to start with zero this is one and this is two and this is three now when you say two that means you have to get a new node here somewhere here and let's say the value is 55 so you have to reach till this node first in this node you have to change the location from here to from this Arrow to this arrow and from here you have to refer this one so first you have to reach till this point now since the index value is two you have to reach till one how do we reach till one is by saying let me just go back there so we'll use a for Loop and in here I would say int I equal to0 and I would say I less than equal to index minus one and I would say I ++ in fact not minus one we say index now every time you Traverse you have to say n equal to n do next that's how you Traverse right okay now there's one change when you say next that means we have to say minus one because we have to reach here this node by referring next because next itself is going for the next node so you have to say minus one you have to come one back once you reach till this point you just have to you just need to change the address right you have to change the address of this node with the new node but even before doing that I will change the address of this node to the this node so this node the new node which you have created that is node so this in this current situation this is node and this is n i want want to have the value of node next to the this value now to achieve that what we will do here is we'll say node. next is equal to n. next so n is this one so whatever value so whatever value is there here will go into this node right because this value is nothing but the pointer or the reference to the next node okay we have done that now we have to update this value so this this node so the 18th node now will have the address of this node and to do that we simply say n. next is equal to node and by doing this our job is done let's verify so I would say insert at two let me just run this code and you can see we got 55 at two so 25 18 and 55 is that simple awesome right so this is how you add the element in between but there's only one twist what if I add at zero location because for zero location is bit different right so if run this code now uh you can see it is adding at the first location so it is not working so for zero we have to put a special condition here so we will check if the index value is equal equal to Zer what we will do is we'll simply call because see when you say zero it means you have to you want to add a start right so you simply call that method start by passing this data so in case if index is zero that means your intention is to start from the first location so you have to say insert at insert at start so this is how you add the element at any location which you want so I hope you got the idea how to add at start and how to add at any location so till this point we have seen how to insert data into list how to insert data at the start of the list and then how to insert at specific index so if you can see we have a value which is 18 45 12 and 25 and then we are also adding five or 55 at the start so if onun this code we should be having five elements right but then I made a mistake so I this code you can see I made one mistake I'm getting this 50 5 two times so what went wrong if you can see the code here we are saying when you when you insert at the start in if index value is zero then you need to execute this code that means we not we should not be executing the other code as well right I mean this thing is is need to be skipped and we are not doing that and that's why you can see we are inserting 55 two times so this code here should go into the else part which we missed in the last video I will just put uh else block here and after putting that else block I will just go back to my Runner and if I run this code now you can see we got 55 only one so yes that was one mistake in the last video uh so I just made some correction here so there should be an lse here it's working now you can see we got 55 so just to draw here whatever we have done till now uh so let's start with the first one so what we are doing is with this line of code will create your first box here and let me just write a box which is 18 and let's give an address which is 501 that's my first address let's let's have one more so when you say 45 we got a second box so when you add this list you will get 45 here here and the value is 45 now let's say this is 502 so of course this 18 here will have an address of 502 so it will link to this box and then when we are saying add 12 so we got one more box here which is 12 and let's say this has some address with let's say 503 so for 45 here we'll have 503 now so we got this link here we got one more which is uh 25 but 25 will go at start so we got a new box here which is 25 initially we were having a head which was referring to 18 at this at the start of location but now that head is not referring here the head is referring to this new box 25 because we are saying start uh at the start so this will have its own address let 495 this is the address can go anywhere it's not just uh when you when you say it will add a start it will have this value it can have any value maybe 5.95 maybe 6.95 or 7.95 it doesn't matter now here it will have 51 so this will refer to the next element so the sequence is 2580 45 and 12 and of course 12 will have null and then we are saying insert at zero location so we are adding one more element which is 55 so we got 55 which is going at and let's say 55 has its own address uh we'll say this is 702 55 will have 495 because the head value and then this will refer here now head will not be referring to the 25 head will refer to this new location so that's how you insert value in between or at the end so we have I've seen all those stuff the next thing which is remaining now is delete right so how do I perform delete operation so let's say I want to delete this 18 here so I want to delete this 18 with index number 012 so I want to delete this index value two now we do that so when you want to delete this index value two so what we normally do is we just so the only thing we have to do is just change this address that's it the only thing we have to change is change this address so here instead of having 501 we can we need to change this to 502 our job will be done right so when you say this is 502 it will refer to the next node and we not be using this node anymore so this is gone okay now how do we execute that so let's go back to our link list and let's add one more method which is delete so before show here I would say delete public vo delete and this will take a index value so I would say index int index now technically the name should be delete at right so it should be delete at because we are specifying the index value okay now we have a special scenario here what if you you want to delete the first index so if you want to delete this one which is the first index here now in that case you just need to change one thing you need to change your head location with this one that's the only thing you do right and how do we achieve that so we say if my index value is equal to zero in that case I will change my head location so the head location will change to the next node and what is next node it is referred with the head node do next so I would say head do next so head will become head. next and that's it this is what you do when you want to delete the first element is that simple but what if it's not a first element so we'll go for else part again and here we have to Travers remember we whenever you want to Travers you have to do something if you want to Travers remember this we were using a for Loop or a while loop we'll go for for Loop because we're using index value right so remember this this code here the same thing from here to here everything remains same we have to Travers to that location so let's go there I will say copy and we can just use reuse the code here so we are traversing right so from so from the head node we are traversing to that location now once you reach to that location so now when you pass a value two here so the index value when is two so this Loop will run only once right because the index value is two so 2 - 1 is 1 and we are going till I less than one which is zero itself so this Loop will ret rrate only once that means the current n value which representing after the loop would be this n so this one will be your n so n is one now I want this value which is 502 because so that if I know this 502 you can assign that value in this box and the way you do that is by using this node in some temporary variable so I will say that there's N1 so when I say N1 now I'm representing this object and when I say n I'm representing this object or this node so I would say first of all I want to assign N1 is equal to n do next that's how you get you will get this reference so if you want to if you want to use this object here you have to if you want to make it N1 you have to say n. next because n do next is N1 right oh but we don't have N1 anymore so we'll say n node n i mean node N1 is equal to null so that we can refer that so we have to declare that object first now once I got n and N1 the only thing which you have to do now is the n object so when I say n. next so the N do next need to be replaced by n 1. next and it's very simple now you can simply say n do next is equal to n 1. next okay and then once you got the value you can simply say system.out.print and you can print the value if you want uh you so just want just want to verify if everything is working properly you can delete the element which you want to delete so I will say I want to delete N1 because that's what we are doing we want to delete N1 right so we'll say N1 and let's print the value of N1 which is N1 Dot data so we'll know at least which one is getting deleted now how to verify this let's go back to our runner and let's delete a value after sometime so say least. delete I want to delete the element at index number two which is 18 in this case let's run this code and you can see after running we don't have 18 anymore in the list yeah this is getting printed in the delete method itself if I don't print something here even it will not print 18 there so yes with the we can delete the value just by using delete method so we have delete at it will you it will be used to delete the value of the particular index so that is awesome uh this is how you delete the element the only thing we did here is we just changed the reference right so this n so this element address is changed by the new one which is 5 502 not 501 in this case and that's it this is how you break the links okay there's one problem this object is still there it is deleted from the list but it's still there if you want to nullify it you can do that you can see simply say N1 equal to null so that it will be eligible for garbage collection right so if I run this code now you can see we we have removed 18 and it is not there in the uh memory as well it is eligible for garbage collection so that's how you can uh delete this uh data in this video we'll talk about stack now what stack means now stack is basically an ADT in data structures and as you know ad stands for abstract data type so when you talk about abstract data type we only focus on the features it provides but not the implementation now in stack it provides you multiple features when I say features we have a list of features like we can add element and normally when you want to add element we use a term called as push you can of you can delete the element using pop so you can do that with the help of pop or you can you cannot say delete but you can take out the element from the from the stack and then you can also look for the elements you can find an element the first element and then then you can do that with the help of peak again we'll understand this in detail later so we can apply a push operation we can say pop and we can say Peak stack it supports a feature called as Le for which is last in first out which simply means whatever element you have inserted last you can access that first so Leo stands for last in first out so whatever element you have added last you can access that first to explain this we'll be having a stack here so the F last element you have inserted is the first you can take out and in stack like you have only one entry point so you have only one entry and exit point that means this is the only entry and this is the only exit so this one will remain closed you can apply a lock here so this one will remains closed the only place where you can enter you can enter the values is from one point from here and you can take it out so whatever element you have inserted last you can access that first but do we use this on real world and the answer is yes example let's say if you are working on a task so let's say if you're working on a project it's a very big project maybe that project is let's say a and that project will maybe will take uh 6 days so it will take 6 days to complete this project and suddenly the old project or the old version which is already deployed on the server it is giving you an error now you let's say you got a bug now at that point what do you think will you complete the entire a project and then you will think about the bug or you will try to fix the bug first and of course right a bug is something which a user is facing nowadays so of course you have to F the fix the bug first and then you have to think about the earlier task so in this scenario you will try to fix this bug that means you are keeping your job a aside and you're focusing on B now so you started working on B and you thought okay in next 3 to 4 hours I will complete this B uh I will I will fix this bug so you're keeping this task a aside and you're working on B now and certainly that this is your lunch time now so you are going for a lunch so let's say this is 1: p.m. and then you want to have your lunch you're going for the lunch now so let's say that is Task C so task C is your lunch time and now you are having your lunch so while you're having your lunch of course you will not be working on B the same time because when you're having a lunch you should do only one thing at a time you should have your lunch so when you're working on you're doing your lunch you will not be working on B that means you have kept B aside now once you complete your lunch what do you think which is the first task you will resume A or B it's B right because this is the last task you have left so when you say B this is what you left last so last in first out so this this is what you will resume and once you complete B then you will resume with your a that is last in first out so the last task which you are working is the first thing you will you will resume now do we use this in some other concept yes example if you have a stack of books so whenever you have a stack of books what you do is the first book so you will keep the first book here then second book then third book and then fourth book so once you keep all this book once once above other the first book you can access is the last book you have kept here right so this is how your so let's say if you have if you have kept one more book here then this is the last book you can access first or this is the first this is a book you can access first what about virtual world so in virtual world as well you know when you talk about Rec applications so in Windows we say alt tab so when you say alt tab it will show all the applications which you are working with so when I say all tab here you can see this is the first applic this is the last access application so you can see that first which is paint uh then I got this recording software which I have opened second last and then I was walking on notepad that's the third that's the third in the list and before that I have opened my my computer and before that I went to Google or Chrome so this is the this is the stack right so the last access element is coming at first here so yes we use stack a lot of time in in real world and in virtual world as well so let's try to understand how do we work with elements so let's say I want to store this four elements here we got five we got 8 we got three and we got two so I want to insert this values in a in a stack so what we do is we create a stack here and again if you want to insert the value we we will use push so using push you can insert the value using pop you can get the value the last value and using find if you don't want to delete the value from this tag you just want to see the value you can use Peak question is how do we implement this in fact you'll be seeing the implementation later but then there are multiple ways of implementing this we have the array way so you can Implement that with the help of array in fact in Array as well we can go for fixed length array then we we can go for dynamic length array or then we can also go for link list so we have different implementation so we can go with the array implementation using fixed or dynamic length we can go for link list but doesn't matter what you go for let's say if you talk about array so let's say if I create an array of four elements here right so we got an array of four elements and this is index value zero index value one index value two index value three now how will you insert the value so this is the only entry point and this is the only exit point right so if you want to enter the Valu you'll be using a method called as push or a function called as push in this push you will pass the value so whatever value you want to pass let's say if I say I I will represent five or 8 or three or two whatever you pass so once you pass a value here that value will be assigned to zero so let's say we got five here then when you say 88 will be assigned here now once you inserted this two value or maybe let let's say add three as well so we got three here now which is the last element you can access so if you have entered three this is the last element you can access but how do you know which is the last element so you can simply use a variable called as top now top will have the uh top will have a index value of the isn't element so index value two we have element three this is the top element and when you insert two now it will the two will go here simple right now how about if I say pop so when you say push everything go in this tag where you say 5 8 3 2 so the value will go down and then when you say pop it will remove the first element because when you say you have inserted two this is where you represent representing your top right this is the topmost element so this is the element which will remove first so when you say pop it will remove it will remove this two and let's say after that if you're inserting 12 so let's say after that you but after saying pop you you inserting 12 so 12 will come here because that's the last element you can access so again that will be go back to two yeah when you delete this element two before uh it will go back to three okay the the top variable will go back to the index number two which is three in this case so this is how you you push and you pop pop so the last element which is added is the first thing you can you have an access So when you say pop it will delete the element it will give you the element and it will delete the element from the stack but what if you don't want to delete the element you can simply use find and use to use find we normally use a function name as Peak So when you say Peak at this point it will give you 12 pretty cool right now let's say once you have inserted all these values in the in the stack and the stack is full right because the size of this stack was four uh we can make it Dynamic we can use Link list but let's say if I'm using a fixed length array so if you're using a fixed length array here and if you have incre if you have crossed the limit of your stack which is four in this case if I try to add one more element so so at the end here if I try to add an element which is one now the moment you say one if you if you try to add one it will give you an error and that error is Overflow error because you are trying to overflow this tag that's one one one case so if you try to push the value and if if your size is full it will give you overflow error what if you you try to delete the element if you try to pop and your your stack is empty let's say if you don't have any element here so if a stack is empty and if you try to fetch value at this point it will give you another error so if your stag is empty and you're trying to pop it will give you error which is underflow error so underflow is when you when you don't have the value and try to pop the valents from it so that's how you have a overflow and underflow concept those are errors basically and there's one more thing here with this push pop and Peak we can use some other methods as well of functions as well example we can get the size of the size of the stack using size method you can also check if the stack is empty with the help of ease empty function so again you can Implement whatever methods you want but these are the basic ones for stack and in this video we'll talk about the stack implementation using Java now when you say a stack basically stack is a class inside Java so if I simply say control space it belongs to a package java.util so you can use all the features of a stack here but then why use a inbuild class when you can create your own so I don't want to use this tack class which is inbuilt I want to create my own class so what I will do I will right click here on the package I would say new and I want to create my own class and that class name is called a stack so I have my own class here and here I will create object of Stack I would say stack nums equal to new stack now in this stack object or the nums object I want to perform certain operations so when I say uh nums do push I want to push the element I can specify any value here if I say 15 so the first element should be 15 and then again I can push multiple elements and then I I maybe I want to pop element which is taking out the element from the stack or maybe I want to fetch element so when I say stack basically it has certain features right as we discuss in the the session we can use push uh we can use pop on we can use pip so we'll be using we'll be working on these methods so let's start with the first one which is push so if you can see in our stack we don't have any method yet so let's create certain methods so the first method is push now to implement push what I will do is I will say public wide push now since we are adding the value we are not expecting any value so let's say push data so we have to send a data and it will add data there so now you can see if I go back now you can push the element and there's no error but then what you will do with the push to make it work first of all we need to create an array right so we have to create an array and we have to also specify the size of the array so let's say if I say we have an array here and initially we'll take an array of five elements so that if you can work on five elements you can work with any number of elements so let's say if I have the first one then the second one then third one and fourth and fifth so we have these five elements here index number will be 0 1 2 3 and four now if you want to add the first element as we are doing it here we are passing 15 right so that 15 will go to the uh go to element zero so somewhere here we'll be having that value which is 15 but then we don't have array yet so even before you push the element you need to create that array so let's do that I would say int and we'll say stack equal to or stack it's an array basically so we'll say stack equal to new int and the size of int is five you can make it Dynamic you can you can take the input from the user what array size you want but time let's specify by by ourself so we are we have created the array and now we can push the element but question arise where so we'll say okay you have when you say push you will assign the value to the address location zero okay so that makes sense we can simply say zero right here right so we can say stack of zero and you can assign the value whatever whatever data is coming it's so easy you can that value which is 15 will be assigned to the index value zero it is that simple but here's a Twist The Twist is what if I if I want to add one more element so if I say nums so if I push another element here we'll say let's I want to pass maybe eight now we we are passing two elements right we got we got 15 and we got 8 now what will happen is the moment you pass eight here the eight will be the value8 will be assigned to zero again with the zero location that's not what we want because if you if you if you run this code the value of 15 will will be replaced by eight we don't want that I want to assign value to one that means we need to count it we have to use we need to use a variable which will count the number and maybe I will use a variable here which is called as top and the initial value of top would be let's say zero we can simply specify top here right so instead of using zero we can say top okay now when the first element getting inserted so when you insert 15 that 15 will be inserted here at this at the top location zero right so we got 15 here now if I send eight oh eight is ALS the the value of top is still zero that means after doing this thing we have to you have to Simply say Plus+ so every time you push an element you just need to increment the value of top so that when you pass eight now8 will be assigned to index value one because when you pass eight the value of top at that point is eight so yeah so that is how you push the element in the in the stack okay now next point would be how will you fetch the element maybe I want to pop the element or even before let let's push all the element and let's say I want to print it I want to print this stag so after pushing two or three values let me just push one more we have a stack of size five in that let's say we have only three values let's say this is 15 8 and 10 I want to print the entire stack so I will say num. show so it should show the entire stack but the problem is we don't have show method in the stack so let's let's go there and say Hey I want a show method here which will print the entire stack now question arise how do you print the entire stack it's very simple actually you can take a loop enhance for Loop here and you can say int n colon tag that's an array and you can print each value and and you can print that value with a space so I will say n and then I will print a space so that you know when you print all the value they should be separated with a with a space sounds cool let's go back and what do you think will it work let's try so I will right click here and we'll say run as Java application and it worked can you see that we got 15 8 and 10 and we got 0 0 so yes it is working so stack is working when you say push but how about pop I want to remove the elements now uh is it simple let's try so when I say pop I will simply say public now pop will also fetch the value so it will it will remove the value from the stack and it will also fetch the value so I would say int pop and this will not take any parameter now how will you fetch the value now temporary we can say int data is equal to okay first of all let's declare that how do you fetch a data it's simple right we can say data equal to the data is coming from stack now the the property of Stack is if you have three elements as we defined here we have three elements right we have 15 8 and 10 now whenever you pop what do you think which element you will be getting and by default the value of 4 and 3 is zero because that's how aray Works in Java the point here is when I say pop which value I will get so it is done with the help of top so we have a top variable here and whatever value we have for top you will be fetching that value because you will always get last in first out so the last element which you have added is 10 so the top referring to 10 now this location so it will give you 10 and the way you do that is by simp simply saying you will say stack and you will say top now you will get that data quite simple right okay now we have to do one more thing now once you say you are removing this 10 we will making it zero so I will REM I will make this element as zero and how do we do that it's very simple let's go back here and say stack of top is equal to zero so we are making it zero so that again you can make it zero you can make it negative value for whatever you want and then at the end we can say return data I hope it will work now so let's run this code and you can see we got oh we have not we are not saying pop so we'll say nums do popop now it will delete the last data run and you can oh not working what's wrong what went wrong here okay so we are saying top top oh okay so we have to do one more thing see as you can see here when I say push the Top Value so when I say push first time the Top Value become one second time Top Value becomes two third time top top value becomes three that means it refering to this value we need to do minus minus we have to decrement the top before using it so we'll say top minus minus is it let's try so I will oh I'm not even printing pop value so whatever pop value I'm getting I should be printing it right so I would say system.out.print I hope this will work but on this code oh it worked now it worked you can see that we are removing the value of is 10 from the tag and you got zero here cool right so that's how you use push and pop now what else we can do we can also Al Implement Peak here and how do we do that so Peak is very simple Peak is almost same as Pop the only thing is in Peak you don't delete the element so I will paste it here and I will say this is Peak okay you will not do minus minus because if you do minus minus it will it will decrement the value but while you're fetching you will be fetching the minus one of it so we'll say minus one and you will not delete the element that's what you'll be doing that's it that's Peak for you after adding the after adding value eight I will Peak I would say nums do Peak if I run this code now you can see we got eight so the last value which I have inserted is eight here and then I'm pushing 10 then I'm popping 10 and you can see that's why the in the stack we don't have 10 simple this is how you use push Peak and pop what else we can do here now we can Implement some certain other methods as well example you can get a size of the stack you can get uh you can you can make this Dynamic instead of saying five whatever size defined by the user or very Dynamic which is every time you add the element even if the initial size is five you can you can change it so you can you can increase the size on the go we have talked about three methods we have talked about push pop and peak in fact we have also used one more function which is uh show which will print all the elements of the stack now with this we need two more methods the first one is size now size will give you the size of the stack and we also need a methus is empty now is empty will be useful to check check if this tag is empty now why do we need that that we'll see in in some time but then let's Implement these two methods first now to implement this let's go back here and I would say in fact after this peak just implement the method which is size so I will get the size of this tag so I will say public in the method name would be size and here let's return top because top will give the size right okay we we have a top variable there so when I return top will what what do you think will it work let's try so I would say go back and run inside run so let me just not pop any element here so you can see we are pushing three elements and then at the end I want to print the size of this stack I would say num. size now it should print the size for you let's run this code and okay it is printing three here but you know you should also print the message I would say size is and I would say size is this one let's run this code and you can see it says size three that's right we we we have three elements let me just add one more and I would say the value is let's say seven and if I run this code it should say size is four oh that's working you can see that it is working and after that if I if I remove any element let me just say pop I would say nums do pop of course it will print seven the pop value but what's the size size becomes three so it it works right when you push the element it will increase the size when you pop the element it will decrease the size that makes sense uh let I want to implement one more which is is empty so I would say public now is empty is a method which returns a bulling value so whenever you whenever the method starts with is it always returns a bulling value I would say is empty so let's return uh if the top is or less than equal to zero then we have to say it is empty right so if this not zero which is more than zero it's not empty right so let's go back to Runner and let's we have to check if it is empty so I will check it here after everything so I will say so I would say empty status I want to check if it is empty we'll say true or false and at the moment you do so we have we have to say num start is empty right and of course it is not empty so it should print false and you can see we got false if I do the same thing before pushing any value and if I run this code now you can see it says true and maybe if I push one element here and after that if I if I pop and again if I print is empty it will it will say true right because it is empty so that's how we can implement this two methods but what's the use of this implementing this two methods do we have any advantage and the answer is yes so what I will do is um let me just do some experiment and let me remove all the extra stuff from here I don't want to Peak I just want to push multiple elements and I don't want to do any of this stuff let's let's keep it simple say I'm I'm only pushing elements and then while I'm pushing I will push four elements and if I run this code so yes we got four elements what if I try to push five elements will it work let's try I would say 17 and if I run this code and yes it does work what if I try to push one more and let's say the value is Maybe 77 in this case if I run this code you can see we got an exception which is array index out of bounds exception it's full right because the stack is full that means whenever your stack is full you should not be adding the value you should print the message to the user hey the Stag is full how will you do that how will you check if the if the Stag is full and the way you do that so every time you say push so before pushing you need to check is it full how do you check if it is full the stack value is five right the maximum size is five so I would say if top if this is equal equal to four that means your stag is full right so if it is four I would say stack is full so I would say system.out.print Len stack is full and this part will be executed in else part okay let's go back here and after every push I will try to print show so that we'll know what is happening so I would say nums do show and let's run this code now and you can see oh at this point it is saying stack of Stack is full oh my bad okay I I guess I'm missing one more thing here after printing this stuff I just want to print a new line so that every output I will get a new line okay so you can see it is it is saying stack is full here itself because I just made a mistake it should not be four it should be more than four why more than four is because every time you say push you can see we are incrementing the value here so it is not four it is five let's go back there and let's run this code and you can see that after this point it says stack is full so it will not allow to push the element after this point so that you can save yourself from the exceptions right so that's how you can check if it is full now what I will do is I will try to pop so I'm pushing all these elements and after pushing this if I try to pop the elements and I would say system.out.print and I would say uh nums do pop it will print it will pop all the elements and not all it will it will pop only one at one point so I will run this code you can see at the end you got only one element which is 17 that's great in fact I will not add all this element I just want to add three elements so that we can save our job so I'm adding only three elements now and if I say pop of course you will get the last element which is 10 right this is the last element which you have entered I would say pop two more times and you can see it will try to pop all the elements so if I say print the size I would say num. size it will print zero because we don't have any element right so you can see we got zero what if if your stack is empty now and if you try to pop now if you try to pop it will give you exception right because you don't have that value before that so you don't have any value in minus one so that means you need to stop it before going before popping the element how do you check so we'll say if so we have to do this only when if your if your stag is empty then you will not do all these things you will simply print a message which is tag is empty otherwise we'll say else and you got the point right so if I say Tab and if I on this code now okay I hope it will work there's an there's a problem here or data we have to ins TR something I would say data is by default zero that's a local variable right so before returning it we have to assign some value and let's run this code now and it worked you can see that it says tag is empty so it will not give you exception so we have to handle we have to take care of all this element as well right so that's it I hope you uh you understood what we have done so we have implemented two things one is size and second is is empty method so using these two methods we can per we can check if the Stag is full or the Stag is empty before performing any operation we have seen how do we Implement stack using a fixed size array so if you can see this example here we have a stack class in which I'm creating a stack array or basically an INT array of size five and then we were able to push element we were able to pop element then we were able to Peak and then we were checking the size of the array and then is it is empty and then we were able to print all the values and then using this we can perform the operation the only problem here here is we have a stack and that's perfect we have a stack here and this stack does have a value which is you know 15 uh 8 and 10 but the problem is the size of this array is only five which means the maximum maximum element you can go here is five elements what if you want to go beyond that what if you want to have a stack which is which is expandable and that's where we have a concept of dynamic size array but then how we you implement Dynamic size array so what we are what we want here is the moment you say uh you have five elements let's say if you entered two more elements here let's say three and six and after that if you try to add one more element which is seven and of course we don't have the aray size of size six so I want to create a new array so it should create an array of maybe um a new array using in which you'll be having all these values now when you say a new array so what should be the size of that array so I'm expecting the size of that array would be say the current array is of size five the next array should be of size 10 technically we'll go with the power of two initially we'll have a array of size 2 if you want to add more elements we'll make it four if you add more elements we'll go for eight if you add more elements we'll go for 16 this is what I'm planning for but then how will you increase the size every time so it's very simple the current array is so let's say if you have a current array and let's name that array as stack so that's your array we will create a new array with a name so let's say if I create a new aray with a name as new stack now this new stack will be having a size of four but the problem is we got the new array what about the values in it so whatever value you have in this tag will be copied in the new array that's what we want to implement that's it if you can do that our job will be done so every time you increase you you go beyond the limit you have to Simply expand your array and okay there's one more thing what if you have a array size let's say 16 yeah so as you add more elements you'll be having an AR size of 16 and let's say you have 16 values and then you are saying pop pop pop for maybe 15 or 10 12 times in this case let's say if you have only three elements now so in total we have three elements and the size of this array is 16 that means you are wasting some memory right or we want to have a feature where you can shrink the size so we want an array when you say AR is dynamic you want it to be expandable and you you want it to be shrinkable so we want to implement these two features and if you can if it is possible for us to implement these two features that is what your Dynamic size stack would be so let's get started so what I'm going to do here is first of all I want to copy the exact code because we will reuse the code here and I will simply copy this code I will say control a copy and let's create a new class and I will call this class as Dynamic stack or it's a d stack you know that will that will look like a dynamic stack we are making an anous tack here so let's say this stack and we click on finish and in this tag class I want to have all those features I will just simply paste it here and if I go back up there might be some issues I have repeated the package and the class name is this dag okay now the first thing we need is you know we cannot simply create an array of size five I want to create a variable with uh with a variable name as capacity capacity and then let's see the capacity of this is two initially we'll have a capacity of two and then we'll go beyond that normally we can go for a capacity of four or eight that will make more sense because normally when you say you have a stack of course you'll be having more elements but just to make it interesting you know just to show you how it expand and Shrink we start with q and then here when you create this array I want to say this is two again you can we can write capacity here right so you're creating an array of that size initially the array which you have is of size two okay that makes sense now of course we will be having a method which is size we have the element which is is is empty so everything will remain same now what will change is the way you push so when I say I want to push the element and I can I don't want to print a message which is stack or full we don't have any option there now if you if you even if your stack is full you want to expand this size that means this code will happen for sure you will be able to add the element the only thing is maybe if your stag is full so you need to expand the size and then how will you expand the size so I will simply call a a function or a method call as expand but when when I want to call a method which is expand so only when your stack is full but how do we know if the stack is full okay that's a good question right so if the size of the of the stack which is say size is okay if I say size is equal to equal to the capacity then that means your your stack is full in that scenario you will expand that's it nothing much in that scenario you will expand and anyway you have to do this task but question arise what you will do in expand what exactly happens in expand okay let's think about it what will happen in expand so in expand what we have to do is we have to create a new array that's the main idea right so let's say I want to get a method called as expand which will be this method here again you can make it public or private doesn't matter uh in this case I'm I'm keeping it private because I don't want to access this expand function from anywhere else else I just want to use the expand only from the current class so I will use expand and then the first step would be I want to get the current length and the way you do that is by saying int length is equal to size So when you say size method it will give you the current length Okay that simple now we have to create a new stack and as we Define in the diagram here we will simply create a new stag right this is the new stag we have so the way you do that is by saying I would say I want to get a new stack I would say int new stack and then I will give a bracket equal to new okay now the size of this tack would be doubled right so if if the current size is two the next size would be four right so I would say in and then I will simply say capacity whatever value of the capacity we have I will simply say capacity into two our job will be done so we got new stack and then we are saying it two that makes sense the next thing we have is okay so we got the new array now we have to copy the element from the current array to the new array now how do we do that so we can run a loop here we can use normal for Loop the best way would be to use in classes again it's your choice okay you can use normal for Loop but since I'm lazy I'll be using some in classes I would say system I would say there's a method called as aray copy so thankfully we got this method aray copy this aray copy takes five parameters the first parameter is from where you want to copy so I want to copy pro copy from the stack array which starts with the Val zero again I want to copy the entire add right so I will start with zero and then it will it will get copied to the new stack and it would be zero and then how how many elements you want to copy I want to copy all the elements I would say simply say length once you have copied everything so in new stack you have all the new values but then unfortunately ultimately we are working with stag array itself right so even if you expand the new stack everywhere we are using this stack reference so of course we have to replace this stack reference with the new stack reference so that everywhere else I'll be using the stack reference which is representing new stack here okay so once we have done with the stack and now if you can observe we have also increased the capacity so I would say capacity is equal to the current capacity into two so I would say I would simply say into equal two so whatever capacity you have here it will we just trying to double it that's it it's so simple and now let's go back to Runner now instead of having the object of stat let's say object of D stack so that we will not get the exception and I will not say pop this time I will only focus on push and every time I post the element I would say show just to get the output i' say copy paste paste so you can see I'm entering five elements the size of the array which we have here is only two right but still we are trying to print five let's run this code let's see if it is working and it works you see that it works so initially we were having two elements no problem we added new element but the moment you added 10 can you see that it is the capacity is not three it is it is four in this scenario we have added four elements and now the moment you add one more element which is 10 again we got the element of size eight so that's how it is expanding cool right in fact you can print the you can print the value using size and you will get the exact size whatever or the capacity if you want to print so that's how we are expanding it so simple that's why you say it's a dynamic array the next point would be how do we pop the elements because I want to also want to pop elements right so I would say pop is working but then every time you say pop I want to decrement I want to shrink okay first of all I will not say is empty do you want to check is empty yeah that sounds good is empty is is a good checking but every time I remove the element I want to shrink so I would say shrink now shrink may not work every time but then whenever your size goes okay when do you want to shrink that's a good question so let let's say if you have an array of eight or maybe you have an array of 16 the size the size of this array is 16 but the elements you have let's say eight at this point I don't want to shrink but the moment elements goes are less than four so let's say if the if the number of elements is 1 2 3 or four in this scenario I want to shrink it and I want to make it half half of 16 which is eight so even if I have four elements I want to make keep it eight let's see if I go beyond that if I if I if I go below that if I say I have one element then I have to make it two right of course before going for one it will become four and then you it will become two so the idea here is if the number of elements goes be below 3x 4 we will make it half so size of your becomes half that's what we're going to do here in shring so let's Implement shring method so I would say click and create a method called a shrink now inside the shrink method I want to perform those operation and how do we do that now first of all we want to know know the length because that's very important so I would say length equal to size and then once we got the size here or once we got the length now how do you how do you shrink it how first of all how do you know that your array is is the number of elements in the array is less than 3x4 now there are multiple ways of doing this formulas we can use shift left shift aray I mean left shift arrows you can do some mathematical calculations you know I'm since since I'm lazy for you now once you got the new array the same thing we can actually copy paste you know uh okay I cannot say copy paste I should be saying code ruse that will make more sense go back here and paste and you can see we are creating a new AR we are copying the elements and then we are saying stack equal to new stack I hope it will work let's go back to Runner and then at this point the size of your stack is8 after that I'm saying Pop I would say I don't I don't want any value I would simply say nums do popop it should remove the element and then I will say nums do show so that I will see the elements and look at the size it has removed the element let me just remove one more and in fact two more just let's go beyond three or Beyond two it will go let's go to two and the moment you have a size two can you see that the the array is getting shrinked and if I go one more time see the size see the address size it is getting shrinked so that's what that that's how you do make a dynamic and in this video we'll talk about Q now we have talked about link list we have talked about stack and now it's time for Q now Q is almost same as stack the difference would be in stack we use a concept of leao which is last in first out and in Q use a concept of fif which is first in first out which means whichever element arrived first will be the first one to go out so it is first in first out now in real world whenever you want to take a movie ticket you join a queue right so whoever arrived first will get the first ticket in a que what we do is we have let's say we can implement this with a different concept we will be implementing this with an array uh so you can do that with the help of array or you can do that with the help of Link list in this example we'll look at how do we Implement that with the help of array in fact in Array itself we can go with the flat array or we can go with the circular array but we'll see the difference now let's have the array of size four where you can have four elements in this array you'll be having two end the end from where you will insert the value and the end from where you will take out the value so this is your insert so this is where you will insert the element and this is where you will take out the element and that's why whichever element reaches first in the que will be the first one to go out now let's say if I insert a value five so the five will be coming from here it will go in this area so the five will be saved here the moment you insert a new value let's say two it will go here let's say if I insert seven it will go here here and let's say if I insert three it will go here so this is how you add the elements now if you want to delete the element so this is the first element which which go out so five is the first element which go out and then all this element which is 2 7 and three they will shift on left hand side so two will come here seven will go on the place of two and three will go on the place of seven and then you can add one more element at the end that's what we expecting from Q so in Q basically we have two and one is from where you insert the Val value and from where you can take out the value so you can call this this area or this thing called as front and this is the rear end where from where you're inserting the value now in Q we can perform certain operations so you can insert the values and you can remove the values the way we did with stack we in stack we use a concept of pop or push so push would be inserting values in the same way in Q we use a different term so that term is NQ so NQ is like inserting or in stack you can say P if you want to remove the element we use a word called as DQ so we have a concept of NQ so you can represent insertion with the help of NQ and you can remove the element with the help of DQ now of course NQ will take a element from you so you have to pass a data and then in DQ don't pass the value but it will return your value so let's say if you say DQ and your front is five so it will give you five but let's say after after that also you are saying DQ it will give you two now with this two methods we can Implement some more methods as well example we can Implement a method of size which will give you the size of the Q uh we can Implement is empty if to check if it is empty these are two methods we can also Implement in this we can also Implement Peak the way we did in stack you can get the value which is going to be removed it will not remove the value but it will it will show you which is the value which which is going to remov now how do we implement this so first step would be we need to create a array so we have to create an array of will go for array of size five I love this number five so I will go for the array of size five and then I will try to add the element now how many variables we need here so basically when we talk about this implementation maybe we need a front variable which will have the index number of the front because every time so initially when you don't have the value in this in the que so example if I create another q here and if I say the Q is of size five so we got a size of five so whenever you don't have any element of course the front and rear both will be not be referring here but then the moment you add a value front and rear both will refer to this value so this is your front and this is your rear but the moment you add more values let's say if you have entered five and then two the rear will be pointing at this point in fact the rear rear we have to shift to this point when you insert two so that you can add three so rear will shift here so every time you add a value you will say rear Plus+ and every time you delete the value you have to say front Plus+ So when you say delete you have to say front Plus+ and whenever you insert you will say rear plus we'll see the implementation and then you will understand what I'm talking about so I will go back here in the que and so you can see I have two classes one we have is Runner class another we have is Q class I will go to q and let's try to implement those methods the first method we have to implement is NQ oh before that we need to create some certain variables the first thing would be I want to create a q itself so I would say q and the size of this Q Q would be let's say uh five initially or this is not what you do it so you say new int and you specify the size five you can make it Dynamic your choice but initially I will go with the static one or fixed one so that it will be easier for you to understand and then we need three variables we say size one of the variable uh then I need a variable which is front which will refer to the front element and then I will need a rear element which will or re variable which will refer to the rear element once you got these four variables and of course the value for this size would be zero front will be zero rear will be zero and then I will create the method so the method name is public void NQ this is my method which will take a int data and then I will try to insert a value here now how do we add the data into a NQ so for that I will go back to my this point and let me just make this an empty empty Q okay so you can see that we got a empty Q here so the name of this Q is Q itself and now I want to insert a value so let's say if I'm inserting a value which is five so in this Q at the index value zero I want to insert five now if you want to insert the value we use a variable which is real and by default real has the value which is zero in fact front will be also referring to the first element as at this point so we'll say front is also zero index number zero index number one index number two three and four now front and rear both will refer to the same element here at the end now the moment you add five so at the rear point you have to insert five now how do we achieve that so in the code it self you will say Q of rear is equal to whatever data you are passing so the data I'm passing is data itself now once you have added the value you need to do one thing after adding the value of five you just need to shift your rear to the next location which is here and how do we do that how do we shift our rear it's very easy you simply say rear Plus+ so you will say rear is equal to re + one or you can say Plus+ that's fine and then once you have added the value or incremented the value now I also want to increase size of it right so now the size is size Plus+ because we are not in fact we can write size equal to size + one just to maintain the same sequence so we have we equal to + 1 and size equal to size + one so every time you add the value the size will increment it's that simple time let's work with NQ and then later will work with DQ I want to see the element as well so let me say I want to create a show method which will print the elements so we we also need one more method here which is show so I would say show method as well and show is responsible to print all the elements and in this show if I want to print elements it will print all the elements in the from the queue uh but how do we do that so what I will do is I will say for loop I will start with zero initially and then I would say okay till what point we have to go I have I have to go to size right and then Plus+ because the size of of the array maybe so you have inserted three values so the size would be three so you have to go till I less than three and every time you go there you just need to print the elements and the way you can print the element is by saying the value Q of I and then you will also print a space after that and here I want to print the elements I would say system.out.print I would say elements elements colon so that you know it will give you a good format of printing I don't want to print it in there that's it it's will work now so we got NQ we got show now how do we verify so let's go to Runner and in this Runner let me create object of Q I will say q q is equal to New Q I got the object which is q and in this Q I will add the element I would say uh n q I want to inq the first element which is let's say five as we have done here now after inserting five I will just simply say q. show I want to see how many elements are there and if you run this code you can see we got five we got only one element that's perfect let me just add one more element here and that would be two in this case I would say two here and when you say two if I run this code you can see we got five and two so every time you insert the value it will it will get added in the queue but question arise how will you remove the element and what happens when you go beyond the size let's say if I inserted uh seven values what will happen and of course if you insert seven values or six values it will give you a very famous error or the exception which is array index out of bound exception till this point we have only inserted the values now how do we remove the values and how do we avoid the exceptions so let's try to delete the values and how do we do that it's very simple just go back here and we just need to implement one simple method which is DQ so I would say public uh it will return the values I would say DQ and it will take it will not take any parameter now how do we remove the elements it is simple right so just go there so when I say I have inserted two values so we got five and two and let me just insert some more values you know I will just try just to explain this in a better way I would say I want to insert some more values so in this least in this Q we got 7 and three right so we got this values I want to say DQ now so the moment you say DQ what should happen is it will remove five from the from the from the Q so it will remove this five now when you say removing the value maybe you will not remove the value the only thing we have to do is we just need to move our front from this point to this point so we just need to move our front right that's that's enough so I would say hey front just go to two here okay that should work I mean we just have to increment our front so what we'll do is when you say DQ first of all I want to return return a value so I would say int data is equal to which value the first value and how do we get the first value is by asking hey front give me your value so front will have front will be index number zero so it will give you the first element which is five that simple and then every time you fetch the value of we have to return the data as well so we return the value here but it's not just about returning value we have to move the front to the next location so how do we shift we say front equal to front + one is that simple and once you do that we also need to reduce the size of it so we'll say size minus minus uh we can say size equal to size minus one just to keep the uh same syntax yeah so we'll say size equal to size minus one so here we are saying size plus one here we are saying size minus one that's it it's so simple let's go back here now you can see we have inserted five values and before calling show in fact if I call show at this point you can see it will print all those four values but if I say DQ so if I if before saying show if I say DQ and if run this code you can see we got 5 2 and 7 oh we got a problem here we are not able to remove the last element we have removed the front as well so this is some problem with the front the problem is you are starting from zero and you are ending at size right that's perfect but then you cannot simply say I here you have to say front plus I because we are shifting the front as well right so you you don't want to start from zero you have to start from front and now if this code uh you can see this is what we expecting right we got 2 7 and three we are removing the element which is five it's so simple now if I try to do this one more time if I say DQ and you are guessing it right we were getting only seven and three so if you if you run this code you can you can see we got 7 and3 so this is how you NQ and this is how you DQ now after deqing it what we are thinking is we only have two elements right so in an array we have only two elements but that's not the case let me prove it let me just add two more elements at the end now of course it should work right because in in total when you say DQ in total you have two elements and I can insert two more elements there I can say I want to insert N9 I want to insert one so this should get added right but the moment I run this code we got an error which is array index outter bound exception why because even if you are deqing it you are not removing the elements you are just shifting the pointers in total you have four values there in fact after this four values you adding two more elements just to prove my Point once again I will go back to the show and at the end when I say show at the end after printing the values I want to print the array as it is and the way you do that is we can use enhanced for Loop or we can use normal for loop as well I can say q and then I will print the element of n with a space there now if I run this code you can see oh we got the exception uh let me just let me just not do that here let's say comment and let's run run this and you can see we are getting oh okay I just forgot one more thing we just need to put a new line in between so that we'll get a difference and if I this code now you can see you can see it is working at this point because you are entering only four values and the size of the array is five so you're inerting four values you're trying to remove those two values so in total you have two values which we are getting but in Array we still have four elements and that's why if you try to enq two more elements there which means you are crossing the index value which is four five so index value last will be four right but the moment you try to add more elements it will give you error and as you can see we got eror at this point you cannot go beyond that point in fact the exact eror would be at here because nine will be inserted because we have space for that not this value the problem is arising because of this real value because if you cross the fifth value which is this one index value this point is four and after that the value for rear becomes five and we don't have an element after that and of course the size of array itself is five you cannot go beyond that at this point what I want is I want it to go back on this first one which is you can imagine this array in not as a linear array but as a circular array now this is just a representation so you can imagine we have an a circular array which we have four elements we have the first one in fact five elements uh this third one so four and five let's say we have this five elements so the in so the starting value would be here so this is let's say imagine your front and this is your rear itself every time you add value let's say when you add value five it's here re value will become two here and then seven here and then three here now if you try to match this with the runner what we just have to do is Imagine This is index value zero and index value one index value two index value three and index value four imagine this is how you work with and then after four I want to go back to zero uh how do we do that that's a question so the moment you add these four values we have added those four values and then when you say DQ it simply means you just need to shift your pointer right so your front will be shifted to zero and one it will shifted two times and your front will represent the seven value here so this is your front now this is no more your front now what about the rear value so rear will shift here so this is where your rear is now if you add a value here which is nine it is getting added what about this one the moment you try to add one it will try to go on index number five we should not be going for index number five we need to go to index number zero that is where you call it as a circular array so after four the index number will go to zero that's it if you can achieve that your job is done oh now that's a question after four how can you make sure that your value becomes zero um okay so let's try now when I say uh zero so can I apply this operation because when I say yes so when you say say 5 Mod 5 will give you zero right because 0 is a remainder so 5 Mod 5 is zero now when you are when your rear value is equal to 1 1 mod 5 is equal to 1 uh when your r value is 2 R mod 5 is = 2 then 3 mod 5 is = 3 and 4 mod 5 is = to four and then here is a Twist now if you say okay now if you say five mod 5 is equal to 0 that's the beauty we wanted zero right and we got zero after that when you say 6 mod 5 is equal to one that means we are going to the same Loop so only thing we have to do is every time you perform this operation you just need to do a mod of five so if the size is five you'll say mod of five same goes with front every because the front also should be in the loop right so you will say front and you will say mod 5 that's it this is what you do now if I onun this code it should work and you can see it is working oh it's not working we still got an exception but why are we doing in a perfect way yes we are but it is giving you error while printing I guess that's the issue if I go back to Runner yes the problem is because of printing so if I go back here in printing as well I need to perform that operation because you don't want to say front plus I you want to say front plus I mod 5 because I don't know the current value of front because it is going in circle so I will run this code you can see it is working you can see we got the value which is 73 91 this is the exact Loop can you see that it we got 739 and the next two elements are coming here which is one I mean 9 and one this is two is the previous element it's so simple that's how you can work with the circular Loop but we still have one issue what if you try to uh add more elements in fact I will say copy and paste let's see what happens I'm trying to add two more elements which is 19 and and let's say 15 so we wanted the value here which is 73 okay these two values because we are removing these two values right five and two because of DQ so this two elements and uh this four elements in total we have six elements let's see if it is working and oh it's working you can see there's no problem and you're it is getting increased that's weird can you see that the elements the size is five and we are getting six values it's because of the loop so till this point we have seen how to add the element El and how to remove the element from a que we here to add some methods some methods like size of the Q then we want to know is the Q is full all we want to know is the Q empty let's start with the two size method or the size method so I would say public so return the size so I would say size or maybe get size will make more sense okay now what it will return is simply it will return the value which is size that's it we say return size and now if I go back to Runner and after doing all these things you can see I'm adding four elements and I'm removing two and then again I'm adding four elements so in total we have six elements there and unfortunately the size of our array is only five let me just remove this as of now so you can see we have four elements here we are removing two and then we are again adding two so in total we have four elements there now if I want to know the size of course I want to print the values but I also want to print the size so I will I will say system out. print Ln and here I would say uh the size and then I will print q. get size so it will print the size as well and let's verify it is working and the moment on this code you can see we got the size as four and that perfectly makes sense because we have four elements there now let's use is full or is empty method so I would say public now how do you know if your if your Q is empty so I would say public Boolean is empty so the way you know that it is empty is by returning if the size is zero so we'll say turn size is equal to equal to Zer if it is zero then of course it is empty right so that's what we are doing here now how do you know it is empty so let's go back here and I will not add any element I will not delete any any element there or maybe I will add two I will remove two let me just comment so you can see we are adding two and we removing two and after that if I say system do. prolin I would say Q dot is empty so of course it will Point uh true so if I run this code you can see we got true but if you add a value and if you don't remove it let's say if icommand this part and if I onun this code you can see we have one element there so it will print false so implemented is empty as well and now let's go for is full so it's very easy let's go back there and say public bullion is full and here we just need to return if the size is equal equal to five because the Q size is five so we'll simply specify size equal equal to 5 in case if you're making Dynamic then you can also use a variable there let's go back here and you can check if it is full so we have done with the empty let's go with full and in full what I will do is I will add some more values I will uncommon this part and I will uncommanded so you can see I'm adding five values there the moment I on this code it will say it is full true but if I add only four values you can see it will print false it is not full so we have implemented is empty is full and get size now how it will be useful now instead of using size here we can also use methods normally this is what you do in Java you always use methods to get the value so you will say get size equal equal to Z or you will say get size equal to equal to 5 so normally we check with size that's one thing the next thing would be whenever you want to add the value so before adding the value we have to verify if it is not full so we'll say if we have to do we have to do this only when it is not full so if it is full how do you know if it is not full so we'll say is I mean not is full then only you have to execute this statement otherwise you will print else message you will say else and you will print the Q is full and here also okay we have to just format the code to look at good and in DQ as well we want to remove only when it is not empty because if it is empty then why what you will remove right so you will say if is empty and this code will be executed only when it is not empty otherwise you have to print a message so I would say first of all this format that and in else part I will print Q is empty simple right so here we are checking it is full or is it is it empty here let's go back to Runner and I will try to add more elements now so you can see I'm adding six elements there and the moment I run this code you can see it will print the Q is full for this element here and the size is five and you got all these elements so that means one is not getting added now what if I delete the element let's say if I don't have any element in the queue and I I'm trying to delete the element so of course if the if you don't have any element of course how can you remove that so if I run this code you can see it prints Q is empty so in both the scenarios Q is empty so yeah that's how you that's how you implement this methods so recap we have get size is empty and is full and this will be used to check before adding the element and before removing the element and in this video we'll talk about a tree data structure now we have talked about link list stack and Q now tree is another ADT now if you talk about tree it is basically a structure where it's similar to linklist you can say so in linklist what we have is let's say if you have a node and you have B node so what happens is a node will have a reference of B node right and then uh B node will have a reference of C node so we have this in linear format in tree what we have is we have the almost similar structure but then with with a difference in tree we normally create a structure like a tree but you can imagine a mdor image of a tree uh normally in tree we have Roots we have branches we have leaves in the same way if you talk about a tree here imagine this is a root node this is a root of the tree and this a instead of having one reference may have multiple reference in this case I'm getting two references uh so a will be knowing about b b will I mean a we'll be knowing about C and then D so after a we have these three nodes maybe this C itself will have more some other nodes let's say we have X and here we have y and maybe this D has one more node here which is maybe K so K so we have all this all these nodes here now this is the topmost node right okay everything every box here is called as nodes so these are nodes here and this the line between this nodes are called as Edge so I would say this is Edge so we have this as node b x y c all are nodes and then this is Edge and the topmost is called as a root node because this is where your tree is getting started this is your root node now if you talk about this a so a has a node which is b a a has a node which is C and A has a node which is d right the same goes with c c has X and Y but what about this K what about this Y and X we don't have any other further nodes here and in fact b as well we don't have other nodes so this nodes are called as a leaf node because we don't have anything at the end of after that so the topmost is called as root node this is where you will start your travel cell if you want to store data as well you will start from the root node and then you will go down and down and down so this is your node and then the end nodes are called as Leaf nodes so these are normal nodes in between but then a has a special name which is root node now this is about tree this is uh so you can represent this with different languages using Java C C++ python now with this we have a very special type of tree which is called as a binary tree now what does binary tree means now binary means two right so if a node so let's say if you have a node which is a and if this node has two more nodes like C or D so maximum if you have two nodes this is called as aan nodes so if you have two nodes so binary nodes and that's why the tree is called as binary tree where every node will have Max to Max two nodes so either we can have a zero node or we can have one node or we can have two nodes not more than that so this is how you represent your banet tree now in fact in banet tree as well we have certain certain types let me just write it here so we have certain types in banet tree so first one is strict banet tree so we have the first one which is strict B tree now what is B strict B tree means a tree where on every node will have two sub nodes or no children so and again we have one more term here so the C and D are called as childrens so if I have a here and if a has two which is C and D then this is called as a straight banet Tre in fact you can also have inside C we can have two more nodes let's say we have X and we have y this is also a strict binary tree now you might be thinking about D so yes we so when when you say strict it means either two or no children so here you can see we don't have any children to D now how about if I say we have let's say x has one more child let's say K now in that SC this is not a strict B Tre because we have only one element or one children node for X so this is not a strict so let me remove that so this is the first type the second type we have is full binary tree now what is full binary tree means where you have a you have a tree where every node either either has two I mean they will be having two sub nodes example we have C and D again and D has two let's let's say we have X and Y and D should also have two more nodes so so that they will come on the same so let's say we have p and Q here right so we have D also has two more nodes which is p and Q so a has c and d and c has X and Y and D has p and Q so all the leaf nodes should be on the same level so you can see they on the same level so this is what you call as a full banet tree where all the childrens are of on the same level the third one is called as a complete ban tree when do you say a ban tree is complete it's very simple where you have a tree where all the nodes are on a level so let's say if I if I Define it C and D again and here if I say X and Y and then we so this this is also called as complete binary tree because you have all the all the leaf nodes on the level so let's say this level is let's say I will I will call this level as l so either it should be on L level or L minus one now which means if you have one mode here or two Moree here let's say we have K and we have C now this is not a complete binary tree because the leaf node are on the are not on on L and L minus one because if I say this is L then this is L minus one but this this will not work so there should be two more nodes here then it will be called as complete binary tree right so this is not a complete binary tree so if I undo so this is a complete B tree now let me draw a simple uh simple tree here so if I say we have a and then we have B we have C and then we have inside B we have D we have e and inside e we have F and we have G and then I will draw a line here so these are all my nodes and inside this F also we have we have two more nodes here let me just draw it uh let's say this is X and this is y so now looking at this tree we have certain terms here so again this is a root node right so this is your root node and this c g y x I mean X and Y and D those are your Leaf nodes now we have to remember two more terms which is height and depth now what it means so depth means let's say if you talk about this e so what is the depth of e it is a b and e so that's a depth of this e what about the height of e so height would be it's the the children's of e so we have F and we have X right maybe this x has two more uh two more two more sub parts here let's say we have K and we have l so now for for E it would be f x and K so this is the height of e so when I say the depth of e would be two so we have so we have two because it is a and b right so A and B those are the two two two notes we have what about the height of e it would be f x and K so it will be three so right so three height is three now what about the depth of a root node it's always zero right because this is the first element so we don't have any depth what about the height of a root node is it again you have to say a b e so we have to go for the longest one so it is a b e f x and K so it's 1 2 3 4 5 so height of this tree is five oh that's a new term now so we we know the height of the root node right so height of the root node is same as height of the tree or you can say height of the tree is same as height of the root node so that's how you we have this two these two terms are important when you go for the coding part so yeah that's it that's what your tree fun tree thing is so we have talked about what is tree we have talked about B tree now we'll doing the implementation of this using different languages so I hope you enjoyed this video let me know in the comment section in the theory video of tree we have talked about different types of tree and we when you implement this we are going to implement that with the help of binary search tree now what's the difference between binary tree and the binary search tree binary search tree is a binary tree the difference is the way you add values now in binary search tree what you do is you always make sure that the smaller element is on the left hand side and the bigger elements are on the right hand side that means if you talk about a root node the elements on the left hand side of a root node will be smaller than the root node and the elements on the right hand side of the root node will be bigger than the root node is that simple that means when you try to create this tree how will you do it so let's say we have this values here now when you work with eight8 becomes your root node okay that's so simple you always get your first element make it a root node next you have seven now s is less than 8 so it will go on the left hand side so that is eight seven here then you got 12 now 12 is bigger than eight so it will goes on the right hand side now of course for the root node both the nodes have been covered what about the next 15 where 15 will go of course you cannot create a one more node here that's not possible for for the binary tree so what you do is you travel now where to travel left or right now 15 is bigger than 8 so it will go on the right hand side but we already have 12 there now 12 have an option of putting 15 left or right now since 15 is bigger than 12 it will go on the right hand side so 15 goes here next element is two now two is smaller than 8 again we starting from root node so starting from eight it is smaller then it goes to seven is smaller it goes on the left hand side of seven which is two here because it is Les less than seven next we have is five so we got we go to eight it is smaller than 8 we go to seven it is smaller than seven we go to two it is greater than two so it goes on the right hand side of two so five goes here so this is how the ban search tree works now once we got the basic understanding let's start the implementation if I want to code that first of all we need for sure we need a class called node right in fact I will not be writing this here let me just remove it what I want is I want from my main method basically I want to create a binary tree there are some in classes but we're not going to use it so let's say binary tree and we'll call this as a tree equal to new binary tree so we want to build a binary tree okay you can see we have some inbuild methods we don't want to use them so let's use a b tree we don't have this class yet so what I will do is I will just go back here and you can see is importing this package we don't want any inbu things so if I go back here and if I sayy hey I give me Solutions more actions I want to create this class B between the same package and you can see we got it I will just move this on this side I don't want to see the project structure so you can see on left hand side we have a main on right hand side we have a binary tree so basically in this binary tree I want to do all those stuff now when I say I want to do stuff basically we have to say tree dot I want to insert value in this tree so I can say insert and I can pass the value let's say 10 I don't know what values I'm passing here or maybe I can I can follow the same value which we have in our notes so we have 8 7 12 15 2 and 5 let's add that okay but unfortunately we don't have this insert okay so what we need to do is we need to create this method called insert inside this B tree and you can see we got insert method okay but when you say insert that means we have to make sure that you inserting a node of course you're passing data here but it should be added to a node right and we don't have a node implementation yet so I can create a node implementation by saying class node now what every node will have now if you go back to link list a node will have two things the data and the next value now in terms of this node here which is a tree node it will have three things data the left node and the right node so I can say int data then we have to say uh node because when you say right node left node they are itself nodes right let's first write left and then right so yeah so we got data we got left node and we can we got right node I think the only problem is in some of the class I think in linklist we already have a node here that's the problem so what I will do is let me just move B tree and this main to some other package I I will just move B Tre to package let's say package com. telescope. three so sometime you have to solve the problems in a go need to import the package from here now importing done okay so problem resolve the problem was I was having two classes with the same name in the same package I just moved it to different package okay so now uh you don't have to do that if you're just writing the binary code you can just skip this line anyway so we got a note and then we are referring to left node and right node so that's node done but also what I want to do is maybe when I create a node I will I want to have a Constructor here which will take the data from me and I will assign that data so I can say this do data to data and that's it so we are basically done with the node concept but how will you implement this tree so every time you want to insert the data in a tree how will you do it so if you say this is a tree of course we don't let's say we don't have a tree here and let's go with the same value I don't know what values we have I can just use this copy okay so we got this value so if I want to draw a tree so let's say someone is giving you the value eight initially so what you do is you create a root node now we know that we don't have a node in the tree so basically the tree is empty in that case you will make this as a root node okay so how do we do that so when you say insert basically we have to create a root node that means every tree need to know about only one thing which is a root node right as I mentioned before for a tree the most important thing is root and then three don't have to worry about other nodes the no root node will take care of the next two nodes so what I can do is I can say node root and then every time I create insert and then imagine that this is a first time so what you will do is you will create your root node here so you can simply say root is equal to new node that's how you create a node just create the object and pass the data and that's it you got your root node in fact you know instead of using this variable as I it should be data it will make much more sense so now if you can see our code this is the thing we have done so so we created a root node called 8 and that's your root node which looks good but then what about if I insert one more data here so if I say three insert I want to insert the next record what's the next record it is seven so when I insert seven where the seven should go now don't you think the seven should go on this side is because we are implementing B search three 7 is less than 8 so it should be on the left hand side so when I say seven it should create a new node and we are doing it you can see every time you say insert it creates a new node but don't you think this time is not a root node it's a left node of the root node how will you do that and how do we even know that this is not a root node so in that case we need to do is we need to check if the root is null then you create a new node I mean then you create a root node otherwise you don't create root node every time so root node will be created only once if the root is null what else if it is not null then we can check for something else now see if you talk about about eight yes this satisfies is because root is null till this point when you before executing eight but when you insert seven we already have a root node right then we have to go for E Els now the question is now we know that this is not a root node we have to either go for left or right so what you do is you basically check for data if the data is less than root. data then you go on the left hand side so when you say you go for left hand side what what you simply do is you say root. left do daa is equal to daa that's what you do but then we have one little problem here because see this will work for this data okay so we will get new new uh thing but we not even creating a new node okay so that problem is we need to modify this to something else why something else let's say we have adding one more which is 12 goes here and then you add 15 now by our logic if the value is greater than root node just add it to the right hand side but to is already there right then we have to move to 12 and it becomes a right hand side of 12 that means we have to jump from root to the next element and then the next element oh that's a recursive process now so that means we have to add a recursion here that's one thing second problem is every time you insert data we need to get a hold on root you know writing the logic here is not efficient way what we should be doing is create another method which is called let's say insert Rec and we'll say this will take two things because when you want to do recursion we have to get a hold on root element or root node so that means we cannot do that in this insert because the moment you a recursion every time you call the method we need to get the hold on the Node where you're calling this so this will not work but that doesn't means we have to remove this I will tell you in sometime why so basically we have to say node root comma data and here okay this should return a node because we're going for recursion so it should return something and now here instead of doing all this logic this logic should be a part of this not this so here we can simply say root is equal to insert record so every time you call insert it will call insert Rec in which you're passing the root node and the data so when you say recursion it will go into recursion we will be using recursion on insert Rec not on insert so we have to change some logic here so this is correct when you say root I'm only concerned about this part so how do we implement this one so when I say I want to add element on the left hand side so if you have let's say two now we know that two should go here but then we have to jump from a to 7 7 to2 that's a recursive recursive process right so what you will do is let me uncommon this now this is not how you do it so basically you say root. left and then you call the function once again by passing the root. left because now we we need to follow the tree right as I mentioned before when you talk about the sub trees if it is not eight left then we have to consider this sub tree now this seven becomes your root so how will you send this as a root so you have to say root do left now this becomes a new root so when you call this function which is insert Rec here this becomes your new root and then you have to pass data as well so you're passing data but what if this is not smaller then you have to go on the right hand side so you have to say root. data if data is greater than root. data then do the same thing but for the right hand side so we'll say paste here so this should be right and this should be right and then at the end you know your root node so just return root that's it so this is the recursion call which we are getting here so that's how basically you create a tree now this is tree is created but how will we know that this tree is created we want to print also right and the way you can print it is like this so if you want to print this tree so let's say we have this tree here and this tree is created by the way we have defined this example here how will you print this now to print we basically have to Travis or we have to travel to different elements here the way you do that is we have different travel cell for tree so when you talk about tree travel cell uh we have to follow certain things first there are three three options here we have in order traversal we have pre-order traversal and we have post order the difference between these three is the way you print your root node okay so when you talk about in order basically what you do is you first go to the left then you go to the root then you go to the right so which is 7 8 12 when you talk about pre-order here here you first go to root which is eight so it will print eight then seven which is left and then 12 which is right when you talk about post it means first it will go for left then right then root which is 7 12 and 8 okay that's your post Auto D here we are going to go for in order so in order simply means first you print the left one then you print the AL not exactly printing but you go to the left one then you go to the root node and then you go for the right one so that's your in order now the way you can do that of course this is also recursive right uh so in order to do that what I will do is I will create a function called public white in order then you can work here but then since we know that in order is actually or traversal is basically a recursive operation where you go to it sub tree and print it or do some operations for the recursion I will create another method which is public void is because I don't want to return anything so I will say in order Rick so here we just want to print it now now when you say print it how exactly we are going to print how we going to travel this of course the output will not be similar to the value we have inserted it will have a different order so what it will do is first it will go for it will print the left one right so when you start from here it goes to eight but then we cannot work on the eight because that's a in order then you go to the left one because in in order you first go for left hand side left sub tree and then from Seven also you can't print seven because seven becomes a root node for the two so we have to go to two now we don't have any left here right this is empty so in this case you will first print two okay so you will go for two then you go for right since we don't have a left we print the root node because we are considering this sub tree now so you will print two and then you will print five so here we have five then you go up then you print seven because we don't have anything so that's a parent for two so we'll go for seven then we print on the right hand side we don't have anything right hand side right so this is empty then you go up you print eight then you go for the right hand side right hand side is 12 on to left we don't have anything so it will not print anything but you can print 12 and then you can go for right hand side which is 15 and if You observe you got a sorted values so that's why we use BST which is sorted values it is is easier to search as well okay this is the in order traversal okay so let's implement this so how do we do it so first we'll start from the left hand side but also we want to verify if the tree is empty if the tree is empty how will you print something how will you travel and how do you know if the tree Tre is empty by using the root element so we can simply check if root but how do you know the root because root will keep changing depend on a tree right uh so we have to also accept root here so we'll say node is root and then you check if the root is not equal to null then only you do it now how will you do it now first of all this is a recursive function right so basically you have to call itself so we'll say in order Rec and then you have to start from the left hand side so always start from left and then keep going till you find the last left element okay and then once you do that then then you have to go for the middle one middle one is root itself which we got here right so we can print this value here so I will say print and if You observe for every node for every sub there is always a root node If You observe for eight seven is Left Right but don't you think seven in this sub tree is a root node so that's a root we are printing when you go to two two becomes a root node right when you go five five becomes a root node for their child which which we don't have then we can so we can print data here so we can say root do data and then we can print uh a space in between as well just to differentiate the values and also I can skip the new line now once you have done with the root element of course not just printing you can do some other operations as well maybe you can add all the values if you want and then here I can say in order but this time you will go for root dot write that's it this is how basically you print the values but from in order we have to call in order Rec by passing the node so you will call this only once in order so that means I can simply create a tree by adding values here first of all now what values we have let me just do that quickly okay so you can see we have added all the values and now it's time for traversal so I will just print in order and it should do the job for us let's try so I'll just run this and you can see we got the values we got 2 5 7 8 12 and 15 that's what expected it is printing in order but let's say if you want want to do Post order so what I can do is I can just change their names or maybe I can say pre-order so I can say preorder and of course everywhere you have to do that now the only change you have to make is in pre-order first you print the root so that means this line will go up that's the only change you have to make in pre-order okay nothing much let's run this and save if this works save this code and run and you can see this is now pre-order so root element is something which will get print first I think this is a sequence in which we have added no different sequence but anyway you got the point so this is pre-order you can also do it post order let me know in the comment section if you have done that and yeah that's how you implement tree welcome back aliens my name is ready and in this course we are going to learn about G now when you build an application apart from learning the language and the tools to build the application there are other things which you have to learn about and one of it is get so that means irrespective of what kind of application you're building it can be mobile web Enterprise you will be using this tool for two reasons one for the Version Control and second for the collaboration and then this tool is actually easy it's just that the concept might been new and most of the thing which we do is through commands and of course you can do most of these things with the help of GUI because most of the Ides provide you with this tool but then when you work with the command line you have more power to change stuff so in this course we are going to learn different things first of all we'll understand what is g how do we do the setup how do you create your own repository how do you create account on GitHub how to insize the project commit the project and how do you push your code on the GitHub repository how do you create branches and we'll be doing that practically so you will enjoy this course and don't worry the course is designed for the beginners even if you have no idea what G is we will take care of so I hope you will enjoy this course and also make sure don't just watch the video practice The Thing by yourself so whatever I'm doing par you can do it on your machine and let me know how it feels or if you have any issues in the comment section get is a distributed version control system now the problem is if you want to understand gate you have to first understand what do you mean by Version Control right and then we can discuss about Version Control System then we can discuss about why it is called distributed and then it will make much more sense so let's talk about Version Control see the thing is when you make a project of course it can be any project it can be a software project or it can be uh writing a book or making videos it's all about a project right and when you make a project the way you work is you create different versions of it right okay so if you don't want to say version let's say you make multiple updates so when I start writing a book I will write something and then I will edit the same file or maybe I want to go back to my previous file so let's say if I'm writing chapter one I will complete that chapter one and then I will go for chapter 2 chapter 3 now at every chapter end I will just try to save it right and then on on chapter two I will try to save a file on chapter three I want to save a file or maybe while writing this chapter in between I will do the save right and saving is very important right otherwise if your laptop crashes you will lose all the data so as a writer we normally say contrl s not once multiple times just to be on the safe side the important part here is we want to make the copies of it now there are multiple ways of doing that what you can do is you can save the same file the thing is if you save the same file you will lose the previous file right or what you can do is you can create a new file so every new update will be a new file and as a software developer we used to do that in my college days I remember when I when we used to create project every time we add a new feature we used to create a new folder and let me know if you have done this or maybe you can just give a smile if you have done this we create a project and then give a name to it let's say the project is for a quiz application so you will say quiz app and then if you think this is now we have added few features we can say quiz app 1 quiz app 2 quiz app 3 of course it will have a new updates new features in that particular new folder right and then the moment you feel that project is complete you say quiz app final then you say quiz app final final because maybe you want to make some final changes and and then you realize oh there's a bug then you say quiz app final final Final very final okay so this is the type of folder we used to create the thing is why do we do this it's because what if you want to go back to the previous version what if you have created a project and then it's working and now you added new features and then you you thought it is working but it's not there there are some bugs you may want to go back to the previous version right now having that version called or having that power of going back to any version that is called Version Control and to achieve this we need to use some software right some system and that system is called a version control system so example if you're building a project you will be saving that file multiple times and then you will not replace the older project the older project will be there right so let's say you have a software with X number of features so you got the first two feature in the first version then you got the third feature on the second version and at any given point you can go back to the previous version also what if you are not alone in the project you have multiple people working in the same project how do you collaborate that's also important right so you will get those features with the help of Version Control and as I mentioned before you need a system so we we call it as a version control system now when you talk about Version Control System we can categorize into three parts here the first one is a local version controls which simply means that as a developer or or as a Creator if I write softwares or if I write a book if I write something and if I want to save in my own machine with different versions of course that is local copy right and in my own particular machine I'm creating different versions of it maybe having a folder inside a folder then having different folders of it or maybe I can just create a database where I will save all these files again that's my choice or that depends upon the Version Control System but the thing is everything is happening on my machine the problem here is what if I want to collaborate with other people I have let's say five people in the team and they we all want to work on the same project how will we do that and also what if I'm relying on my machine and then one day my machine says okay I'm done I'm not going to start again how will you get your projects and that's where you can't simply rely on local copy and that's where we have one more option which is called cvcs so VCS is version control system cvcs is your centralized version control system now in this case what you do is let's say you have five people in the network or five people want to work on the same project so what you will do is you will use a central repository not the local one but a remote one so all these machines which you are working on in different locations in the world you will be having a centralized version control system now at this point every time you want to make a change you have to get the copy from the centralized system make the changes in your machine and then you have to save it on the centralized version control system which is C cvcs now the problem with this is okay first of all let's talk talk about the advantage the advantage is if you're working in a fire of a group everyone one have access to it so they will know what you're working on so let's say you have done something and then when you say save now basically since we're talking about get let's use the word which we using get called commit so let's say when you commit something to the centralized server now at this point the other people will have a recent code they know what you're doing right but also there's a problem the problem is everything is stored on a central system right what if the central server says I'm done I'm not going to start again your your data is gone now right so that's the problem with the central ized version control system now that's where we have to look for an alternative where you should be able to have a copy on your machine as well and not just the current version you should have the history as well because in the centralized version even if your server goes down at least you have a local copy of the current version yes you have it but we don't want just current version what we want is the history right and that's where you can use something called a distributed Version Control System where you will have a local copy but not just the current version you will have the entire commit history so let's say you have created version one version two version three everything will be present in your local machine as well and you can you can collaborate with the other developers or in your with your colleagues basically so we have done with the first step of what is g get by understanding something called Version Control System Now The More theorical Concept you know it will be easier for you to work on the on the git software right so basically git is a distributor which means you will have a local copy of course you can have a remote copy as well there are lot of options available example let's say if we talk about GitHub gitlab bid bucket all these are your remote repositories so you will have a copy there plus you will have a copy on your local machine as well so let's say someday if you want to work in a long flight and you don't have internet access you can still work on your project because you have everything in your own machine so let's understand the history of git now it is interesting that's why we have this special video for it the thing is if we talk about any open- source project so what happens in open source project is we have multiple people working on the same project but you will say hey that also happens with the normal projects right the thing is in normal projects we have limited set of people let's say if you are working for a company and you are building a project you have let's say a set of people in a team they will be working on it and then you trust everyone because they are a part of a project in the open source project anyone can contribute let's say one day I decited to be a part of one open source project of course it will take some time for me to understand the project and then if I feel hey you know everything is good in this particular project so let's say it's any OS let's say we have this own telescope OS and then one day I want to contribute to that particular OS I want to add a new feature of let's say a Bluetooth connectivity just for an example I can write my own module and add that in the OS in my machine but if I want the entire world to use it I have to send them the new code right but then how this thing will work how multiple people in the world can contribute to one project in the earlier days we used to do something called archive files or sending the batches right now this is the same thing happened with Linux as well now if you know Linux Kel very famous One the founder is lus toal and he is very particular about how Linux should work what other the features we should have now initial days when 1991 to 2002 it was more about uh if you want to provide the features you can send patches or you can use archive files but then in 2002 they started using some some proprietary tool called bitkeeper now bitkeeper I think was free at that point and then they started using bitkeeper so that they can get the contribution from the world easily now they don't they don't have to go through a manual process of doing that so they can simply use bitkeeper and they can send their code and then they can do the merging as well now the problem is at one point the policy of bitkeeper changed and they started charging now the Linux was doing it for free and then the entire world was using Linux for free so they thought it's a good time now to move away from bitke keeper and that's where git was formed of course they tried to make it very different from bitkeeper so bitkeeper was working a different way and get worked in a different way again what is that different way that we'll understand once we start the actual implementation but that's how in 2005 get started it's not like someone thought hey let's do this version control system no they were using something else it's just that because of some issue between the bitkeeper and the community they moved away from it and they started using git as I mentioned before git is different from all different version control system right even different from bitkeeper and since it is different it has given some amazing features it is very simple to use it is fast uh it also allows you something called branching again once we move towards the advanced part of G we'll understand what branching means and it's a fun concept to learn and also to implement and it is fully distributed which makes gate very famous and I know we have done enough buildup and the question is how do we use git we want to collaborate and we want to work with Git so that we can collaborate with different people and we can build a project it doesn't matter is it a proprietary project you're working in a company or open source how do we use it and that we'll understand in the upcoming videos now it's time to install git of course right we want to work with those commands and as I mentioned before if you want to master git or if you want to use the features of G command line or basically commands are the way to go now how do you know that in your machine you have git now if you are using Mac basically in Mac if you have installed xcode you will get git by default but if you are using Windows you will not get that so how do we verify so it's very easy you just have to use the command prompt so I will open my command prompt here and or Powershell uh so you can what you can do is you can search for get version so basically this is the command which you have to fire and when you say enter you can say it says the term git is not recognized as a command okay so if you want to get git on your machine if you're using this on command prompt you will get a different message example if you do that on command prompt and if you say get version this is the message you will see so it doesn't matter which window you're using Powershell or command prompt both works but in both of the cases we don't have git now how will you get git it's very simple you can just go to the website or you can just go to Google and search for get download now this is the website where from where you can download the git the current version of git is 2.41 so we can get that one and it will ask you for which OS you're using now since I'm using Windows here we can click on Windows if you're using Mac you can go for that and as I mentioned before if you already have xcode installed most of time for some reasons you install xcode even if you don't develop iOS application uh so do check if you already have G by saying G hphone iPhone version if you don't have it you can follow the similar steps you can click on download there and you will get it for maic OS for Linux you have to set the path and that should be simple so let's go for Windows here and once you click on that it will ask you for which version so basically you can go for a St installer which is suitable for uh if you have only one machine but let's say if you want to move between different machines you can go for portable one but I prefer to go for a setup inside machine and 64bit because my machine is 64bit so I will click on that so it's only 57 MB so depend upon 58 MB so depend upon when you're installing it which version you have you will get that now once you got this download it's very easy to do the setup you will say click so given a prompt click on yes okay and we don't have a choice right we have to accept the agreement uh now this is a place where you will install G I will say next okay it says I already have G in my machine because I think I already installed it before but I will say okay go with that particular folder that's fine with me now it also give you something called git bash now at this point we'll not be working with Git bash we'll be using normal CMD and uh anything else looks good click on next Okay click on next okay so it will give you certain options you know when you want to edit something and get which particular uh IDE which you want to use or not editor which you want to use now I will go for default one which is Wim or you can choose whatever you want maybe let's choose notepad for this particular series I don't want to confuse you with how to use Wim commands so let's use notepad here and click on next now this is this is something important to remember now when you work with the First Command of Git it creates something called branches okay so we have this amazing feature of branch in git Now by default you will get one branch which you work with and they call Master Branch now when get came into existence way back the name was Master but recently again doesn't it doesn't look good right Master Slave so they went for a a normal word which you can use so we got main so if you can see we have an option of main here so I will go for let get decide and when we are creating the project we'll make it mean we'll not be using Master otherwise you can change it from here as well that perfectly works I will say let get decide uh next and here you can see I'm going to use command not get bash so I will go with that and click on next okay I will use open SSH and open SSL so nothing to change much I will let you know when you have to change the basic configuration fast forward and Marge yeah okay so when you say get pool this is what you to select I will say okay and next enable system caching required now it to some experimental features will not be checking at this point we'll click on install Okay so it will take some time to installation let's wait for it and as you can see it is installed I don't want to see release notes I will click on finish and done we got the G set up now how do you verify of course when you open your command prompt or Powershell let's say here if I try to do that once again you can see it say still says git is not a recognized command the thing is after installing get you just need to restart your command prompt and I will again open my Powershell of course you can search for Powershell from here and you can search for get version and you can see we got the Git Version so now git is properly installed okay now once we got the git installation now it's there right it's there in your machine but how do you make it your own every time you do get commit every time you want to push something you need to have your own identity of course we need to know right who is pushing the code because in git we have multiple people working together on the same project so whenever someone pushes the code on the server you should know who is doing it and that's where you need to have your own configuration the way you can check that is by saying get config and when you say enter okay you can see it will give you certain things now basically in G config there are certain things which you have to mention at least two things one is your name your username and second is your email with the same email if you're using a public repository let's say gitlab or GitHub you'll be having a public account right with the email ID so you have to use the same thing so if you want to know what are the existing variables we have so what you can do is you can say get config I want the global values and I want the list of values when you say enter okay so the thing is in my machine I already have this configured okay so this is not the first time I'm using get uh so I have this values but in your machine you will not be having this values so let's do the setup how do we set the values here now since I'm using my personal email so that's why it is blurred because of the Privacy issue uh but yeah so you can use your own email ID there so how do you set a value it's very simple you can say get config so let me just clear this screen once so if you want to config you have to say get config and then you have to say Global because you want to set it globally and then you need to mention user dotame and whatever value you have so let's say I want to set naven here enter so that is what is setting my name there next I want to set my email ID as I mentioned you need to set at least two remaining we'll see later user. email and I want to set my email ID and again for privacy issue it is blurred I will say enter and values have been set now if you can check the same command uh you will get your details there so that's how basically you can make this git your own now how do you create your first project as I mentioned before this is not for just for coding you can use git for any purpose the moment you want to create something with people or maybe if you want to create something maybe you want to write a book maybe you want to write poetries or essays you can use G there and you can maintain the versions of your stories so once we have done with this setup so now we have G Ino machine right let's try to use G now the thing is normally as a programmer we use G for different languages example let's say if I'm making a project in Java I can use git to create different versions of it and I will be committing it I will also push that on some PR public repository and then in the world we don't have just one programming language right we have so many programming languages now if I go with any specific language here of course it will create trouble for others people let's say if someone is doing into JavaScript or python they will not feel comfortable with Java right and the beauty is doesn't matter if you're writing a programming language or a essay some lines of statement English statements it's all same for git it's not like get recognize the programming language and say hey I for this programming language I will work in this way no that's not how git Works git basically says you give me the file give me the statements I will take care of it so G will not look for the code right so in this particular course we're not going to focus on any programming language let's say we are writing a code in multiple files doesn't matter the language so it's important for us to understand how do you store that file with Git how do you work with G to merge different files so whatever lines I write it will be normal English maybe at pseudo code for that matter but this can be used by anyone even if you're writing a book or if you're writing a script for a video okay so let's get started so what I will do is first of all if you want to type something you need a editor right now in this particular course I'm going to use vs code because that's very famous uh so in my machine I already have vs code installed so let me open that so I got my vs code open here and I will just reduce this size off a bit yeah okay so this is my vs code uh you can use any ID for that matter and then in this I will just open a folder so this is where I will be working I will be working in general so let me open a folder and let let me create this folder inside my user which is nven in this case and I will create this folder as first project of course this can be anything but let's say first project and opening it now imagine this as your working directory so what is working directory example doesn't matter if you're writing a book or writing a code you will put that in a folder this is what your folder is so you can say this is your working directory right and now I will open a terminal now as I mentioned before we need to do everything on a terminal right so if you're using vs code you get terminal inbuilt or if you're using some other IDE or some other editors let's say notepad in that case you will be using your command prompt right now whatever we did before we did that on a command prompt here we are using a internal command prompt of vs code we call them as terminal here again same thing different terminology in fact you can also select do you want to work with powers shell or do you want to work with any other thing so in this case when I say terminal It Is by default using Powershell so that's fine we have tested this on Powershell it was working so let's continue with it basically if you want to understand git you have to understand different areas in which git works now basically for git or not for G in general this is your project right so you can imagine this first project as your folder now whatever you do inside this git has no idea what you're doing okay you have to let get know that hey you know I want to track my progress I want to commit I want to create different versions right and to tell that to get you have to do something now basically apart from your working directory we have something called a stage in area now staging area is a place where git knows okay in your project you have let's say 10 files out of 10 files you have five files which you want get to track now how will you tell that and that's where staging area comes into picture you have to tell G hey whatever is there in the staging area please take care of it other files you can ignore that okay so staging area becomes important because that's where G works apart from that we have something called a commit history as I mentioned before you you can create different versions right and every time you commit you create one version basically and the beauty is you can go back of course I will show you those things but this areas are important so you are working with this directory and whatever you do in this directory G has no idea okay and if you say hey G do something with this git will say what are you saying bro so basically git has no idea what you're doing so you have to push that into a staging area how will I do that we'll see that later and then one more area we have is the commit history now how exactly all this thing looks like will we'll see that later but you have Commit history where you have to do committing now this two which is your staging area and your commit history they become a part of a different box which is your git lot of people do get confused between a working directory and a g repository there are two different things it's just that it looks similar so they say okay this is my G repository no this is different from uh the G repository this is your working directory okay so now what I will do is whatever files I have I will create here they have no so git has no idea about it right so I I need to create a local repository here so when I say local it means it's there in this machine what if I want to share this with everyone else in the world or maybe with my with my team in that case I will create a remote repository this can be done with the help of gitlab or bid bucket or GitHub your choice but those are the options we have now here uh we need to create a g repository or local repository the way you can do that is very very simple you just have to use a command git as we are using first of all before we're going forward I will say Git Version and you can say this is a version right now if you want to create your git repository local repository you have to use something called init okay so once you do that you it will create a local repository in your project okay so before going this what I will do is I will use one more command now this command is very important which is called git status and we'll be using this multiple times so every time you want to do something you will use git status a lot so when I say G status it says there's not a git repository okay so we don't have a repository G says hey you are trying to run status but where is a repository as I mentioned before the folder here which is first project is your working directory if you want to use Git You need to initialize git so you have to say git in it and this will create the empty git repository and now you have git here so where do we find it so you can see we have this path which is c/ users SL slir project SL doget now this is your folder of course this is a hidden folder but this is where it tracks everything so the staging area uh the uh commit history everything will be present inside that git folder and this is different from your working directory example whatever file you create here will be a part of your project but not part of dogit that's important to remember in fact till the end of the project or end of the series this is something you have to remember by default when you create a file it will be part of your working directory you have to let git know that this should be a part of git and that goes into staging area okay so now again let's do G status and you can see now git status works but then it says git status is on Branch Master oh now this is something we forgot to do remember when we were installing G I've mentioned that by default when you initialize your G project you will get a master Branch but what we want is a meain we don't want to use Master we want to use something else it can be main it can be uh demo it can be any any other name for that matter here we have the master we don't want Master but even if you have Master here it says there's no commits yet okay so we have not created something we have not done the committing as well again what is commit we'll do we'll see that later but at this point uh we don't want this name right we don't want master so what you can do is you need to remove git now do we have a way to remove git here no we have to delete this folder so you have to delete Dot G okay so we can say RM hyphone RF and we can say delete git okay it says why okay don't have sufficient rights okay that's fine I will just delete that from outside okay so how do I delete that so what we can do is here we can we cannot say hidden folders right so let me jump to that folder okay this is a folder right now here we have dogit we can simply shift delete and gone now if you try get status you can see it will say not get repository and now let's initialize it now the way you can do that if you say get in it it will by default give you a master we want main right in that case you will use something called hyphen B and Main so basically now you're saying that so again what is we'll talk about that b later when we talk about branches but here we are trying to say that when you initialize this project make sure that you have the main name not the master when I say enter okay it says in empty project created or empty G repository created and now if you say G status you can see the branch name okay so it's R right so the hyphen B is Branch here so branch is main not master in this case and we are going to continue with main till the end of the course now once we have done with the insuring of the project or initializing of the G how do you add files that we'll see in the next video so basically we were able to create a project here of course we got a empty folder which is your working directory and we also able to get get the git in it remember we have we got a main branch uh we have initialized the G project now basically the way you can check if everything is working fine you can say git status and we are doing this from last one video right so git status will give you the status of your git project it says on Branch main that means we are on the main branch now of course in future we'll see how do we create different branches and it says there's no commits yet so of course we have not done the commit yet and then the project itself is empty now what I will do I will create a very simple file now the thing is it doesn't matter what type of project you're working on a software project or a book project you do create different files right now I don't want to go for any language specific things here related to programming let me create a general file and let me name this as my first code dxt or dxt so instead of going for java. C.C CS which is cop. CPP let's go for txt now this can be a coding file as well it G for everything is same it will treat txt as any other file okay so what I will do here is well I will write a very simple statement and I will say hello world okay so that's it nothing much a very simple line in my first line of code now the thing is this project or this file is there in your working project right if you remember we have talked about those different areas right we have a working area so working directory where this is where you edit your files and as a programmer you work with but if you want get to handle this you need to add that in a staging area if you don't do that what happens let's say let's try now here what I will do is I will say again get status it says there is a untracked file now the thing is if you want to commit something if you want get to take care of some files for different versions you have to give that to get and the way you can do that is by adding that particular file to the staging area now at this point we have not done that and that's why it says so let's read everything so it says on Branch Ma that perfectly makes sense no commits yet of course we have not done the commit yet but it says there's a untracked file okay there might be multiple files but here we only have one which is first code.txt okay uh it says nothing to add to commit but untrack files present okay you know what happens let's say we have this file and now I want git to handle it now the way you can add it to the git by using something called a commit right so we use git commit imagine git commit as saving something but then git will not directly save this you have to make sure that you add this in staging that's what it says here now if you want to add a particular file to the staging area this is what this is the command you have to use so what is a command it is get add so that's the two words you have to use get add and then you have to mention the file name the file name here is my first code.txt okay and when I say enter now this is added in the staging area and now once you have done that let me just clear the file or clear this screen once you have added the file to the staging area when you say enter now you can see it says something it says on Branch main makes sense no commits yet of course but then it says changes to the committed it it says there is a new file added called First Code now is it available or is it committed not yet this is not committed yet it is just that this is into a staging environment yeah if you want to unstage it you can use something like this you can say get RM cached file uh you to unstage but we don't want to unstage it we want to commit it now how do we know that we have different commits as I mentioned before it also maintain the history right so whatever work you have done so let's say in your project you have till now you have done 50 commits and if you want to know all those commits uh you can use a very simple command called git log so with the help of git log you can see all the initial commits and you can see it says your current Branch main because that's my main branch does not have any commits yet so that means the five is there in your working directory the file is also there in the staging but not in the commit history now to achieve that to commit this we have to pass another command and that command is very simple which is the commit okay so the thing is one of the question you might be having okay so once we say commit it means it goes into the local commit right but how many times we should we should do that and after how many files edit so normally let's say if I can think about my project and if I'm working on one feature I will be thinking okay okay once I complete this feature I will say commit right so that's why the project will be stored that's not the case actually it depends upon lot of different factors you don't work alone right you work in a team so sometime your company defines how do you how should you commit how often you should commit uh your company defines what type of messages you should have that's right so for this commit you have to also pass the message uh so for this particular series we are not going to see when to commit it's just that we have to understand how do we commit okay again when to commit is something you need to decide based on your project but then there's a common saying commit early and commit often right so we are doing committing here now if you simply say get commit you can see it will not okay it is giving you some message here please enter the commit message for your changes and blah blah blah okay so what I will do is I will just edit uh go back because this is the we okay so abouting commit due to empty commit message basically commit has not done the thing is is when you want to commit you have to make sure that you also pass a message of course right every time you commit something you have to specify what changes you have done there so I can say uh if you want to add message you can say hyphen M and then you can pass a message my first commit now of course this type of messages should be logical uh it can also defines what features you have added maybe you have resolved some bug you can mention that there or uh I have seen few projects or few companies they also follow something called a issue track tracking so let's say if there's a issue created or some tickets created you also mention the ticket number which ticket you have resolved okay something like that and when you say enter now it says one filed change one insertion that's right so basically we were able to do our first commit here so this is your first commit it says create a mode this is the file name and now this is getting tracked by get okay cool uh after doing this I will say G status and now it say is on Main Branch nothing to commit working tree clean okay that's good now if You observe there's one thing here which I intentionally missed which is this part what is this number that looks weird right see the thing is git also provides you something called Integrity it means once you save something in a git commit you cannot change it without git knowing about it so basically every time you want to make a change git will know that something has been changed how it basically follows something called a check suum now check suum is something imagine check suum as a fingerprint for a data so example if every time I change something in a data it will change the check sum as well okay even if a small change in your uh in your code will change a check sum and git creates a check sum for every commit so if you try to change something here in the commit itself because of course a local file right if you make any changes G will come to know that hey it's not matching you have made the changes but I don't have any idea about it so I have the older version so basically checkm is very important now this is done with the help of sha 1 and it creates not this number of characters it creates 40 characters okay out of which we only focus on the first seven so if you can see these are the first seven hexa decimal numbers so we got F d914 D5 of course when you do it on your machine you will get a different number right because this is unique to this particular data but behind the scene this is not seven This is 40 characters to display it is only showing the seven characters here okay now since we have done our first commit let's try git log so git log will give you the log of all the comments when I say enter now you can see it gives you it shows something it says okay what I will do is I will just clear this screen so that we'll only see this particular message okay so let's see what it it says it says there's a commit okay makes sense and who is the author for this commit this person naen and that's my email ID which is hidden which is blurred and this is is a date so this is when I have done this commit and now when you see different commits here there's also something called header now this will make much more sense once we start working with branches but at this point uh we have head to Main and the message is my first commit the more commits you do you will see all those things coming up there and everyone have a different commit and this is the 40 characters thing I was talking about so that's 5 10 15 oh this must be 140 okay so we only see first seven right if you can see up here so when we created this this was the first seven characters right it's matching with this the first seven characters right but then this is in general or behind the scene is 40 characters now that's how basically you do your first commit so we are done with the first commit right and when we said get log we got this particular commit here and we are happy with it right we got the first comment basically so what I will do is I will make some changes let's say I want to add some exclamation marks here okay so when I do that and if I want to uh let's say I want to do the get status and as I mentioned before this G status will be using a lot so when I say G status it says oh G status on Branch everything is clean maybe the file is not saved okay that's the issue so you have to make sure you save the file because even git is not sure what you have done so every time you make a some changes save it in your working directory first and now if you see it says changes not stage for commit basically there's a file which we have created and now this is into a modified stage now that's right the thing is every time you make a change or let's say every time you create a file it is into working directory right and then you try to Stage it so that goes into the staging area so we got a working directory then we we move that to a staging area and then from the staging area we do commit so that it will go to the local commits right so those are the steps which we take so before making this changes we had data in the local commit and then it was there in the staging as well the moment you make some changes now it goes into again a working directory now this is not stag example how do I know this is not staged example let's say if I do a commit now so let's say if I'm doing a commit and this time I will say this is my second commit uh of course this is not a good way of writing message but just for learning purpose we are doing it so when I say enter now you can see it says changes not staged for commit basically if you want to commit something the data the file should be there in the staging area this is not so that's important so every time you make a change add that to staging area then move that to the commit now how do we add it we have done that before right we can simply say get add in fact let me just clear this screen so I will say get add and then I will say first code.txt and then when I say enter okay so now this is into a staging area and now once we move that to staging area I can say enter and now we have done with the second commit so in total we got two commits okay if I do a git log to check how many commits I have so you can see we have two commits here this was your first Commit This is your second commit and of course with every commit you get the message there you get the time uh which is mentioned here and then you also get the author who has done that so maybe if you are working in a team you doing some collaboration so you will also know who has done what commit and and that's important okay so now this is where my head is now the thing is imagine head is a pointer so even if you have multiple pointers in your local commits as I mentioned local commits I have multiple uh it's it's basically multiple commits right so you'll be having a pointer which one is the latest one or which one we are pointing to so here we are pointing to this one and again we'll focus this more once we move towards the concept of branching okay now what do you think if I add one more exclamation so what's the step first save it the moment you save it of course it becomes a modified version so it is not Stag and you can see we also get a message here or not a message but on the in vs code especially if you're using some uh idees which supports git in a good way and vs code does that and you can see it says M which is your modified and the moment you add that to your staging you will this will be gone but what if of course staging is important right so from working you move that to staging from staging you move that to commits but what if you want to directly commit something like this let's say I want to do the commit and I will say this is my third commit I'm very bad with commments but this should be something like added exclamation mark or uh solve a particular story if you're working in agile methodology but here let's say I have this particular commit and now I want to do the commit without staging can we skip staging and we can if you want to skip the staging part you can simply use a hyphen a as an option so say get commit hyphen a then hyphen M for the message and then you can pass the message basically you are skipping the staging thing so if I say enter now you can see the commit has been done without going into staging area Okay so that's important if you don't write hyphen a without staging it will say it is not tracked so if you if you want to move from the working direct to commit this is how you do it okay now uh if I do git log once again to check everything is good so you can see we got three commits U this is the third one recent one and if I PR enter you will see also see the previous commits so this is how basically you use hyphen a to move the file from working directory to the commits so let's try to understand the diff command so basically in git we have this option of finding the differences between what you have worked and what is already there in the comits so let's say if I make some changes here so what I will do is I will first of all check uh this status of this and you can see there's nothing in my uh staging or nothing to track and I will make some changes now so if I say hello world so that's the greeting and then I will say take input from user and add the values so let's say I'm writing a pseudo algorithm here and when I say save and if you can observe especially in in vs code you can see this green line This basically means you have added something okay in the file and now if I go back here and of course uh I can simply do a g status of course git status will give me the changes here but what are the changes I mean of course git status will show you that this particular file has been modified but what exactly is modified I want to know that so what I can do is I can say get and I can uh type something called Tiff now when you say get diff and when you say okay when you say enter it will show you the changes which happened in the file and these are the changes you can see it says uh there have been few changes the number of lines now this are the number of lines which you have added uh so we got hello world in fact there's a change here as well of course we have not changed here the only thing is I said enter right that's a new character now now basically this is the older one and these are the new changes now if you want to see okay I'm doing a commit it says it is untracked I want to know what changes I have made so you can use something called a g diff so basically it will check the comit it will check what data you have in your working directory and then it will track the changes and now once you have done that maybe I want to add this so I will say get add first code.txt and enter enter and now once you have added let's say get diff now you can see there's no difference the reason is now this is not into a modify this is into a staging right now once you move this to staging I can also commit but now what if I want to find the difference I've have added that to staging area and now I want I want to find the difference in this case again you can do to get diff but since we are into staging I can say hphone iPhone staged and now you can track it so if you have your file in the working directory and if you want to check the difference you can use get diff which is differences and if your file is into staging area then you have to use hyphone iPhone staged to check the changes because it's already into staging area once we know the difference we can simply do the commit now and of course for commit we have to set pass the same thing we have to say get commit and let's say I'm basically working on a project where you have each task given a number a story number so I can also say story 3.1 and I can say message is user input so basically that's how you can mention some messages which should be logical yeah and now if you see git log you can see the entire history so this is the recent one which you have done uh and then this is the this is your third commit and if you go down you will you will find more let's try to understand and how do we remove a file from a g repository so let's say you already created a file and then you have pushed that to a git repository and now you realize oh that's not the file I wanted to put that into git repository now why you will not put something in a git repository now think about this you're working on a project and you have multiple files and one of the file is basically where you put your credentials let's say I create a file here so this is where I will put all my password just for my references so I will say all my credits. txt and this is where I will basically put my username password so I will say username is nvn 02 and my password is rdy 03 so this is my basically username and that's my password and I just want to save it for my reference so that I I can just connect with the database or something and I want this to be only in my particular machine I don't want to post to This Server right and after this let's say we also have some read me files so we got read me.txt or readme.md basically we have a concept of markdown you know if you want to make some document beautiful uh we can use markdown to make it more beautiful that's what get uses so basically we get readme.md and this is a important file in in every project so we'll also see that and okay so you can see we got youu here which is untracked basically so let's say in me me I will say this is my first project author is N Ready company is teliscope okay so let's say we have this values here credentials we have already mentioned and let's also do some changes in the first code in the first code now you want to submit data to the database so let's say we have all these changes in the code okay this is Imagine This is a coding file of any language which you love and we have made the changes Here and Now what I will do is I will just see the git status when I say git status you can see it is it is showing that you have one modified file and that's correct we have done the modification and you got two files which are basically untracked and that's why it says you can see if you see here we have U which is untracked okay now I know what to do uh in fact if you do status once again you can see there's a option here as well uh no changes added to the commit use git add or git commit hyphen a so hyphen is basically an option of adding directly in one line itself but let's say if I want to add all the files in the staging how will I do that of course uh if you want to add all the files you can simply say get add and mention each file so this is first code.txt then you say enter again do it for the next one or what you can do is there's a shortcut you can simply say dot now dot simply means you can add all the files in the in the staging area enter and done now all the files are been trct so you can see now it says a which is added and if I say get status now so you can see we got a new file two new files and one is modified one and if you want to make the changes you can simply say get commit hyphen M and you can pass the message you can say read me and story 3.2 okay so let's say I'm adding this three and then you can see we got the files in the database now basically your get dat G database B now if I say get status what happens if I say get status it says everything is clean nothing to commit and now everything is been tracked in the git repository that's cool right but then the thing is I will now after working for let's say a few hours I will decided to push this to the GitHub repository or some public repository now at that point what happens is everyone can see my username password I don't want it basically I don't want this file to be there in my git one way you can do that you can just right click here and you can say delete move to trash and done your job is done but the problem is if you say get status it's already there it says it is deleted but is deleted from your working directory not from your git right so one thing you can do is you can say but if you do git commit uh that's that's tricky now right so what was the idea here is don't delete it from here a get status everything is clean don't delete from your working directory you have to delete for first from the git and the way you can do that is by saying G RM that's the command you have to use and then you have to mention that it is basically cached so we have to say cast and then mention the file name which is ceds do txt now the when you say enter it says removed and you can see now it is untracked so basically first you have to delete your file from the get now it is untracked and you can happily delete now delete moved and now you can say get statas everything is cool you can say changes to the changes to be committed so I will simply say get commit hyph M remove the creds file and done so now you can see we don't have anything here and we don't have anything in the git and that's how basically you can Safeguard yourself from your unexpected file uploads now if you remember when we talked about git we have mentioned there are different types of repositories right now basically when you can have git tool which is a distributed Version Control System you have a commit history in your machine right and then of course you can jump between the commits but what if you want to collaborate with everyone so let's say you are working in a team and then they are working on some feature you are working in some feature how will you collaborate and that's where we have to use something called a remote repository now what is a remote repository so example let's say we have six people in the team they have their own machine they have a local copy or local repository and now if if you want to share we need to use some remote repository some server which will be public so that they can fetch and they can do something of course this can be a private to the company or this can be public so that you can access it from anywhere but let's say at this point let's talk about public because we are not working for any organization where the security is most important we are learning it right so let's talk about the public here now there are different options we have in fact uh the options which you're talking about they also provide you something called private again we'll we'll see those options there uh okay so we have three options here there might be multiple repositories but multiple remote repository options but here uh the most famous is GitHub then we have gitlab the second one and then third one is bit bucket now bit bucket is given by atashian which is a very famous company they provide different tools for the developers uh so you can use this and few companies are using it most of the companies who want to use the private features the paid private feature they can use gitlab or they are using gitlab basically and then uh for the public uh for the normal people or for the public we have something called GitHub now GitHub also helps you to create your own profile and that's why if you go for the interviews or if you talk to people they will say hey give me a GitHub profile it's not like you can't work on gitlab or bit bucket it's just that GitHub is very famous among a normal public so we are going to use GitHub here of course you can switch to any repository your choice but let's talk about GitHub now first of all you need to have your account on GitHub okay so how do I get that it's very simple click on sign up enter details here whatever email you have and you can log in once you are signed up you have to also make sure that you're logged in and let me just do the login fast and I'm just hiding my email ID there so that we can keep it privacy and here we go so I'm trying to log in and that's my GitHub repository okay so I have lot of repositories here because uh when I teach something I also push that code on GitHub so that you can see and you can see all the repositories here there might be some private repositories which you cannot see but that's how it is okay so we have some repositories here now what if you want to create one now first of all how we can use GitHub it's very easy let's say if you want to search a particular project uh so what you can do is you can search for something example let's say I want to search for Linux gardel which is a very famous uh repository so you can see this is Linux the Linux kernel which we use it is open source and the entire source code is available here now the owner of this is lonus which is lus to World everyone knows it right so you can see this is the entire code of Linux and you can get this on your machine okay it's very easily now it also analyze the entire code base it will tell you which languages it has used so you can see 98.4% of this code is C uh some shell script some python script and make file so these are the languages they have used contributors you can see more than 14,000 contributors we have for Linux and you can see these are the some options so at this point when I'm recording this video there are 8,000 more than 8,000 people are on this page okay so that's interesting everyone want to look look at the code and they might want to modify it or they want to add some new features so basically for the project you can also give Stars example if you like this project you can give a star here or when you create a project people can give you star and that's how you become a good GitHub star uh otherwise or you can pay some amount okay I'm just kidding so so let's say we also have option of forks here now example we'll talk about what is Fork later and this is 48,000 Forks already happened here now this is the entire source code I can get this code on my machine it's very simple you can click on this code and you can click on download Zep okay and you will get the entire source code but we are not going to do that options we have two options here which is https and SSH we are going to do this but how will you use this it's very simple now when you have this project what you can do is or for this even to browse this you don't need a GitHub account I'm I'm uh if I'm not wrong uh you can get this you can simply copy this okay so you can copy the link link okay first of all we have mentioned the option right download zip that's one option second is you can copy the link and you can go to your terminal and here you can pass a command which is get clone now clone is basically copying something right so you have a code in the repository and now you want to copy it so you can say get clone and you can mention the address and the moment you say enter it will copy the entire Linux source code on my machine so you can see it is downloading it will take a lot of time because I think it's it's heavy I've never tried it or maybe I've tried it before but never completed the entire download you can see even with this speed only two person completed and that's the entire size so I don't want to do this I will just cancel this okay I don't want to download the entire stuff I just wanted to show you how do you get it okay so that's how you can copy uh there are different options here we have https and then we have SSH okay so we'll see later how do we use this but at this point you can see we have some repositories but what if you want to create your own repository it's very simple if you can see let me just increase a font a bit Yeah so if you can see there is an option of plus button so I want to create my own repository we have done that for the local machine right so when you say get in it it creates a local repository in your machine now I want to create a remote repository for that you just have to click here and click on new repository click on that okay this creating yeah now this is the page you will get now that's my ID GitHub ID and this is my repository name so let's give a name to it of course the repository should have a good name and we have done that for our own machine as well right uh example when we were doing vs code remember we created a project and that was your reposit name as well here let me create something so I will say get course because that's what we are doing and it is available so again this is not publicly checked it will just check do we have in my account so I don't have this name in my account so I can use it you can give a particular description here so you can say get course demo and then you can make your repository public or private now Linux we were able to search it is because it was a public repository now let's say if you're working on a project and you don't want to share the code with the entire world of course at that place you will be using a private one so of course you can share the link with your friends or not friends but with your colleagues and then you can work on it public anyone can search on GitHub so for this I will keep it public so that you can also see the repository later when you're watching this video and then there's a option of add a readme file so at this point I will not do that now readme file is important is because let's say if you have built a project and if you want to mention some steps what is this project how do you run this project uh how do you contribute so all these things you can mention in the read me file but at this point I don't want to add it there's also option of get ignore which we'll see later and then you can choose a license that licensing option here uh if you want to keep it open source and if you want anyone to use your things at this point I will not choose any license and click on create repository and done so you can see you got your repository here now when I'm recording this video there's option of go pilot for AI programming and I will not be using that also I can invite collaborators at this point I I don't want to do that just scroll down and you will see all the steps now first of all certain things which is very important you can see a link here now this link will change based on where you are clicking so if you're clicking in HTTPS this is what you will get and most of the time when you're downloading URL or some repository you will use https but we will use SSH so the advantage is okay let's talk about those Advantage later in in sometime so let's say we got this link here just try to remember we get we do get a link a unique link for for your repository this is unique now how do you create how do we work with this repository now one thing to remember we have a g GitHub repository which is on the internet somewhere I don't know where it is and then I have a local repository what if I want to make some changes how do I do that how do I push the code from my machine to the local repository or to the remote repository now to do that what I will do is I will open my terminal so my terminal was already opened it's clean first of all let me create a folder here and let me name this folder as git course okay same name but with my remote repository and then I will jump into this particular folder the git course and I will say I will check do we have any folder here you can see I don't have any files or folders okay first of all I'm I'm into Mac so LS there's no folder that's cool now what I want to do is I want to create some files now first of all we don't have any file let me create one file now it can be any file okay and you can simply simply follow this steps here to do that I want to push one file from my local repository to the remote repository and you can simply copy paste and run it will work but let's try one by one just to understand how it works so the first thing we have to do is we will be using we will be creating a file which is readme file remember we have talked about readme so to do that I will say Eco and I will push some data here so I can say get course demo and I can type multiple lines of data here but let's say we have single line and then I want to create a file called read me basically this is a command Eco is a command using which you can Eco this message to this particular file so we we'll say readme.md now MD is basically stands for markdown if you want to design some read me file and it should look good so you can use something called markdown okay and you can see if I say LS now we got a file created and if you want to see the content of that file you can say read me MD and you can see this is a Content so using cat command I can just see the content so I I do have a Content now so LS we got a file the next thing we have to do is we need to create a git repository the local copy okay the local git which is not done yet because we got a folder but it's not get enabled so to do that I will say get in it and we can follow the steps or otherwise we can also create a branch name because by default it will create master right we don't want Master we want Main but let's follow the same steps so you can see it has created a empty repository and that's done so let me clear this we got a repository as well if I say LS you can see we got uh we should have got the folder I think it's hidden so yeah so you can see we got a folder here which is dog now this is where it will have a all the folders which we have talked about before so this is done the next step is we need to add because if you say get status now we got a file but that is not a part so it is a untracked remember we have a working copy then we have to Stage it now how do we stage it we have done that before so I say get add read me dot MD so now we have staged it now after staging if you say G status you can see we got a new file and this will be committed now now how do we commit it's very simple you will say get okay I'm just trying to copy the code from here okay so on the left hand side you can see we have the code or the commands we'll say get commit we'll mention hyphen M for the message and here I will say first commit enter so that's my first commit right but again this commit is happening in my local machine so that's done next we have to create a branch so I will say get Branch okay now why do we have to create a branch here the thing is by default we got a master Branch right and we we have mentioned before let's not use Master let's use something called mean and that's what GitHub is also promoting so I will say main enter and you can see now we got a main branch okay now once you got a main branch what next it's time to connect to the remote repository now this is a local repository we need to create a remote repository but the problem is how will you connect your local repository to the remote repository now that's a big issue so we have to setar something called an SSH now how do I do that now basically there are two options one is https you can also get the steps here and you can see something is changing here if I click on SSH things are changing the the advantage of using so first of all when we use https basically to push anyone can push right this is my remote repository you can also push how do I know that who should who should be allowed now that's where when we use https it it will ask you to login so if you are saying you are naven how do you prove yourself so you have to login otherwise you can use SSH the problem with login is you have to log in every time what if you can just set up your machine once with a key and then every time you do something it will automatically detect who you are and that's why you have to set up the SSH now the way you can do that is by using very simple thing which is SSH keyen hyphen o this is a command we have to use SSH hyphen keyen space hyphen o now this is the only time you have to do it okay you have to do it only once it's not like for everything you have to do it for one machine you have to do it only once okay I just messed up there so I will say SSH keyin hyphen o enter and you can see now now it says generating a public and private key basically that's how you create a key to communicate between the client I mean your local copy and the server so it says enter the file name in which the file will be stored I don't want to mention anything here it will go for default name I will say enter the thing is in my machine already have this created I will just overwrite I will say yes you will not get this option if you don't if you have not done this setup before then it will ask you for a pass phrase I don't want to mention any pass phrase It's like a password enter don't want to mention anything and done I got my my file created now where will I find this file what I will do is I will just go back to my folder and if I say LS in fact LS hyph a okay first of all we got SSH created this is not the SSH okay let's see the SSH where it is so if you can see this is the folder which we are concerned about this is where it went so I will just go to that folder using cdspace do SSH enter uh let me just clear this screen and now if I say LS hyphen a you can see we got our Pub published here let me open that so I will say cat ID do Pub and we got a key right this is where you will find your key in the pub now where do I use this key we got the key here right but how do I use it it's very simple you can just copy the entire key okay copying is done where to paste it it's very simple if you can see here let me click on here I don't want to lose this page so I will just open a new tab using the control or command button I will click on settings and you can see setting is open here and then if you scroll down there's a option of SSH and if you see this I already have some keys here ignore that I will click on new SSH key I will give a title to it git course one because I already have one before and then you paste your key that's it and click on ADD SSH key and now your client and your server is connected so connected is not does not mean it's continuously connected every time you push something it will verify okay it's coming from a right person because I can see the SSH key which is mentioned here and on the local machine they are same okay so now once we have done that in the terminal let's go back to our folder so which folder we we were working with I think get go yeah and if I say LS hyphone a just to verify and you can see we have the same thing now if I go back to my steps so we are done with this step right branching now we'll just use this particular one and that to for SSH okay so I will just copy this and paste it here now what we are trying to say in this is we are doing git remote because that's a remote repository of GitHub we are adding a origin with this link so this is where we are trying to connect and when you say enter connection done okay so basically we are trying to map it once you have mapped it now we have to move this file which is in the local repository because this is in local right the remote is empty at this point so what I will do is to push the code just copy this paste so what we are saying here we are saying get push push means we are pushing the data from the local copy to the remote copy and then we have have we are also mentioning that there's a different branch and uh origin so basically U is Upstream set up set up the upstream and then the branch name is main okay this is something you have to remember sometime you will messed up with Main and master if you referring to some other documents in the earlier documents earlier versions people used to mention master and now we are mentioning main so you might get confused there and if I say enter it is pushing the code okay it will take some time uh but before pushing it is verifying hey the key is there is it the right key yes I I think I I only have one key here enter and done the pushing is done that's what it says uh it says counting objects done done total uh done setup took track okay looks like everything is done how do I verify I can simply refresh my page and we got it now this is a file which we created in the local machine right and if I open this you can see this is the data we got okay now this looks cool right so basically we were able to push the repository from the local machine to the GitHub and every time you work on a project now these are the steps you have to follow so so basically we were able to push the code from our local repository to the remote repository right and then we follow some steps and it is done now that was your first commit right but what if you want to do some more you want to build the entire project and you want to push of course you will not do it from normal command prompt you will be using some IDE to build your project now this can be for Normal book writing as well or making a video script as well uh but let's stick to vs code here so of course you can use any ID for that matter or any editor so if you're using a normal notepad of course you have to use a terminal command prompt external to your editor but since in vs code we have creditor editor as well and then we got terminal so what I will do is first of all if I want to use vs code I have to open that folder now which folder now if you go back to your terminal uh this is where we are right so if I clear and if I say PWD which is present working directory I am into this folder which is G source so whatever files we have here is into this particular folder which is git Source now I will open that in vs code so I will click on file I will say open folder it's in a git course open so basically I have opened the same folder okay so we got that file here right so if I open this this has the same content which we had earlier but what I will do is instead of working on this file let me add some more files or at least one file so I will say let me say this is my user service. dxt now basically let's say if you're building an application the reason I'm doing txt is because it can be any programming language right Java python C it's your choice so let's say if you're saying user service we are building an application where I want to do something with user service maybe I want to accept user data I want to process some data on it so let's say user service and here I will say get the user data so that's it nothing fancy just one line of code in fact this can be a normal programming code as well but just to keep it simple for all the languages we are using a normal text so we are saying get the user data and save now if you see this particular file is there in your working directory it is not even there in git and also this file is not even there on the GitHub this is what we want to push on GitHub how will I do that so let's go back to vs code and if you want to do any any of this we have to say new terminal and we got a terminal here and here basically we have to say git status now when you say git status you can see we have a git repos inside this particular folder but this file which is user service. txt it is untracked we need to track it so how will do it it's very simple uh to track it we can simply say get add and you can mention the file which is user service. dxt enter and now we have added that to into staging area How will I know that let's say get status and you can see now it's into staging area and we can commit it how will I commit get no at least I think the at this point you remember all the commands right so get commit hyphen M and you can pass a message so you can say user service created now it's always a good idea to type at the same time when you're watching this video so that you will know what is happening right so here if I say get commit and now we have committed it and that's that's why you can see the U here went off now okay committing done but does that mean it is available on GitHub no we have done the commit on on the local machine right now we need to push it on the GitHub and for that we have to you remember that command get push so let's use get push and then you have to mention origin so basically for pushing you have to say push and the origin is the URL remember the URL which we have used before that is what is representing this origin and then you have to mention the branch the branch is main we're pushing on Main Branch I will say enter it will take some time and done you can see pushing is done so from your local machine to the GitHub let's verify go back to GitHub now it's still not here refresh and you got it so you can see we got this file which is user service. txt and if you open this you can see we got data which it says get user data it is available now right so let's get back to our project this is these are the two files we have and if You observe we have two commits here now what are the two commits if I open this two commits commits here this is the first commit which we have done where we created one file which is with me. MD and this is your second commit which is user service created now you can actually at any given point you can go back to the earlier version where you don't have let's say if you don't want user service I can click on this particular file and it will give you the status of that commit so in the in the first commit we had only one file right that's what you can do uh so it's it's fun to navigate between different commits if you want to uh go back to any commit and if you want to see what changes you have made uh in fact you can click on the file as well to see the changes so if I go back here and if I me if I go back to any file okay let's go to commit and click on any commit here so it will mention what changes you have made so we have created this file which is readme.md and this is the change you have made in fact you can go to the other commit as well which is here and you can see what changes you have made of course it makes sense when you have multiple files and you can see all the files which are changed okay so that's how basically you add your files to the remote repository let's talk about tags now what is tag we'll talk about that later but before that I just want to show you one more thing which is about this remote now if you remember in the earlier video we have done this push right so from the local machine we were able to send data to This Server right so B basically we were able to push it now it has four things first of all we have a git command of course because we using gate tool next we have Push basically we are pushing the code right whatever data you have and then we have something called An Origin here okay so so we'll talk about origin because that's very important for this part then we have we are specifying the branch name so it can be main it can you master or any other branch which you create let's talk about this origin why it has to be always origin of course we have done origin but it can be different as well the thing is for this particular repository what we have done is when we talked about the remote server we only have one example if I say get uh remote hyphen V so this this is the command using which you can see which is your origin so we can have a origin for fetch so from the server to client this is the link we have to use and from client to server again we have the same link because we only have one remote repo there and by default the name is origin of course this can be any name you can change it as well so you can rename it or you can connect to different repositories not just one and you can pull and push to any other repositories if you want the only thing you have to do is you have to change this one so instead of pushing it to region you can push it to some somewhere else okay so keeping that aside now let's talk about tags now basically uh we have a concept of tagging now if you have observed every software which we use it has a version number example I'm using vs code here and if I go to help and if I click on uh not Co not help here because in Windows you will you will get that in help in Mac you will get that in the above so if you go to about vs code here so you can see it is giving you a version number here so every time you download a software you will get a particular version of course you will also get the updates for it so you can simply go here and I can check for the updates so you can see it says restart to update but I don't want to restart now but you will also get update this so the current this is the current version and this is a commit now this looks weird right why do we have a commit in the about that will'll see in some time and also observe some things because in the next video talk about this thing in detail okay so we have electron we have chromium we have nodejs and all this have versions okay so we'll talk about it in sometime but important is we have a version here now this version is basically a tag so let's say if you are working on a project and then you have done several comments and now you think okay I'm all set to release my next version so you can give a tag to it and then once tag that once that version is done you can go for the next version so basically how do you give this tag the version number to each commit so not exactly each commit let's say you have done 10 commits and then now you think okay the software is up and running we can publish this particular product to the users we can give it a tag first of all how do I check how many tags I have for this project now the way you can do that is by using something called get tag and when you say enter it will show you all the tags which you have given till now for this project and this is empty is because we have not given any tag so let's give our first tag here how will I do that it's very simple you can say get tag and then when you say tagging there are two options here one you can use annotated tagging or you can use lightweight tagging now if you really want to release this to the users and if you want to give more information about the user I mean who is committing it or who owns it so if you want all those information use annotated okay otherwise you can go for lightweight which we not going to see so let's see annotated here where every version will have different uh information about it so for that we have to use a hyphen a as a option for the annotated one and then now you can mention your tag number so basically these are your version number so there are different ways of using version number uh some people simply go with 1.0 like this version or you can also mention V here so V 1.0 so we can go with this particular one which is V 1.0 this is our version which we specifying here and then you can also provide a message in fact we need to do that otherwise G will open a editor which will where you have to mention this otherwise let's say m to give the message and I can say first release so you can give something like this as a message and when you say enter now tagging is done so you got the first tag now if you want to check you can say get tag again and you can see we got one tag here and of course uh you can also see detail information uh so you can say get show V 1.0 now this will give you description about this particular tag and when you say enter this is what I was talking about so for this particular tag here uh this is the the tagger who has given the tag and then so that's my name and my Emil ID which is hidden uh so the commits which I've have done here is this is the commit which we have done and this is it so I have done this okay first of all let me just check all the thing done okay so if you see here we have done the tagging right but the tagging is available only in my machine in the local copy so every time you push example let's say if I do some changes here uh I want to say this is get user data and I will say process so basically we have done some changes right process the data so we have made some changes to the software and now uh every time you make a change you can say get status you will get the status what's the next step okay so we have to add so you have to say get add user service. text what next again we can say G status uh it says now we can do the commit let's do the commit here so I will say get commit hphone M and you can give a message now I can say uh processing user data so we have added the new feature right so let's do that and of course in the real life project it will be a bigger code we're just trying to keep it simple enter committing done but the commit has happened only in local machine we have not done on the remote because if you see this is a local one right if you go back to your remote uh and if I go back to get course you can see uh we have done the commit yesterday so basically I'm recording the video one in a day so we have not done the commit here so if I open this we only have get user data we don't have process user data so we have to push right how will I push from my command line uh I can say get push origin main right this is the command you have to use and when you say enter so it will try to push now and that's done so pushing on the server done let's go back and refresh so you can see it says now and this is a new commit which we have done and if you see the data we have added new line okay this is working right but then if You observe on the server there's no tags here you can see Zero tags in fact we before the commit we have created the tag in fact you know after the commit we have done the changes right we should create a new tag here now which is V 1.1 this is a new tag and we'll say a message we can also do a daily release right 27th June release so normally you know a lot of companies they release their product in six months or one month let me do it every day so I will say enter now so we got a new tag how will how will I know that we have different tags here so we have seen the command right which is get tag now you can see we have two tags but if you go back to this server we it shows zero tags here so if I refresh zero tags so what we need to do is we need to push the tags as well separately we cannot simply uh push the code and will have a tag no you will not get that so what I will do is I will say get push origin and and we have to mention the tag so let's push the 1.0 and that's done so you can see the new tag is created and if I refresh you can see we got one tag and if I click on this tag this is a version one and if I see uh this in fact you can download the source code and it happened 4 minutes before right okay so it will give me the source code itself okay we don't need the source code okay but you got the idea right so basically uh we can jump between different codes based on the tags as well and now let's see let's also push the version 1.1 so we got two different tags and if I refresh you can see we got two tags here and based on which tag you have you can download it so this is 5 minutes before and this is 1 minute before so different tag will have different codes okay so you can see this is first release and this is the 27th June release awesome the this is working this is how you basically work with the tags so to this point we were able to work with our own repository on GitHub and local as well but what if you want to work with some other repository maybe you want to clone the code and you want to see what is happening there or maybe you want to push or you maybe you want to do some changes and use it for yourself or push it to this remote repository again how will you do it now first of all let's go let's find some project now since we are using vs code which is open source so what we can do is we can go to GitHub uh so we can do a search here which is vs code now if you search for vs code you can see you will find the source code this is the source code uh which has so many stars here and this is from Microsoft so let's use it now this is the actual source code of vs code now the version which we are using is 1.79 remember when we were doing in the last video if you go back code about so this is a version right 1.7 9.0 so the current version they have is what so you know every project will have a readme file you can open readme file which is here it will have some updates right otherwise you can just go here and you can look at the commit history as well so this is the commit history and you will also find tags here okay so if you go back to code and if you see tags so you can see this is the latest version that's verified uh which is 2 weeks back and this is a version which I'm using 1.7.0 and if you want to see the commits of it so there are so many commits for this particular tag uh and you can also see the you can also see the numbers so how will I how will I get the source code so if I go back to vs code and we we have seen that right so if you want the source code of course this will be a bulky project so if I click here you can see this is where local this is where you can get the thing you can also download the zip but we don't want to do that what I can do is I can use https or SSH and of course I will not be pushing any code so https works for me I will just copy this and go back to in fact I will not do that in vs code let me use a normal terminal so I will go back to terminal here and I will say let's go back to the folder okay let me see where I am so I'm into my home folder let me just clear this screen and this is where I will say get clone I I will mention that particular URL the moment you say enter it will download the entire vs code repository in your machine so it will take some time so basically the entire project which we have here we are getting that in this machine so by time is downloading let me see if I can find something interesting here so the entire vs code is built using typescript 2 person JavaScript there's also rust elements here and and CSS HTML and others and you can see the number of contributors more than 1,800 contributors that's great and then there's also branches here which we'll see later but if you expand this you can see there are so many branches here created important is the main branch which is most important Branch here okay done so you can see I got the vs code and what I will do is I will open that vs code in my vs code okay this is inception right opening a source code in the vs code so let me go to vs code and open that folder so I will say say open folder and in nen we got vs code open and voila this is the entire vs code source code right uh this is the entire code in fact you can also verify with the readme this is the readme which we had there right okay now what I want to see I want to get some information about it so what I will do is I will say terminal uh and I want to know some things from this so basically I want to see the log first so when I say git log and look at the all the commits which happened here uh so you will see their name who has committed it and what is the email ID you can see the time when it is committed and if you say enter you will see all the list right we got the entire list and it's it will be huge now if you want to come out of this just type Q and you are out okay and now what if you want to see all the tags I can say get tag because now we know the commands right and these are the tags so they started with 0 10.1 and if you go down the latest one is 179.2 let's see so if you go down okay still searching so these are all the versions of vs code okay so you can see uh 1.9.1 is a last release or they have the tags I'm not sure if they have publicly Rel released it but they have the tags and this is a version which I'm using and maybe I want to know more about my version which I'm using so what I can do is I can say get show one 1. 79.0 that that's a version I'm using so you can see this particular tag got a commit from this particular author and on this day oh great and I can also see what's what they have done by exploring this commit this particular commit okay you can see I got the commit number now so this was I was looking for right so I don't know why I was not not able to find that but yeah it's here in fact when you say get log you can see the lot of logs here right now if you want to print only one I mean the commit number and just some information there's one more way you can say get log pretty equal to one line so what will happen is by Comm by entering this command which is git log pretty one line you will see the commits and the uh the message there and this is how you message right so you will also get an example by looking at other people's commit how they message something so you can see we also have some Number issue number and uh upgrade numbers so it was always helpful to look at other people's comments or messages so that you will know how you should be writing your messages now what if you want to make some changes let's say if I go back to any of the file this is the first time I'm downloading the vs code on on this machine or or exploring the vs code source code I can just go get into source code and vs let's say code uh there is some node code here okay so let's say in this particular file if I want to make some changes I'm not sure will it work if I make a change but if I make some change let's say uh is anything where I can type any message here an existing message or let's say I just want to say uh on Windows let's say I give a colon maybe I have done a very big upgrade to this particular project now how will I push it of course you have to commit in your local machine first so what you will do is you say if you say get status and you can see that I have changed one file this is a file we which we have changed c. TS and if you want to you have to add it first you have to say CLI dots okay so first of all you have to get the path right uh you have to go to that particular path and then you have to say commit so if I say get status again uh this is the path which you have to go for so I will just copy this let say get add enter so that's the path done and now I can simply say commit so get commit hyphen M and I will say major update and I'll say enter and done the commit is done but that's only in my local machine okay and now if I want to push this to the server let's try what happens even if go something goes wrong we'll see later how do we solve it now first of all I want to know the remote right because how will I push so remote is origin so I will say get push origin main I'm just trying to push in the main enter and it will ask you for the username password the reason it is asking me to sign in is because we have used https right and if you want to use if you don't want to mention username password uh basically we have to use the SSH now even if you are logged in it will not allow in fact let me just try that once again I don't know why I have cancelled it okay so it's authenticating and done so you can see I'm logged in but it say is permission to Microsoft vs code get denied to this particular username so I'm not allowed to do that right so I can basically Fork this project or maybe I can just create a branch even that is not allowed let's try if I can create a branch here uh you can see you cannot create your own Branch right so you're not allowed to do that so you have to be a part of a contributor here so once you're a contributor then you can do that that's how you basically work with the projects okay so we have done enough talk on this uh we'll try to see how do you push in your own project in fact we have seen that uh in the upcoming videos we'll also work on branches which will be quite fun so yeah that's it from this video where we have seen how do you download a source code and do some experim experiment with it we'll talk about the git Branch now this is a very awesome concept which is there in git and this is what makes git one of the best version control system now what exactly branching are how do we use it how do you merge to branch branches and what will what may go wrong when you merge two branches we'll try to understand that in the next few videos so let's try to understand what is branching first and for that we need to get back to our project now this is what we were doing right on vs code uh we were able to get the source code from the GitHub but we don't want to work with this of course because even if we can do some changes I will not be able to push on the remote repository so let me go back to our repository so I will say open recent get goes now this is a project which we are working right and if You observe you only have two files here nothing fancy just two files and we got readme.md we got user service text okay makes sense and then uh the first thing you have to do is you have to always check G status if everything is committed uh you don't have anything in the unstage OR modified stage so you can see uh we have git status everything is clean so can go forward now if you go back to your GitHub and if you see we are into git course now so basically if you see there are different things on this screen one thing interesting is we have one branch now what exactly this Branch means see the thing is whatever you do okay whatever work you're doing and you're pushing from your client side to server side or your remote repository you have a branch and this is going into the main branch now this is the name of the branch which is Main and by default when you create cre a project of course uh you you do get either Master Branch or main branch based on which version you're using and which tool you're using so you have to check this what is your branch name first of all so for me it is Main and you might have some other Branch that's completely fine and there's no rule in the world that you should have only this Branch name or that Branch name you can go with any branch name that's your choice but commonly we use main uh nowadays and you can see we have one branch that also means we can create more branches right so what about by default you're doing that goes into the main branch right and if you remember when we were pushing the code from the git client or The Terminal to the server we were using mean as a branch so we used say get push origin mean right but what if everything is working so let's say you have a project everything is working and now you want to do some experiment now one of the thing you can do is you can of course you can do experiment let's say uh I'm happy with my code uh which is working fine and then I want to do some experimental code in this user service maybe I want to create an AAR for a user create an AAR for user and all these are experemental okay I'm not sure how will I do it so I'm just trying to add this feature and now let's say this is a very bulky code and this is not the only file you changing maybe you changing multiple files and you thought it would work and now it's not working okay and then U the release which you did to the users the earlier version it has some bugs okay and how will you you solve that bugs now because the moment you make changes and when you push this to the server or this when you push this to the GitHub repository how will you come back because you have updated your main see one thing you can do you can uh you can go back with the commits you can you know we can actually time travel between the commits so you can go back to the previous commit but again why to do all those things what you can do is whatever experimental feature which you are working on or maybe your colleagues are working on so you can work on a different branch now this will not affect your main branch okay example something like this you can just click here and you can have an option here of creating a new Branch you can simply type let's say a feature one now this is a new branch which I'm creating here okay so you can do it from here on the GitHub or which is the best way we can do it from the command line right so let's go back here and let me create a branch so what I will do is whatever changes I want to make I will not do that in the uh main branch I will do that on a separate Branch now first of all we have to create a branch so how do you create a branch it's very simple you simply say get uh there are two options basically for branching one is checkout now this is the older one and most of them are familiar and used to check out the new feature or new the the new command which we got is switch I think this came in 2.23 version so it both are okay if you want to use checkout if you want to switch that's your choice now for me I feel switch looks much better than check out because sometime you get confused with all these words right when do we use pull when do we use fetch when do we use check out so switch actually makes sense when you want to switch between two different branches right so but then let's go with the older one which a lot of people are familiar with so you can simply say check out and uh you can mention the branch name so the branch name is feature one now this branch is actually not created okay so we don't have this Branch yet and if you type this command and if you say enter you can see it will say the feature one did not match any files known to get so basically we don't have a particular Branch name feature one so we need to create one so for the first time you have to create a branch right once the branch is created you can switch using with using git checkout feature one so what I will do is I will just type this command once again get checkout now since we we are doing this for the first time if you want to create a branch you have to say hyphen b or you can also sayy Branch both works and the moment you do that it will create a new Branch okay so now the branch name is feature or feature one and it also says you know switched to a new Branch feature one so basically now you have two branches you got a main branch and you got a feature one branch how do you know how many branches you have so what you can do is you can say get branch and you can see now we have two Branch we have feature one branch and we have a main branch now the question is which branch is active and that's where you will see this star okay so star means you are I mean this is basically a pointer which means currently you are on feature one one feature one branch and you can see it's also in green color now whatever changes you make will be happening in feature one let me show you let's say I will do some changes here okay so I'm adding new feature because this is experimental Branch right so I will say create an AAR for user okay I will say save and Imagine This this is a lengthy code I'm just typing one line but uh yeah it can be a leny code so once I do that what I can do is of course I can say get status and you can see in git status it says on Branch feature okay it's not saved okay my bad uh I was expecting some uh unstage and then it was saying something okay now it is you can see I failed to save it and now I have saved it so it says modified now once it is modified you have to say add and you have to mention the file name and then you have to say get you have to commit this right so you have to say get commit hyphen M and you can send the message you can say uh experimenting with user outar okay so basically uh we are doing experement with the US user out that's what I'm saying here and I will say enter now this is committed okay now question arise in which branch it is committed it is committed in the feature one branch okay main branch has no idea what you're doing with feature one branch so you can imagine imagine uh this is your main so this was your last commit right before doing the branching this was your last commit and main main knows about this commit now what we did is we have updated some feature in a feature Branch main has no idea that's a new commit and only feature knows it feature one branch knows it okay so this looks cool right now if I say get log so you can see it will give you all the comments this is a recent one you can see this is a recent one it says experimenting with user out just try to remember this now what I will do is I will just go back to the main branch and look at the code as well so this is my working area right look at this code so the user service has one more line here right but if I move to Main Branch how do I switch how do I switch between these two so you can say git Branch or the git switch so either you can use check out or you can use switch I'm using switch now you can also use checkout that also works I'm switching to main the moment I do that look at the changes that line actually went it's because main has no idea about that new line but if I go back to feature once again you got the new line so this is a beauty between when you work with different branches right uh in fact if you have more files in the feature one it will also show those more files and when you go back to main again it will come back so that's how basically you create a branch and you work on it of course you can create multiple branches There's No Limit but what is use of creating a new Branch right of course if you can do something you can do it on Main Branch and if you're doing something new on the feature one branch how will you get that data in the main branch again that's a question we still need to answer but we'll do that in the upcoming vide now in this video let's try to create some more branches so that we can also see how to delete a branch and also what's a shortcut to switch between branches so let's try this first one let me create some more Branch here so I will say get how do I create more branches I can simply say switch of course you can use check out or you can use switch that's your choice if you're using a checkout you have to use hyphen B option and if you're using switch you have to use something called hyphen C now you can say hyphone C or you can say hyphone iPhone create that's the option and then you can mention the branch name let's say I'm creating a feature 2 Branch okay so I'm creating one more Branch now so in total how many Branch we have we got three branches how do I I so either you can say get Branch now this will give you the list so there's one option another option is hyph iPhone all again both does the same thing uh but the difference is in this G Branch you will see the local branches in git Branch all you will see the local branches as well as the remote branch and if you can observe in the remote we only have one branch at this point if you go back to your GitHub we only got one branch okay we don't have this feature Branch yet so so we only only have one branch but we'll see how do you push a branch to the GitHub now at this point we can see we have only three branches so we can check those things with the help of git Branch or if you want to see uh the remote as well you can say git Branch all now let's say again if I say G Branch uh we are into feature two right now what if you want to move back to the last Branch now since we say switch it has cre a new Branch but let's say if I want to go back to the previous Branch I can simply say get switch hyphen the moment you do that now you can see it was switching back to feature one because we came to feature two from feature one okay so if you want to do that shortcut of going back to the previous Branch you can say hyphen that will be making it easy now once we have seen this let's also talk about deleting the branch so you can say get branch and you have to use hyphen D so if you want to delete a branch you have to say hyphen D or you can say hyphen hyphen delete that's an option and then mention the branch name which is feature two let's say I don't want to work on feature two so you can simply say feature two and done so you can see it has deleted the branch and now if you check the branches you got only two we got feature one and we got me so now let's try to push the branch which is there in the local repository to the GitHub remote repository so you can see we have a feature one branch but if you go to GitHub you don't have it there so even if I refresh so you can see in total we got one branch so let's create one so what I will do is I will go back to my vs code and this is where uh okay even before pushing it I just want to make few changes so that it will look good so what I will do is first of all I have to check which branch I'm into I'm into feature one branch and in feature one let me create one more file which is for the admin so let's say this is admin service. txt and what admin can do is add and remove Mo users change the rules okay something like this okay so again I assume I'm just writing one line of code but behind the scene there are multiple lines of code so that it will look good okay so now what we going to do is first of all we are into feature branch and in feature Branch if you say get status there's one file which is untracked so we need to basically add this file which is the admin service now once you have added it it's time to commit so I will say adding admin service okay so basically we have added one more service now and if I say uh get status everything looks good but if you see now we are into main branch right so if I see the branch soorry we are into feature one branch and now if I switch to the main branch just look so the moment I say enter see what will happen to the files the moment I say enter you can see there's one file which I have been removed now because the main branch has no idea about this file and that's why you can see there's also cross here but in main we only have two files the moment I go back to the feature Branch now feature one and if I say enter and now you can see we got three files so this is this is how basically when you work you can actually work on those files okay cool but then I also want to push this to the GitHub remote repositor right how do we do that it's very simple now so you can say get push origin because that's the origin we have and now we have to mention not main because in the earlier case we to say main right but this time we are pushing a different branch which is feature one and when I say enter now it is pushing this particular local repository to the GitHub repository and it's done you can see there's no problem it says on my remote repository which is github.com n ready2 uh gour.et it has a new branch called feature let's verify let's go back here and you can see it still says one branch but you can see there's a notification here feature one had recent push less than a minute ago okay let me refresh to see uh two branches here and you can see now we have two branches this is the main branch but if you switch to the feature one branch you can see you have three files okay so this is the beauty of uh using branches right so you can also see that on the server okay that's how basically you push the branch on the GitHub repository so how exactly things are working here so what happens when you create a new branch is it really copying the entire project and doing it so let's try to understand that in the next video where we understand how branching basically works so let's understand what happens when you create a branch so let's say you have just done the git initialization on the working directory now at this point what happens is you get a different area inside your working directory which is called your git right dogit folder now basically whenever you do your commits of course you have to do this commits in some Branch right and by default when you say get in it it creates a master Branch so let's say in our quiz we are creating a main branch right so default for us is Main Branch right now every time you basically do the commit so let's say when you when you do the first commit what git does is it creates something called a snapshot so it will take a snapshot of your files right and then it will give it it's a commit number that's your checkm basically uh we have seen the EXA code values right so that's your check some and then there's one more thing called pointer now how the pointer looks like let me show you so if you say G log and when you say enter remember this we have seen this thing before there's something called head so at this point it is showing me a feature because we are into feature Branch what I will do is let me just switch to I say get switch mean and now if I do the same command again uh you can see it says head is towards main let's say we don't have a feature Branch at this point we only have main branch and then the header so that's your main pointer so it is pointing to the main branch right and Main is basically pointing to your uh to your commit now there's a recent commit is this one right now let's say next time you are doing one more commit in the same branch of course so what it will do is it will take a snapshot now in this snapshot basically you will have those files which have been changed okay so it will not simply copy every file it will copy those file which have been changed now you have a previous commit you have recent commit with the new changes and then it will also have the parent commit why because if the files which have not changed is there in the previous commit right so basically it will have a pointer as well so we'll see that later how the parent look like so we it create a parent so it also has a parent it has a check sum uh and multiple things it also save data in a threee format but just to keep it simple let's say we have checkm and the commit ID or the commit hash so the main is pointing to the new commit the head is pointing to the new commit and now we are doing one more commit so what will happen is with the new commit you'll have a been pointing to the new commit and head poed to new commit H yes at any given point if you want to go back to the previous previous commit what you simply do is you your pointer will move from the latest Branch to the previous commit whichever you mention latest commit to previous comment is that simple right now what happens is at this point when you create a branch what is happening now basically you have a main commit right the main branch the moment you create a new Branch both the main branch and the new branch which is Feature Feature one in this case is pointing to the same commit now let's say you decided to upgrade your features in the feature one branch at this point it will create a new commit and your feature one is pointing to the new commit right what about the main main is still with the old commit right so that's how basically you create two different branches and you can work them you can work on them individually if you want you can also M you can also work on Main maybe you want to do some more commits there uh because maybe uh after the main commit uh you got you found some bugs of course you working on feature Branch but let's say you you found some bugs in the main now of course you will not directly work on feature one you will what you will do is you will create a new commit from the main so the main will be pointing to the new commit now and the feature is commit is pointing to the other commit so that's how basically it creates the branch now if you want to visualize this uh of course you can do git log it will give you all the logs right but there's one more command you can say get let me just clear this screen so there's something called get log and you can draw a graph here now to this point we have not done any merging that's why it is only showing to the main one so it says what are the commits what what was the message which you have done uh so the comment and the message and the commit and the message if you say enter you will see more now if you want more visual representation what you can do is you can install one extension which is called G graph in vs code and okay it's it failed to fetch okay so you can see there's something called get graph here this is the logo if you want to download and when you say install so it will take some time to install I think it's done and if you want to use it you have to go to this particular icon here which is for the get or Source control and there's a option here so if you can see this is a button you which you have to click and now you can actually visualize everything so if you click on this uh this these are the things which happen with this particular commit and this is what I was talking about the parent for this for Commit This is a parent right this was the previous commit so if I want to show you in the console as well uh we are into feature right we should focus on the main not feature because in this we are into main so I will just click here so yeah this is the commit right the recent commit and that's what we have here and for this commit the parent is some is one commit before that so you can see it says pent now of course for one particular commit you can have multiple pents we'll see that later but at this point you can see we have only one and that's why say there's a plural form right which is parents and that's how basically you can see the things here so yeah explore this and we'll try to understand this more so you can see at this point we have created a new branch which is a feature Branch now everything else else is happening on the new Branch so yeah that's how Branch basically works and now we also want to understand how do you merge two branches that we'll see in the next video so basically now we have two branches right right so if you want to see them again what we can do is I can just clear this screen and I can say get branch and you can see we have two branches right and now what we want is we want to basically merge these two branches we want to merge mean and feature one so of course we have done the work on feature one it looks good I just want to merge it to the main branch how will I do it uh so the first thing is first of all let's explore the files I am into my main all right and if I go back to get switch just just check what are the difference we have here so if I say get switch to feature one and you can see uh there is extra file here which is admin service I just want to check if everything is good it's clean I also want to verify if do if we have admin service on the GitHub so if I go there if I refresh uh so yes we also have admin service here so that looks cool right so now what I will do is I want to merge the data or the features in feature Branch to the main branch how will I do it how do we merge so to do that uh what we need to do is we need to use a very simple command called merge basically there are two ways of doing it one is merge which we are doing now and the second one is rebase we'll see later what rebase is but at this point let's focus on merge so we we are saying get merge and then we just have to mention the branch name but to do that we have to make sure that we are in the main branch so if you see if you check which branch we are into we2 feature Branch so we need to move so we'll say switch to the main branch and now we can do get merge so we just have to say get merge and the branch name which is feature one and when you say enter okay it worked the merge is complete there's no problem sometime you do get problem we call them as merge conflicts we'll try to create merge conflicts later and we'll also see how do we resolve it but at this point there's no issue and you can see we got get merge and it's working how do I verify this if you go back to your main branch of course we are into main branch and in main branch you can see we have admin service okay that looks cool I just want to see if get status and everything looks good because the commits which happened in feature one also came into the main okay uh the way the way you can verify is by doing a log and you can see uh we were basically done this thing in the feature but now it came into Main and we can also verify that with the git graph and if you see here uh that's why we have done the merging right so now it came into Main Branch from the feature Branch this is what it says okay so merging is complete now if you go back to your GitHub and if you see here in feature when we have three files but if you look at your main branch we only got two files because the update is not happened on the server okay there's one thing which I missed normally whenever you do this merging and pushing on the remote repositor of course you have to push right the first thing you do is you pull your remote repository we have not done that because it was safe for me because I'm not doing any we don't have any collaborators I'm not working on mean separately but that's a good idea always try to pull first okay because when you say pull so from the remote repository you will get the updated code on your machine before merging okay so how do you pull basically if you want to do that it's very simple let me just clear this screen so you have to say get pull the same way you do push so you have to say get pull origin and you have to mention the branch which you want to pull which is Main in this case right this is the step which you which should have done before merging okay but anyway we have done the merge and now it's time for us to push it to the remote remote repository so we say G push origin and we have to push mean let's do that uh things are looking good there's no problem and done you can see the pushing is done let's go back here and when you say refresh now in the main you can see we have three three files so this is updated and we are good okay so that's how basically you merge welcome back aliens this is nin ready from Talis learnings and in this video we'll talk about jdbc so so jdbc stands for Java database connectivity now think about this world think about this information world what where what matters is not the websites not the software what matters is data right so if you talk about this concept like information technology so everything moves or everything works around the information right example think about WhatsApp why we use WhatsApp to share information right why we use this banking software to check our bank balance and all those things right so the the whole fun of having this software world is because of data so the center of Attraction for everything is data right so when you build a software why you build software because you want to process data and from where you will store this data so we require a database so you'll be having a database in your system where you will store all your information now this database can be flat file system this can be rdbms this can be uh no SQL this can be big data doesn't matter it's a database right so let's say you are using a rdbms which is relational dbms you can talk about mascle we can talk about Oracle SQL servers those are your rdbms right if you talk about mongod DB so that's no SQL if you talk about hup big data so that's a different topic if you talk about files there are different Ty of databases right so we'll be working with rdbms here so if you have a database here so that's your database and you have a Java code because to fetch data from the database we have to fire SQL queries right so to work with database we have to fire SQL queries now if you're working for a company you're not a it expert let's say you are you're working for a bank now you want to fetch details of an of a customer you will not fire a query right you will click on a button so that means you need to create a GUI where you will click on the button and the data will be fetched from database that means you have to connect your frontend application so that's your front end application with the backend database so this this is your database and to connect Java with database we require a connectivity right and that concept of connecting Java with database is called as jdbc so jdbc stands for Java database connectivity right so whenever you want to connect your Java application with database we require BC now here uh we have four different types of drivers because when you want to connect Java application with database we we need to use one driver so we have four choice we can use uh Native driver which is odbc Bridge basically so we have odbc Bridge which is used for Ms AIS we can use native API we can use pure Java drivers depend upon you what you use so there are for certain there are four types of drivers so main intention is not to talk about drivers but to connect your Java application with database so whenever you want to connect your Java application with the database you have to use seven steps so the first step so we have to follow seven step so from 1 to 7 so the first step of the connectivity is you have to import the package so that will be your first step now which package that will talk that we'll talk about this later so we have the package second step is you have to load that's load and register the driver so first step is you have to import the package second step is you have to load and register your driver the third step would be connecting or establish the connection establish the connection the fourth step so first step is import the package second is load and raise to driver third is connect establish the connection the fourth step is uh create the statement the fifth step is process or oh let's say this is execute the query and the sixth step will be process results and the last step is you have to close the connection and the statement so these are the seven step for jdbc the first step is input the package the second one is load and register driver the third step is establish the connection fourth is create the statement fifth process the results or fifth is execute the query sixth is process result and 7eventh will be close now to to remember this steps just imagine you want to call someone you want to call your friend and you will ask him as let's say you're calling your friend in your college and then you're asking if results are out or not so you will call him right so before calling what you need a phone so of course you got a phone right so let import the package next you need a network you need a network and you should be able to call so that is load and reg driver you got a SIM card right now once you have a mobile phone once you have a SIM card or working SIM card the next is you have to establish the connection so you have to call him you have to dial a number and you have to press on dial button it will call then you have to think what you have to say so you have to say results are out or not so that's the statement you have to mention that statement next you have to speak that statement that is execute the query then after saying you will get a response that is you have to get the response that is it out or not so do you will be getting yes or no so that's process the results and once everything think is done that's the close right so if you follow the seven steps you can easily write any jdbc application now question arise which package you're talking about so this is import the package right so the package we have to use here is java.sql package so that's the package we need to use here so with java.sql dostar the Second Step so we can break down the second step into two parts so we can see the first part is is load the driver and the second part is register it so we can say this is point a and point B so we have 2 a which is load and 2 B which is register now how to do that so which driver so depending upon which type of dbms you're working with so if you're working with my skill so we have a different driver for Oracle we have a different driver for post GIS we have a different driver but let's say in this in this example we are talking about MySQL right so for the driver for MySQL is com dot MySQL do jdbc do driver so that's the driver name we have so it is com. my. jdbc do driver so you have to load this driver and you have to register but hold on uh when you say you want to load it but from where where from which location you want to load it so you have to download a jar file a library from the internet and the library name is it is M so that's Mas connector so if you are using net beans in net beans we have this connector endb so just you just have to right click on the libraries and import the library again we'll see that once we start with the Practical okay so so we require this Library so if you're using net beans it is UIL if you're using Eclipse you have to go to Google search for myql connector and download the jar file okay it will be approximately 172 KB if I'm not wrong so if you download that software then you have to load it in your project and again we'll see how to load in the Practical session and then you have to register now once this two step is done no okay okay we are remaining with this register how to register to register this we need to use a statement which is we need to use a method which is called as for name so forame is a method which is a reflection API method which will load which will load your driver so you just have to mention here com dot masc dot so this this this driver name jdbc do driver so you have to write this method which is forame okay that that's how you register a method a register a driver so this method for name belongs to a class class so so we have to say class plus do forame so that's the syntax we have okay don't worry I I can write the I will write the code for this once again yeah so we have the next step is establish the connection now in order to establish the connection you have to instantiate the interface call as uh not that this one it is connection so there is an interface call as connection and you have to instantiate that object or that interface next is create the state so statement will be of two types one is normal statement in fact it's of three types one is normal statement then we have prepared statement and the last one is callable statement so we have three types of statements here one is statement prepare statement and callable statement so when you want to execute a query so you can execute the query with the help of statement object uh when you have a query which is inbu so you have a predefined query but you have different values you can use prepare statement again we'll see that how to use it and then you want to execute the pl which is the programming procedural language in SQL so you can use callable statement so this is for stored procedures okay now once we got the statement you have to execute the query now whenever you execute a query you will get a response right so let's say you want to fetch the data from the T database so you will get a table structure so the response can be a table structure second let's say you're not fetching you're inserting a value so whenever you insert the value into database it will return the number of rows affected so you're getting something so you can process the results whatever you get and then everything is done you can simply close it how to close get the object of statement close it get the connection get the object of connection close it so that's how you uh do the seven steps now how to write the seven steps in a programming way so this is the steps right so before continuing just pause the video write down all the steps on your notebook so that we can refer okay so let me remove all the statements now okay so let me write this in a coding format so let's say we have a main method here so that's your main okay again this is public static void Main in that you have string argument so that's that's uh that's that you can imagine right so the first step let's start with the first step the first step is you have to import the package so the package name is you have to write a import statement and the package will be java.sql dostar so that's the package we have to use right the second step is you have to load the driver now to load the driver you have to right click on your library fold Library folder inside your project properties so on the right side when you open net beans or Eclipse you'll be having a library folder there you have to add a Mas connector driver okay again we'll see how to download that in the Practical session and how to assign the value or how to assign the library now once we got the library the the the second part of the Second Step which is 2. b or 2B is you have to register a driver for that the driver name is com do masq dot it's jdbc Dot it's oh it's hold on it's driver right and then you have to load this with the help of a method which is forame so forame is a method which will load your driver and this forame method belongs to a class called as class uh this C need to be Capital because this is a class so is class. for name in backet you have to mention the driver name now once we got the driver name or loaded to the driver register it next step is create the connection how to create a connection we have to say connection we can say connection con that's the object name or the reference name the problem is connection is an interface right so we cannot say connection con equal to new connection because we cannot instantiate the object the interface directly right so we need to search for a method which will give me the object of connection so someone has implemented the object uh of connection some somebody has implemented the interface connection and now they are providing the object so the method name is it is get connection so get connection is a method which will give you the object of connection okay how it does doesn't matter right because we are not concerned about how it works we are concerned only about the connection object now this connection object will take three parameters the first one then we have second one and then we have third one okay so it takes three parameters so the first parameter is the URL the second parameter is username and the third parameter is password okay so the URL for your database because in your dbms let's say if you're using MySQL you may have multiple dbms so which database you want to work with so you have to mention that here second to access MySQL we have to specify the username and password so this username and password again we'll replace that thing once we uh got the link now how to call this get connection method right because it it's a method right it will belongs to a class so the class name is driver manager okay so this get connection this is a static method that's why we are using a class name which is Drive manager now once we got the object of connection which is Con the next step is we have to create the object of statement how to do that so we can say statement St equal to now again the statement is an is an interface so we we can use the object which is Con dot create statement right so once you got the statement object so this is your 2 B step this is your uh third step and this is your fourth step this is your first step okay now the fifth step is you have to execute the query so how to execute we can simply say St do execute query so that's the method you have to use and in bracket you have to specify the query so let's say I want to fetch the whole table so I will say select star from student so let's say I have this table which is student now it will fetch all the data right so this query will fetch all the data so what we need to do we need to say we need to save data somewhere right because if you find this query I'll will be getting a table structure okay so let's say in this table I have two rows one is R number and second is name right and so I'll be getting that table in this will in this I'll be having some records so let's say one naven two uh let's say Raj and and three Mona so we have these three names right so we have Navin Raj and Mona so now if I F this query this table will be stored inside a object of result set so we'll say RS so result set is a interface which of which if you're creating the object which is RS it can hold that table now this six step how to process results so to to to fetch all these values there is a method with RS called as get string or get int so to fetch role number we can say get int to fetch the name we can say get string okay but the problem is if I say get get int which int value I'll be getting this one second one or third one that's the question right so initially your pointer will be somewhere here okay before the first value so your pointer is there so you have to shift your pointer to the first location and how to shift we have to say RS do next as soon as you say rs. next it will shift your pointer from this location to one now if you say RS dot get int and if you specify the column number you have to specify which column the First Column or the second column so you can say the First Column it will give that value one okay and you can store this value anywhere or you can just print it let's say I want to print both the values so I can say I can use a space in between so that that's your first value second we can say RS do get because the second column is a string so we can say get string and in bracket you can mention two so this two will give you the second column data which is naven so we on the first row if I say get in First Column it will give me one get string second column it will give me nine if you want to go to next row it will say uh again RS dot next now this will take you to the next row now if I write the same line here I'll be getting two and Raj right so we have to do this for three to four times depend upon how much rows you have instead of writing this code repeatedly we can use a loop right so we can use a loop which is while loop and we can put this rs. next inside a while loop and we can print this data with the help of system.out.print Len right so this is system.out.println but will it work inside a while loop because while loop needs true or false right we want to check how many rows we have maybe if you if you are here how can you go to next line when you don't have that row so this rs. next will do two things the first thing it will shift the pointer second it will check if you have that next line right so that's that's how you this rs. nextt works and once you have processed all your data the last step will be you have to close the statement object which is st. close and con Dot close sounds good so that's how you write your application using jdbc so you if you if you execute this code this is your seven step if you execute this code you can connect with database so we have written this to fetch the value we can also write the methods or we we can also write a query for inserting the difference will be uh when you talk about SQL so we have in SQL we have something called as uh ddl we have DML and dql we also have TCL but that's not important here so ddl is whenever you create the structure of a database or a table that's ddl whenever you want to insert delete the values it is manipulation and whenever you want to file the query for fetching the values that is dql so for dql we use execute query let's say you want to insert the values that that comes on DML right so for that you have to use a method which is called as update execute update again we'll see that once we start with the Practical okay so that's it from this and so before ending the session so normally after every video now I'll be asking you some questions about the technological world so so for today's question is who is the founder of Oracle if you know this answer just comment in the comment section let's see who answers first so that's is from jdbc see you on the next video Welcome Back aliens this is nin ready from Tesco learnings and in this video we'll talk about Java database connectivity so once we have seen the theory of it now this time we'll talk about the Practical implementation so if you have not seen my theory topic just search for Java database connectivity Theory Theory by Tesco okay that's my channel name so if you search for this on YouTube you will see my theory video so uh it's not compulsory to see that video it's just once you see that video you will you will get a better understanding of how to work with how to work with jdbc so let's make a quick recap recap what we have done so whenever you work with jdbc you have to follow seven step steps right if you follow the seven steps it is very easy for you to connect your Java application with database and you can fetch whatever you want values you want you can insert values you can fetch values you can modify values you can do all those stuff right so the first step here is to import the package the Second Step will be to load and register driver so that is register the driver right and the third step we have to do is we have to create a connection so we have to create a connection and then the fourth step is you have to create let's create a statement the fifth step will be you have to uh execute the query okay and then the sixth step will be you have to process the results okay which is this and the last step here is it's your close Okay this is not the uh exact seven steps you can actually modify as for your requirement so this is what we do when we want to connect with database right we have to write the seven steps now which ST which package to import so the package name is java.sql package next is when youbody work with jdbc so we have four types of drivers in jdbc okay and we need to want we need to load one of the driver so let's say if you're working with Ms AIS so you have to work with j jav jdbc driver or jdbc type 1 driver which is jdbc odbc Bridge uh the problem is with the advanced uh the new new version of java doesn't support obbc so now we cannot use obbc feature using Java 7 or Java 8 so we need to go with uh the specific type of drivers for the specific type of dbms so let's say if you're working with Oracle we need to go with Oracle driver so if you're working with mySQL you have to go with mySQL driver in this example we'll try to connect our Java application with mySQL so for that we need to use MySQL driver now how to connect MySQL driver so first you have to load the driver so the driver is com. MySQL do jdbc do driver so we need to load this class as driver which belongs to a package which is com. MySQL jdbc but the problem is it is not there in your system by default okay you will not find this in your uh Eclipse by default if you're using net beans well and good we have this driver inbuilt but if you're using Eclipse you have to download this this thing from uh internet you just have to go to and you have to search for myql connector jar so you have to search for this statement MyQ connector jar and the best place to download anything is mavin repository so if you don't know about mavin mavin is just a global reposit where you have you will have all the libraries available doesn't matter which Java framework you work with you have Library there okay so just go to mySQL connector go to mavin and search for MySQL connector you can select any version which I'm selecting 5.1. 38 uh which is released in December 2015 and that's okay for us if you click on this version uh you can actually use dependency if you if you're familiar with mavin if you're not then it's okay uh we can use MySQL jdpc Type 4 driver okay and we can just click on download here and you can see it is getting downloaded so we have a file of approximately 1 MB okay now let's go back to Eclipse uh so we need to load this driver okay so we have to load and ra driver so anybody work with external jar files we need to add that jar file here okay so you can see we I've already added that let me show you how to describes to how to add that just right click on your project and go to build path configure build path here let me remove this and I will show you how to add it once again just click on ADD external jar and so I have that file in my downloads folder okay so that's downloads and you can select this file here and click on open so you can see we got our jar file here and click on okay so once you have imported that file we need to Lo Lo and register how to do that we'll see then we have to create connection object so we have to create the object of connection interface and then we need to create object of statement interface then we have to execute the query and then we have to process the results okay and then we have to close the connection so let's start with the first step the first step is you have to import the package right now which package to import so this is let me write this in the first line itself so we'll write here the import statement which is import java.sql Dost star because we want all the interfaces and classes so that's the first step the Second Step would be you have to load and register driver now when you say load and register uh what we need to do here is we have to use something called as class do forame so class. forame so this forame is a method which will help you to load the class so you just have to mention the class name okay the class name here is driver okay once you specify driver this class. forame is responsible to load the driver for you you just need to make sure that you you're throwing the exception here because uh this forame method will it's a it will throw the checked exception so you have to uh do here th those exception or you can we can also use TR catch now the problem is this driver what we need to also specify the packages for this because we need to provide the uh qualified name so we have to say this is com. my. jdbc do driver so we need to mention the qualified name okay if you want to if you want to ex understand exactly how this class. forame works so I have a video on that just search for class forame telescope okay so make sure anybody search for any topic just mention teliscope there okay so that you will get my video uh it's it's very difficult to explain each and every topic in one video how that exactly Works what's important here whenever you work with uh jdbc you have to use for name okay time just remember that if you if you are curious about that just search for this video you will understand how class. forame works not just for jdbc but you can use class. for for multiple things okay now the next step is you have to create the connection object right uh so for that we need to say connection con right so we need to also import the package for this we have already done that we have say we have sa St right now the problem is if I go to connection it's an interface you can see it's an interface the problem is you cannot create object of interface directly right so we need to search for a class which implements connection or we need to search for a method which will give you the instance of connection okay let me repeat you need to search for the method which will give you the instance of connection and luckily we have a method called as get connection so get connection is a method in Java which will give you the instance of connection now this get connection is is a method of a class called as driver manager okay so driver manager is a class in which you have a method called as get connection which is a starting method okay so if you go to driver manager you can see it belongs to the same package which you working with which is java.sql and in this package or in this class driver manager we have lots of classes available right and one of the classes which we want to use here is get connection because this get connection method is a static method which Returns the OB which Returns the instance of connection right that's what we want here so to achieve that uh we need to pass three parameters as you can see here so we need to pass the URL we need to pass the okay that's not the method we want to work with we want to work with the other type of get connections yeah so this is the get connection method we want to work with we have to pass the URL we need to pass username and password right so just go back to our class uh here you have to mention three things the first thing is the URL so what we'll do in this main function itself let me create a string and we'll name that as URL so that's the first thing we need to send the second thing is the username so we'll say this is UN name okay and the third string we need to pass the password okay now what is the username here so let me start with username it is for my system it is root and I have not specified any password here okay uh so I will keep it blank and the URL is so you need to mention a thing which is your database name but hold on we don't have database yet so before creating a database let's mention URL U name and we need to pass pass okay that's the password you can see there's no problem now so it says Dr manager get connection but for that we need to First create a database so for database we can use any type of dbms you can work with Oracle you can work with masq you can work with msais you can work with any type of dbms you like okay so for this example I'm taking mql uh so first step you have to install mql just go to Google again and search for MySQL download or MySQL server it will take you to the official website of mysq just make sure you download the myql from here okay and the problem is when you work with MyQ it's a command line interface you have to execute everything using command line uh that will be not that will not be that flexible right so what you can do is you can use a GUI for it which is MySQL MySQL workbench uh so just go to Google and again search for myql workbench okay workbench so this is a GUI tool where you can work with uh database creation table creation all those stuff okay so I have already done that I have installed myql in my system so if you want to learn about uh myql or SQL stuff just again go to my channel search for Okay search for MySQL tutorial telescope okay so just to give you a tool to my channel so just go to YouTube and search for teliscope learnings Okay so that's my channel name so if you go to my channel I have lots of playlist okay so you can learn everything here so you can learn about uh Java tutorial you can learn about Android you can learn about Java 8 new features you can learn about design patterns C programming oops concept uh you can also learn about mavin here and the most important thing for this is you can learn uh with where is MyQ it should be somewhere here so let me search for it yeah so you can see we have a Mas tutorial here so you can just go to this playlist and you can watch all this video to understand how mascle works okay and after this you can learn lots of things here how to use eclipse and you you can learn about ejb GUI stuff all the stuff here okay yeah so just search for Mas tutorial you will understand how to use work workbench okay let's get back to workbench now and here we need to create uh a table in fact I have already created a table here which is student okay in of wasting of time I have created a table with student so if I if I say select start from student I think I'll be already having some data because I've taken this session for a batch okay we have only one record which is one Ronnie so let me insert some values here so we'll say insert into uh okay what's the table name it's student then we say values so we just specify the first thing let's say it is uh user ID 2 and we'll insert our name as naven okay that's the first thing let's turn this code okay now we have the second record let's enter one more so let's say three which is uh Mahesh and if I run this code we got three rows now so if I run this code okay so we have three rows what we'll do in this code is we'll we'll uh we'll give the value and we'll try to fetch the name okay and the query for that will be it is Select we need to fetch the username so username from student where user ID okay user ID is equal to three right if I this code we got mahes right so that's what we want to do in Java so uh so we first we have to mention the database name the database name here is alien so you can see we have we are working with a database which is aliens here right and okay but the problem is masule normally works with okay so when you talk about the URL patterns or URL we have to specify jdbc colon myc colon aliens so that's the compulsary things because it specifies that you're working with JBC and the dbms you're working with is mle and inside myql you have aliens but myql is a server level or what you say Lan landan type of dbms where you can have the database on other machine you can access with other machine so we need to also specify the IP address here since it is there in the in the local machine I will say Local Host and we need to also mention the port number so I'm using a port number which is 3307 if you're installing M for the first time the port number you'll be getting is 3306 okay so you might be having a port number which is 3306 okay so make sure you you know the port number now once we have done with the Crea creation of the connection now let's go to the next step which is here we need to create a statement which is very easy now so just have to say statement okay we'll say St equal to now in order to create object of statement you just have to say con do create statement even this statement is an interface uh so we have to use create statement method because this create statement Returns the object of statement right and now if I done with the statement now we have to actually execute the query so before executing let's State the query here itself so in double codes we will mention the query so let's copy the query in the from the mycle which we have already typed and let's paste it here so that's the query we have right so we want to fetch the name of the user with role number or with user ID 3 now how to execute the query it's very simple just say st. execute query and then you have to pass the query here now whenever you execute this query let's execute this query here in myql so you can see we are getting a data so what data you able to see is it just Mahesh and the answer is no right we are not just able to see Mahesh we are also saying this username as the uh as the label right so this thing will return you not just Mahesh it will give you the whole table not exactly the whole table but it will give you mahes in the table structure so we need to store that thing in result set because result set has the power to store chunk of data in tablea structure okay so in this RS you have your data now now how to fetch the data it is very simple uh we can store that data somewhere so we say string uh name this is where you want to store data so we'll say RS do get string and you have to mention the column number so let's say if you're fetching multiple columns so you have to mention the column number or you can also mention the column Name by in Double C just write username so we want a data with the column name username okay you can have multiple columns right so you have to be very specific which data you want that's why you have to mention username there okay we can also mention the number so let's say you can mention one you can mention two uh you can mention the column number right so that's it if I if I try to print this value which is name okay it should work now and it should print mahes if it doesn't print mahes then there's some problem with the code so let's try before that let's do the last step which is closing stuff right so we have done with the processing let's close it how to close very simple it's first you have to close the statement so you have to say st. close and then we have to create we have to close the connection also we'll say connection. close let's run this code and boom we got the error the error is before start of results set it's because whenever you run the query by default you can see the pointer here right like so this pointer by default this pointer will be before Mahesh okay so we to in order to F the value you have to say rs. nextt so rs. next will take your pointer to the next recorder and this time you can actually fetch mahes so before fetching the value you just have to say rs. next because this is responsible to take you to the next element okay so that's how you uh use jdbc steps okay now we can do uh some more stuff here uh we can insert the values in in fact we can also fetch the whole table not just one record we can fetch the whole table and uh we can also insert some values so in the next video we'll talk about how to insert values and how to fetch the whole table so I hope you liked this video so thank you so much for watching and and and before going off let me ask you a question uh it's just U I have this habit of asking questions so uh the question for this session is uh who is the founder of Apple okay so Apple Inc so you have to tell me the founder of Apple just use the comment section to answer the question so thank you so much for watching and do subscribe for further videos so in the earlier video we have talked about jdbc in which we have tried to fet data from the database right so in this video we'll try to implement the this the wear clause or we'll try to uh fetch one value with the oh we have done that right so we have tried to fetch this one right let's let's try to fetch all the values so instead of fetching only one value we we'll say I want to print all the values so let me just write the query in masc first without any where clause and if I say select star from student it will give me whole table right so we have one which is Rani two naven and three Mahesh right so now we'll try to print this data using Java for that we have to execute a query which is Select sta from students without any work clause and now when you try to execute this query you will get the whole table right so inside this RS you don't have one column or one row now you have two rows or so you have two columns and three rows right and so let's try to print this so let's say if I say rs. next if you're saying rs. get string it will give you the second column right because it is username here let's let's uh let's write the number so if I say rs. guest string one now this time it will give you the First Column which is user ID uh which is int by the way so let's instead of writing get string we can also write get int which will give you the integer value we also want want to print okay let's let's say this is not name now this is uh user data in which you'll be having both the things we have user we have user ID we'll give a codon in between and we'll try to print the username which is get string and in this you have to mention the second column which is two right and here we try to print user data so what I'm doing so I'm just firing a query which is Select star from student which will fetch the whole table and if I run this query you can see we are getting the first row which is one Ry but what if you want to get the second row now for the second row we have we have to again say rs. next because it will shift your pointer to the next row okay and again you have to say. next it will it will switch your pointer to the third row here and to do that just repeat this statement so let's say copy this and paste it here okay we don't have to declare this once again because we have we declaring it once right or what we can do we can declare it here we'll say string user data initially it will be blank okay and then then you don't have to declare it once again right yeah so now if you have three rows so you have to write this stuff three times if I run this code you can see we are getting two rows now so we we got one Ronnie to naven and if you repeat the same line now same three lines once again and if I try to run this you can see we got three rows right so that's how you need to fetch data from the database by writing rs. nextt every time but hold on how many times we will write this so instead of this what we can do is we can use a while loop here so we can say a while loop and no need to mention okay and you can write this rs. next as a condition because RS not rs. next will do do two things for you because we don't have any meth as RS is that we don't have any meth as has next right which is there in the collection API so we need to say next that's the method we have to use for the next record so r. next will do two things it will shift your pointer to the next row and it will also give you the return it will also return you do we have the other row there if you have other rows or the next row it will it will return true otherwise it will return false and this code we got the same stuff which is one Ronnie 2 Na and three Mahesh right so that's how you fetch the whole table but let's say now the next thing we want is we don't want to fetch data we want to insert data into database okay so yeah that we'll do in the next video so in this video we have talked about how to fetch the whole table using jdbc in the next video we'll talk about about how to insert values into database so thanks for watching and do subscribe for f the videos so now once we have done fetching values from the database this time we'll try to insert values so you can see we have three records right so we have one Ronnie two naven and three Mahesh let's say we want to add one more row but not from myql I want to do it with the help of java so of course we need to write this query right so what we can do is we can simply copy this query so that we can use this query in my code so let me just paste this query here but the data I want to insert is not three Mish I want to insert for let's name moini so we have four and moini I want to insert this value into database now uh we can yeah we can take any name doesn't matter right so what we'll do here is to insert to insert that value uh we cannot write execute query right because when you want to insert value into database so we have three types of languages in SQL right so we have ddl we have DML and then then we have dql so whenever you work with ddl which is data definition language which is which let's say if you are creating a table when if you if you are changing the structure of your database it is ddl data definition language DML is when you want to change the value of a table let's say you're inserting a new value you're updating existing value you're deleting existing row so that is DML whenever you file the query for fetching data so that is dql we also have TCL for which is transaction control language let's say you want to Grant permission you want to revoke permission so that is TCL okay timing we are talking about ddl DML and dql so whenever you work with dql which is data quy language where you fet the data from the database we need to use execute query because this execute query Returns the object of result set because we have we are getting data from the database but whenever you work with DML let's say if you want to insert data so while inserting you're not fetching the data of the table right so instead of using execute query we will be using execute update because execute update so there's a method execute update which will give you so it will take a parameters SQL query and it will give you int it will not give you object of result set right now what is this int so this int represent the number of rows affected so let's say if you if you in if you insert data into database it will it will affect one row if you update a table by some let's say if you're updating three to four rows at the same time so you'll be getting four it will return the value four which which is which means we have updated four rows so we cannot write uh RS dot we we canot write RS there we have to say account so this is the number of rows which are affected right and then since we are not send fetching data so no need of uh while there no need of this string we can simply print a statement so we can say uh count plus rows affected okay even I'm not sure it will be rows or rows so we'll use SL d s okay because if two rows then two rows then if it's one row then one row affected right so now if I run this query it should work so you can see we got one row affected right and let's go back to the SQL uh let's find the query and if I F the query you can see we have four moini right so we can insert the data with the help of insert Clause uh insert query and then for inserting we have to use execute update but let's say we don't have this data available we have this data in the variable so let's say if I have a variable as user ID and we have five there and then we have one more record which is this is of type int right and then we'll be having a string which is let's say the name is uh let's say Ali right so we have this name so we have username user ID 5 and user name Ali I want to insert this values into database for that I cannot write for here we have to write user ID and then we instead of writing moini we need to mention this is username right uh but hold on we want to fetch the value of user ID right so we cannot write user ID inside double codes because that's a variable so we need to separate this user ID from the double codes so we'll end the double codes here and we'll say plus because we are trying to concatenate this we'll say plus double codes okay again the same thing here after single code we have to say plus double codes oh not double Cotes here but before this you have to send double codes and then again plus double quotes right so we need to open and close double quotes very you know by very uh by taking proper precautions because it may be uh you you might be ending double code somewhere else and then it will give you error right so this is how we need to write double codes and then plus operator and if I run this query now so you can see it says one R affected and if I go back to the database if I run this query we got five records now it seems easy right because we can give double Cotes and plus but let's say we don't have two columns we have 10 columns in that scenario we have to we have to do lots of plus and double codes right so this is not the best way of doing it so the best way to handle this type of things whenever you want to insert values where values are coming dynamically so we can use some something called as instead of using statement we can use prepared statement okay so as we have seen in the theory we have talked about this right so if you have not seen the theory of this uh just go back to my channel once again and search for the Java database connectivity Theory by telescope okay now so uh we have uh yeah so instead of using statement we'll be using prepare statement now what is prepar stat mean so whenever you want to work with the predefined queries you can see the query is fixed but the the value will keep changing in this scenario we should always use prepare statement how to use prepare statement just say State instead of statement just write prepare statement here that is prepared statement right let's Okay that's prepare statement and instead of create statement here because whenever you work with prepare statement we have to use our method which is prepare statement okay and in this prepare statement will ask you for the query it is a predefined query right and whenever you work with prepare statement you don't have to write all these double quotes and plus you can simply say I know the query but I don't know the values now when you say I don't know the values just add question mark there okay so instead of adding the values just write question mark right and uh so before executing so you can see I'm executing the query here right so before executing we need to First change the values right so we'll say six and we'll give the name as diali okay now so instead of uh so before filing the query we have to update the value so you have to replace this question mark with six we need to replace this question mark with the help of diali how to do that just simply say St dot so to update the value just say set int now this set int will take two parameters the first parameter is the number of question marks so it is the first question mark and the value is user ID okay then you have to say s do set string for the second question mark and then you have to mention the username right so we we are replacing these two things the first question mark and the second question mark right and let's run this quod and we got error congrats uh we already okay so we have a error near the double codes okay what the error everything seems good insert into students values okay everything seems good there should not be any problem there okay let me write this once again no watch the issue okay so we have we writing one we are wrting two we are using prepare statement of the same package so no issue with that we inserting user ID we are inserting username and we are not using okay everything seems good let's run this once again and there's a problem again so it says check the manual there's some something wrong with double quotes Okay uh everything seems perfect nothing to worry okay write this query once again so values um question mark comma question mark don't be right okay so that's query and this is prepare statement con. prepare statement everything seems good okay that's prepare statement still there's an error so let me just check see we have we have made one mistake here since we are passing the query here we should not pass the same query here because once you use prepare statement you have to mention the query in the prepare statement not in the execute update right and if I run this code you can see we are getting one row affected now right let's go back to database and if I run this code you got six diali right it's that simple so that's how we need to use we need to insert values we can also update here we can write update query we can write delete query we can do all those stuff here and that's how you do insertion you that's how you fetch values so in the next video we'll see how to use a design pattern called as D design pattern so when you work with uh a maybe a project or Advanced software we need to use some design patterns right one of the design pattern we should know about is D which is data access object so when you walk with database database uh stuff which is jdbc you should always write your Dow you should always write your database steps inside this Dow layer so this Dow will be a new a class in which you'll be having all the steps so that U it will create some layers so that if you have any problem with the database you will only change the Dow not the whole software again how to use that that will'll see in the next next video so I hope you like this video just click on the like button there and do subscribe for further videos welcome back aliens this is nin readyy from thco learnings and in this video we'll talk about one of the method which is very famous when you work with jdbc is Ford name so the class name is class and the method name is for name so we use this method to load the driver right so to load and register driver we use class. for name but what exactly does so for that what we'll do is we'll try to do a simple code without jdbc so let's say we have a class here which is pqr okay so we have this class pqr and in this class we have a static block so if you don't know the static Block it's a block which will be which will be executed once the uh once you load the class so we have a static block and then we have something as instance block so this is an instance block so let's say if I print here and I will say this is in instance and we'll print here s out in static okay and if I run this code so you can see we we are not calling anything from the main method and if I run this code so you can see we got nothing but as soon as I create object of pqr so I will say pqr obj equal to new pqr and if I run this code so you can see it executes the static block and then it executes the in block now why is the case is because the whenever you want to create the object so this is let me just write here in the command so this is instance block and this one is a static block so whenever you create an object so before creating the object it will first load the class and while loading your class it will call the static method or static block while you're creating the object it will call the instance okay so that means whenever you create the object you get two things the static block and the instance block right but let's say I don't want to create the object but still I want to call the static block so what we can do is we can load the class so in order so if you load the class it will execute the block right now how to load the class so to load the class we have to use class do forame and we can specify which class so I want to load the class which is pqr okay will it may throw an exception so let's run this or so let let's apply the exception if I run this code you can see we got inst static because we are not creating the object we are just loading the class and then we have a method which is new instance okay again throws an errow I guess yeah and let's run this yeah so if I say do new instance it will create the object so when you not when you say do instance do new instance it will create the object but if you if you just say without this if you just say uh forame it will it will execute only static block so that's the advantage of using class. forame but let hold on whenever you work with jdbc so that's the fun that's the fun of class name okay so we use class. forame to load a class so that when you load the class it will execute the static block right so whenever you work with adbc code right so you write this statement which is driver manager uh we we we create the object right so we we say connection a con equal to driver manager and before that line so before this line so the step is you have to load and register driver right so how you load so whenever you want to load the driver so we need to use something called as driver manager Dot do register register driver in this you have to create object of the driver so that's this is the statement we should be using to register a driver because we have to register the driver service here so let's say if you're working with MyQ you have to load a mcle driver if you if you're working with Oracle you have to load Oracle driver and to do that we have to first use this driver which is M connector so the address is Comm do myql do jdbc dot driver okay so it will turn an exception okay let me just write in general exception instead of writing all this stuff there exception so this is the step we have to use so this is the step which is responsible to load the driver right so let's say a reg driver so let's say you are using uh Oracle so it will be Oracle driver here but instead of doing this stuff what if we can just specify com. mq. jdbc do driver so this stuff here is same as this thing but the question arise how it's same it's because whenever you go to the driver class of your M col connector in this you have a static block and this static block does the same thing which you you are trying to do sounds simple right so this thing here so whenever you call class. for name it will call it will load the class right this class and whenever it loads it will execute the static block and whenever it execute static block it will call the method which is register to driver so that means you don't have to write this statement because you're writing class. for name so that means whenever you want to register a driver so class. forame is not the method which Lo which which ra a driver it is actually this static block of driver class which loads driver this or which register driver so this class. for it just loads the driver so that you can register driver here okay so that's the basic understanding of class dot for name so that is from this video and do subscribe for for the videos in this video we'll talk about jdbc with some design pattern for this example I'm using myql server 5.1 you can install any database server you can use SQL Server you can use Oracle you can use Ms access or you can use MySQL any any latest version so let's create a database first we'll say oh in fact I have a database with my name so we'll use that database so we'll say use nain N is a database name here and then in that database let me create a table so we'll say create table and we'll say student and this we'll have two variables say our two columns we'll say um RO number type in and we'll say size is five and we'll say s name it should be vard and the size of Vare will be 25 now let's insert some values so we'll say insert into student values we'll say value is one we'll say value of 11 and we'll say name is Rahul so we have RO number 11 as Rahul so inserted let's check so we'll say select star from student right so we can see we have a role number 11 and his name as Rahul now what we'll do is uh we'll insert some more values we'll say uh 12 and instead of Rahul we say Ganesh and say 13 oh this is 13 and say shadab so we have three rows we'll say Select Staff from student we have these three names okay now what we'll do is we'll go to our Java code now our main task is from your Java code you have to you want to fetch name uh of let's say you want a name of Ro number 12 that means you need Ganesh so what we can do is here we'll we'll say uh I want want a student we'll say name is S1 I don't know I don't know his name I want to get his name I what I know is role number so what I can simply do is I can say get name uh in bracket for role number 12 isn't it if you write this statement you will get s name and you can simply say s out and then we say s S1 do get name or S1 sorry S1 one dot we'll say s name right now the problem is we don't have me this method called as get name so what we can do is again uh before going for before coming to this video I will recommend you watch my jdbc Basics video so that you'll understand what I'm doing here so we don't have a method called as get name here right so we have to Define that so we'll Define that method somewhere in this class we will say class uh student d a o now what Dao stands here is data access object and this Dao will have a method called as public string or public student because it will return a new student and Method name will be get name or what we'll do is we'll simply say get student with an role number 12 so we say get student RO number 12 and it will be get student and this we are passing a variable with say role number simple and we'll say return we'll create a student here we say student s uh equal to new student and then we can simply return s but hold on we don't have a class called student here so we we'll create a class called a student so we'll say class student again you can create all these classes in different different file also okay and in this class we'll have two things one is role number and second is string s name okay so we have a class student with two two variables s uh two variables RO number s name and here I'm returning a student object but I don't want to return a blank student I want to return a student with ro number and name now the role number of student I know this role number is equal to role number so this s role number here is this RO number here okay and and here I want to call this so we'll say student not student student da AO we'll say da a equal to new student Dao okay and then we can use the object of Dao to call get student simple so now we have uh now we we'll create object of student Dao it will call get student in gu student we are passing this role number 12 so we are getting role number in RO number we 12 and then that 12 comes here but what about s name because when you return this when you return this s it should also have S name and the problem is we don't know what's the s name right and that's why we have we want to fetch that s name from database so what we'll do is we'll say simply uh we have to follow those seven steps of jdbc so if you want to know the seven steps you can go to my previous tutorial on jdbc and and you can check seven steps so I I'm directly jumping to the second step in this I have to import a library which is MySQL driver I'm done with that so this is your MySQL driver here how to add this you have to go to library right click add library and you can go to mySQL here somewhere here MySQL D drive and you can click on ADD Library since we have already done that so we will not repeat it again and here uh we say we have to we want to load the driver so we'll say class. forame um here will say we have to mention com. my. jdbc do driver uh there's a chance that we'll be getting an error so we'll go for track atch we'll say surround block with track atch uh we we we'll go for General exception I sure printing using a logger we can simply print the error using S out a ex okay now here next step is you have to call you want to create object of connection so a create connection con equal to since connection is an interface you can see it's an interface uh if we going to go on connection it's an interface right so we'll say the to create object of interface we require Drive manager. get connection it will ask you for three parameters first is connection string role number sorry uh this connection string username and password so we'll say password is zero username is root and we have to say jdbc colon MySQL colon sl/ localhost again a recommendation if you don't know what are what I'm doing here or the steps of myql JBC go to my previous tutorial okay and then after Local Host we'll say uh we'll type the database name and database name here is database name it's n okay next step we have to create a statement so we say statement St equal to con do create statement now we we need SQL query so we'll go up here and we'll say string query equal to and the query I'm using here is Select s name from student where role number is equal to ro number right so with this role number will come here and the qu will become select s name from student where role number is equal to 12 okay and then and then we'll say uh st. execute query because when you say execute query and you have to pass the query here it will give you a result set so we'll say result set RS equal to s s. execute query in this result set what you you'll be getting is is the result okay what result you'll be getting is you will say select the same query here we'll say select s name from student where RO number equal to 12 so you're getting here is his and get ganes right so by default your pointer will be here and you want to shift to Ganesh here so we'll say RS do next you have to shift your pointer and then we'll print s out not s out we don't have to print here we'll say now we got how can you fetch the name we'll say uh name String name is equal to rs. get string and then we can pass the column number and the column number here is one is because we are fetching only first only one column and then we'll say uh student which is s do s name is equal to name so this name here will go to this s name and then you can return uh SE here so this s name is same as this s name and you can print the s name using the following steps and now if I run this we are still having an error okay so we'll say if you get any error return n and let's say let's run this once on this code the output you'll be getting is oh there should not be any double codes here and when I run this code now the output is it's ganes right so that means for just focus on this now if you ignore the below part is Doo We're creating a doo class and then we are just saying get student with role number 12 you're getting a s name which is Ganesh if you write 11 here and if I run this now it's Rahul right it's that simple if you don't if just just see this number of lines you can achieve your jdbc connection this three number of lines in fact if you want to in if you want to save a student you can simply say da AO do add student and we can pass S1 or maybe S2 creating a new object it's it should be that simple okay in fact we'll do that we'll add a student using this thing in the next tutorial I hope you got the uh got a thing we are doing here and uh thanks for watching and do subscribe for further videos now let's achieve this add student method here now that means we have to create first a student we'll say inste of fetching like I don't want to fetch now fetching is done and I don't want to BR this I just want to save I want to add a new student so what the new student will be is we say student uh this time we'll say est2 equal to new student since we want to create a new student here and then the student number we say student role number will be let's say 15 and student name student s name name will be let's say uh we'll go for archa okay I want to store this name here so what we can do is we can say dao. add student which student S2 but unfortunately we don't have any method with that name so that means we want to create that method now so we'll create remove this Gap first and somewhere here let me just minimize this code method okay and after this method I want to create a method public void I don't want to get anything so we'll say add student this student will accept a student object will say s and we want to save that so that means we want to create a query and this query will be insert query now when you work with insert in jdbc uh don't go for statement go for prepared statement that means if will say insert into student and since I don't know the values I will say question mark comma question mark so that I can replace the question mark later now I want to achieve those seven steps again so what I can do is I can just take out this steps from here and Weare declare it outside so in the class here I will say connection con equal to null because I want to go for this instance declaration so I don't have to declare here in the previous method which is get student and we'll everything we'll do here and then instead of we'll require one more method which is public voice connect so that it will create the connection doesn't matter if this get student or the or our add student I want this two method or this two statement should be outside once I create the connection I don't don't have to do it again again we require a tri catch block here so we say surround with TR catch and we'll go for some generalized exception and we'll print ass out ex I'm just I'm just taking out the common methods in both the common uh lines or statements in the both the methods and here what we can do is now we can say I want to create a statement but this time prepare prep statement so we'll say prepared statement we'll say PST equal to con do prepare statement and we are getting this Con from connection and this query will be query and I want to replace that question mark so say PST do set end role number oh sorry it should be the question mark number and this should be role number not simple role number it should be S do ro number because the data is in this s uhoh I'm getting an error okay I want to have to handle this we say around with trackage okay again I don't want to go for specific type of exception I go I will go for the general exception and we'll say out ex and let me just cut this line from here this one cut and past and to go for replacement of second question marks we say search string and this we say two and the string will be s. s name clear and then what we can do is we can just execute the query so to execute the query we'll say execute update so say execute because when you insert you have to update it when you file the query you have to say execute query and that's it once you say update it will store that data into database but before calling add student I need to call now connect because I'm creating I'm going for a new method call as connect here see this so once you do the connection once you call the connection it will call this statement and this statement so the instance for this Con is creating on this statement so we have to create a connect first so first you have to call connect and then add Str clear and now you for done this I hope it should work uh no errors let me go to my MySQL let me just say select St from student you can see we have a new student with r number 15 and name is Arch right so this is how you can add student to can remove in fact you can do you can write coding for uh D ao. remove student so what we have to do is we just un pass the role number we said 12 so it will remove the student with ro number 12 but again you have to write a code for this you have to write delete query so you have to write a method in this you have to write those steps to remove that method or to remove that uh student simple so this is how you have to use Dao thank you so [Music] much so welcome back aliens this is n r from th learnings in this video we'll talk about how Sate works so just imagine we have a CL client machine there and we have a server right so when you talk about this website world or this web world so a client sends the request to the server expecting a page right again you can you can ask for anything on internet but let's say you're asking for a page now now this page can be a static page it can be a dynamic page when you say static it means it is already made and when you say Dynamic it means it will be built at one time so let's say a client sends the request which is request so it goes to a server on your server you already have a file right so that file goes to the client request done and so we got the request and response finished right but let's say if you're asking for a page which is different which is from the which is which will be built at one time so a client sends the request to the server now server says hold on I don't have any page with this with this link so you have to make that page right and you have to build that page so that request goes to a help application can you see that we we got an helper application on This Server now this helper application is also called as web container okay so in this web container you'll be having svets now what is serlet serlet is basically a Java file which can take the request from the client on the internet and it can process that request it can provide you a response in the format of HTML page right so this solet will convert your request and it will take your request it will fetch the values it will process the information and it will send the HTML page right so let's let me just go back once again so from client side request goes to server on the server side since it is a dynamic request it will go to your web container which is so whatever web container we have so we have uh Tomcat we can also use glassfish we can use Joss we can use web spear in fact all these other servers those are called as app servers but they also have a feature of web server so basically we always use Tom Cat in fact in my in my subsequent videos you will find all the things ins using Tomcat right so that Tomcat is a web container so on your server if you want to achieve web the dynamic web web pages you need to install Tomcat okay so you can see that is Tomcat there so that web content is a tomcat so when request goes to your Tomcat Tomcat says hold on I got a request now but that request is for abc. HTM and we don't have any page as abc. HTM right so we have to build that page so that request goes to a sulet hold on seret will be having a Java name right example when you create Java classes let's say I want to add two numbers again a basic example if I want to add two numbers I will be having uh so I will be having the subet name as add serlet that means when I request for abc. HTM the page which should be getting called is your serlet how can we do that so what what we have is with in your inside your inside your container you have a special file which is called as deployment descriptor this is how it looks so you you have a file which is deployment descriptor in which you mention for which request which Serv should be called okay so let's say in uh I mean in your Tomcat server you have maybe 50 to 60 servlets so every serlet will be mentioned I mean every request will be mapped with one serlet it may happen that you you have multiple request trying to access the same solet right right everything need to be configured in a file which is called as deployment descriptor and the file name is web.xml so this file here is your web.xml file so let me repeat when when client send a request it goes to the server now server thinks okay this is the dynamic page let's send the request to the web container now web container will check okay this is this request will be mapped by this soft that can be done with the help of web.xml file so you can see in the web.xml file we have two tags we have subet tag and we have subet mapping tag so in the subet tag you have to mention the class name and in your subet mapping tag you have to mention the URL pattern so for this URL we need to call that particular serlet but hold on how do we get a seret now and that's very simple you can see we have a file here so this is a this is your file structure so when you want to create a solet it's a normal class okay which extends a HTTP serlet since we want this serlet features right we cannot create normal pojo which is your normal class we have to create a special class which extends HTTP solet because this HTTP solet will provide you all the features which you need okay now when I say all the features it means it should be able to take reest from the request from the server it should be processing the request and it should be sending the response how it will be done that we'll see in the next video but it Tak the request it process the information and it will send the response now that response will be in HTML format again you can send the response in Json format or normal spring you can send the response in XML format you can also send the uh response in the HTML format now that HTML page this which is a dynamic page goes from your server to your client machine in the format of response object right so now we have one more thing right we have web web file here do we have a replacement for that because the problem is we are Java programmers right and we don't like XML okay that's not the case but we but let's say we try to award XML now how do we award XML so in in this subate 3.0 we got a new feature which is called as annotations so we can use annotations on your subet so you don't have to use XML files or webml file you can you can do your mapping using annotation again how that done that is done that will say in the subsequent tutorials but this is how your serlet basically works with the Tomcat servers okay so a quick recap we have a client machine request goes to server from the server request goes to your web container web container uses a file called as web.xml to detect which Serv to call and it calls the serlet that serlet will process the information and it will send the response to the client machine that that that's how it works in the next video we'll talk about how does how exactly your web container searches for this subate and how it's get loaded and the life cycle of the subet now let's start with the actual work now where do we code to work on a project you need to use one of the app or the software which is called ID now IDE basically stands for integrated development environment it means it's a it's a software where you will code where you will compile the project and where where you will run the project now that different IDs available for servlets in fact for Java field we have certain certain IDs available so one of them is net beans we have Eclipse uh we have intell J so that if these are the three topmost softwares available for Java developers now which is the best one I would say intell J is the one of the best IDE available for Java but the only thing is it has two versions one is community version and second is the ultimate version and if you want to work on serers side coding you need to use ultimate one and unfortunately you have to pay for that otherwise you can go with eclipse this is the next best IDE available and the awesome thing is it is free to use and then we have net beans normally in academic purpose they use net beans a lot but I've never seen uh a development environment where we are using internet beans but then people may be using it but I I prefer eclipse in fact the industry which I visited for training they all use Eclipse now we will see how do you get eclipse in your machine now the first thing would be open your browser so just go to Google and search for Eclipse download now if you search for Eclipse download you can see it will show you this option which is Eclipse IDE for Java e just click on that and now you can see we have for different OS we have different setups here now you can go for any version uh I mean depend upon your OS example in my machine I'm using Mac so I will be going for Mac OS X if you're using Windows depend upon which bit you using maybe 32bit Os or 64bit Os you choose one of them and if you're using Linux flavors you can choose this one now since I'm using Mac I will be going for the third option here click on this and then you will click on download and after in some time it will get downloaded and you can see it is getting downloaded here now it will take some time based on your internet speed now we don't have to actually wait for this I already have a setup in my machine so if I go to finder you can see I already downloaded this software on my machine and the only thing is since it is start. gz you can simply double click it and it will give you the UN zip version right and the unzip version is eclipse and the moment I double click on this you will get your IDE so the version which I'm using is Eclipse Mass here you can also use neon that works fine now it will ask you for a workspace now workspace is basically a folder where you will put all your project so what all your working projects will be there in this folder you can choose whichever folder you want and I would choose uh a suret I would say a seret workspace I would click on okay okay and as you can see this is the eclipse ID so let me just make it full frame and I will close the welcome window again you can enjoy this welcome window where you have some tutorials uh you you can see some sample code but then let me just close it and this is how your Eclipse will look like on the left hand side you have your project Explorer so all your project will be explored here on the right hand side you have some task list you have some outline to work with so we don't we don't need that as at this point now this is the area where you will get your code window as you can see we don't have anything here yet but then this is where you will code and this Tabs are very important now this is one of the tab which will be helpful for debugging your application whatever wrong with your project you can see that here then this is the tab which is important the server tab now you might have guessed till now since you are working with seret you need a server to run it on right and in the theory video we have talked about it we need a server which is a saet container where you will run the svets and the Ser container which we are going to use in the course is Tomcat so of course you need to link your Tomcat here but how do you do that that's a that's a different thing then this is for data source if you want to explore your database connections now this is what you have on the on this side now let's talk about this one this is a perspective so if I if I click on this perspective these are the perspectives which are available now you want to wondering what this perspective means now if you are working on servlets or any Enterprise level code let's say suret JSP ejbs you will be selecting Java e now Java e offers you those features where you can configure Java Java e stuff example the server type which you're getting here thanks to the Java e perspective because if I change different perspective let's say Java which is used for normal code Java development and if I click on okay you will see there's something change and the server tab is gone now so these are the perspective you have so you can see if I click on Java a perspective you will get servers if I click on Java perspective here you will not see servers there and if I again open the perspective there are G perspectives so if you want to see the repositories uh there is a jba perspective if you want to work with j now time being we want to work with Java e so let's stick to it click on okay and you got Java e perspective now the next thing would be you need a project and you need a tomcat server now once you have your Eclipse up and running it's time to configure Tomcat so before configuring Tomcat first you have to make sure that you have downloaded tomcat and we don't have it yet right so let's go back to the browser and search for Tomcat download now it it will take you the official website of Tomcat so let's go for the versions now you can see we have getting option of Apache Tomcat 7 apach a tom cat 8 I would choose eight here and you can see we have certain versions now this is 8.5.2 and the below one is 8.0 I prefer the complete version so 8.0 is something which I will be using here you can also use 8.5 nothing wrong with that now in this which one to select again depend upon your OS I would select uh t .zz because it it works on Mac perfectly so I will click on this one and it will download the software and then you also need documentation now why we need a source code here that we'll see later but time B we got these two softwares one is the the tomcat and the documentation oh we have to click on that as well so it will download the documentation as well now I have those things here so if you can see I have this setup for Tomcat and the source code as well time will let's not think about the source code think about the stormcat double click this one and it will unzip and it will give you the unzipped version of Tomcat can see that we got Tomcat here but how do we use it of course we can use it on command line or terminal but then we want to configure that to Eclipse so go back to eclipse and now you can see in the server tab now maybe you will not see a tab there on your machine how do we get it so in case if you don't find this tab here just go to window and say show view click on servers it will show you the tab there now we have a tomcat downloader we just need to connect it now how do we do that just click on this one now it will ask you which software you want to connect or which server you want to connect so click on Apache and you can see it shows lot of versions this is old version let's click 8.0 click on next and then it will ask you for the location where you have your Tomcat so click on browse and move to the location there so I came to a folder where I have my tom cat can you see that this folder double click that and click on open so there's no error click on finish and this is how you link your Eclipse with Tomcat but you're still not sure will it work now how do you verify if your Tomcat is perfectly working right click here and say start now if it is getting started perfectly you can see it says started that means your Tomcat is properly configured and it is working but maybe sometime it will not work now when I say sometime there might be different issues uh one of the most common issue is the port number which Tomcat is running on is used by someone else maybe you already have a HTTP server in your machine let's say Zam server let's say some some other server which is using the same port number now which port number I'm talking about so if I double click here this is the port which is used by tomat which is 8080 which is uh the HTTP port number now if you have any software which is using this port number then your Tomcat will not start now in that scenario what you will do is you will double click here and you will change your port number you can change your port number you can make it 8081 so now your Tomcat will run on 081 if you save this but I don't want to do that I will say I'm happy with 080 so since it is working on my machine I will not play with that now how do we access or how do we use Tomcat here so what we can do is we can just go to Local Host and type colon 80 now before hitting enter what I will do is I will go back to my eclipse and I I will stop the server just to see if I if I don't have the server there so if I stop it and and after stopping it if I go back to browser and if I say enter it will give you an error it says the site cannot be reached because of course you don't have this port number running here now if I say start let's see what we get so if I say start here and go back to the browser and say enter now can you see that we got a different error it says the page not found but that's fine is it your Tom Cat working yes can you see that the response is you got it from Tomcat that means this URL is hitting the Tomcat that's what we wanted to check but you can also get the amazing homepage of Tomcat if you do one more setting uh you can see in this server location you can also choose use tomat installation it will show you the uh server page if I go back here okay we have to restart the server right click and restart this is how you restart the server and go back to your browser I hope it will show you the homepage of Tom K I can see that we got the page now in which you can you can use this options as well if you want deploy any application but this this is for only uh seeing stuff because since since we are using Eclipse we will be deploying everything from the eclipse you don't have to use this page anymore but it's your choice you can use it so your toit is working your Tomcat is up and running now the next step would be how to create a project and in this video we will see how to install eclipse and how do we configure Tomcat on eclipse on windows so step one would be we need to install eclipse of course you need jdk as well so I hope you have jdk installed on your machine in this machine I have Java 9 but that's fine you can also install Java 8 or Java 7 okay so step one would be you have to download Eclipse so I will search for Eclipse download to be specific we want Eclipse Java e version so I would say Eclipse e download and you can see this is the website you will get and if you click on this okay now the version is bit different this is a version Indigo but we want the latest version so I would select oxygen packages here and from this we have to select Java e developers and in that also we will be going for the different versions as it is Windows 64bit machine I'll be using 64bit here once you click on this it will give you option of download and you can go ahead and you can download this the moment you click on download it will get downloaded by the time it gets downloaded let's go back to the page and you can see that's why ecli so famous you know there are different flavors available so if you're working on Spring framework you can also go for spring which is based on eclipse or for different reason we they have different IDs available now once you got Eclipse downloaded the second software we need here is Tomcat Tomcat being a server for your salet I would say Apache Tomcat you can go for any version I will go for Tomcat 9 and let's click on this one in the earlier video which we have configured on Mac I have installed apach Tomcat 8 and M is just to give you a different flavor that you can work with any version we are installing nine now once you get this page you have to search for so first of all this is in beta version but that's fine it will work you might be facing some issues but then we can we can handle that this you can see we have so many options now we will be going for the zip version because we don't want to install it we just want to load your Tomcat in a clipse right so we click on the zip version this will download the Tomcat 9 and we also need a source code distribution so that you you can see the source code as well so click on zip again how to configure this that we see later but you can see we got these three softwares these three softwares are there in my folder here so if I go to softwares you can see we have the three files and now let's unzip the this version so I will right click and say extract here and by the time we can also unzip the Tomcat version so we can right click on tomcat and then we will say extract here so once we got these two softwares you can see we have this two folder and now I will open Eclipse now inside this folder you can see we have a software called as eclipse.exe double click on that and this will open Eclipse now we it will ask you for the workspace now workspace is basically a folder where you can put all your projects I would say the workspace here is my Sid workspace and I will click on launch it does take some time to open because this is the first time opening eclipse on this machine so once we got this IDE available you can see we got the welcome page let's not touch it you now and click on this welcome page now you can see we have we got different areas this is where you will on the left hand side you will get all your projects uh we don't need this outline and task as of now now this is important the server tab here now if you are working with serlet this is very important you need a server tab now sometime you will not find these tabs here again you can see we don't have console tab here because that is that is where you will get your uh system.out.print and messages now if you want to see this thing you can click on window there's option of Show view in Show view you can select console if you want console there let me just click on Console you can see we got console here if you want server you can also click on server now since we already have it here we don't have to do that otherwise you can select that and it will show you the tab now you can see it says there's no server available now how do we connect our server here so we'll click on this one now it will ask you for which server you want to connect you can see it gives you so many options here now we have to expand Apache in Apache we have you can see we have different version now since we are working with tomat 9 let's select this one and click on next in this you have to select the path where you have your fold let me just go to that path so as you can see I have selected that path here I will click on okay so this is the path where I have my aparture Tomcat which is which we have downloaded and unzip now once you got it let's click on next and nothing to configure here click on finish this is how you connect your Eclipse with Tomcat so this is your Tomcat here but we have to also verify right if it's working so whenever you before creating a project it's always better to test your Tomcat so I would say start and you can see uh it is something is happening behind the scenes we have to make sure that it is starting at some point and it's started now how do you verify if it started now first of all you need to know which port number your tomat is using so if I double click here on this this is the port number you are running your Tomcat on which is 8080 to verify this let's go back to your browser and here to verify this we'll type Local Host colon 880 so we'll type Local Host colon 880 and if I say enter you can see we got the response from Tomcat now since your tomat is up and running that's why you got this response now if you want the awesome Tom Cat web page you can also also select this option the second option which will show you the Tomcat homepage if you want to see that and once you do that configuration you can also restart your machine now again sometime you know your machine will give you some issue if you already have a service running with this port number maybe you have a Zam server or some other server which is using 8080 port number you can change it from here uh you can change your Tomcat port number to 8081 or 8082 your choice and saved it and restart the server everything will work uh once I've done that configuration let me go back to my browser and I will say enter can see that we got the awesome Apache Tomcat homepage so this is how you install eclipse on Windows and this is how you configure Tomcat now once you have your eclipse and Tomcat configured it's time to start with our first project now how do you build a project or how do you create a project in Eclipse it's very simple you can just go to file there's option of new and click on a project so it's very easy just go to file and click on new now it will give you certain options like jpa project Enterprise application project now which want to select we will be selecting Dynamic web project now this is one option you can go for or the other option would be just right click on the project Explorer and send new now there's option of dynamic web project let's choose it from here and now it will ask you for a project name we can give any project name here I would say the project name is demo app now that's the first thing you have to mention the second thing is you have to mention the Target runtime now Target runtime is basically a runtime where you will run your project if you remember we have installed Tomcat 8 that's why you're getting this option you can also choose a new runtime you can configure multiple run times in your machine but one project will use only one runtime at a time next is the dynamic web module version so this is the version of seret API so basically serlet is a API using which you can build web application the first public Reas was 2.2 but now we are using 3.1 let's choose that and nothing to nothing specific here everything will be default click on next nothing to specify here click on next now it will ask you for the context that that we'll discuss that later now this is important generate web.xml deployment descriptor now this file is important here we'll check mark this and we'll click on finish Now by doing that it will give you your first project and see that we got your project here let's try to understand what are these folders we have in this project so the first folder we will be looking at is web content expand it you will see the there are two folders one is metf and one is web INF metf is not that important as of now once you start working on the Enterprise things we we can see that but now the most important part is this web in folder now it's actually very important because this folder has this file which is web.xml what if I say this is the one of the most important file for your Ser application this is where you will configure most of the stuff now we will not be looking in deep in this file yet we close this and minimize the web content the next important folder which will be going to use is the Java resources now you might guessed it right we are working on Java so of course we have to write Java code now if you're thinking sub it will be something very different not exactly whatever you have seen in in your code Java so it will look on the same page now this is where you will create all your Java codes so whatever files you will write you will you will do that here in the in the resources but time being again will not do that let's go back to web content and let's create a simple page because that's what we wanted right we wanted to create a page which will print hello world so what I will do is I will right click here and I would say create a HTML page and that's the beauty it's not just about Java it's about making a web application and HTML Remains the default design language I would say the page name is index. HTML that's the page I want to create and in this page uh you can see the page went to the web content folder this is where you'll be having all your public Pages now in this it will give you certain tags like head tag title tag we don't we will not be using that okay I should be increasing the font size here so you can see we got our page here we got the text so you can see we have head tag we don't need those tags yet now we have a body tag and we'll be using so yeah we have a body tag here and in this body tag I will simply print hello world okay we got hello world here and then Simply Save this but how will you see the output it's very simple just right click on your project that's one way or you can right click on the page itself and you will say run as run on server if you have a server it will of course run on that I will click on run on server it will ask you for which server as I mentioned you can have multiple servers will mention that I want to work on on Tomcat 8.0 click on next now it will say Okay configured app which is we are working here click on finish it will restart the server the server which you are running here Tomcat server click on okay it will the server it will deploy your application on the tomcat and that's why we said deployment you it will shift your project into your tomcat and you can see that we got the output we got the output as hello world it's as simple as that so yes we can create a static page in your Java web project as well this is the address which we are using and we got hello world now you want to wondering this should be getting opened in a browser right why it is getting opened in Eclipse the amazing thing about Eclipse it it has a internal browser so you can see the the browser here this is the internal browser using this browser you you can actually go to any website uh you can say I want to go for I want to go to google.com even it will take you to google.com yeah this is the internal browser so in case if your external browser not working working someday you can use Eclipse browser but that's not the main reason why you use this so yes we can we can write our code we can test our code here we can see the output but I'm not a big fan of internal browsers I love external browsers because let's say if you are also using some designing tools or designing things like maybe jQuery JavaScript CSS in those terms it's better to test your app on the external browser so what I will do is I will I will switch from internal browser to external browser the way you do that is by going to a window and say web browser and you can see that it gives you option of internal web browser we don't want that I want to go for the default or we can go for default one or we can go for Firefox so let me select Firefox here so that I will see the output on the Firefox and let me just close this window now and let's run this so I will simply click on this and okay I know that we have to select tomad why you asking me every time so you can say always use this server and click on next and click on finish the moment you do that it will take some time and it will open Firefox can see that you got the output on the Firefox browser now so this is how you create your first project in web uh in Java but then we are not yet started with salet why do we need salet here so let's create a scenario where you will need solet let's say you want to add two numbers how do how will you do that uh you might be saying hey we can use JavaScript and we can add two numbers but then hold on uh I think two numbers can be done with JavaScript but what if you want to do some server side coding you want to fetch data from database you want to uh perform some operations which is which requires some server processing not just uh on the client side in that terms you need to use server side language which is Ser in this case so just to explain that I will go for a normal code which is form action so this is how you send a request to server right we use form action so you say form action and here you will ask for two values so let's say I want to I want to ask for uh the first value the second value that's what you do on the page so I would say enter the first number and I will give a colon and here I would say input type equal to text I will say name equal to uh num one because this is where you will enter the first number so I will say num one and after that I want a break tag and I would say this is the second number and I will change to num two I got two numbers I inan two text field where I will insert the numbers I I want a submit button here so I would say input type equal to submit now this is where you will submit your submit your request but the question arise where I mean who will handle the request once I've done with the coding here let's go back to Firefox and let's verify if we got the design and yes we got the design here and if I say 7 uh maybe seven and 8 it should print 15 right so if I click on submit qu you can see nothing is happening it's because we have not specified anything in the action you will say hey add those two values so this this is the URL which you are using which is ADD and now if I go back and if I say the same request first of all we have to refresh the page and I click on submit query can see that now it is sending a request to add and see the in the address bar the request is going for the add request but unfortunately we don't have any page called as ADD page so how it will work and that's where you need to create a seret so in the last video we have seen how do we create a project in Eclipse which is a web project in this case and we have also created one page which is index.html so what we are trying to do here is we are taking two values the moment you click on submit we want to call add but what exactly this ad is the purpose here is just to add these two numbers that's it we can do that with JavaScript but here I want to use a server site technology and we want to use Java and the only option you have is seret so what we will do here is let's create a solet but how do we do that it's very simple actually just right click on your project and say new here I will select a class but you might be wondering we are making a serlet right it should be something new oh not exactly we can create a simple class and that will behave like a solet I know you don't trust me I will click on class here I will create a simple class which will add two numbers so we will call this as a add solet again you can use any name I love this name which is ADD solet I will go with that and here in the package I will mention the package name I would say com. Tesco you can use any package name but then you have to make sure that your package name is unique and that's why we always use a reverse of your domain name because your domain names are unique so of course your package name will also be unique and here I will click on finish a very simple class so you can see we have a very simple class which is doing nothing now in order to make a salet you just need to do one thing there are different ways of creating a salet we will talk about the life cycle of suret and what are the complete hierarchy time being just to make it simple I will use a class name which is so we just have to extend a class which is HTTP svet so you just need to extend HTTP suet and your job is done the moment you extend a servet class your class becomes a servlet so you just have to extend HTTP servlet okay now this sounds cool the next problem is when you click on this submit button it will call add right and we are expecting when you call add it will call add seret will it be happening automatically that's different thing we will talk about that later but imagine if this class is working if you want to perform any operation you need methods that's what we have learning Java if you want to perform if you want to do any any task you use methods now here as well I have to use a method but which one now sulet says if you want to use a method name you can use a service method because sulet is a server component right and what server does it provides service so we can use a method which service can I use some other method name the answer is no if you want to make it working you need to use a service method name now service is a method which belongs bels to a Ser life cycle so of course you have to go with this method name so a service and here I want to accept the request from the user and I want to do some task now the question would be how a user will send a request I mean that's okay so but let's say if you got a request here how a user will able to send data and this sofl is able to receive data as we have seen in the video the theory video which we have done whenever you work with service method or any server you have to work with two different op object one is the request object and second is the response object so you have to pass a request object here so I will say HTTP Ser request object and RQ the thing is you don't have to actually create this object by yourself this object will be created by Tomcat you just need to get a reference of it so you don't have to say new keyword or nothing something like that you simply say HP Sol request and you say RQ and then we sayp Ser response so basically we need these two objects here now using the request object you can fetch data from the client and using the response object you can send the response to the client so it's all about request and response right now in this method service once I got this object what I will do is I will try to get data from the user now a user is sending two values right so I would say in I where you will have the first value so I would say re EQ now this request object will have your data so I can say get parameter because when a user is passing values a user is passing parameters and and I would say I want to accept the first value but what to mention here now as a user you are sending two values right you are sending values in Num one and num two the first value is there in Num one so I would say I want to fetch value from num one okay now there's one simple problem get parameter will give you a string what we are expecting here is an integer so we have to convert that right and the way you do that is by using parcent our favorite method which is parcent and this string will be getting converted into integer the same thing can be done for the second variable I would say I I mean J here so this is J in J integer. passent and here I will say num two now you can see we got both the values we got I and we got J the only thing we have to do now is add them and the way you do that is by saying int k equal to I + j a simple code Java concept okay now the question would be we have done with addition how do I print it so so normally in code Java what we do is we say system.out.print and I would say uh result is and I will print the value of K so I'm expecting that this should work now will this work let's try I will run this application I will go back to index I will say run as run on server so you can see we got this page here and in this page I will I will say 34 and 21 or 12 so the answer should be 46 let me just click on submit oh it's not working it says 44 our favorite error right and there's one thing which you have to observe look at the address bar we are sending a add request that is perfect and we are also passing the values so we are passing the value of num one and num two in the address bar so this is called as a query string so query string is a part where you can send your data from a client to a server so let's go back to the server now the question would be why you not able to all this ad Sol it for the obvious reason right if I if I look at this ad you I mean when you click on the submit you you're calling ad right so you are calling add and you have a Serv name which is ADD servet don't you think it will get confused between these two we have a add subet here and we have ADD here to solve this problem let's try to understand the flow the moment you click on submit now since you are using Tomcat Tomcat will say okay you are requesting for a page the request will go to Tomcat but Tomcat don't know that this this is the class you have to call now it will ask you hey programmer you a user is trying to call AD sovet you must have defined the flow right so whenever a user call add it should call AD solet where you have configured it and unfortunately we have not done that yet so Tomcat will look for this file which is web.xml web.xml is also called as a deployment descriptor this is where you will have all the configuration so if I open this you can see we don't have any configuration for this servlet this thing we don't need as of now I will I can remove all this stuff that that was a welcome page what I want here is I want to configure my selet so I want to say whenever a user request for add I want to call add subet and the way you do that is by putting two tags here one is a subet tag and the next one is subet mapping tag you just need to use these two tags that's it a subet tag seret mapping tag now in inside subet tag you have two subtags here so one is a seret name and the second one is suet class in the subet mapping again you have to Define two tags one is the suet name and the next one is URL pattern now this is unique right we have a servey class name that makes sense we have a URL pattern because you are sending a request right so if you look at the address bar this is a basically a URL so that makes sense but why we have subet name it's because in your application you might have multiple svets may not be one maybe 10 12 50 100 you're not sure now for every subet you need to create a subet tag and a Ser mapping tag so there will be a confusion which subet mapping is linked with which subet and that's why we use the subet name so these two are linked so they should have the same name and I would say the name is ABC again you can go with any name we time when we are going for for ABC you have to make sure that if one pair which is seret and ser mapping they should have the same name if you have another seret you'll be having another pair of this now what about the subet name or the subet class the subet class is the sub class name you have to mention which is ADD sulet and the URL pattern is your ad so slash add because you're requesting for a add request so add will call add Sol it but then we have to provide a fully qualified class name and when I say fully qualified class class name it means with the package name so the package name is com. telescope dot so you have to mention fully qualified name so a class name with a package so com. telescope. add solet now everything seems C it should work now now how to verify let's right click on your Ser right click on your server say start go back to your browser let's refresh this page I will say submit oh something is happened you can see there's no error okay so we have done with the first step at least we have not getting any error if I go back to the browser or the eclipse you can see we got the answer but the answer is coming in the console but why on the console uh for a very simple reason you in the code if you can see we are printing the data on the console so whenever you say system.out.print you always print data on a console I don't want to print it on a console I want to print on the page the the client page you can see we got a page here but this page doesn't have the data now to understand this question arise who is sending this this page it's actually a serlet serlet is sending that page in the format of response object now in order to write the data on response object you cannot say system.out.println you have to say it is response. get writer. print Len so basically you are taking the object of response and then you are fetching a writer object of it and you are saying print Len I know this looks confusing so what I'll do is I will try to create this object outside so I will say response Dot get writer so if you can observe this get writer get get writer returns an object of print writer so I will say print writer this is what it returns so I will say print writer out equal to rs. get writer we have to import the package for print writer first of all and here instead of using get response. get wrer I will say out so what you're doing is you are printing data on a response object this is the object which goes from the client from the server to client now it is throwing an exception it says it might throw IO exception uh so you can say throws that's one of the best option you can go for so I would say add throws declaration our job is done and it should work now the data should be printed on the client machine I will go back here right click on the server and say restart it's doing something let me just go back to my P browser and I will say refresh click on submit can you see that we got the output on the client page this is what we expecting so this is how you create a simple s which will accept two values and it will add those and it will print data on the web page just to repeat we have created a Class A simple class in the in any package you want and you just need to extend HP serlet class in which you just have to define a meth service you cannot go for any other name other than service you cannot say ABC pqr yes we do have some two extra methods we can use but time in let's focus on service it takes two objects request and response in the same sequence and then you need to fetch those two values add those two values and you can print it you can do you can do anything you want but this is what we have done this example so I hope you understood how to get a how to get a salet and how to send a response so let's make a quick recap whatever we have done till now so if you can see we have an HTML page here so when you run this code let's run this code first I will say run as on server so it's opening up browser and you can see we got the output page which is it is asking for two numbers I will say 3 and 4 or 34 and 4 just to test what is happening so as you can see we got the answer as result is 38 now the point is when you talk about this protocol which we are using here which is HTTP protocol now this protocol has certain methods to work with now if you're coming from networking background if you read about s protocol we have five methods we have get post put delete and options uh but then we don't use all in the web page normally when you want to send data to the server we use a post request and whenever you want to fet data from the server we use a get request now here by default it is using a get request now get request is something which we use when you want to fetch data and the post is something which we use when you want to submit data now here you can see we are expecting an answer but then we are also sending values right now whenever you use a get request the values goes into address bar but then hold down where did I mention that I'm using a get request Now by default if you don't mention anything in the form It Is by default get method which means if I say a method by default is get I don't want to work with get I want to work with post now in that scenario what will happen is the the data which is you are passing from the address bar will not be using the address URL to send data example uh if I remove this get and if I use post because by default is get I want to use post by doing that I will go back to my page I will refresh it and I will send the same data with 34 and four now this time you can see we got the same answer but look at the U RL it's not there the query string which you're passing is not there so that's the beauty about a post request now when to use get and when to use post so whenever you want to fetch data from the server go for get when you want to submit data to server use post now when you submitting data it can be also your username password so when you want to verify your credentials you use username and password right so in that scenario use a post request now here as well I mean in this case we don't want to maintain any secrecy we can actually use get get request but then just to understand how post works we are doing this so post will not put your data into a URL now it doesn't matter you're sending a get request or a post request request will go to service method it will execute now I have one more Point here the point is I don't want to work with get request I mean as a as a programmer of of this svet I want to say I want to accept only the post request I don't want to accept a get request now can we control that you know you can once once you get the request object you can actually fetch what type of request you are getting is it a get or a post request but then why you have to manage all those things solet says specially HTP solet says Hey programmer don't worry I will give you two extra methods one to work with post and want to work with get so instead of using service we can also use do get or do Post now in this case I want to work with post request so I can simply use do Post Now by saying do Post what we are making sure is this will only work with a post request it will not work with a get request so let's run this code and let's verify it so right click here and say or we can simply restart the server that will that should work and if I run this if I restart the server let's go back to our browser and here if I refresh this page say submit and you can see is working so with the post request do post is working but how about if I go to a client page and say Hey I want to work with a get request in fact you might be thinking uh as a client why they will change now I don't know if you know about this uh if you know the browsing to tool but then we in the in the browser you have a way to change it you can see that the get request is not working with this so what I was saying is you know even if you mentioned the post request on this sub I mean in the code if you mention post request still as a user you can change it from the post to get request so you just go to inspect element and there's a way you can just go here and say hey uh the programmer who is designed this web application said you will be working with post but as a user I want to do some experiment I'll be going for get request so yes you can change it and you can the moment you say submit you can say it will not work because a programmer says Hey user if you want to call this software the only way is to send a post request you it we will not accept get request so yes that's the control you have so it will only you work with a post request not a get request so refresh and submit this is a post request it is working so yes we have two methods one is do get and one is do post so we have two options do get and and do post but how what is happening behind the scene when you click on this submit button it will check what type of data you're passing so you are sending a post request so it will go to the web XML file and say hey I I'm searching for a URL which is ADD so your webml says hey go to add salet but your Tomcat internally has a service method because as you can see we are extending HTTP solet so request will go to service method for sure request always goes to service method so in the HTP solet we have a service method so if I search for service here and this this is where you have service now depend upon your request it will call a do get method or a do Post method so even if you are mentioning do get a do post it will be called through service so that doesn't mean service is not in the picture now because we have started this series talking about service method right so yes service me is still there but what is doing is it is running behind the scene so it's that cool now how about if you want to work with both the methods you can do that actually you can Define both the methods here one is uh do get and one is do post so you can copy this code and you can say this is do get so I want to have a get request and a post request both uh and both will perform the same operation you can you can do that it's it's an option for you so you can use a get request or you can use a post request with the help of do get or do Post now don't you think we are repeating the same code so do we have a solution so that we can write it only once yes you can you can just create another method and in that method you can just write all the statements and what you will do in do get and do poost is just call that method particular that's it your job will be done so that's how you work with do get and do poost for this example we only need do poost so let's remove this and it will work so once we have talked about those two beautiful methods which is do get and do Post in fact I just want to do one change here let's continue our discussion with get method and here also I will say do get not do post and let's verify this if it is working so I will go back to my server as usual let's restart the server and open your Firefox here we go let's refresh and says submit it works can you see that it is a get request it works perfectly now what I want is let's say I want to add two numbers which is happening in this case and I also want to find a square of this number now maybe we have a scenario where you want to add two numbers and you want to find a square of that it's very easy actually you can you can say k = to K into K and then you can find a square but this is a very simple logic right let's say you want to build a very complex logic and the logic which is finding a square imagine this is a very complex logic and then you are already doing that in some other suret now in general what I'm talking about is let's say if you have two suret one is a square servlet and one is ADD suret can I call a seret from a suret it may not makes sense in this example why do you call a Serv from a Serv but have patience in in the in the further videos we'll discuss why do we call a Serv from a servet because we are also going to see JSP and we'll see how all this thing works in the real life but time being let's say you have scenario where you want to call a solet from a solet because in this case what we are doing is we are calling a solet from a page right what I want is I want to get one more salet the way you the way you do that is by very simple just right CLI on your resources in fact on your project and say next I want to create a new class and I want to put this class in com. telescope I hope that's the same package we have maybe the class name I want here is a square sovet and this Square sovet will be extending a class which is HTTP sovet okay let me browse it and it's here finish and you can see we got a class which is which is extending HB solet instead of doing that you can also do it here now in this I want a method which is public Vo do get so that I can send a request and of course this do get need to exess two things I will simply copy this code I'm damn lazy to type it so I will just uh do that okay can that we got do get method now what I want is I want to find a square of a number okay first of all I want to call this s later we'll try to find a square how do you know that this server is getting called it's very simple just print something I would say print writer out equal to re. writer this is how you get the writer object so that you can print you can write something on the page once I got the write object and and I would say out. print Len and I would print hello to square that's it I just want to print this message now in this page index. asml page I'm calling I'm I'm sending a submit request which will call add this ad will call add salet but I want to see the output which is hello to sq how do you call that particular solet it's very simple actually just after this line in fact you don't need to print anything now because we are not going to print this data here I want to print that on the second svet to call that s basically we have two options one is request dispatcher and the second one is redirect again I will talk about this in detail in the 3D video the two difference between this two time in let's focus only on dispatcher request dispatcher so how do we use a request dispatcher because we are dispatching the request from one Ser to the servet the way you do that is by creating object of request dispatcher you will say request dispatcher and I would say Rd again you can use any name but then how will you get the instance of request dispatcher because this is a interface unfortunately so the way you do that is by saying RQ which is request object dot there's a method call as get request dispatcher this will give you the instance of request dispatcher here you just have to mention the subject name in this case not the sub name basically you have to mention the URL I would say I want to find a square but the question arise how will you use this Square okay when you send a square request how will you call this one and we have one more we have to also say rd. forward by passing those two beautiful objects which is request and response so you have to send a request object and a response object and you can see there's exception so I will simply say add a throws there okay everything looks fine and it needs to call this saet but it will not work why because if you remember if you want to call any salet we have to make sure that in the web XML file you mention that sub mapping right so we need to create the serlet and servet mapping for the SQ serlet so the request we are sending is for SQ and we have to change the name if you remember we'll say pqr here again it should be a logical name but then just to show you that it can be any name I'm using this pqr PQ or ABC ABC and this Square the subet is square subet right so when you send a request for SQ it will call Square suet so let's go for this uh flow so when you click on submit it will call add seret from the ad sub I'm dispatching the request to sq but we don't know what is SQ is and for that in the web exml file you mentioned whenever you get a request for SQ you have to call SQ serlet and this is your SQ serlet here now will this work that's the question just let's restart the server or maybe I will just run this here so I will say run as run server and let's enter some values I will say 34 and this time let's say two submit and you can see that we got hello to sq so yes we are able to call a server from salet the next thing would be I just don't want to print hello to sq what I want to do is so this salet should be able to pass the data the K value to the SQ seret how will you do that because see we are forwarding the request right now there's one way you can do that is by using a concept of session management so normally when you want to send data from one server to the Ser or maybe you want to share data between different servlets we use a concept of session management now we'll talk about session management later the easiest way to send data from a serlet from ad seret to sq serlet is think about this this request dispatcher is forwarding request object and response object right and the same two objects are being used here so don't you think if you can put some data in the request object anyway you're passing that object right so can you just put the value K in request object something like this can you say req do set attribute so yes you can set an attribute by saying hey this is a K value and the value itself is K now what is 2 KS here this is a key and this is a value so you're passing a k value which is maybe whatever value you're passing from the user and then you are naming it K so that when you want to refer this on the another s you can refer that with K so basically we are able to send this K value from add Ser to sq seret but how will you fetch it here now since you are setting the attribute in the request object here you will fetch it from the request object so I would say intk equal R eq. get attribute so we got get attribute and here you have to mention K so we are passing the value from K okay now it returns an object of an object what you do is you have to convert that into integer format so you can simply type cust it so when you have an object format you can type cast it to int and that will perfectly work and here I just want to print oh first of all we have to find a square of a number so we say k equal to K into into K and and here I would say I don't want to print the value I I don't want to print hello to sq I want to print uh result is K so now it will so when you when you send a request now it will first get added those two values and then you will get this Square as well will this work let's verify let's go to our index page or maybe I will just simply restart the server and then go back to the browser here I will say refresh and I will send a value so that we can see if it is if you're getting the square square of a number 6 and 2 which is 8 addition and then when you square find the square of it it should be 64 and we got 64 right is that simple this is how you call a solet from a suret so in the ad seret you use equ dispatcher now there's another way using which you can do that that is redirect and we'll see you right in the in the next video so in the last video we have talked about what is sovet how sovet works right so we have also seen the life cycle of sovet so the first method in the Ser life cycle is in it then we talk about service and then we talk about destroy now if you observed in the Practical implementation when we have implemented that service method right we have provided two objects one is request object and response object so those two those two objects are here so in this video we'll talk about those two objects one is HTTP request object and HTTP response object now why we need do this two objects so let's say we have a client machine here and then we have aof saet now when I say saet it means server when I say server it means saet because we are learning a server technology so we have a saet here and then we have a client so let's say this client want to add two numbers so if you want to add two numbers that means you have to first send the request and you have to also pass those two values I mean let's say let's add two and three so if you want to add these two values you have to pass those two values right of course now how do you pass those two values maybe not just this values I want to also talk about what is the client address I want to also send about what I mean which browser client use so if you want to send all those information you have to pass it in some format right and since we are working in Java technology we will work with objects so what we can do is we can put this values in in an object right so we can put those values in an object now this object which is going from the client page to the server will be called as your request object so request object is an object which will hold your values now once the Ser got the request you have to send a response right so what we have to do is when you're sending a response okay when you send a response what you what you send so basically sometime we want to send a data we want to send a HTML tags we want to send a video we want to send an image so you can send all those information in one particular object and that's your response object so while sending the data we'll be sending a response respon object now this response object will have all the data which a client need right so request object will have all the data which a Ser needs and response object will have all dat which a client needs right again it's depend upon Ser what it what what it wants to send right so let's say in this scenario a Ser want to send a value five right so this response object will have a value five now it can be of type this data can be of type text this data can be of type HTML this data can be of type let's say a video so you can have all this format in your response object again you can specifically I mean you can you can specify what format you want to go for right again how to do that that was in the better implementation but that's that's possible you can specify what type of data you're sending you know in terms when you talk about the web services world where you return Json value you don't return a page or a text value want to return a Json value in that scenario you can specify what data you want to send from the server machine to the client machine with the help of response object okay so we have request object here and then we got a response object now where do we use it so if you remember in service method we use two objects one is request object and the next one is response object again when you say request object is a part of HTTP Ser request and response object is the object of HTTP server response but then question arise can I do we do we need to create object of it and the answer is no we don't have to create object of HTTP server request your tom cat says Hey you want an object I will give you the object so Tom Cat will give you those two object request object and response object you have to specify what you want and Tomcat says I will give you that okay so in the service method you have to specify the references so that Tomcat will give you those objects to you make sense so yeah so these two are actually interfaces okay HTTP server request is an interface h server response is also an interface not a class but the implementation is done by the tomcat and object will be given by the tomcat and that's why when you want to run a web application you need a tomcat server right so yeah so that's how your request and response object works this is the theory part of it we'll see the implementation of it as well uh in the next video we'll talk about how do you call a Ser from a seret is it possible let's see how how to do that in this video we'll see how do you call a serlet from a serlet so let's say we have a seret one here which is S1 and then we have S2 so these are two different servlets now in the first scenario let's imagine you have a client here and client want to send a request to S1 normally we do that by creating a button on the page or you can submit form so when you click on submit it will call S1 that's when you configure it so it will call S1 and S1 will send a response to client now basically there are two objects which comes into picture one is is the request object and other is the response object so a client will send a request to S1 so S1 will use a request object to fet the data from the client and S1 will response will send a response to the client time let's say that that object is request one and response one because we have subet one here but let's say I have a scenario where subet one should be calling subet two so instead of sending a response here a subet one is supposed to call a subet 2 how do we do that so as one can call S2 with the help of a special feature called as request dispatcher so we have a concept called as requ dispatcher using which you can do this let's name that object as Rd so request dispatcher object is Rd here so using this R you can call S2 from S1 so you just have to create object of re dispatcher and you have to mention the name for the S2 which is Ser 2 it will call it but normally we also forward these two objects so when you call S2 you send the same same two object which is request one and response one to the S2 so that if you want to send some data from S1 to S2 let's say if you have a variable called as X in that X variable we have a value which is five and that is there in the servit one if you want to send that to servit 2 what you do is you add this value X in the request object so that you can access that data in S2 as well because anyway the second Serv will be using same two objects now if you want to send a response to the client a client will send I mean the response which will get from the client is same rs1 so rees1 goes to the client page because the same object is getting forward here right so client don't even know that response is getting from where is it from S1 or S2 in fact in the URL so in the URL the address is still for S1 so a client as a client okay when I say client I'm talking about the browsers okay so a browser don't even know that from where getting the response is it from S2 or S1 because in the address bar you still have S1 that is dispatcher now this works fine when you have the same website let's say the all this soft we have S1 we have S1 and S2 on the same website uh let's say the website name is th.com so if if this on the same website it will perfectly work but what if you have a scenario where S1 belongs to th.com but S2 belongs to some payment Gateway let's say PayPal so let's say if you want to donate to pay tesco.com you will be using PayPal for the service now in this scenario the.com S1 will send a request to S2 which is Paypal now in this scenario client should know that something is happening behind the scene so this is another scenario where we so if you want to do this a client so the request to PayPal should be done from the browser so let's say if you if you click on donate button on this on the.com the.com will inform browser hey you are getting redirected to PayPal and that's what happens when you buy something online so let's say when you go to amazon.com to buy something when you click on check out it takes you to payment Gateway it let you know that you are getting redirected to payment Gateway now in that case you cannot use request dispatcher right because a client don't know in request dispatcher that you you're getting re you're getting redirected in this case you will be using a special method called as send redirect uh so we have a method which is send redirect in servlet using which you can actually know your client will know that you are getting redirected to the to the next servlet so in the other scenario S1 will call S2 but in this scenario S1 will inform in redirect S1 S1 will inform your browser hey you are getting redirected to S2 that means when a browser so let me just do it once again here so imagine we have a browser so we have a browser here now browser send a request to S1 now S1 let's say S1 want to call S2 but we cannot do directly right so S1 will inform browser hey just go to S2 now so now in the first scenario it is your request one for the next request for S2 a client will send a new request which is req and a client will get a new response which is r S2 in Rea you basically send two different requests this this is important to remember yeah so you sending two different request because in dispatcher we were using the same object to send data like example if you want to send this x if you want to send this X in this S2 you can simply do that with the help of request object but in redirect you cannot do that because in redirect you are using two different objects two different request object and that's where if you want to maintain the data if you want to send data from uh if you want to send X from S1 to S2 if you want to use we are sending a get request then you'll be passing this two values I mean you you're fishing this two values you're adding it and then you are setting the data in the request object so that when you file a request or when you send a request to a next next to which is SQ by using a request dispatcher you can simply forward by passing request and using the same request object on the second solet what we are doing is we are fetching it from the request object we are storing in our variable and then we are performing the operation and print now in this video we'll try to do the same thing but with the help of some other thing which is send redirect now how do we do that so what I will do is I will come in this section the thing is I how do I call a second servet because without this thing you cannot call them so that's a question how do we do that so what we'll do here is now since uh we are not sending a request object let's remove this because in Cent redirect we don't do that so we have a response object so we'll say response dot send redirect because in this scenario what you're doing is this solet which is at solet is sending a response to a client by saying uh response. send weite by saying hey client just go to square serlet that's what you have to do go to a square serlet now a client will send a request to the square seret let's see what is happening behind the scene so right cck here I would say run as run on server let's go back to Firefox and here I would say any number doesn't matter so I would say 8 7 and five click on submit you can see we got an error it says null Point exception do get what's that in the SQ sub we have the issue the issue is not with the ad Ser the issue with the SQ seret now if You observe the URL it is SQ because what is happening is you are sending a request to add but your add serlet says hey go to sq so your client is sending a request to sq and if you see so send rect is a method belong to response object and you simply say redir to sq now the issue is you can see in the request dispatch the in the request object you're trying to fet the object of course we are not sending request object then how can you use the same object here that's why you got you got that another Point exception but basically let's not try to do all these things we'll try we'll we'll do this once more with the another example but then here let me just try to print something so that you know I will get some output at least on the console yeah nothing somewhere maybe on the console I would say SQ called just to see if if this Ser is getting called or not uh I will restart my server once I do did that change and I will go back to my browser and refresh send and you can see it is able to call SQ by doing nothing you can see we not we're not getting anything on the page but if you see console in the console we got SQ called so yes we are able to call the second subet from the first salet and and the what there's one way one way you can verify this the request is going to sq that means this is the second request not the first request so in total a client is sending two request add request and uh SQ request now the point is how will you send a data because that's important for me right I want to send this data okay now what if I will go back there and I will uncommon this because I want to of course I want to calculate this one right and the way you can get that is let's say because we are not sending data in the request object so I cannot say get get attribute but how about think about this if I say get parameter will this work so I would say get parameter and I would say data is coming from let's say k itself okay I mean of course we are not sending data but uh let's try let's try let's do some experiment here and I would say request object. get parameter I'm expecting that the data is coming in the K format normally when you say get parameter the parameter comes from in a request you know is it a get request then if is a get request it will coming from the browser address URL remember if it is a post request then a different way of fetching data but the data should be coming from the client let's try so what I will do is I would say uh I I have to convert that of course I have to say integer. pass int and I will convert whatever whatever coming on the way so the value of K will be converted into int and you're calculating it now let's verify I will go back to my server as usual restart it go back to the browser and refresh the moment you click on submit again we got the same problem right number format exception because the data which you're getting is null and you cannot format it but what if just after the question mark after the SQ if I say question mark and k equal to 9 okaye that it worked the moment you pass a value in the URL it worked because the second serlet is asking for a value from the parameter and normally when you say parameter it's a do get request so of course the data is coming from the from the URL and that's why it is working but what if I want to make it work without even changing the SQ I mean the address bar or the URL in that case what if in the ad servit itself I can give a question mark here and I will say k is equal to plus K I mean think about it the the response which a client will get is a new URL which is SQ question mark k equal to whatever the value you're passing let's try with this I will restart the server and I will will submit the same value 7 and5 click on submit can it worked and in the address bar if you can see SQ question mark k equal to 12 so that's one way you can pass a value from one sub to another subet with redirect because in redirect what is happening is this sub which is ADD solet is responding to the client by saying hey client go to sq and the URL mention k equal to 12 because the value of K is 12 and the same thing is requested by the client so this this is requ requested by the client thanks to ad seret and this is what you're getting now this type this technique comes under session management again we'll discuss session Management in a theory video but then we have a concept of session management where you can transfer data from one server to another server with the help of three concept one of them is session one one of them is cookies and the thing which we have used here is called as URL rewriting because you're changing your URL and that's why it is called as U r l righting so that's that's one of the way again we have two more ways one is session one is Cookie so we'll discuss that in one of the video in session management but here we have worked with rud and that's how you forward the request so yeah that's it from this video so what we have done till now is simply go to this page which is index.html where I I'm asking for two values num one and num two when you click on submit the request goes to add where add will call your add seret where you are accepting these two values you're trying to add them what we also trying to do here is we are trying to call another solet or we are sending a request which is SQ which is handled by the SQ subet now there's one thing we have to remember is we are using a response. send redirect which means the ad sof will send or it will respond to a client by sending a signal by by sending a signal which says hey just go to the SQ solet now SQ sub it will try to find a square of a number but which number we have to send this ke right and we have done that in the earlier video we have we have talked about this that we can pass the value by saying SQ question mark and then we can pass a value so the way we do with the quy string but do we have another option let's say what if you don't want to use URL rewriting which we have done in the earlier video now you might be thinking what's a drawback and doing that because it works right what if you want to pass multiple values what if you want to use the same value in multiple subet not just this serlet maybe you have ADD subet which calls SQ serlet then SQ seret which which call some other solet in that scenario if you want to maintain the data throughout the session oh that's a good word now there's a concept of session which means whenever you go to a web application it will try to maintain a session for you and that will be done by your Tomcat of course how will you use a session here because session is there throughout the throughout your visit dat right if you visit one subet second Sol third solet it will be there for you so what if you can put your data into session so once you put your K into session here you can use that session value in the next salet how do we do that as I mentioned session will be given by Tomcat we just have to get the hold on it and the way you do that is by using HTTP session so HTTP session is an interface we just have to say h be session session and as I mentioned the object will be given given by tomat itself you simply have to say request. get session we just have to get the hold on it and once we got a session object with us we'll simply say session do set attribute because session will be there throughout your visit to the website right if you go to ad Serv if you go to sq servit even if after sometime if you go to some other page which is the same application the session will be available for you Prov you don't close the session Prov you don't remove the value from the session we can do that by the way so we'll say session do set attribute and I would say the value is going in a format of K and the value itself is K so I'm sending a attribute which is K and the value is K how do we how do we access that here how do we fet the value now of course we are not sending data into request object we are sending data in a in a session so the way you fetch the value now is by saying so remove that we'll say session. o we have not instant we have not given the reference for session here we have not declared it here so I would say HTTP session HTTP session session equal to the same thing uh request. get session and once we got an hold on it once we got the hold on the session object I would say session dot get inside at subet we have say set attribute here we have to say get attribute it's that simple and here I would mention I want K okay now there's one issue when you say attribute it means it will return return you the object we don't want object what we want is the int format right so we'll simply type cost it we'll say int and we are done so this will give you object and we have to convert it into int we just simply typ casting it again we can we can say integer passant as well we just have to convert it into string but multiple steps right this is simple we see we can simply typ cast it with int now this should work let's verify I will go back to my HTML I will say right like run as the on server so it is opening my fire fox and here I would say 2 and three which is 5 into 2 which is 25 answer should be 25 and got it can see that we got the answer so first it is adding those two values and then it is it is finding a square of a number quite simple right so we can use session as well now as I mentioned the data will be available in this session for you throughout provided you don't delete this data from the session can we really do that yes we can actually we can delete data from a session uh so we can once you got the hold on it we can say session dot uh there are there's a way you can say is it remove attribute yes you can say that we have remove attribute and you can remove k here now once you remove K of course you cannot fetch the value so you can fetch this value only when you have the value in the in the session so using this you can remove it that's how you can use session normally we can use session to maintain the login so let's say if you log in on your website and if you want to verify if you if if a user goes to another page and if you want to verify the user is already logged in you can you can maintain the data in the session that's what we do this looks cool right so session looks cool do we have some other way in fact we'll be working on on session more in the future topics once we start with JSP but can we do something more here and the answer is yes what we can also do is we can use cookies as well so the way we have session we can also use cookies now how that that works so what I will do is I will not use session anymore let me remove session let me remove session from it everywhere I want to use cookies here so I would say intk again and I would say zero how do we do the same thing with the help of cookies let's get back to add and here I would say I want to work with cookie now what is Cookie basically so whenever you send a request to the server and a server will send you a response now in that response object so the response object which is coming from the server will have a cookie and then when you when you send the request to sq Ser you can send the same cookie again so difference would be the cookie is coming on the client side and then you're again sending the cookie to the server now if you want to imagine this imagine you're going to a shop to buy something or let's say if you are going to a restaurant or if you're going to a restaurant where which provides you Self Service where you have to buy the product first you have to pay the amount and then you will get the get the uh dish or whatever you want to eat so let's say you you want to buy something which uh which cost you around let's say 50 bucks and you are giving 100 bucks so you don't have a change with you so the the product cost you around 50 bucks but you have you're paying 100 bucks and the shopkeeper says or the restaurant person says hey you don't have a change now of course you don't even you don't have a change he don't have a change what you will do he will say okay wait for some time once I got the change I will give it to you you go back to your seat and then after half an hour you go back to the Casher and say Hey I want 50 50 bucks back now will he remember you of course not right after half he may forgot who you are so in the earlier scenario what you should have done is the moment he says hey wait for 30 minutes he will give you a token on that token it will it will write he will write 50 which means when you go back to him after some time you can show that token which says 50 that's a cookie when you go for the first time he will give you a token so that when you go back to him after some time you can use a token I hope you understood something about that example here I will try to implement that with the Java code so I would say cookie that this is how you create a cookie you say cookie uh cookie basically belongs to javax package Java x. serp and I would say cookie equal to how do we create a cookie it's a class basically so we can simply say new cookie okay we got a cookie which ask you for two parameters the first parameter is the name of it I will say K and the value of it which is K as well so I'm have a name which is K and the value is K we got a cookie as we got our cookie we cannot simply pass K here it should be string how do we convert this skin to string we can simply append a double Cotes right so integer append double Cotes is string now once we got this cookie we just have to add this cookie to the response object so that it will go to the client I would say response do add cookie and I will pass cookie that's done we are sending this cookie to the client now this means this add server will send the response to the client and client again will send a request to sq saet now when a client send a request to sq seret again it will send the cookie as well oh that means we have to first accept the cookies and the way you do that is by saying oh how do we do that so when a serlet send a response to the client it is response object right and next time when a client send a request to the SQ seret that means the the cookie will be coming in the request object right so I will say req dot do we have a get cookie method so I will try to get guess that oh we don't have get cookie we have get cookies that's weird why get cookies why not get cookie it is possible that on the client side you don't have one cookie you have multiple cookies quite possible right we don't want okay so it will give you all the cookies because client even don't know which cookie you need so client says hey I will give you all the cookies you figure it out what which cookie you want okay so since client is sending all the cookies I have to use an array of cookies so I would say cookies AR equal to let me import the package as I mentioned we are not getting one cookie we are getting cookies so that's why we have an array and from this array I want to fetch the cookie which I want how do we know which which I want for that I will use an anounced for loop I would say for in fact you know I will just declare this variable up here so that I can use it later I would say for enhance enhance for Loop Works something like this I would say cookie c equal colon cookies it will give you it will run a loop through to all the cookies and then I would say what how do we fetch a cookie now so I would say first of all I want to know which cookie I want so I would say if the cookie name so I would say cookie C do get name is it the uh yeah so we have get name if the name equals K that's what we want to compare right if name equals K this is our cookie I would say k equal to C do get value so we don't want all the values we want only value of that cookie which has a value which is the name of with the name with K okay now it says get value will give you object I don't get value give you what string we have to convert that into integer so I would say integer do parent okay so this is how we can fetch cookies and let's verify it is working I'm not sure let's run this code let's verify I would say restart so here it is asking for two values I would say 8 and six and if I say submit you can see we got 196 which is the square of one of square of 14 so it works so yes you have multiple options in the last video we have seen URL rewriting in this video we have seen how do we use session I know how do we use cookies to send this data uh let me know if you have not understood the example here of cookie I will try to create a theory video where I will explain how that cookie flows from a client to the server so that's it from this video we have talked about session we have talked about cookies I would also recommend you to practice this code try some try different examples not just addition of two numbers you can also try login username password uh you can also try to use I maybe you can you can do some other operations as well so try it out in this video we're going to talk about salet config and salet context now when you talk about salet config and servid context we use these two things to get the initial values for a saet or for the application let's say example if you want to specify a username password if you want to do any initial setup let's say if you want to specify the file path where you will do it so normally what we do is we have this XML file which is web.xml in which you can mention some initial parameters and then you can use those parameters in your code example let's say I want to specify my name and which phone I'm using so the way you can do that is by specifying a tag called as context param and in this context param you can specify two things now this will be a a name and a value pair so you can see we have Pam name I would say name is let's say my name I would say name and then the name for the the value for that name is my name is naen so I would say naven here and then I will use one more context param which is for which phone I use so I will say copy and paste here and I would say uh which phone I have so I would say I have a Samsung phone so I specified two things here one is the name and second is the phone which I'm using so these are my initial values but how will I use it so first of all let's let's see how will I use it so if I if I go back to my solid file again this is a simple solid file here and in this in this do get what I will do is I want to get that name right I want to get the phone or name so first of all I will create object of print writer so that I can print something I would say print writer outon Dot get writer now once I have a printed object with me let me import the package for that and then I will try to print something would say out. print and I will print high that's it I will say hi and I will give a new line so I would say BR I I just want to print a new line and then once I have done this I will go for the next thing which is I want to print the value now the way you can fetch the value of my name or the value of a phone which is Samsung in this case we need to use a special object so we have to create an object of subet context so basically subet context and subet config the both are interfaces and the way you can see if I go hold on that you can see it says I which is the interface in this case so when I say subet config or subate context in this case if I subet context I would say con equal to I would say CTX let me import the package for that now if I want to instantiate it or if I want to get the object how would I do that now luckily your Tomcat says hey don't worry I will give you the object of Serv context so the moment you run the application it will give you the Ser context object for free you don't have to create it and the way you get the access to it is by saying get seret context there are multiple ways of getting this object uh one is by using get Ser context second by using request do get servit context if you want to use it because when you say get servit context you are getting this method from HP solet because hpet extends generic solet so you're getting this object from there or this method from there ultimately we are getting object of ser context Now using this object I can say CTX do get init parameter now get init parameter is a method which will give you the value of an attribute which which you specify here example if I specify name here it will give you the value of the name which is navini in this case but how would I know it so I would say print Str Str equal to so I will say string s Str equal to CTX doget init parameter and then I will print the St Str there I would say print Len I'll print SDR so it should print high naven so because I'm Printing High and then I'm printing my name as well let's try let's try this so I will say run as run on server and you can see that we got the output which is hi BR no okay the the BR tag is not working yet because I'm not using HTML tags and I'm using internal browser maybe that is the case here it's not working or maybe I will use a space maybe I'm using internal browser that's the issue but ultimately we can see we should be getting output which is hi naven right okay now that's your subate context object that's how you can fetch the value in fact you can do the same thing for uh for phone if I say if I remove this if I say phone of course you can fetch both phone is p is capital so we have to say p capital and it will print it will print the phone now so if if I refresh this you can see we got high Samsung that's how you can fetch the initial values okay that's that's best thing normally we can use file path the way the location of your database maybe your username password for database you can set all those things here but we also have Ser Serv config right why it is why it is useful now subet config says okay first of all seret config in seret config as well we can spef we can specify the initial values the way you are doing with server context the same thing can be done with with subet subet config as well the only difference is in your application you might have five to six subet maybe 10 subet or maybe multiple servlet the seret context is an object which will be shared by all the servlets which means if you specify this values here this will be shared by all the servlets what if you want to have a different configuration for different servlets example when I say name which is naven uh for this particular Serv which is my sub maybe I have multiple servlets here for this specific solet I want to specify the name as let's say sukes I don't want nain there so if I fetch name it should print sukes and the way you do that in the subet tag itself because subet config works with particular subet so if you have five suets in total you will get five subet config objects but you'll be having only one subet context object uh so I would say init parameter here init pram and in this init Pam you have again same stuff we have to say Pam name I would say Navi uh I would say name here I would say value which is in this case I would say Suk so the init param here is Su so when I run this code now oh first of all I have to CH make some changes I have changed my webml file which is Su here and in this case what I will do is instead of fetching value from Serv context I will do that from Ser config this time so I would say Sol config and then on this side also we'll say Sol config let's change it to CG let's import the package for Sol config and here as well I say CG do get init pram now you can see the code remains same the things which I have changed is from servit Context I went to servit config and here also I'm saving Serv config now if I run this code if I restart the server and you can see it says hi Suk even if you have the context value as naven but Serv config value is sish right so it will give you the priority to that particular solet now if you create an ex let's say if I create one more Serv as second servet or maybe addition servet in that subet if I try to do the same code but not with Serv context but let's say sub it context so if I sub context here and if I say name because we don't have we don't we have not specified the config there so it will print nine so if you have multiple suets which will share the same value of course go for salet context and if you have if you have a different value for different suets good to go for subet config so I hope you got some idea between Serv config and Serv context and in this video we'll see how do we configure svets with the help of annotations to understand that let's go with of flow so what we have done till now is we got this page which is index.html where we are asking for two values the moment you click on submit it will send a add request now when you send this ad request it should call AD serlet but question arise how would your Tom Cat Knows for which request you have to send what request and which Ser to call and that's where your deployment descriptor comes into picture so this is your web.xml right so this is where you mention Hey whenever you get a request for add you need to call add sovet whenever you send a request for SQ you have to call SQ sulet so this is where you do all the configuration right and it's okay you know people do love XML but not all not everyone loves XML you know some people find XML blit clumsy to work with they want to work with annotations because annotation will make your work much easier so what I will do is whatever you write in web. XML I will just simply comment it because I don't want to use XML anymore in fact you can delete this code but just to show you this code for reference I'm just keeping it here but that is commented so this code will not be coming to coming into action when you run this code so now if I comment this part and if I run this code once again let me do that I would run as run server and you can see in the Firefox I'm asking for two values I would say four five moment I click on submit you can see we got an error error which is 44 now why 404 because when you send add request you don't know or your tomat don't know which to call right because we have commented because we have commented this portion here the another way of doing this configuration is with the help of annotation so what you can do is on top of your solet you can write web serlet so you can say web server annotation which is this one and here you have to mention for which URL you you want to call this solet so I can say whenever anyone sent a request for add just call this svet the same thing can be done for your SQ sub now so we'll go back to sq subet and paste but this time to call SQ sub we have to use SQ and the same SQ we are using here you see that so we have the the same configuration what we have done in XML so in XML we have to do so many tags but using annotations it will reduce a lot of your work right you don't even need this XML anymore so you can delete the entire part so you can just simply remove this so you can see we have blank XML file and if I go back here let's restart the app restart the server something went wrong you can see that we are getting error okay so I guess we are missing a slash so we have to put a slash request you have to mention slash before the request URL because that's what we have done in XML file remember so let's do that and restart the server and you should be getting your output so you can see that no problem in the in the server now and let's go back here let's refresh and click on submit you can see we got the answer so there are two ways of configuring one is XML second is annotations and annotation will make your work easy in fact if you have learned hibernate or any spring framework or any other framework we have two choice we can go for XML or annotations and recently we are using annotation for all the projects so instead of using XML you can go for this option as well this is how you use annotations so that's it from this video in the next video we'll see why why do we need JSP and in this video we'll see why do we need JSP till this point what we have done is we got this form so when you click on this submit button it will call add and we have a soft who is responsible to handle your request and it is working perfectly right so you can see we are asking for two values we are adding those values and then we are printing on the page so just to see the output what I will do is I right click here I would say run as on server and on the Firefox that's what that's the browser I'm using so if I say four and if I enter the value let's says six so it will add those two values that's it so if I click on submit you can see we got 10 so we are simply adding two values now let me just add some twist The Twist is see ultimately on the client page we I mean on the client side we are getting a page right so if I right click and if I say view Source you can see a view Source itself is this data there's no HTML tag and nothing so we don't have HML tags right because we are not building anything we are not creating any HTML tag here and that's fine even if you want to make a web page you just have to make sure that your response is of type HTML you don't have to write any tags but what if you want to customize something what if you want to change the background color maybe I want the background color to be blue here or Canan or maybe red how do I change a background color even if I want to do that where should I do it now if you want to do any if you want to send any tags because see ultimately this client page the client side will receive HTML tags right so if you want to send HTML tags you have to do it from here so that means with your text you have to also send the HTML tag so you have to say HTML open and you have to say head open and then you have to say in fact we we can skip that part and we can simply say body BG color equal to in single code I'll mention the color as and that's my favorite color so once you are opening everything and inside this body oh we have we forgot to close the body tag and that's very difficult you know to track the tags and if you make one mistake you will not get errors you know in HTML you don't get errors you don't get output so that's one thing which is scary and how do we close all the tags again we have to say print Len In fact print will do and here I would say body close first you have to that's that's important right you have to remember the sequence as well so you have to close the body tag and then you can close the HTML tag now you will say that's okay right we can do that so we can simply pass the HTML tags in the form format of out. printer and as a user a user will receive all these tags in a proper format let me show you the output ofart the server I'm using a shortcut to restart the server you can try it by right click and restart let's go back to Firefox and now if I say refresh and submit you can see we got a page with a different color and the output is output 10 so if I say view Source you can see we got the HTML tags as well and everything is working fine the thing is to create a simple page we have to write all these tags right and it is possible for us normally we write each tag in different out. printer l so that we can debug later here I'm just doing some Shortcut it is working what if you want to create a beautiful page example if I go to any website let me just go go to Wikipedia and inside Wikipedia if I go for any text let's say if I go for any title doesn't matter uh so let's say if I search for this earthquake okay we got this page right and if I right click and if I say views Source you can see that we have so many text here and we have to write everything thing inside out. printen so just to create a beautiful page you have to write all those things in out. printen do you think is it flexible of course not right and that's where the problem starts because designing is done by designers and the backend work is done by developers and so developers sometime don't like designing and designing I mean designer people don't they don't like development how they work together because the Java developers they will write Java code and they will ask designers hey designers just put your tags inside outd print Ellen and they will not like it right so all these designers they went on strike they said hey son microsystem we don't want to use Java technology because in Java we are supposed to write HTML tag inside Java code that's not right so Java says okay don't worry we will give you a new feature which is called as JSP in JSP we'll do reverse you know we we will write the Java code inside HTML oh I know you don't trust me let's do it so what I will do is I will do the same thing I want to add two numbers but this time I will use Js P now what is JSP JSP stands for Java server Pages now instead of using seret we can also use JSP to do the same thing a seret here is simply accepting these two values adding it and sending the response the same thing can be done with the help of jsb let me show you how so I click on your project and say Hey I want a JSP page and I will name this JSP as add. JSP we can have any name doesn't matter and this JSP you can see that we have HTML tags here and then inside this HTML we will write Java code oh first of all I want to change the body color right that's important so I will say BG color equal to I would say Canan that's the color I want and here I want to add two numbers right so of course we have to addite the same code so I will just simply copy this code fetch the data and add it okay there's one thing missing now the request object right in seret we are creating the creating the reference here in JSP we don't have to do that JSP will give you this object by default so JSP will give you this reference variable by default now these things are also called as implicit objects in JSP again we'll see that later so once we got this Java code now I want to print the data on the client page right so I would simply say out. print Len and your job is done you don't even have to create object of out or print writer because that's by default in JSP I would say output uh colon I would say plus and that's it so what we do in salet we write HTML inside Java in JSP we do we write Java code inside HTML and this is much simpler right because you are focusing on HTML here and here you're focusing on Java you're not mixing it up they are separated with oh we have we are not separating them how do we separate them because if I run this code as it is it will display this content on the page I don't want to display this content on the page I just want to run this code in that case you will use a delimiter so you you will use angular backet percentage and you have to close this tag as well so here you have to say percentage angular bracket so whatever code you write between these two things it is it is it will be tweeted as Java code so that's simple right so we will not be using a subet now let me just close this I will only use JSP so you have you don't have to send the request for add we have to send a request for JSP name so whatever JSP name is in this case the JSP name is add. JSP just mention that so simply say add. JSP it will call this add. JSP so no need for any annotation nothing this should work let's verify so I will right click here I would say run as onun server I can see that we got the output here and I mean we got the page I would say8 and N you can see we got 17 and oh is it really working okay what I will do is I will change the color I doubt there something I think there's something wrong going on let meart the server I guess server have not restarted that's the problem so let me just go back here refresh and submit oh it's working so I was waiting for this thing which is add. JSP I know we hate red color it is very it's giving some trouble to the eyes I will go back to Canon I was just verifying everything is working okay you can see we can see we got Canon is a pleasant color right and you can see we got add. JSP on in the address bar so yes this is we are calling JSP here so which is better JSP right because in Sate we have to do so many things uh we have to get a class we have to extend a class we have to get a method which takes two parameters and then create the object of print writer out out and then you can do everything in fact in that also if you want to design something we have to write HTML tag inside J inside Java code but in in J in JS it's very simple right you write HTML code you write Java code separately and everything everything works fine so yes JSP is a good option but then why to learn saet if JSP is so beautiful why to learn svet so we have some uh reason for that I will uh so in the next video we'll see that why we still use S it when you have JSP can we work with JSP and Sate together I hope you enjoyed this video where we have seen why JSP came into picture and how we can use JSP that we'll see later on saet we have to create a seret we have to define a method which is main which is service method and then we have to create object of print writer then you can print hello world in fact when you want to add two numbers we have to create all those Stuff Plus we have to create uh we have to fet the data from the request object right and for that we have to define those two objects request and response in case of JSP you get all those objects by default right that means JSP is much simpler to serve it and from today we'll be want we we'll be working only on JSP we will not go for suets right but that's not the case if that was the case then why would why would anyone will learn suet why directly we can start with JSP so what if I say what if I say JSP I mean you cannot run JSP right you can only you can only write code in JSP you cannot run JSP it's because if you remember when you work with Tomcat we have suets right so sulet runs on Tomcat because Tomcat is also called as a sulet container right that means in Tomcat you can only run suets you cannot run JSP but then we have seen the output right we have created a JSP page and we can run JSP page on Tomcat how that is happening then s cannot run JSP so what happens is as a programmer even if you write a JSP code that JSP code gets converted into servlets right so it doesn't matter if you even if you write a JSP page that is getting converted into serlet the only advantage of writing GSP is it is is it it is easier for developers to write JSP files instead of working on Ser right so question arise how can you convert so it's actually not Ser to JSP we are moving from server to JSP topic but when you run it it goes in this direction so you write JC page and that's get converted into a seret but then how exactly is possible so let me create so on the left hand side I will create a serlet class and then on the right hand side we'll compare that with JSP how exactly or or we can write a JSP page here okay let me just just let me just go for JSP here and S let here so how exactly it's get converted so we have a JSP right so in JSP we have all the HTML tags right so JSP is all about HTML and then somewhere in between we have Java codee right so let's say we have HTML K = to I + J and then we can say out do print and we can print the value of K it's that simple right so we can we can assign those two values and we can add those two values and then we can print the output the same thing if you want to do in subet how how do you normally do it right so let's let's ignore this topic I mean Let's ignore this logic if I ask you to create a subet I mean it doesn't matter what you want to do in the subet if I ask you to create a seret what is something which which to your mind step one we have to create a class right you should provide the name of the class you have to extend that class with HTTP serlet then you have to create a method service or maybe do get or do Post you have to pass two parameters request and response you have to create object of print writer and then you have to close everything that means it doesn't matter what is the logic of the seret you have to do all these things and I forgot one thing import the package right so you have to do all those stuff here it doesn't matter what is the logic you have to do all those things right so let's say if I if I create a subet here we say public class and I will name this subet as demo subet which extends of course which extends HTTP subet right we have that thing there if I open this you have to define a method which is which is a service method right so I will say a method service this service will take two parameters request and response so we got these two objects if I open this in this we have to create object of print writer which is out and then we have to do something we have to complete something so this is object this is the your print object so it doesn't matter what subject you want to create this is something which is which is there right your JSP says hey don't worry if you want to write some logic here write the logic right in JSP we are not doing any of those stuff so what happens is when you when you run GSP page it will convert itself into a salet so what it will do this is fixed right but hold on there's something which we have to Define here the class name so what happens is your JSP name let's say if this is demo. JSP your class name will become it is demo I mean demo JSP do JSP so that's your that's your class name right so whatever file you have so it is demo I mean not JSP it is demo JSP okay so demo. JSP will become demo JSP there so whatever file name you mention here that that will become your sub name then that will be extended by HTTP subve you don't have to do it right it is that is which is standard this is something which your which your tomat will give you it will say okay we will give give you a service method do get a do post this two object will will be provided by your tom cat so whatever you write inside this code whatever you whatever you write inside this tag will I mean this will go into the service method so it will it will directly copy paste so this will be copied here and that's why you get this out out object you don't have to create an out object in JS page because your seret will give you that object you don't have to create a request object because your seret will give you that request object you don't have to create a response object because your Serv will give you that response object not just these three objects even your session object your application object everything will be given by JSP I mean your to server basically and those objects are called as implicit objects in JSP again we'll be seeing that in the session in the future sessions okay so yeah that's how it works so it gets converted from JSP to subet is that awesome right now what else uh I just have one more query here what what what do you call this what do you call this serlet I mean this tag not s this tag so this tag here in JSP is called as scriptlet so it is scriptlet so this tag which is angular bracket percentage sign will be called as I mean this sign will be called as scriplet okay now what else we what are the what other thing we have see whatever you write in this tag will go to your service method right so so this code will be coming here so whatever write in this script light will be coming in service method what if you want something here I want to declare a variable I will say int uh let's say some other variable let's say value I want to create a variable something like this see whatever you write inside scriptlet will go to this service method right I want something here in the Declaration section I want to decare some variables there how can you do that if you write that inside this tag it will go inside service now what's the choice so what we will do is let's let me just expand this imagine we have we do have all those things up we have HTML tag we have body tag somewhere there so we have one more type of one more tag here see if I use this tag again it will go to service so what will say see I want to put some data which is not in service okay I want to put some data or some declaration or some statement which is not that inside service it is outside the service so we can use a not opor there so we can say not right so it is a service but not service right so it is declaration so this section will be called as declaration so we can so whatever you write inside this so if I say int value equal to 1 so this code will go inside declar section right so anybody you want to Define any variable which is which should be here which is instance variables if you want to create any method let's say with this service I want one more method as show so you can you can create a show method inside this tag in the Declaration tag so if you if you create a method here that method will be going in the class so whatever you want inside the class and outside the service method that will will go into declaration section Perfect Right do we have one something more we have one more in fact we have two mes the first one is uh let's say if you want to import a package in your file somewhere somewhere here you want to import a package maybe date API maybe date class maybe uh database driver class if you want to import any package so what you do in Ser we simply say import and then we write it there right so normally we import the packages out inside the class what about JSP where do I mention I want to import the package so for JSP we have one more tag in fact in fact I will remove this so to JSP so we have one more which is percentage at the rate right so if you write in at the rate so you have to say add theate page then we have to say import and then we'll say equal to double in double codes you have to mention java. util DOD and percentage close so whatever you write here we will go into your import package so since you are mentioning import attribute so that's that's something you have to remember this import is an attribute we have to assign the value to it we say equal to and then we have to mention the package name what if you have multiple package we just have to give comma so it is java. u.d comma java.sql do prepare statement comma java.sql do statement we can have multiple commas and we can assign everything there right so that's three tags so this is scriplet this is declaration now this tag is called as declarative oh this de this is directive my mistake so it is directive so this this is directive this is declaration this is scriptlet now do we have one more yes we have so let's say if you want to print something so in should of using print out. print we can also use angular bracket equal to so we have to angular braet percentage equal to K so whatever you write inside this tag which is percentage equal to will go inside out do print K so whatever you write inside this this tag which is percentage equal to will go inside your out. print right is that awesome right so this tag here is called as expression so we have four types of tags in JSP we have direct so directive goes as so if you want to import the package if you want to do any any other extra stuff that is directive if you want to write something inside the class outside the service method example method definition variable declaration you can do that in declaration section so we have to use percentage not if you want to write something in the service method we have to use angular bracket percentage percentage angular bracket that is your scriptlet tag if you want to write something in out. printer and directly we can use the expression tag right so this is JSP for you so whatever whenever you create a JSP page it's get converted into a serlet right so even if you're writing a JSP it will be into saet so again which is better depends if you want to show data on the page okay if you if you if your intention is to create a page GSP is better right but if your intention is just to process it for that we have S it again which one to use when that we'll see in the next video in the last video we have seen how powerful JSP is and how easy it is to work with JSP and then we have also seen that even if you have JSP which is so wonderful we still use saet and then we have seen the theory video where we have discussed that whenever you work with JSP it gets converted into suret and that's why suets are better when you want to write logic so in this video I want to show you the practical way of seeing how can you convert a JSP into Serv or even if your Tom Cat does that for you what code generates to understand that I'll be using a different IDE this time because last time we were using eclipse this time just to show that I'll be using net beans because net beans have this cool tool to do that now if you don't have net beans in your machine that's fine you can simply search for net beans and you can go for the latest version which is 8.2 and download it and you can install it it's very easy you can just click on download and you can simply install it there's one important thing when you say download you have to make sure you are going for Java e version because Java essay is basically for code Java uh if you want to go for the Enterprise version which is which supports uh Java e then HTML and then the servers of course you have to go for Java e version now once you download it you have to install it now once you have done with the installation and if you open that means this is how it will look like just close this start page we don't want the start page what I want is I want to create a simple project so what I will do is I will right click here I will say new project now while selecting a project you have to make sure that you're going for Java web and sa application click on next we'll give a simple name here I would say to do JSP okay you can give any name doesn't matter and then click on next this is important you have to select your server now while installing I have not checked Tomcat I'm only installing glassfish so it's there so I have a glassfish in my machine uh if you have Tomcat that's fine tomat will also work and then click on next I don't want to select any of this thing here because we are not working on Frameworks click on finish and you'll be getting your first web project in net beans and it will give you HTML page which is awesome now we don't need HTML we want a jsv page so let's do that let's create a simple jsv page very simple where you can add two numbers or whatever code you want to write so what I will do is I will click on these web pages I would say new hey I want a j JSP page and your net means will say okay I will give you JSP page just name it I would say this is home.jsp you can give any name doesn't matter right uh we don't have to say JSP it will be done by your net beans that's what it was saying in the info info here and then you will click on finish you got your page you got home.jsp and it doesn't uh it doesn't have any Java code now what I will do is I will just increase the font size here okay that's done I have increased the font size going to settings and now I'll be adding some Java code here now what what Java code we can simply say add two numbers and just just print it there so I'll be using this tags which we have done earlier so I'll be using the scri tags and here I will type some code I would say out. print Len and here I would say 7 + 5 so when when you get the output it would be hello world and 12 right that will that will be the output let's see and to run this code it's very simple just right click here and say run file it does take some time and I guess it was trying to open a browser for me so is it yeah it's there you can see that we got a page and it prints hello world and we got 12 as well so that means the Java code is working but how can you convert this into salet and of course it is done by Tomcat or glassfish you don't have to worry about it but if you want to see the salet file how it will look like so you just have to right click on your home.jsp and click on view seret now when you this page context object once more I don't know we I don't use it much but it gives you all those objects and it it is giving you all the prerequisite code now the code which we have returned so if you can see our JC file if you see this file this is the code which you are writing right of course you're not writing it but it is done by your net beans but that's fine it is our code because we can change it and we have written our Java code where it belongs to it belongs to out. write so in Serv it as well this is what you do right you write all the HTML tag inside out. print and or right it is doing the same thing and look at your code here this is your Java code so as I mentioned one of the video whatever you write inside that scriptlet tag or inside this bracket belongs to belongs to the service method or uh maybe do get or do Post depend upon what they have implementing so your service method here so whatever you mention in that tag belongs to service and that's how you can see what is going on here quite simple right so it's hence Pro what happens behind the scen so when we write to JSP code it gets converted into Sate and whatever you mention in those brackets belongs to a service method now there's a question here and the question is normally when you talk about Java classes you know this is also a Java class right because it has a class it is it has certain methods and then variables now whatever I write inside this JSP those tags belongs to the service method right but what if I want to write something outside maybe uh I want to declare a variable which will be used by multiple methods so of course I'll be I will be making a instance variable and how do we do that how do I create a v because of course you cannot change this file how do I change this if I want to create a variable here can we do it in JSP if I write if I create a variable if I say int I is equal to 9 now this variable will be created inside your service method I want to create this variable outside the service method that's the first thing we want to do the second thing I want to do is about the packages let's say if I want to import a package because here you can see we have import packages what if I want to import a jdbc package what if I want to import uh some other package maybe log file package how will I mention that in JSP because if you write import here the import statement will go inside your your service method which which we don't want I want to import the package which should be the first line of the code how I how do I do that so those things two things we have to do and that will be done in the next video in the last video we have seen how this JSP gets converted into salet and we can see the salet code right and then we were having this issue the issue is what if I want to write this code which we had return here in some other tags example maybe in some other section uh so in sub edits I want to declare a variable as a instance variable how do we do that and how do we import the package so let's achieve that so in this video we'll talk about those four beautiful tags available in JSP now let me just name those things for you first so I will write it here so the first one we'll be talking about is declaration the next one is directive then we'll we'll talk about scriptlet and we'll talk about expression so we'll talk about these four tags again we'll go in random order but then at the end we'll be covering these four tags here now by default the tag which we use here it is called as a scriptlet tag so whatever tag which we are using here it is basically a scriptlet tag now why a scriptlet tag because whatever you mention in this tag or in this section will be a part of service method so by default it doesn't matter how many tags which you write it's not that you you cannot you can only write one or you can have multiple tags here maybe before hello world also we can write those tag and if you want to perform some operation there so if I do this here if I say okay will so in this section I will say out. print and and I will type let's say hi so if I say hi now and if I go back to my page and if I refresh uh you can see we we got high and of course we got this text because I have written there but I got high right and if I see this GSP if I see the subet of this now if I say view salet we will be having that high here you can see that it's working so thing is you can have multiple scripted tag but but all this codee will go into your service method what if you want something outside service and that's where your declaration tag comes into picture example let's say I want to I want to create a new variable so if I do that here so instead of say hi I will remove high now and I will type in I want to create a variable called as a a coefficient and I would say coefficient is three so if I say CS3 and I want this variable to be created outside service that is instance variable the thing is if I if I just run this code and if I say view salet you can see if I say view salet the variable is still not here the variable is inside service if you can see the variable is inside service method because by default that's what that's what happened what I want is I want it to be outside and the way you can do that is by saying hey this tag is not a scriptlet tag it's a declaration and the way you mention that this is declaration is by putting a exclamation sign there so as you can see we have declaration and we have scriptlet so this is scriptlet this is declaration so we are saying hey whatever code we have here don't put those things inside service so when you say don't in programming we say not right that's that's a not symbol but basically this is a declaration or you can also say this is not scriptlet okay this is two tags we have we do we have something more example if you want to import a package how do we do that uh to to use that packet what we will do is let's use something which belongs to our packet example something like scanner so if I say scanner again the scanner doesn't make any sense in this code because we are not going to accept any value from the from the console but let's say if you want to use any class which belongs to our package in this case we are using scanner and if I say control space you can see it belongs to java. and by default java. utl is not the default uh package which you get you have to import it the moment I say enter you can see something has changed here we got we got a tag now this tag is not this tag is not simply uh angular bracket and percentage we have one more symbol here which is ADD rate now whenever you have a adate symbol or add rate symbol when with this angular bracket and percentage it is called as a directive tag so this is directive so basically if you want to import a package or if you want to use some external tags that's where you'll be using the dtive again we'll talk about tags later how can we import external tags and then uh how can we use different features of of dtive but time B when you say add that is directive tag so if you want to import a package this is what you do but if you want to import multiple packages if you want to use start you can do that you can put start here now there's one thing you have to observe when you say import it becomes an attribute so we have to use a equal to symbol so if I save view subet for this file now and if I go up oh it is not compiled properly let just do that here I'll go back to this code I will say refresh one so that or run file and if I go back oh it's not working there's some error okay scanner is giving an issue of course because we have not written the code properly I will remove scanner there because we don't need that we only need to import the package so once I have done that if I go back to my uh solid I hope yeah you can see that in Sol because java.util dostar so this is how we use those three tags we have declaration we have detective we is added it and we have scriptlet now we have one more which expression and this this is very special now what is special let's say I have this variable which is coefficient which is three what if I want to print the value of it maybe I want to print the value by saying my favorite number is so if I want to print this print this line and at the end of this line I want to print three which is the coefficient value how do I do that so of course I cannot simply type go if because that's a string it has to be part of java code so what we do is we simply use those scriptlet tag we use angular bracket percentage and then we use percentage that will make it a Java code but then we cannot simply type c we have to say out. print Len right that's what we do we say out. print Ln and then we can we can just put that in a bracket now if you run this of course with semicolon and if you run this code now it will print my favorite number is and then it will print the value of Co which is three but you your JSP says hey just to print just to fetch the value of variable you don't have to do all these things what you can do is you can take your variable and put a equal to symbol there the moment you put a equal to symbol which means you're trying to fetch the value of it so JSP is done and I mean in the back end JSP will convert this to out. print Len so whenever you have a equal to symbol with this percentage is called as expression tag so that's your expressions so basically we have this Four Tags here declaration detective scriplet and expression and I will again run this code once just to show you the just just to show you the solet of it so code is running you can see the output is my favorite number is three that's working but let me let me see the solet of it so if I if I say view Sol it we got the input package we got the coefficient which is working we got in the as instance variable but if I go down you can see it says out. print and we got CF so whatever you have you writing in that expression tag becomes a part of out. print and that's the beauty about about this in this video we're going to talk about JSP detectives now as we have seen in JSP we can use different tags like we have scriptlet then we have directives we have declaration now in directive itself we have three types the first one is at page we have at include and we have ADD tag TB in fact we have seen an example of page so when you want to import a package when you want to do when you want to specify the language we can use add page so if you want to specify something for the entire page we don't we don't do that in the scriptlet we do that in directive now example importing a package or maybe if you want to mention that this page handles the exception or not you can use add page there now if you want to use some other JC file in your file example if you already have a JSC page and if you want to include that page in your current page you can use at include and then we have tag lib now as name suggest it has something to do with tags and the library of tags now what happens is when you talk about HTML awesome language right but HTML has limited set of tags which you use what if you want to use something very specific something extra now we can use external tags as well example when you work with spring framework spring framework provides its own tags now if you want to use those tags we use add tag Library again we'll see a Syntax for that now let's focus on the first one which is ADD page again we'll be doing a example but let's focus on the theory first now if we talk about add page which is the page tag now page tag has a particular syntax so we have to use ad symbol and that's very important for directives we have to use ad symbol and then we use page now page mentioned that we want to use we want to specify something for the entire page and the way you can specify that is by using attribute and a value attribute and a value and we can have multiple attributes and and those values but what these attributes are this is a list of attributes you can see now this is not the entire list we have some more attributes but these are commonly used attributes which we use example we have the first one which is specifying the language uh the scripting language in your JSP and of course by default it will be Java uh you don't have to even mention that and why why we'll be using different languages in JSP so we have Java there the next one is extends now as we know whatever JSP which we use gets converted into suret right now what if you want to extend a particular class in your as in your servet so the way you can do that in JSP is by mentioning the extend attribute and then the class name what if you want to import some packages then we can use import statement so we have to say import and then we can specify the list in fact you can specify one package or you can give a comma for multiple packages then we can also mention if you want to use sessions in your JSP so if you don't want session you can specify false then we have a concept of buffer now sometime you have a buffer concept so if if you are sending a data to a client and if you have a buffer in place it will not send a data till your buffer is full the moment your buffer is full then it will send data what if you don't want your buffer to get full I mean what if you want to send data before you get before your buffer gets full we can use autof flush then we can specify the content type example when you send a response to a client it can be HTML page it can be a it can be a image it can be a PDF file so you can mention the content type in this content type attribute then you can mention the eror page now example when we talk when we will be talking about exception handling in future but let's say if you want to specify the page which will be handling the exceptions you can do that in other page you can mention the page name here and if you want to specify the current page error page itself again you can use e error page you can set it through again we'll be doing that in detail later so let's not waste more time here and if you want to specify some information about the page you can do that here we also have a concept of e which is expression language again we'll talk about that later but once we use e if you want to ignore El you can specify that here and then we have thread save concept so if you want to make your JS page thread safe you can just make it through so these are the attributes which you have for add page now if you want me to show the example for that it's very simple just go to your JSP so I already have a JSP page here created which is home.jsp and if you want to use ad page you can see that we already have it in fact when you create a new JSP page in Eclipse uh it will give you by default displ attribute you don't you don't have to mention that so when you say page you can specify the language again this is optional by default it is Java so don't have to mention that and you can see if I go here it says uh default values Java so you don't don't have to mention that then we have content type which specifies it is text or HTML and we have page encoding as well again which is not not needed now if if you want to specify some Moree attributes you can do that here so you can say hey I want to import a package you can do that so you can say import and you can say equal to in double codes you can mention the package which you want to import maybe I want to import java.sql dostar you can simply do that in fact not just entire package you can you can pick one interface if you want so let's say if I want to use statement now again it's not a good practice to use SQL queries in JC page but just for the example it will work so you can see I'm saying import java.sql do statement what if you want want to import more classes it's very simple just say comma and you can mention java. random so you can mention these things here and it will work what if you want to specify mode Imports and you know sometime it doesn't look good to import all the thing in one line so you can you can actually write more import statement so you can create another page tag and we can do that we can have multiple page tags and then you can specify import and whatever you package you want to import maybe I want to import Java do uh U at list maybe I want to use it so you can do that so you can have multiple import now import is the only attribute which you can use multiple times in your in your directive otherwise for for for content type for language you have to set only once now once we have done with the import uh let's we can we can explore some more example if I go back here uh we can specify autof flush we can specify eror page in fact we'll be doing that data in detail so let's not focus here so we got our we we know how to use page and we have to we know how to use input now if I go back to this uh we after page we have so this okay this we also have an example here you can see that now if I go for include so the next one is include now what is include normally when you create when you design a page we have this habit right we create components example every page will be having header every page will be having a footer so instead of writing code for header and footer for in every page what you can do is you can just write header code in one JSP and you can include that JSP in our current Js example in in this example we have home.jsp right and then we have header. JSP let's say in header. JSP if I write some code uh let's say if I use equal to symbol and if I say 7 + 6 this is a Java code basically I'm doing that here just to print a number 13 here and then this headup page I want to include that in our current page you can simply do that you can just say at you can say percentage at and you can say include and you can mention the page name which is header. JSP and that's done so it will include your page here oh syntax it's file so include is a tag and then you have to use a file attribute and it is done and now if I run this code let's let's try this so if I will say run as on server I already have a Tom C in place let's say next let's click on finish and it will open my page okay you can see we got the output and the output is 13 so this is not the code which which we have written in home we have WR that code in header so your header is getting included in the homepage so that's the beauty you can use include tag and then we have one more which is tag Li now tag lib is something if you want to use external tags again we'll not be discussing this example here we'll be talking this once we go with ex extra advanced stuff in JSP but let's say if you want to use external tags what you can do is uh we have to mention the URI because when you say external tags of course it will be having a library and that Library will have a URI you have to mention that URI here and then you can mention the prefix now why prefix let's say if I want to use a tag here which is let's say uh maybe naven tag and and we know that naen tag does not exist now if if your browser triy to interpret this n tag it will give you error so for that you have to mention so we have to mention the prefix you have to say FX colon Navin which means this Navin tag belongs to FX which is this FX here again this prefix can be anything it can be a b or pqr doesn't matter so whatever prefix you mention here you have to mention that here so whenever I use n it means this naen belongs to this Library which is this URI and in fact if if you want to use H1 tag which belongs to HTML but sometime let's say if you want to customize H1 tag you want to have your own H1 tag you can do that so you can say H1 belongs to FX so don't use H1 of HTML don't just use HTML just just use H1 of my own Library so this is what you can do with tag lips so these are the three directives we have in JSP in this video we're going to talk about implicit objects in JSP now if you have seen solet in sovet what we do is if you want to use any object like we have we have request object response object those objects will be created by Tomcat but then still we have to refer them right example in service method we mention request object and response object and then if you want to print something we need to create object of print writer and then if you want to use session we need to instance we need to say HTTP session then session and then say we get session uh in the in one of the video we have talked about Sol context and config so yes we are responsible to create at least the reference of it now some people do call it as creating object but normally we create reference so that it will be referred the thing is in JSP because JSP says Hey programmer I'm trying to make your work easy so it gives you a lot of features right example you can write a Java code inside HTML but JSP also gives you some objects to use now those are the seven beautiful objects we have request object response object page context now there is something new which we have not seen in sovet other than that we have seen request object we have seen response out session application now okay now this application is same as what you use in Ser context So when you say Ser context object that is referred as application in JSP and then we have a config object now technically this all the things should be in small data because those are objects right so we have response we have p page context uh we have out session application config so these are your seven objects now we don't need to go into details here because we know this things right we are doing this from from session one we are talking about request object we are talking about response object now the beautiful thing about this is you don't have to instantiate them uh so if I have a JSP here first of all let me show you a svet so let's say if you have a saet you need to create this request object and response object uh also the out one then config context all those stuff but in JSP what we can do is we can directly refer them example if I if I want to fetch a value so I can simply say request I would say request. get parameter and if you can observe there's nowhere I have I have mentioned about this request object so JSP gives you this object so you can you can easily use it now the same way we can use response object now there's something new here which we have not seen in Ser which is paste context so let's let's let's spend some time here now basically page context referred to the page example let's say if you are working on JSP and if you want to work with any of the scope or if you want to set the attribute normally we set the values we get the values in fact in session management also we have talked about it we can in one sub we can assign the value in second sub we can fetch the value that can be done with the help of request object that can be done with the help of session object or we can do that with the suret context as well but let's say if you don't want to go in detail DET on that if you just want to work with a page then JSP offers you a object of page context now page context is an object which belongs which is an object of page context Now using this page context you can set the value or you can get the value so you can say hey I want to set the attribute and you can set any attribute here so that so that you can use this attribute later example if I say hey my name is naven so I have assigned the attribute now so whenever I want to fetch this value NV I can fetch it with the help of name so you can do that with the help of get attribute Now by default it stays in a page so the scope of this page context is the same page now when I say scope what it means uh so when you have a session scope it it means you can use the value anywhere in this session so let's say if you open five pages in that particular session you can access that value in those five pages when you say request scope it means the current page and the next page so the page you requesting even that page can have the the data in the request scope but if you talk about page context it is available only in the current page but what if you want to specify some some scope and we can do that so we can specify the name then the value and then we can specify the scope as well so if you don't want this to be only in the page scope you can mention that it goes into session scope as well so you can use a constant so we can say page context dot so we have a constant called as session scope basically all these are int value so session scope itself has a in value we can also use request scope if you want so there's something new which we which we can use in JSP uh let me just recap if we talk about Scopes we have different Scopes available we have request session sub context or we we can refer sub context AS application scope but if you want to refer to the current scope the only the page scope you can use page context Now using page context object we can actually set the value in different Scopes the way we have done here so that you can refer that in some other scopes some other pages and then we know how to use session we have seen that in in serlet then we have application which is nothing but your seret context and then we have Ser config so we don't have to actually instantiate Serv config the way we have done in salet if you remember this example we said seret Serv config CG equal to get Serv config you don't have to do this in JSP we can simply use config and it will work so if I say config dot so you can access all the methods here remember this get and get get init parameter so you can directly use config and it will work so that's how you work with implicit objects so the best thing is s JSP gives you all these object for free you don't have to instantiate them again in subsequent tutorials we'll be using all these objects so let's let's not spend more time here we we will see that in further sessions and in this video we'll talk about how do we handle exceptions in JSP of course we have done that in Java right so when you work on code Java when you work on svets it's easy to handle exception because we all know how to use St CCH that's the general way right so if you have learned exceptional handling in Java you you know we have throws we have uh try catch now can we really use those things in JSP in fact if you have solv it what you can do is let's say if you have an exception here you have a very common line uh we know about these things right we have checked exceptions we have unchecked exceptions so if you say you have a line which is k equal to uh9 by 0 now for sure we know this line will generate an exception but that is an unchecked exception so your compiler is not giving you any error for that so it's your choice if you want to handle this and it's always good practice right now how do we handle this so what we do is we write try and catch but can we do that in JSP uh the answer is yes so if I go to this JSP and if let's say if you have some lines here but I want to ignore all the lines I just want to focus on one thing here which is int K is equal to 9 0 that's one line I want to write here of there will be some other lines as well now how do we handle the exception here so if I run this code of course you will get an exception or you will it will stop the execution it will generate an exception so I would say run as run on server and let's see what happens on JSP so you can see we got an exception here which is error 500 we all love this errors so you can see that it is showing the exact code where you are getting the error which is uh line number 13 and this line line of exception now the problem is it is stopping the execution so of course it is this thing thing is viewed so a client can see this code and the second problem is it is stopping the execution we don't want to do that we don't want to stop the execution and the way you can do that is by using a try catch here so I can say a try let me put this thing in a try block and then we'll say a catch I would say exception e and in the exception I would print something of course not out. not system do print I would say out. print Len and in this I want to print K or not K but of course I I want to print there was some error and I also want to print the message of error so I would say e. get message that's what we do in normal exception handling right in fact there are different ways you can maintain log file if you want but I don't want to do those things here so you can see I'm trying to handle the exception and once I have done this I will go back to my JSP or the output page just refresh and you can see this time it is not stopping the execution it is just it is showing you the error now still we have an issue here because if we talk about the web World in a web world if you get an ex setion you should inform your client hey what went wrong there should be a proper proper navigation you can see I'm printing the messages here itself this is not a good practice when it comes to web development of course for COD Java for Str application we can surely use this but when it comes to web world what we do is we create a separate page because in your in your application you might be having seven to eight pages of course you'll be having more than that so if you have so many pages it's it's better to have a dedicated page which will be showing the error which will have a different color of course you can print you can have the uh entire page in red color how do we do that so what we'll do is we'll create a separate page which is an error page so what I will do is I will go back here in in my web content I will create a new JSP and we'll name that JSP as error. JSP and in this page I want to show the exception whatever exception there was now how do we do that so first of all if you remember when we talked about the implicit objects there will be one object which is exception object but exception is pretty special that was that that's why we have not seen there example if I simply use a thing here which is the exception tag and I can simply use exception object that's it but you can see that it is not working it is showing exception cannot be resolved that's a problem here how do we solve it that's a different thing but I want to call this page uh maybe I I will just use this thing later I just want to print eror till this point we'll do this after some sometimes for time being I would say say comment this part okay I want to print error now the way you can call this page is by mentioning so somewhere you have to mention right that in case if there's any problem in this page you have to call er. JSP the way you do that is by using this page attribute page tag this is very important tag right we have seen that before I don't want to import import any statement here that was just for the previous examples so here I'll will mention whatever error goes in this page will be handled by this error page so this error page name is error. JSP so we are mentioning Whatever Whatever Gets wrong on in this page this will be calling error. JSP and it should work I guess let me just go back to my HTML or the output and I will refresh this time and you can see that it is calling it is printing something it's printing error and this error text is in error. JSP so for sure you're calling this page if you want to make sure you can also do this thing uh you can change your page color to Red because that's how you show the uh something is something is wrong right so if I refresh you can see we got an we got R red color so yes it is calling this error. JSP but I also want to know what is going wrong the way you can do that is by mentioning is by using this thing so you can you can you can print exception message but there's a problem it it it says it is unable to resolve the exception now the reason it is happening is this page should know that this is the error page and how do we do that so we in this thing you have to mention one more attribute which is is error page we have seen this before right when we talked about the page tag uh we have seen what is e error page so you can simply mention is it a error page of course it is so we have to say true and now if you have a error page so this page is error page right and that's where you can use the exception so point to remember exception is an object which only works in a page eror page and and now using this exception object you can print the message you can say get message whatever went wrong and now once we have done that go back to this page refresh and you can see it says error divide by zero that's that's your message right of course you can print whatever you want you can say get message or you can just whatever data you want to pick up from the exception object so this this is one way or this is the best way of handling the exception in JSP of course if you're implementing U MVC you'll be having a different way of implementing it but if if you are using simply JSP or sofl you can use this thing so that's how you handle the exception there's no compulsion that you should be using this you can also go with try catch that's your wish but as we have seen it's always to follow convention that's how you handle the exceptions in JSP in this video we'll see how to connect a JSP page with database with the help of jdbc of course so let's say you want to create a web application where you have some pages and those pages need to FES the data from the database so what you will do so we have multiple ways right what you can do is you can create a web project again you can create a normal web project like this one or you can create a mavin project so when you create a mavin project the advantage what you get is you know you can add the dependencies I mean you can add the jar files by itself right so that is one way the another way is you can create a web project then you have to add the J files by yourself let's do it by web project if you know mavin you can go with go with that as well okay so let's give a project name as demo JSP and we will select our Tomcat server again you can use any server which you have glassfish or any of this everything will work click on next nothing to specify here just click on finish you can see we got our project here and in this project first of all let me create a JS page so we'll SE a new JSP and we'll name this JSP as let's say uh profile. JSP so we just want to print the profile of a student I will click on finish and you can see we got our j page here just to show you something what I want here is to print the rule number 101 I want to print a name and then I want to print the marks let's say 88 I want to print this details and of course when you want to print this you have to use a br tag in between right and we'll say be tag let's run this let's see if it is working or not so that we can go ahead let's click on that run button click on next and finish and run the server I hope it will not show any weird stuff on my screen okay so you can see we got a data which is 101 naven and 88 so this is what the page we are getting right but I want to F this data from the database now do we have a database here so yes I'm using a post G database you can use use any datab database which you want you can work with Oracle you can work with masc you can work with uh any anyone in fact uh soon I mean later I'm trying to explore more on post so I'm using post here but that's fine you can use any dbms which you want so I I already have a table here which is student and in this student table I have some data if I say view all sources you can see we have this data here so I want to fetch the last row so which is R number one three marks 5050 and name is n so I want to fetch this one how can you do that how can you fetch the data from the database it's very simple you can simply you have to use the jdbc steps now I'm assuming that you know jdbc steps if you don't know you can watch my video completely dedicated for for jdbc okay where you have to use seven steps still I will try to go with the steps the first step of jdbc is let me open my suppl where I will where I will specify the steps so the first step we have to do is import the package or we'll say import package and the package we need to import here is java.sql dostar which which we which has all the classes and interfaces which we need so let's import that package so normally when you want to import something in JSP what we do is we use this tag which is atate and then we specify not atate but percentage atate right and then we say page then we say import that's the attribute and then you have to mention the package name which is java.sql dostar I want all the classes and interfaces once we have done with that the next step is you have to load and R driver so you have to load and register the driver now when you say you want to register the driver which driver I'm talking about so since see we have uh different dbms right and every dbms has its own driver so when you say MyQ it has its own driver post G has it own driver in fact Oracle has its own driver so if I go here for post gra the driver name which we use is this one which is org. postgresql do driver in case if you're using MySQL you just have to specify your driver as this com. my. d. driver okay now since we are using post G here I will use a post GIS driver now since I want to add a Java code inside my jdb or inside my JSP you have to use we have to use a scriptlet tag in which you have to specify the first one which is class which is class. for name in this you have to mention the driver name which is org. postgressql do driver okay again if you are using MySQL then you have to use com. MySQL do. driver which we have mentioned here you see that not here this one is for Mas this for post I just hope I have entered the right text yes the next step is oh hold on we are missing something when you say you want this driver this will be so your your machine will try to get this driver right but unfortunately this is a third party library and in your machine you don't have that can you see that in your web content you don't have that Library you have to make sure that you have your library in this in this folder so let me just do that let me just uh add that here so again from where to get it you just go to Google and search for MAV repository that's my you know this is the place I get all my repositories so just search for post Grace post Grace I mean post SQL post Grace click on search button then go to this thing and you can select any version I'm going for the first version I don't know it will if it work with work or not but let's try uh so you click on the download thing here click on download it will it will download the thing for you okay so let me just add that in my folder just you have just have to paste it here okay so I have pasted that here can you see that we got our postris driver here and it should work now let's try uh then so we have added that jar file The Next Step you have to do is you have to uh go for you have to create a connection so we'll say connection con equal to for connection we got the package connection con equal to driver manager that's the next step right so driver manager. get connection now in this get connection you have to specify three parameters the first parameter is the URL so let's create a string URL here we'll say string string URL and in this double codes you have to mention the URL that's your database URL again which URL to go for so if you're using post this is the URL so it is your so what you're saying here is you're connecting with jdbc so your JSP code is connecting with jdbc jdbc is connected with the post CR and post CR is connected to this database now database can be on the network so you have to specify the network address and the database name for post chis the dri name and the uh the port number is 5432 for MySQL the driver name and the the port number is 3306 and then this is this is one change you have to do in MySQL you have to mention MySQL okay so let me just copy this and we'll paste it there paste now once we got this the next thing you have to mention is the username so we'll say string username we'll say equal to for my for post gra for my machine I'm using username as postgress if you're using masq you will get the default username as root so I will paste oh not this one I have to say postgress Okay and then you have to mention the password now luckily I don't have a password in uh I do have a password yeah I do have a password so the password for me is zero okay again uh depend upon what password you set for your dbms I have seted the password which is which is zero so let me just type those things here we say URL comma username comma password you have to mention these three things now once you got the connection let's file the query right now to file a query we need a statement object so we say statement St equal to com do create statement that's the step you have to follow right create statement then once we got the statement the next thing which we need here is we have to uh file the query so for that we need a query here so we'll say string SQL equal to that's your query right SQL equal to now what want to fetch is the complete data we say select star from student where role number is equal to 103 we we are very spefic spefic now we want only one record now once we have done that let's F the query the data will be going into result set we say result set RS equal to it is St do execute query where you have to mention the query which is SQL now once you find the query here you will get all your data in result set what we have to do is we just have to say rs. next so that we will we will daverse between the records again to get I mean to get more details about this how all these things works you can you can watch my video on jdbc if you don't know jdbc my main agenda here is not to teach you jdbc my main agenda is how to connect JSP and jdbc okay it's not that difficult it's just that some people were having some issues so I'm making this video okay so let's print let's print all the values so what we'll do is we'll say RS dots in fact once we got the data in RS what we can do is we can go here and then we can say role number we can print RO number here we can use an expression tag here we can say percentage equal to rs. get string and that will be your first string okay then we can use a br tag here just to you know get the Break Tag then we can say Name colon uh we have to use a percentage atate equal to we'll say rs. get string again uh I know that RO number I mean the the RO number will be an integer but we can fetch that in a string format okay why it's not working uh okay I forgot I mentioned okay RS do get string what's that get string I can say two right uh we can take this in one line and then we can mention a br tag and then one more which is your marks we'll say marks colon let's copy paste this the entire stuff copy and paste and in here you can mention three right we can we got role number we got name and we got marks I think we are done let's run this so this this is the data which we are printing here and after this let's say if I want to have some break tags or that everything will be coming after two lines let's run this let's say if it is working or not it should work I guess and you can see we got the data from the database it's that simple right so you have to write all your jdbc steps inside JSC page so that's it from this video we have talked about how do you connect a database using JSP and in this video we'll talk about MVC design pattern using seret and JSP so in the last video we have seen that whenever you work with JSP it gets converted into seret right so that means even if you're working with JSP you are actually working on suets right so but if if you talk about the coding part if you want to write a code GSP looks much easier compared to Serv right so does that means from today we'll only work on JSP not exactly the problem is uh there's one issue with JSP the issue is whenever you create a JSP and when you when when you run it for the first time it will get converted into subet right and it will take some time to do that right so for the first time you're wasting some time so that means uh serlet Works faster than JSP because in s you D run the seret in JSP you have to the JSP into Ser it that's one problem second problem it is recommended that don't write your business logic inside JSP never never do that never write the code the actual Java code inside GSP so if you want to process something always go for svet so s is something which will process your information right and then JSP we should be using only for the view now when I say view as a client whatever you get on the page or on the browser that's the view right when you go to google.com what you get a page right that's a view when you go to facebook.com what you get is the page that is view right so JSP is best when you want to create a view but hold on we have HTML for that right why you use JSP when you can use HTML the thing is using HTML you can only create static pages and JSP generate it means let's say we have a client here and then we have a jsv page here now this can be a static page or it can be a dynamic page if you want to create create a static page you can simply go with HTML form and you will get it but the moment if you want a dynamic page you have to create a JSP right now this JSP page will be having it will be having some static content and it will be having some Dynamic content example let's say if you go to uh Facebook right so on Facebook you can see we have we have some text there on the on the menu bar right and then we have some text on right hand side left side so there are some text on that page which is static it doesn't matter who logs into Facebook you will get the same thing right so you can see Hello I can see Hello same thing uh you can see settings I can see the same thing settings but the moment I click on my news feeds if I click on the home the feeds I'll be getting is different from your feeds that means that content is dynamic so we what we can do is we can create a layout we can create a layout using JSP now that page will be only layout it will be only the design there will not be any data right so when you request for a page right you are expecting this JSP in return so what you're expecting is the view okay see as a client we are only concerned about views we don't know what is happening behind the scene right when you go to Amazon you don't know what is happening behind the scene you simply get a page right in the same way as a client so as a client what you expect from the server is the view but hold on we have to add data here right because this is just a design you want data as well from where you get the data to get the data first of all you have to send a request right so let's say this request is for get page okay you have any any request you can say get student get page or maybe get products so you're requesting you're requesting for this this data which is get page you're expecting this page now this request will go to something called as controller right now we have a controller let me just write this is a controller not c will say this is a controller so whenever you send a request so so in Embassy design pattern whenever a client sends the request it will go to the controller now this controller will say okay I got a request here what is my job is to call this view so this controller's responsibility to call this View and then this will this view will go to the go to the client but hold on there's something which is missing here the missing thing is controller is calling view but again you don't have a data right so what it will do is this controller will send the data as well so when a controller calls the view it will also send a data with it example let's say you want so controller want to print something here example let's say this view is actually a student profile it's a student profile okay so we have a design but we don't have our data there right so this controller will send the the student information to this view right but in which format so that format let's say it's an object this object will have student info or student info and we will call this as a model so whenever a client sends the request it goes to the controller controller will call view by sending the model object so what is model object it's the object which will have the data now that model object will go to your view now we view will replace all the blank text with the actual data from the model and then what you will get is the is the page right now as a for a client it will look like a static page because we don't know what what happened behind the scene but that's a dynamic page the page is created I mean the page is filled with the model data and that data is generated by your controller now in this Java technology or Ser technology this controller is normally created with the help of servlets right so your controller is your svet your view is your JSP so normally we create views with the help of JSP and then your model are actually normal pojos now when I say pojo I I know it's a very different term right so some people know about this some people don't know about this in fact some years back even I was not knowing about pojo that's fine pojo simply stands for plain old Java object so if you want to send a data we can send a data in the pojo format you can create an object of a student let's say if you can create a student class that student class can have role number marks name age email ID and you can send all this information in one object one student object is that simple right so yeah so when you want to implement MVC in server technology a client will send a request to the seret and that Ser will be called as a controller that seret or the controller will call the JSP but this JSP needs some data right and that will be done with the help of a model object and this model object will be a pojo which is plain old Java object and then we have a view which is your JSP so now question arise where to write the business logic if you want to do any processing it should be done on the controller never write the business Logic on the view view technology which is JSP JSP should be only for the view vieing things this controller is for your uh this is this is your processing thing and then this will be this model object will only hold the data again never do the never perform any operation inside model object right it should it is only for holding data but then uh you know there there's also there's something called as n tier r architecture now what is that uh instead of doing all the processing here because let's say let's say let's say we have a database here okay we have a database and you want to fetch some data from the database so if you want to generate this model you're fetching data from database how it will work so you have to connect your controller with the database right you have to write this you have to write those databas steps inside this controller which is your serate again that's not the good way of doing that never process anything I mean never actually write the uh what do you say the the uh operations or the per the U the functions or methods inside controller controller is only for accepting the request and then sending the response if you want to perform any operation here what you will be doing is you will be getting one more class here right so the controller will call that class now this class is basically your service classes again this is a normal class this is a normal class is just that since we are performing some operation we will do that in a service method right I will not do that so when you send a request to the controller controller will will ask hey service class it will be having a object service class will have some methods and that service class will give you the object right so service class inside this class you have to write the datab datas steps makes sense right so in future even even if you change your database you don't have to change your controller right you just have to change a service but then when you work with database actually what happens is we have one more layer here and this is called as a dow layer so you call the controller controller will take the help of service and service will take the help of Da again this thing is not compulsory in MVC okay MVC is simply this part we have a client we have a controller we have a model we have a view but then if you actually want to implement something we also use these two classes service class and DOW class now what is Dow it is Data access object so if you want to interact with database the best way is using da classes again I have a separate video on da just go just search for jdbc da telescope on YouTube you will get my video there okay so yeah that's how you that's how you work with MVC this is just a theory of it we'll talk about the pratical implementation in the next session okay yeah that's it uh anything else here let me know your doubts and there's one more question for you in fact I have this habit of asking questions in fact last week I started with a quiz online quiz uh so this is a question for you and the question is uh if you want to make a app okay if you want to make a Android app what are the options you have so go to Google and search for different options for making Android application okay example let me give you one example you can we can do that with the help of Android Studio you can do that with the help of some ionic framework let me know more options so that's it in this video we'll talk about jstl so the prerequisite to learn jstl is suets and JSP so if you have basic knowledge of solet and JSP this video will be helpful so what is gstl so gstl so let's write it here so jstl stands for JSP standard tag Library okay now why we use this is because you know when you work with saet so we write HTML code inside Java code and that's not that helpful right and then in JSP uh in JSP we write we write Java code so inside HTML code we write Java code but still as a designer we are not that comfortable to work with Java codes right so if you can convert your Java code into HTML like code it will be much easier for designers to to write the code it will be easier for them to maintain the code it is easier for them to change the code right so that's why we have this concept of gstl and if you see the output of gstl or the code of gstl it's more like HTML right so let's try to do that so to explain that how gstl works let me go with an example here and I will create one more I will create okay for that I will just simply uh okay I will create a variable which is string I will say name equal to I will put my name nain and then I will send this name to the jsv file so let me first call jsv file and how do you call it we say request request dispatcher so it is request dispatcher we'll say Rd equal to new not new but request do get request dispatcher and we have to mention the name of the JSP right which is display. JSP right so what we are doing here is we are calling a JSP file from a from a solid file and we can say rd. forward and we can pass those two objects so what we are doing is we are creating a string here and then we are calling this JSU page now in this JSU page we'll try to fetch that string that value which is name here okay first of all when you sending it you have to make sure that you add that value somewhere so I can use a request object or we can also use session object we'll say this is label again we can have any any label here and we can say I want to send name so I'm I'm using a label as label and then I'm sending this name so in this name I will be having naen right now if I run this code of course it will call your JSC file right but we are not we are doing we are doing nothing here now how do you fetch that value so before fetching the value I will simply print hello world okay and let's let's run this code if I run this code on a server again I have a server here and I'm just calling that uh it will take some time okay yeah so you can see we have we are calling a subet which is demo subet and this subet will call your JSP now since I'm running this for the first time it will take some time for your JSP to get converted into seret and then showing you the output so you got the output as hello world here but I don't want Hello World I want to print I want to print naine here now how do we do that so for that we have to use some Java codes right in fact we can write Java code here and to write Java code we use the scet tags and inside this we can write we can fet that value we can store that value in string we say string name equal to request. get attribute good attribute and you can mention what type of what type of value you're sending so I'm sending a label which you have to fetch again this will return you an object so let me just convert that into string so I will say two string right now once you got the string we can say out. print Len and we can say name so what we are doing here is we are fetching value from I mean we are we we have this value nven which is going to the JSP and then we are fetching the value from the request object and then we are printing it again since we are using request object here to set the attribute we will be using request object to get the attribute right let's run this let me just run this once again and you can see we got the output as naven now why we are getting naven is because in JSP we are fetching the value which is sent and then we are printing it there is one way but if we can see as a designer you are comfortable with lines of code but the moment you see a Java code you might not feel confident about it then Java says okay chill we can have one more way here we can use uh we can use something called as expression language so expression language was introduced in JSP later and how it is used so we can use a dollar sign here with a curly brackets and inside your curly brackets you can simply mention the label okay whatever label you sending there so if you are sending label here we have to mention that here okay so if you say dollar and cly backets label it will fetch that value okay let me show you that let me just say once again and you can see we got two naen the first na we got because of this Java code the second naen you got because of this line here okay now let me just remove this Java code here I don't want Java code so using this one line of code we can get our output which is nine now this is expression language okay but expression language was created just to just to I mean just to get used inside jstl so if you are using jstl then you can use this and how it was how it was introduced uh so what we can do is I mean we can we can have one more way of gstl way here so in order to use gstl we need to use some special tags example in gstl we have a Out Tag instead of using out object we can use a Out Tag but the problem is Out Tag is not available inside HTML right it's a custom tag and whenever you want to use custom tag we have to use a tag library right so we have to add a library for gstl now which Library we'll be using here so we'll use percentage add it we say tag Li and we need to use a gstl tag okay now question arise which how to use gstl Tag here that will see in the next video the point to remember here is we are using uh we can use e instead of using your Java tag to use to print something on the server now again uh it's not compulsory to have that in request object itself we can also have that in session object we can also have that in page object because you know right in JSP we have different objects so you can have this label in any of the scope it may be in request scope it may be in session scope it may be in page scope or page context it can be any anywhere so that's how we use e in JSP in the next video we'll see how to add the tag library in gstl to use it in the last video what we have done is we have created a subet where we are sending a name and then we are printing that name on the JSP page using this label tag here and this is e right now what we'll do now is let's try to use jstl so we'll do the we'll do the same thing we'll print something here but not using e but will be with the help of jstl again we can do that with e but you know El was introduced just to have an attribute in jstl so let's see how to do that in jstl so if you want to use this with jstl first of all let me just give a Break Tag here so if you want to use this with jstl we have to use a special tag called as out the problem is out is not a inbuilt tag inside HTML right so this is a c tag and whenever you want to use a custom tag we have to import or we have to use that library now to use that we'll say percentage at theate and the way you can add libraries inside your your Java JSP is using T tag Li then you can mention something called as prefix now prefix means whenever you want to use any any gstl tag we'll be using that prefix so let's say if you want to use out we can say out is a gstl tag by mentioning C at the start then you have to mention URI now what is URI U is a location where you have your jstl okay I mean the the definition of your GST libraries now what URL URI we need here so if I say control space you can see I'm getting lots of options here so we'll be using this one a code one okay so this is the path for gstl code Library so inside inside jstl we have lots of libraries we have jstl we have okay now we are not getting options okay so you can see we have code we have a format we have functions we have SQL we have XML out of which we'll be using we'll be using Code function and SQL in this series so we start with code okay so we have to mention C and then okay yeah so we have to mention URI and then that's it you can whenever you want to use out now we have to mention C colon and then you can mention a value you which you want to print so let's say I want to print only hello world nothing else if I want to print hello world we can say see out value hello world and save this now but you have to make sure that whenever you want to use jstl you use the library here you have to add this Library which is jstl Library so jstl 1.2 you can download this from Google just go to Google and search for jstl and there will be some website which has this Library you have to also make sure that library is there in your web web app Library or you can do that by adding one of the ways by adding that library inside lip folder of webif okay now once you do that go back to your browser and say enter you can see we got naven and we got hello world now why we getting nav because we are using this label why we are getting hello world is because we are writing hello world let's say I don't want to print hello world here I want to print NV itself so I will use dollar and the cly package and we can say label okay so you can see using expression language you can mention we can mention that here so even that will work but this was introduced in 1.2 JSP I mean JSP second version not the first version okay so in in the second version of GSP we can use this but not so in the earlier version we used to work with this okay so that's how you use out uh Out Tag Out Tag in gstl now what else we can do here this is just a basic let's go for some something else uh what what what else what other tags we have here so if I say C colon you can see we have list of tags control space you can see we have catch now catch will be used when you want to do exception handling right we can use for each so let's say if you have lots of uh we have uh we have a list which you want to print we can use for each we can also use import to import some some other website in your project uh example if I say import and if I say tesco.com but not this way you have to mention the whole path we say should be SL ww.is score.com so you can fetch the entire source code of t.com in your code okay let me remove this one okay let's see how to fetch the code if I say enter now uh that's the okay so you can see I'm fetching my entire website here okay I was not able to fetch everything but we got I got my website here in fact I can if I mention some specific thing if I say online. HTM okay online. HTM and if I run this code now okay you can see we got our so that's that's that's my website registration details if you want to register for the course right if I say details it works it doesn't work yeah so we have to provide that link as well but you can see we got our we got the page here we have not got the CSS for all Stu but we got the page so we can use import what other other things we have we can say C colon uh we can use let's see what else we have so you can set the so you can set the variables you can remove the variables let's say if you want to set the variables we can say set and we can use property name we can specify the scope till what point we'll be using this uh then we'll be using value what value you want to store and the variable name okay so you can use set we can also say remove for removing the elements but in this example what what I'm passionate about is to use an object let's say any in this I'm not creating object of string I want to create object of student so what I will do is I will create now first of all we need to put that string so that we'll not get any error let's create a class here and we'll name we'll say this class as student which belongs to com. teliscope in this student class let me have some variables here okay some properties I will have this properties as private in RO number okay U let let me just remove private just to show you something if I remove the private and if I say string name just two variables okay and if I go to the ser and if I create object of that student if I say student oh we also need a Constructor using which you can assign the values just to make our work more easy we'll say Source uh Constructor we'll fetch both the variables here we click on okay and in in this sub it now let's create object of student we say student s equal to new student by passing those two values we'll say one and name I'll be using is naven I love my name I don't know why but naven and and instead of sending the label I don't want this string name here I'll be sending a student I you can use any name here any label and here I will be sending s so I'm sending s with the label student now in this display. JSP if you want to print label what will happen is since you are printing the label if I go back to my browser if I say enter okay it is printing nothing because it is not label it is student because we have removed label right so now we are using student okay why this why this error anyway ignore that if I say enter now you can see we got something and that's your hash code okay I don't want to print the hash code I want to print the actual data so what you can do is you can say student dot uh you can specify the property I want to print name is it name uh what is that yes it is name so if you want to print name you can say st. name and if I run this code we got an error this error says property name not found hey hold on we have a name there right why it is not able see it also specifying that name property is not inside student why it is so it's because whenever you want to use JSP and when want to use any object you have to make sure that you're using beans okay and how to make a bean to make a bean you need gets and Setters so that's the only way you can fetch the properties you have to say Source you have to say get as a set for both the variables we'll say okay you can see we got all the G set here and if I save this if I go back to my browser if I say enter we got na there right it's awesome right so we can we can send the data from your from your serlet to the JSP in the object format you can see I'm sending an object here and then we are fetching that object with the help of expression language now again you can use also use that g that jstl c Out Tag the main intention here is not to send one value let's say I want to I want to send list of students and I I don't want to print only one thing I want to print the entire object and to print the entire object you have to make sure that you implement a two string method in your code so let's implement the two string method if you are familiar with code Java in code Java we whenever you try to print the object it calls the two string method right so I I would I would like to print the two string method of that because if I leun this code now it will print both the stuff RO number and name but let's say I don't want to print only one thing I want to print the entire stuff let's say I want to print uh I don't want I just don't want one object I want three objects or four objects what you can do is you can create a list of student here so I can say list of student and here we can say studs equal to now we can create new add list and you can do all those stuff but since I know whatever object we are working with how many Val we need we can simply use arrays do aslist and inside this we can create multiple object I can say Student First object one and name is naven the second object let's say we have new student uh 2 comma R three and then we have new student this is three comma in double codes we'll say Pro so what we are doing is we are creating us we are creating an add list with this all this values I don't want to send one object so instead of sending one student object we can say students I I'm sending lots of students and the object name is studs okay so from this subet I'm not sending one thing I'm sending a list now how do you interpret that list here because if I say students and if I go back to my browser if I say enter you can see we got all this data here but you know in one particular list I want to print that in iterative format one after the other for that we have to use a for Loop right we we normally use for Loop the normal for Loop in gstl it provides you a special Loop called as for each Loop so we can use for each Loop and inside this for each Loop you can mention certain parameters the first one is items now this items is the least name so we have to use dollar okay first of all we need we need that double codes in this you have to mention the Leist name which is students right just verify let's go to subet and it is students then you have to mention the variable name now what variable so one I mean Which object will hold one object so I will say s so when this Loop runs it will so it will it will fetch one one object it will fetch first object then it will fet second object then the third object and every time you just have to print s right and but that should be inside the curly brackets okay and after everything we'll print a break statement so that it will it will be on new line if I go and run this code now you can see everything we got on a new line right in fact if you want to print only one specific thing you can also do that we can say s do name it will only print name now not the entire object you can see we got the names here so that's how we use gstl so we can use for t tag which belongs to jstl and we can mention items we can mention the variable name right and then we can iterate using the name there so that's how you use gstl code tag in fact we have lots of tags just try to explore on those tags okay uh in the next video what we'll be doing is we'll be working with the S the functions or yeah functions type of fun functions tags okay and in this video we'll talk about jstl SQL tags so anybody want to use SQL tag we cannot use code we have to use SQL okay now what is the use of SQL tags here so let's say you have a jsv page and you want to connect with database now what you will do so to connect with database you need to write some tags using which you can connect with database normally what happens is you have to write Java code you have to follow those seven steps to to do that that but JS says hey don't worry we have some tags using which you can connect with it can connect with the database and you can fetch the data let's see how to do that so what we'll do is first of all let's create a database in the m in the in any database you use maybe myql Oracle I'm using MySQL here so if I go to mySQL I already have a database which is Mario inside that database I have a table go as gadgets okay and if I if I see what is what is there inside gadgets you can see we have G G name and price so we have 1 2 3 we have iPhone as name then laptop and then OnePlus 3 we have this price here I want to fetch these elements or this values on the page so of course we have to connect with database right now do you connect that first of all we have to use something called as a data source now data source will Define what is your database name I mean first of all which database you're working with second what is the database name then username and password and you can do that with the help of data source tag so we have to use data source tag in which you have to first mention the database I mean the variable which will be using to refer that we'll say DB next you have to mention the driver name because every dbms will have a different driver and for me the driver is com. MySQL do jdbc do driver okay and then you have to mention the URL I will say URL is jdbc colon MySQL again for for different dbms we have different URL I'm just assuming that you know how to connect database with Java and database these are the same steps then I will use my port number which is 3307 for my machine Mario the database name and then you have to mention the username which is user equal to root and then password which is the next one in fact when you say you don't have to type this you simply say control space it will give you thing the the amazing thing about my machine is I don't have my password so I don't don't have to specify that if you're doing this for the first time if you have a my password set it that now what next now once you goone to database we have to use the query so I have to say SQL colon query okay in this query you have to mention uh your SQL query which query you want to execute now once your query get executed it will store it the data somewhere and that data is your V so we'll say RS so we will store the data inside RS now if you have seen my data if if you have seen my video on jdbc we normally use object of result set the same thing is here which is RS result set here then we have to use data source which data source you are working with and we have to connect with this DB so we'll say uh we'll use expression language here and we'll say DB okay we with double codes now once you have done that let's mention okay what else we have to mention here then you have to mention the query so we finish this here here we'll say end tag and inside this you can mention the query now so here you can mention select star from gadgets okay now once you specify query so what what will happen is when you execute this code now it will try to connect with your database now once it is connected then you will you will get your data source by DB and then you will you will execute this query and the results will be will be stored inside RS now question is how to print all these values now to print the values we have we can use we can we can specify individual values right we can say C out and then we can specify value right and we can we can fet the value from result set and we can print but I want to print all the values how to do that that we'll see in the next video but you have to make sure that if you want to execute this thing you need a mycle driver okay so you have to add your MyQ driver here so again you can connect you can download that from Google or somewhere so you have to make sure that you are adding a masc Conor here so what I will do is before starting with the next video I will be adding the masc connector here just go to Google and search for mql connector for or M connector J so let me just write it here this mql connector slj download this charart file and save it here in the LI folder now once you do that you can use the myaso connector and yeah so in the next video we'll see how to how to print that in this video what we'll do is we'll we'll try to complete the last example the example which we were trying to complete so we have connected with the database using data source and then we have also applied the query here which is Select start from gadgets let's try to fetch the data now you have to make sure that when you fetch the data we we use we use this Library which is mass connector can you see that I have added that Library here let me expand this so I've added that Library here which is maso connector okay and it should be there inside web app library now once you have done that uh we need to also take one more tag library for for code because we'll be using a for each Loop here so we'll say tag lip prefix we'll say Okay so this should be SQL right because you're working with SQL and this would be C okay and then we spey the URI equal to in this we have to specify the code tag so this is code and this is SQL and let's use a enhanced for to print all these elements so we'll say contrl c i mean we'll say C which is a code tag we'll say C colon SQL or not SQL we'll use for each and here we'll specify from where you want to fet that for that you have to say items now this items will be having list of elements right but hold on from from where you'll be getting this list of element so you'll be getting this list of elements from RS because R is the result set right you have to fetch one more element you have to say rs. rows so this rows will give you the list of elements and then you can store it in you can store one value somewhere you can say you can store that value in let's say a row so one row will have one row right and now we can print that row we can say colon out uh we'll say value equal to and we can fetch we can print the value here we can say I want to print row of uh or maybe not row let's let's use something better we'll say Gadget which will look better right and we'll say here Gadget dot uh we can print what we can print GID then we can also after that we can print okay is it working something is missing here this tag okay and then we can also print the we can with us with the colon in between we can print the name of the gadget so we can print the name here and then with a colon in between we can print the price of it so we'll print the price okay so it will print everything now let's try let's let's see if it is working or not let's go to the browser okay that's weird and if I say enter can you see that we got the values here but hold on we are not getting the names because it is not name it is G names and if I say enter you got all the records here it's just that we have not added a break break before so we'll say break here and we'll say enter you can see we got all the records from our database right so that's how we can use SQL data source to fetch that value okay so this is how you can use SQL data source to fetch the value from database and you can do that with the help of gstl and see how powerful it is now if you look if you if any jav programmer or any HTML designer can understand this you don't we don't have any Java code there right so the J is that powerful so that's it from this video we have talked about SQL tags or SQL yeah SQL tags and jstl in this video we'll talk about jstl function tags now we have seen the code tags right so we have this C where we have we are using code tags but jstl also provides you some function as well example if you want to find this length of the string or maybe you want to check if the current if the if a string I mean if a word is there in this particular string uh if maybe you want to uh check if the particular string is is matching with the end of the string and all the stuff you know if you want to if you want to do some if you want to perform some operations we can use inbu functions of gstl so in order to use gstl function we have to use uh again the tag lip but this time we'll say Okay prefix we'll set FN and we'll go for URI as HTTP colon SL slash let's ask it for function so this is functions now what are what are the functions available so we'll go for one by one uh the first one we'll use is let's say maybe length which is easier so let's let's try to find the length of the string so I will take a string now uh okay how to assign a string so let's create a string we'll say set where we'll name the string name as s Str and we'll set a value and then the value would be naven Ries or Navin ready is a Java trainer okay so we have this string here and I want to I want to find the length of it so I will print length colon I want to print the length here and how do you do that we'll say uh we use we'll use this uh percentage I mean the dollar sign and then we have to Pro angular brackets in this you have to mention St Str but if you mention s Str it will print the string right let's try out let's run this code let's see how this goes uh okay just printing the string I don't want the string I want the length so what we'll do is we'll use a function here we'll say function colon and the function name is length and in this length if you pass St Str it will print the length of it if I say enter now you can see we got length which is 29 I don't know it is 11 uh 13 14 15 maybe it will be 29 29 if you can count it it is 29 right so that's how we use length function that's one of the function we can use what other function we have uh let's say I want to what else we can do we can use we can use split here I want to split this string based on Space here so whenever you have space we have to we we need to break that I want to split it so what we can do is let's not print this length now see when you split it will create an arror right because if you split instead of having one variable you'll be having an array which will have naven ready is a Java trainer so we'll be having six words now to have to store to store that six words again we can use uh for each Loop here to print everything and we'll set okay what we'll do here is we'll first of all we'll specify the items now this items would be uh I cannot say St Str because if I say St Str it will be one string right I want to break the string now how do you break it it we'll use a function okay so we we'll put that into a function but that function name would be so we'll say function uh the function name is split okay so you have to use split and then you have to pass this s inside a split but hold on on which basis you want to split so if I split with on the basis of space you can see if I'm specifying comma a space in single quote it will split the elements based on their space right and then we can store the elements somewhere we canl say I want to store the element in s and now I can print the element uh before printing I want a break now do you print it it's very simple we can say a dollar then a curly brackets and S and now if I go back there if I say enter you can see we got all these words here as simple as that right so we can we can have a string which is N Ready is a Java trainer and we can split that with the help of double code I mean with the help of space and then we can print all the elements that that is that is split method what else we can do we can split based on the a later so we have a here then we have a here then we have a and a we have lots of a right let's try to do that let's let's see if it is working with a you can see we got with a as well okay that's how you use split method what else we have uh we can also use another method like or fun or functions like index off now index of will give you the index of that particular string let's say I want to find this index number of e now how do we do that we'll say index colon uh we we'll use a function here so use to use function again we have to use this thing then we have to use FN colon the method name is index off okay and you have to pass two things first the string which is St strr the second one is the the string which you want to pass I want to pass let's say is so it will print the index of a so it should be 11 12 13 maybe or 12 12 it should be 12 hope so it will be 12 yeah you can see we got 12 so that is the index number of 12 that is the index number of a so that is your index of uh we can also use we can also use contains now can we use contains we'll say uh is there we can print is there okay it will only print is there and because if I want to check if this string contains Java so I can do that we can say a dollar then we can use a function here function colon contains okay and then we can use a function by passing a string not a zero but a string so we can pass a string the first parameter and the second parameter would be your okay watch uh the string itself let say this is do we have Java there let's try uh yes Java is there so is there true it is there but if I say does it have JSP let's try uh you can see false so you can use contains in fact normally we use contains and if condition we can also do that we can say we can also do if here we'll say C colon if in in your code Tex we do have if and we can apply test here because when you say test when you want to check you apply test we say equal to and we put everything in double quotes here which will check and if okay we have to also we have to also end if so this is a if block here and inside we can print uh we can print uh JSP is there right if this matches will print is there otherwise it will not print okay we got an error there uh the error is it should be in single quote now because we are using a double code there my bad okay so you can see we are printing nothing because it is not there but if I print if I check for Java and if I print Java is there it will say yes Java is there right so that's how we can use contains function we have more some more functions we can check with ends with if this function ends with or if this string ends with Java I mean with trainer then we'll print you are right buddy okay it should ends with it should end with uh trainer uh okay it's ends with not ends with ends with and if I say enter uh you're right buddy but if if if if if you say Java trainer is a Java trainer in Mumbai and if I on this now so you can say last word is Mumbai right so it is not matching so you can also check with ends withth okay what others we can have so yeah so the so we have all these functions what you can do is can explore more function you can simply say dollar then curly brackets FN colon and if you say you can see we have lots of methods here we have join uh we have replace withd you know try it out try all this all these combinations you can also use two uppercase two lower case the same way we are doing this example we can say St Str and it will it will print into uppercase if I run this code uh two lower case right so you can see everything is printed in lower case here if I use uppercase it will print everything in uppercase if I say enter you can say we got n javat in Mumbai it's do awesome right so the jstl has so much of power so that's about function uh jstl functions in this video we'll talk about filters in serlet now this is the most ignored Topic in serlet technology so we we learn about we talk about Ser request we talk about response we talk about the life cycle now this is one of the important stuff which we use in saet I mean it's not important to to write this in your server application but you can use it now why we need filters so let's say uh we have a client here and then we have a a server in fact I should you know uh for all my videos I should have this static things because in all my servate videos I'm going for a client on a server right that's how it works so let's say we have we have a client on a server and on This Server we have certain servlets let's say we have a serlet we have have a seret and we have a serlet now what you want is whenever a client sends the request of course when you send a request request goes to web XML file and then that's how you figure it out which serve to call right so let's say from this client you're requesting for this particular subet and after some time you're requesting for this particular subet and after sometime you're requesting this particular subet so we have sub a b and c now whenever you request for any of the Sate let's say if you want to maintain some log files you want to maintain the security Now for A and B you want to maintain the log file and for B and C you want to maintain the transaction and for a b and c you want to maintain the security so with this feature of a b and c you want some extra features like uh maintaining the log files uh then maining the transaction and maintaining the security so what you can do is you can write those code so if let's say if you want to maintain the log file you have to change your a soft and you have to add the log code there right you have to use log 4J and you have to add the lines in a subet the same thing you have to do in b subet as well because here also we want to maintain the log file now let's say you want to use transaction so you want to do transaction management those things also you have to write in b and c and then you have to write certain codes for security if you are allowed to so let's say if a client goes to serate you have to verify if that seret if that client is allowed to use that serlet so for that you have to check something right again that check will be done in a b and c right so the point is instead of writing those thing inside your ABC can we just take it out now since A and B has a common feature of log B and C has a common feature of u a transaction and a b and c has a common feature of security can we just take these models out and the advantage would be if you can take those common things common thing outside and those common things will will make in such a way that you can plug it anywhere when I say it can be plugged anywhere let's say in future if you have D and if you want to if you want to achieve transaction here you just have to plug that request and your seret will not even know that you're doing in the back end so what we can do is we can add filters in between so let's say we have the first filter here this is your first filter which is filter fub1 we got a second filter which is FS2 and then we'll be getting a third filter which is F3 now you might have guessed it the first filter here is for log so we can write a filters which will so this filter F1 will work for log files this filter F2 will work for transaction and this filter the F3 which will work for security right so using S filters we can we can create some filters which will be intercepting your request so whenever you send a request if you're sending a request for a it will go through F1 first so if F1 says okay I got I have maintained the log then it will go to F3 it will check for the security and then only your a will be called in fact if you are let's say if you are sending a request and F1 says everything is good I have maintained the log F3 says no this guy is not allowed to use SOL a F3 can return the request right then a will not be will not be called right so you are secure now uh let's say for the second request if you're sending a request it will first go for log maintenance then it will go for transaction then it will go for security right and any of this filter can return the response I mean they can send the response back so they will act like a Ser but those those are not actually Ser those are filters right so if you want to have something if you have certain certain things which are common like log maintenance transaction security we have some other thing as well you can write those things inside filters so the advantage of using filters they are pluggable you can so if you have a new subet in if you have a new subet you can add a filter in between in fact let's say in future in future if you have one more one more filter let's say that is F4 which is let's say for any of the XYZ purpose what you can do is you can add F4 here because those are plugable right your Ser will not even know that you're using filters and is that awesome in fact this filters they don't know which filter you're working with example if you're working with F1 F3 don't even know that equest is coming from F1 and that that's awesome right in fact your F2 don't even know that after F1 you're getting the response so these filters are also individual so they don't know how I mean how it is working I mean how other filter works right but then your tomat says if you have multiple filters and if you want to if you want to check for if you want to go for multiple filters what we have to do is we have to chain them so this is called as filter chaining okay so we have to filter chain I mean which we have to chain all these filters again F1 will not know that it is sending request to F3 it will be done by Tom cat but then where you have to configure this uh so when you write a filter F1 you have to mention once this check is done once this is done go for filter chain so what whatever you have in the next thing it will execute that one but then question arise where to configure all this thing where to mention for which request we want to call which which filter that you have to configure in your favorite file which is called as web.xml file so web.xml file will handle uh will so you have to configure in this file which filter you want to go for makes sense right but then question arise how can you create a filter so to create a filter we have to create a class which will extends servit filter okay and then you have to use three methods now if you go if you remember your seret subet sessions we have talked about inate method we have destroy method we have do get a do Post in the same way for filters we have a method like uh init we have a method like do filter okay that's do filter and then we have destroy okay so we have this three methods now in it and Destroy something which we normally don't use right we never use that thing in fact if you want to do some initial configuration you can do that but let's say we don't use that uh in this you have to pass two parameters request and response and that's how you can check right example if somebody is sending a request to a seret how would you know if he is allowed or not so that data will be there inside request object but then let's say after executing this filter F4 if something goes wrong filter F4 can return a response right it can send a a response with the help of response object right so that means your filter can I mean they can work as a seret but then that's not the main purpose of filters so filters are basically to intercept your request right so that's it that's about the theory of filters we have we we have a concept of ser filters in in servlets and it it we we can also do filter chaining you you can also configure individual filters if you want to configure filters we can use something called as filter config so it is also available the way we have Ser config the the same way we have filter config for every particular subet we have subet config for every particular filter we have a filter config right so it's it's that's it's damn simple again we'll see the Practical implementation of this in the coming session so that's it that's for this video and in this video we will try to implement subet filters now if you have not seen the theory of this concept please do that so in the description area you will find a link for salet filter Theory so let's get started with the example here so you can see we already have two pages here in fact one is JSP second is sovet and in this JSP we just have two field one is a ID a name and the moment you click on submit it will call it will send a request for add alien so basically uh let's say you have a alien and we alien has a name and an ID and the moment you click on submit the request goes to to my sulet and this is where you will you want to perform some operation and you can see I'm using annotation based configuration here we have web sulet request goes to alien add alien and then this will get executed now just to do something here what I will do is I will just try to print the name of the alien here so I would say first of all I want to fetch the name so I would say string a name equal to request. get parameter and here I would say a name and it will be assigned to a name and then I will print welcome plus a name or maybe I also want uh you know the ID of it so I would say int a ID is equal to integer passent and here I would say request. get parameter and here would say a a ID now once we got this thing once we got a ID and a name I just want to print a name here it's very simple stuff right so before applying any filter here let's try to run this application so I will go back to home and I would say run so you can see we got a page we got the output here I would enter some values I would say let's say one1 and here I would say enter naven the moment you click on submit request goes to the suret and you can see it's printing welcome naven everything is working perfectly but what if I make a mistake here let's say uh if I say 101 it should not be I mean if I enter minus uh 10 when you say ID it should be positive number which starts with one but if I say submit you can see it is still accepting it now we can do this validation on salet as well but most of the time what you want is you want a separate filters who will Who will check all these values not just about the initial values maybe you want to check for cookies you want to check for if the user is already logged in where you will do that and of course we can do that in cets but it is always better to do that in filters because filters are something which is pluggable now some people have this debate you know instead of using filters we can use suet as well yes we can use suets the only thing is Filters provide you those concept of pluggable things you know you can plug filters anytime you want and one filter is not dependent on another filter the way we have suets because in suet you have to mention which next seret you want to call but in filters we don't have that stuff right we simply say Hey you have done your job now call another s another filter and you don't have to mention which filter let's see how that works here so what I will do is I will do this filtering I will do I will do this checking in a filter now how you create a filter it's actually a very simple step the way you create svets right click on your project and say new and search for filters you can see we already have a filter here click on that we can put that in the same package of serlet so serlet belongs to package com. teliscope let's put the filter in the same package again you can have your own package doesn't matter but let's have com. telescope and we'll have a class name as my you can have any name doesn't matter you can say first filter second filter I would say ID filter here as you can as you can create multiple filters here we'll say next now for which request I want to do this so whenever someone calls add alien this is where I want to call I want to initiate this filter click on okay and click on next nothing fancy here click on finish and you can see we already have a filter here everything is there but we don't need this stuff so let's remove that we don't even need this command let me just clean this stuff a bit so what is this thing here so you can see we already have a filter the name of the filter is ID filter as we have defined in the creation of it now it implements an interface called as filter now filter is a inbuild interface in in server API and you can see okay I don't have a source code but doesn't matter you can see it belongs to uh it is a interface it belongs to Serv API in this filter interface we have destroy method do filter and in it so let's say before before working with this filter if you want to insize the values you can do that here and in this the video we have also talked about filter config right so every filter will have it its own filter config and then if you want to do something destroying you can do that here maybe you want to uh instantiate a new object you can do that in in it and once the filter has done its work you can you can destroy the you can release the resources and Destroy but at this point we are not doing all the stuff right so the only method we are concerned here about is do filter now do filter is almost same as do get do Post you know we it accept the request and you can process values but it has has a special feature which is filter chaining now as we know it's not just you can use only one filter we can have five filters right you can have 10 filters so by default it will call so when you run this application it will call the first filter so let's say for ad alien we have five filters so it will call the first filter once it has done its own work it will call the second filter the second filter will call third filter but how will you mention that this filter has to call itself or the second filter that's where we have a concept of filter chain so all these filters will be in a chain and every time you have done with your work you simply say chain. do filter which means it will call the next filter and you can do your work here okay now here's the thing what if this is the last filter and last filter says chain. do filter now since we don't have next filter it will call a svet okay that's a sequence it will complete all the filters first and then it will call a salet but as of now we only have one s one filter let's work with that so here I want to check first of all I just want to print something just to see if it is working uh where I will print it I will print on Console I would say in filter just to verify you know it's always better to go in step by step way I just need to verify if this filter is actually getting called and to do that I will I will run this application again so you can see same steps I just need to click on submit and you can see it is saying welcome Navin because we are not doing any filtering as of now but it is calling in filter that means the sequence is it is going going from your page to filter then serve it okay that's great but now uh in fact how would I know this you can just if you if you want to try you can just print something in this uh do get it will be getting printed after in filter but now I want to do filtering right I want to check what are the values which are coming now as you know when you want to work with request object we need a HTTP server request object right and here we have a subet request object but we need HTTP Ser request object so we can do that we can say HTTP serlet request I can say req is equal to I can typ cast it with HTTP subet request and request so what we are doing is we are we are basically typ casting the object so we are converting this object from server request to http serice request and now once we have this object we can actually fetch the value and I can say in fact I I will just copy paste the value because I don't want to type it again I will fetch this value here and paste now once we got Aid I can actually verify it if I say if a ID is less than one because I don't want this value to be less than one in case if is less than one I will not do chain chain. do filter because this thing should be done only when it is greater than one in fact I what I will do is I will say greater than one this will make more sense right so do chaining only when it is a a ID is greater than one so it will go ahead only when it is Aid is greater than one it's that simple now will that work let's verify I will just rerun the application here and and okay I'll just refresh this application and this time I would say minus 10 and here I would I will say naven and click on submit you can see the moment I click on submit it is doing something but it's not calling so you can see it is not printing anything here but at least it is not calling a svet how do I how do I know it is not calling a seret let's print something here so that we can verify I would say uh in salet because this will verify that it is calling a Sol it or not and I'll go back here run this application again the same values which is minus 10 now if I click on submit you can see it is not it's not printing in serlet that mean Ser is not getting called for sure okay but I want to print something on a page right because if Serv is not getting called it should print something on a page now in that case I would say else and I will use this response object oh first of all I need out object right so let's do that I will say print writer out is equal to response. get writer once we have this object I will say out do print Len because filter has the power to send a response back to the client I would say invalid input yeah so we can do that in filter as well so let's run the application again and click on submit and you can see it says invalid input it is not calling a salet and it is printing invalid input so that's the part of filter so even before calling a salet we can do some filtering okay so let's say if I'm putting a right value so let's say if I go back here I I will enter right value I would say 10 but the name which I'm printing is only of one character which is n and if you click on submit it is still working but I don't want anyone to have only name n right it should be at least more than three uh letters now in that case we can create another filter so we can create another filter and we can do the same stuff so that will be your assignment so what your assignment is is create one more filter and almost the same code and make it work the only URL so make sure that you have the same URL and let me know if it is not working for you so that I can make another the video I will paste a code for you so this is this is your assignment create another filter and if you have able to do that mention the comment section that you have done it and if you have not if it is difficult for you just let me know so that's it that's how your salet Filter Works today we'll talk about how to do login for suret and JSP applications you know when you build a website so there are lots of things you have to consider so you have to consider about user requirements you have to consider about uh your requirements you have to consider about server space and the most important thing about this is a login application right or a login module so let's say You're Building any e-commerce website if you're building any social networking website if you're building a website for your own college or for your own company you require this model which is login right now is it that easy to build a login application for a website and the answer is yes it is very easy but if you consider some uh twixs if you if you consider some uh some topics in seret it it makes it bit difficult because uh if you are creating a login application for a normal software it's bit different than what what is when you do it for website because when you talk about websites we have web servers right and web server works with HTTP protocol which is stateless so let's say you are accessing a page and after logging you're accessing that page so you got the access now when you go for the second page you need the same login access right so again you have to login what we want we want to maintain this state of the login and then for that we have to use some session management okay now so in this video we'll see how to manage the login throughout throughout three to four pages or maybe the whole website using sessions okay now so let's imagine you're working with uh application where you have three pages so what is website basically when you talk about websites it's a collection of web pages so let's say you want to build a big website we required web pages right so let's imagine the first page you have here is login page so we have a login page here we also have a page which is let's say about us okay then we have a page which is welcome page or we can say homepage and the last page we have let's say one more we have videos page right so when you have all these pages in your website if you combine all these Pages you will get a website right now from this web from this web pages there are some web pages which requires which requires a login access other other Pages like about about us don't needs a login or don't need a login page or login access so anyone can go to about us without login but if you want to go to welcome page you require a login if you want to go to videos page you require a login but not for login page which is itself a login page right so once you give your credentials inside login page so let's say you will it will ask you for username password so let's say we have un name and password here now once you enter this detail of username and password now it will it will verify your username password right it it will verify your credentials now if you are are a uh what do you say a a normal or authenticated user then it will provide you a login access otherwise it will reject you right but let's say you got the login now the page you can see is welcome page right and in welcome page it will show you some data about you uh so like welcome naen and it will show you all the last access you have done then from that page you'll be having one more link let's say for videos page and to access videos we require again login example when you go to ud.com if you go to my own website tesco.com to access some pages we have to login right and uh so in my website I don't have the login for videos that's good right but there are web pages where you have to login first so that you can access the particular web page now from this page videos if you're going to this page now if you say you want to access this so you require a login right so from login you're going to welcome and from welcome to you're going to videos sounds perfect right but let's say if a user he not logined okay suddenly he types a URL which is let's say uh for me let's say my website name is abc.com and if I say slash if I say login do JSP so what which page I will get is this page right which login page now after login automatically it will go to welcome page and from welcome page if I click on a button I will go to videos so the the URL for videos is this it is videos. JSP now anyone can type abc.com /v videos. JSP and they will get the access to videos page right and where where to ask for login there if they know the UR they can directly access that right and that's what we want to stop them that you should not you're not allowed to access this page without login and that we can check with the help of session okay so there are two ways of doing this the first way is session the second way is cookies now it's a big debate which one is good either we can use cookies or we can use sessions now cookies are much easier to work with because what happens uh your server will give you the cookie which you will you have to store inside on the client machine right so your client is responsible to save that cookie now since client has the cookie client has the access to cookies so they can modify it they can hack the server or they can play with your server so instead of that go for session where you save everything on the server side which is safe for the server right of course and why a client will need your data or server data right so of course session is much better than cookies if you talk about login if you're talking about maybe e-commerce e-commerce module then cookies will be better but time B for login session management is important now how to achieve session so if we talk about uh suets so we have a we have something called as sttp it's sttp session in sovet so whenever you whenever you login so once you verify your credentials of course right before so once you enter this detail when when you will click on this button which is login button so once you click on this button it will call a subet right so let's say let me Define a subet here so let me remove this and we'll be having a subet so let's say we have uh a sub we'll name this as verify. HT verify. Java that's your subet right so once you click on this button you have to call this subet and inside this subet you will verify this two details right of course you know uh how to fetch values from the client side using request.get parameter then you will apply if else by checking if the values are correct now once the values are correct you will send the request to welcome again you can use request dispatcher or you can use uh one more thing uh redirect so let's say if I'm using redirect I got the page which is welcome right so from this this from this subet code I'm sending request to welcome but before doing that we need to do one thing we need to put data into this session so what is session so let's create object of HTTP session so I got an object here which is called as session right so that that's my object how to create this object we have sttp session session equal to request. get session again we'll see that when we start with the Practical so this is just a theory theory of this after this we'll be talking about the Practical implementation so in that we'll see how to achieve this okay so once we got the object of session now we have to set the value so you have to say set attribute so that's the method we have to use so that's attribute attribute and then we have to mention two things the label and the data so label I will say this is UN name comma the data is let's say whatever data you got from username so if username is naven so it will be naven here if username is admin so you have to send admin here so let's say I have stored that data in uname itself so the label is also uname and your data is also uname now once you send this data to to the welcome now what you have to do is on this page which is JSP page so this will be a JSP page inside this so let me remove this now so once we got into this page which is welcome. JSP let me write that page here now so in this you have to check for this session again so before executing any statement from this JSP we have to first verify now how to verify so we have to apply if here and we can say if session dot get attribute because of course right once you set it we have to say get attribute if this un name is equal to equal to null now when you will get this null is so let's say if user is not logined directly he's trying to access welcome. JSP so it is abc.com welcome. JSP that means this page was not visited user has not provided the credentials directly is trying to access welcome. JSP in this scenario this uname will give you empty results that means session. attribute will be null and if this is null that means user has not login what you can do is you can again call login page how to do that we can simply mention here response which is response do send redirect and we can mention which page to call we say login do JSP okay so once you call welcome. JSP without loging it it will check if your name is null then it will directly call login.jsp that means if anyone's try to access a web page which is a secure web page without login it will he will be redirected to login page if this is not the case then you can continue with the example and you can show welcome. JSP right now let's say uh in this page you have a button for video so we are providing a link let's say AHF so we have AHF for videos now once you click on this button once you click on this uh what do you say uh anchor tag once you click on this videos here it will take you to videos page again you will you can see all the videos on that page now you'll be having a logout button maybe on welcome also you'll be having log out and here also we have log out now what to do once you enter log out or once you click on log out button so let's say we have a button here in in this web page itself or let's say in videos we have a log out button so we have a log out button now once you click on this button what should happen it should also call a sulet okay so let's create that serlet here so we'll say this is log out. Java now in this what you have to do so when you login so when you when you log in for the first time what we have done is we have seted the set the attribute for session right which is UN name and guess what you have to do in this in logging when you set the data in log out you have to remove the data right so you have to use something called as session dot we have to say remove attribute and then you have to mention which is UN name simple right now once you once you remove this and if you go back to welcome page the thing is in welcome page you will get this as null and it will redirect you to login.jsp so you have to do this for every secure page okay let's say in your website you have maybe 15 pages and after 15 pages five pages are secure pages and 10 pages are normal pages so for those five pages you have to write this thing on every page on every JSP page you have to verify if this is null then loog uh redirect it to login page and for every log out button it will call this thing which is session. remove attribute okay so that's how you provide the what you say the login feature but there's one more thing here so let's say uh whenever you access a web page so what happens is on that web page we have your browser has the feature of caching Pages now what will happen let's say you you're you're on the welcome page you click you click on a log on you click on log log out button now once you click on log out it will take you to login page right I mean you have to write here we have to say this is after removing the attribute we have to say redirect me to login page right so once you write this statement here it will take you to login page now what will happen when you click on back button now once you click on back button it will again take you to welcome page and you are hacked so let's say if you're using a website and you you have entered your details there and then once you say log out you think you're safe now but yeah hackers comes to on take your machine they say back they got the access to your website right so that's not the case so what we to do is we have to remove the cache Pages or we have to stop that cache of the pages and we have to do that with the help of some meta data or meta tags again how to do that but that we'll see in the Practical implementation so if you want to include login in your web page or in your website you have to follow all these things so that's a general overview how to use sessions instead of cookies and we'll see the implementation in Practical itself this video we'll see how to implement login module in seret and JSP so we have already seen the theory of this right so what we need we have a login page here which is login.jsp we have welcome. JSP we have videos. JSP so if I go to login.jsp it should give me two uh it will it should give me two text field where I will enter my credentials username password and if I I click on the button if the username password is correct it should call a page which is welcome. JSP in which we'll welcome the user and we'll print welcome space username and we'll provide a button or a link for videos page and once you click on the videos link it will take you to videos. JSP page and here we'll try to uh show you some I will will try to show some uh video something like that so uh we have this three pages and what I want I want this welcome. JSP to be a secure page and this videos. JSP to be a secure page [Applause] welcome and videos they are secure pages so if I play this if I run this login.jsp now if I run this okay it will open in my browser I hope it will be in Firefox okay that's Firefox here so that's my login page okay so I caness I can access login page I can access uh welcome page welcome. JSP so I don't want my user to access this welcome. JSP page okay so this should be accessible only when the user is logined so what we'll do is we'll try to provide a login button here or we'll provide a form so we'll say this is form we'll say form action and we'll mention let's mention so we'll say double codes and we'll mention here form action we'll call A A servet here which is let's say this the sub name is login okay and we'll keep the method as uh method as get now we require a username so we'll say enter user name and we'll say this is input type equal to text and we'll say this name is equal to un name okay and we'll say BR the next thing we require is enter password okay and we'll say this is input type equal to this is a again this will this will set as a password so that you will see that uh star symbol there and we'll name this as pass okay and then we'll say BR now next we require a button so we'll say input type equal to text uh not text this time it will be submit and we'll say value what it should show is a login button right so now once you type all this codes once you click on this login button it will call a login dot or login. Java file which is login. class file which is solet right now let me create a solet here so we will say not here we in Java file so say right click new let's create a serlet and it belongs to a package which is com. login I have already created that created that package here com. login and the class name let's say this is login. Java once you click on finish it will give you the full source code for solet right so we're not concerned about this thing let's make it simple we don't want do Post we just want do get here okay not even comments okay so we just want do get and inside do get what we want so let okay so now we need to F first fetch those details so we say string uname equal to and we'll say this is request this is request. get parameter and inside this get parameter you have to mention that you want data from you name now this un name here which is the request.get parameter you have to fetch data from this field that's why un name and next we have to fetch password so we'll say string pass equal to request. get parameter and we'll say this is again pass okay now once we got these two details now let's call let's verify now how to verify we say if again you can fetch this or you can verify this data with the help of database so you can create a dow layer you can request your database to give you the username password and then you can verify but time let's make it static we'll say if my un name is equals if it is equals to telesco and and the password need to be equals with let's say learnings if these two are same so if my username is teliscope and password is learning in that case we can call welcome. JSP now how to call that so before calling we need to also very we need to also add the data into session right because how will you verify by when you're calling welcome. JSP how will you verify that the user has login oh let's not do that first initially let's keep it simple let's say directly we'll call uh welcome. JSP will'll simply say this is response so which say response. send redirect we'll say send redirect what we want if the username and password is wrong or if any of the one is wrong we'll call the same page again we'll call login page again so we say response. send redirect uh send redirect and we'll mention this is login.jsp so it will again call the same page so let me show you this and if I say enter so I got login page let me enter any any data here if I say login oh it's not yet deployed I guess okay let me just refresh again okay so let me try something else if I click on login you can see we got the same page again right so if your username password is right it will call welcome. JSP if it is wrong it will call login.jsp now what will happen if I if I if I'm not logging it okay let me just change my browser let me use Opera and inside Opera if you paste I if if I paste this link which is welcome. JSP you can see I've not login right and let me go back to this and say welcome. JSP and we got welcome it's because we have not mentioned that uh we need to check for the login now how to verify that user is login anyway you you're able to access the welcome page right so what we can do whenever a user login we'll will set the data into a session now how to use session we can see HTTP session we'll say session equal to now if you don't know how to use session just go back to the video where I have talked about how to use sessions and then we'll sayp session we'll say this is request do get session okay so once we got this session now we'll set the data into session let's import the package okay now it is session dot we have to set the data into session so we have to say set attribute we'll say this is we'll set the label which is let's say username as a label and the data we going to send is the UN name which he has entered while login so we say this is UN name sounds good so we have set we have uh we have set the data into this session now in the welcome page we need to check so before executing anything we we need to check that was the data he has entered so how to how to check that uh so we'll check if if this session. get attribute since we are when you assign data you use set attribute to get the data we use get attribute if the attribute with username is equal to equal to null because if user directly trying to access this page without login this will be null right if that is the case we'll again call response do send redirect let's call login.jsp okay if not then it will print welcome let's try let's go back to Opa so is it Opa this is this is Opa now I'm trying to access welcome. JSP without doing login and if I say enter you can see we got login page awesome right it's this is this is possible with the help of this thing here which is session now let's say if I'm logging if I'm trying to log in here we'll say this is delisco and the password is learnings and if I say okay we got log we got welcome page right but the thing is I want to show the username also so we can use jstl here which is language and we can directly write dollar and we can write here U what usern name so this is jstl so whatever you have so let's say in session we have this one wait session so inside session we have username right so in this welcome page it will fetch the username from the session so that's how you use jstl which is Java standard tag library or expression language to be specific now in this welcome page what we need we require a label we require a what what we require we require a a link for videos. JSP so what we can do is we can provide a anchor tag we say AHF equal to we'll say videos. JSP we can mention a anchor tag close and we'll mention this is videos we'll say videos here and once I click on that link videos here it will go it will take me to video. JSP right let me just enter this and we can see we got welcome telescope and it says videos here if I click on videos it will take me to videos page and how to include videos so can I include my video there let me try I've never tried it let's me let me go to YouTube and let me search for my let me search nin let me search Java tutorials yeah that's me uh let me fetch this video okay copy the link okay and let me paste it here I don't know will it work will it work I don't know oh it will not work of course right this is just a link right so let me just show you the link not the actual video but a link right for that we have to use a video link so we got a link for my video you can you can actually use a video link on your video tag on your website and you can embed this link there what we want is we let's imagine instead of this we are getting a video so uh if I try to access this page directly without login so let me okay we have we already log in right let me use it now this time we'll use Chrome and inside Chrome let me paste this link and you can see we are able to access this page right without login so what we need to do we need to do the same thing just go to welcome page copy this code and paste it here sounds good and now if I go back to my Chrome and if I say enter it takes me to login page awesome right so that's how you use login but there's one more thing let's say if I'm providing a log out button here so if I say log out so let me provide a log out button so we'll say form action I will provide log out only on the welcome page so I will say form action uh it it should call the log out. sulet or log out. Java file and we'll create a button here so we'll say this is input type equal to submit and in this the text we will we can show is value we'll say this is log out simple and once you click on this logout button it will call a log out. jsv page so we will say new uh log out not GSP log out. serlet so we say new sulet this will be com do login and it belongs to the name it will be log out right and let me remove all this extra stuff here let's only focus on the get method and inside this we'll mention whenever you click on log out button what should what should happen is it should remove your data from the session so we'll say this is session request. get session right and we can just import the pack package by pressing control shift o and we'll say this is session dot so while log you set the session while log out you remove the session or remove the attribute which attribute we have to use we have to say this is user name and we need to do one more thing which is session. invited so that it will remove all the data okay now let me I hope it will work now so let me run this and again this will be opened in a file Fox that's my default browser for the eclipse okay so now let me enter let me login so a Tesco and this is learnings okay so we we we got the log we got the welcome page so I'm login right if I change my tab here if I go to windows. JSP I'm able to access this page is just because I'm logined let me log out okay that's it I have logged out let me go to videos. j page or different Tab and if I click on okay you get the login page awesome right so that's how you manage this session or you manage the login session in sulet and JSP we can even navigate from after once you click on log out button you can navigate to the uh login page we can simply write this is what uh response. send redirect right and then we can mention this is go back to login page so every time you log out it will take you to login.js page awesome right so that's how you work with uh login module in sub JSP so let me know if you have any doubts any questions let me just go for a quick recap before that uh that's a login page when you click on this login button it will take you to login. login solet where you will verify your credentials if your Lo username is right it will take you to welcome. JSP otherwise it will take you to login.jsp but before going to welcome. JSP it will set the attribute as a username when you go to welcome. JSP you can print the same username but before that it will check are you logined if you how can you check that you are login it will check for the attribute if it is not if it is null then it will take you login.jsp otherwise it will show you the button or link for videos when once you click on this button which is videos it will take you to this page and if you you can also click on log out button here once you click on log out it will go to log out serlet and it will remove the attribute so that's it login using serlet and JSP practical part two in fact I made the earlier video last year and luckily I got good number of views on that video and people were able to run this code and everything is working perfectly but then with one particular issue and that issue is when you click on log out and when you go back using back button you can see the P last page which is the a secure page and this this should not happen right example when you go to Facebook and when you click on log out now when you click on back on after going to Facebook it will not show you the homepage right it will go to it will go to login page automatically how that works because if you see our code in fact I have written the same code in Eclipse now as you can see we have the code ready here and what I will do is okay in fact if you have not seen my first video make sure that you watch this video because then it will make more sense otherwise this will be what you say a question mark video for you so make sure you watch this video first and in fact you will find the link in description are here and you can continue with this video then okay so if I run this code if I run this code once again if I say right click and run as run on server uh okay it's running on server now it will open my Firefox as a browser because that's what I've set in my Eclipse so the moment you run this code what's happening is is it running or it's taking some time so by the time it it opens let me open Firefox as well and you can see in the Firefox uh we have to navigate to login.jsp because that's where you have your things we'll say uh login.js in fact before going for login.jsp what I want to show is I will say welcome. JSP if I click on enter you can see we got a login page because welcome. JSP and the video videos. JSP those are secure Pages if you want to go there first you have to login so we will say Tesco and L and you can see this this is one more issue we are able to see the password that's not that that's not a good way and if I click on login you can see we got the welcome page now if I click on videos here you can see we got video link in fact I I want to change this as well we'll do that in sometime if I go back and if I click on log out now we are logged out of the system right and if I click on the back button can you see that we are able to see the a secure page now this is not a good thing right this is this is what you say a security flaw now how to avoid this so we'll do all these things in this video first of all let me change something let me change this password from l i mean instead of saying this password uh let's make it dot now how do I do that I will go to my eclipse and I will say hey I don't want to see the password so I will go to login.jsp and when you whatever you have text for a password let's mention password because this will make sure that you cannot see the password okay we can verify that if I go if I refresh this page now and you can see whatever password you type now it will be in that dot format right now you cannot see the password if I click on if I press L you can see we got Dot so we got our first thing the second thing I want to do is uh if you can see in the form tag when you say form if you don't mention the method name it is by default get and in fact in Ser it as well I'm using do get now again this is not a good practice whenever you send data from client to server we have to make sure that we are using a post request because whenever you send data to the server you should use post whenever you are fetching data from the server you should use get now in this scenario I post makes more sense so first of all I have to change this to from do get to do Post because I want to send a post request now and in login.jsp as well I will mention the method as post so that's one of one more one more change which I have done here so we got a post request and we are using do Post there so we have done two changes the Third change what I want to do is in this video videos. JSP in instead of uh instead of seeing that link example if I if I logged in here and if I oh what's wrong okay we have toart the server because I made some changes in this subet so let me just do that let's say restart the server and if I go back to my browser if I click on login now oh I forgot to enter the password if I click on login um it's taking some time because the server is still getting restarted what's wrong I'm using a get request is it a get request here okay this is post request that's perfectly fine in login. we got do Post what's wrong here let me just run this application I don't know what is going wrong there okay it's not able to fully restart the thing as well okay it says H method get is not supported by this URL but why we are using a post request everything is perfectly fine there okay so this should be do post and we got do Post uh nothing nothing else nothing okay so now sometime it happens you know the you can what you can do is you can clean your project just to make sure that everything is perfect you know you you also get some cachy files in the uh in your Eclipse as well okay let's so we have done with the cleaning and now let's run this project once again if I run as run on server we start the server and let's verify okay server is getting started and I will go to Firefox just to verify everything is working let's restart refresh this page and we have to call login.jsp it will ask you for two things let's insert insert teliscope and password L and if I click on login oh it's working perfectly now I guess there's some issue with the temporary files okay if I click on videos here you can see we got the link I don't want a link here what I want is the video the way you can do that is uh okay what's what's going wrong there is we have written the videos link as it is this is not a good way instead of hiting the link of the video you should be using some embedded text now from where you will get the embedded link so if you go back to the video and inside every video will have in fact on YouTube every video will have this option of share now if you click on share there's option of embed now in this embed if you just copy that embed link and paste it in your eclipse your job is done now the moment you click on uh I mean the moment you pasted the JSP if you save let's go back to Firefox let's refresh this page now instead of getting a link you can actually watch a video in fact you can play this video uh and it will get play and as you can see it is getting played now okay let's go back so we have done all those changes now the most important change what we want is okay let me do that once again the most important change is if I click on log out if I click on back this should not come now first of all before solving that issue first understand why the issue is coming the issue is there because you know we have this awesome feature which is called as H HTTP protocol now HTTP protocol provides you lots of features example session is provided by HTTP uh we got uh cookies and the most important part is cash now cash is what makes your web browser housing experience way faster right if you go to Facebook if you go to YouTube you know what I'm talking about you know you can access all those pages as quick as possible because we do main we we do maintain the cash now same thing is happening here when I click on log out and when I click on when I go back this is the page which you are getting here is a cached page what I have to inform to my browser hey browser please don't catch these Pages because these are secure pages I don't want this to be catched but how do you interact with your browser now whatever you want to mention to the browser you can do that with the help of headers now every browser read headers as well so how do you how do you send a header that I don't want to save this page not not just videos page but also welcome page let's do that in welcome page first now the way you can do the the way you can send the headers to the client is using the response object so we can say response dot there is a method called set header so you can send lots of to the client one of the method is set header now inside this you have to mention two things the first is the label so what you are sending a label is you are sending a label for cach control now your browser understand what Cas cach control is you can pass your instructions here now if you don't know what the instruction is it's very simple in double cach you have to mention no cach again you have to byard this because this is browser specific or this is what you say uh this is browser is something which a browser needs this thing okay so we'll say no Cache no store and you have to mention must revalidate for every action I mean every time you call this page it should revalidate if it is logged in or not I mean whatever you want to whatever you want to check for the validation right so you just have to set the header as cash control no cash no store rev viate I will copy this code and let me just paste this in videos. JSP as well because I want it there as well so what I will do is once we have done all the changes let me restart the server okay and if I go to Firefox and if I refresh this as well now if I insert L and if I click on login okay it's it's happening something is happening behind the scene okay you got you got your page if I click on videos here it's working perfectly if I go back and if I click on log out now so log out successfully if I click on back can you see that we are not getting welcome. JC page that means the the it was not cast and your browser is not able to give you that welcome. JC page so this is what we want right when you click on log out it should it should show login.jsp and it should not allow you to go back now this was one way the another way is you can disable your back button right but then this is not a good way because if you disable your back button what will happen is uh it will give a user a bad experience because no one wants to disable their back button how how dare you to do that right so no one will use your website so this is one way I mean this is the best way uh which is response. set header now the thing is uh this works this perfectly works on HTTP uh 1.1 right this is the this is the this protocol version supports this type of text what if you have the older version of HTTP working in that case you have to set one more thing because this will not work there so you have to say set header and you have to specify pragma and the value for that would be no cache if you are using so this thing if you are working on the older version which is HTTP 1.0 but then if you are using any proxy server then you have to say response. set header and you have to say expires because proxy server will have your cache if you don't want to maintain the cache there just say expires and you have to mention the time zero so for 0 minutes or 0 seconds you have to mention there this should be I mean that expir is 0 second the moment you access that page it will be gone right for pro proess so 1.1 for H I mean for 1.1 the first line 1.0 second line and for Proxes the thir third line the same thing need to be done on all the pages which whichever Pages you have so in the videos. GSP also I will do the same thing move this okay done so that's how you can stop going you can you can prevent from back button so yeah that's it from this video I have talked about the back button working um okay now what else I want to do here so there are some some more changes uh the changes like you know if you talk about this login. J login Java here we are checking statically right we are verifying the static coded text now the thing is this will this doesn't look good because the data should be coming from database so again we'll be doing that in one of the video and then we'll also do JavaScript validation now I want to show you one more thing in this video which is very very important now in uh in one of the comment I saw is they were talking about this thing session. attribute username so I'm studed writing username here which means uh I mean according to the comment it says whenever a user logged in it will allow all other users now that doesn't make any sense because if you talk about session object this is browser specific which means the moment you change your browser a new browser will have a new session object when you are verifying here you are verifying for one particular browser right let me prove my point so if I go back to Firefox you can see if I log to the system now I'm logged in if I go to if I type here manually if I type videos. GSP I will be able to do that because I'm already logged in but if I go to a different browser example if I go to Safari here and if I enter the same text if I say enter you can see it is not opening videos. JSP it is opening a login page because this is a new browser a new session and of course if you have a new session it will uh it will it will send you to login page again right so yeah that's it from this video in the last video we have talked about how do you provide this login access right so we can enter username and password and you will get authenticated but then you know we are verifying so if I go to my login. Java you can see we are verifying using the static values we have a theis and L as a username password now this is not how we write code right we always make sure that the data is there is coming from database but question arise how do we do that here we have not done any database code yet right so the best thing would be you have to create a database first and let populate our database so that we can verify so step one let's create a database so again you can use any database which you like you can go for uh myql you can go for pis you can go for Oracle which is your favorite uh I will go for myql here because this is my favorite if I open m workbench this is the GUI version right and I already have a local instance created here and if you don't know how to how to use this do watch my video on how to use MC workbench uh but if you already know that's let's continue so first of all we have to create create a database right uh in fact we I do have lots of databases here but let me create one or let me use my na database as you can see we already have a n database here I will use that one in that database I do have some tables but ignore those tables for time being uh I will say create uh okay I will say use naven first I will say use naven so I'm using this datab datase and now I want to create a table so I will say create table and I mention the table name as let's say login I mean there should be a logical name for this login will do for this example I will say login and in this login I want some Fields right so I will say the first is username that that is your name of type Ware which will which will be off L 20 and then we'll be having a password we'll say password and again this one instead of saying password we'll simply say pass and we'll say this is where C and this is also 20 again you can have more than 20 uh 20 uh text so you can change it in that way you can have a username of 50 letters if you can have a password of 100 letters if you want to torture your audience or your or your clients okay we got this table here let me execute this query and you can see we got our table ready now let's insert some data here just some demo data so that we can verify we'll say insert into login there should be a registration for form for this again you can write your code you can create a form for a user to register themsel but time let's imagine if you already have data here so I will say insert into login values I'm inserting some values here I will say the name is naven or will have a small letter N so that's my username naven and my password would be let's say um nine okay this is my password which is 9876 again that's uh you can have any password there so so let me just run this query let's see if it's getting inserted and everything is fine it is got inserted let's have one more dummy value uh let's say this is um we can have any one we can say uh Tusk and the password for Tusk would be let's say Tusk we have a username and password both as Tusk so if I run this query you can see it is working let's F the select query just to verify if you got the data there so if I say select start from login and if I say run you can see is working we got nav 98 876 and we got Tusk and Tusk that's that sounds good right now what's the next thing the next thing is once you got your database already once you got a table let's verify using uh using Java code now how do I do that now first of all when you say you want to verify you this is not the right way right this is not the right way of verifying it normally whenever you want to connect with database uh there are different layers we work with we work with this is not a big application we can write the Java code here itself in fact we can write the Java code in this in this method itself in instead of this if condition here we can write the Java code but then what will make more sense is if you create a separate class because whenever you work with database it's having a separate class will always I mean it it will always give some advantage and that separate class will always be in a different package okay not compulsory but then it's better to have that in that way I will say this is login da now da is a special keyword which you you should know uh da simply means no it's not a keyword but it's a standard we always use da stands for data access object so whenever you want to interact with database uh it's better to work with Dow classes and I will say this is belongs to Dow package again you can you can use login repository you can say login database you can say anything you want but Dow makes more sense and if I click on finish you got your class here Dow class now in this Dow class I'll be having a method called as public and I will I will say this method will return Boolean so if you want to verify if if this login was successful you will say true if the login fi you can say false I will say check uh details or check credentials we can you can use anything we can say check details or we can say only check anything we do right so we can say check details check credentials so let's have a check method which will take two parameters one is username and the second one is password right both need to be taken what will be taken here and once you got this values let's return false by default it will be false right okay now how do you verify so first of all to to connect with database we have to write certain steps now if you have if you know how to work with jdbc uh you can continue otherwise what I would recommend is if you have not learned jdbc before uh do watch my video on jdbc first because that then it will make more sense uh you will find those videos in the description area okay this is one way the another way is you can actually get the Java file using mavin so if you if you are familiar with mavin you can convert this existing project into mavin okay and then you can add your Java file uh not needed in this project but then you know we can actually try it out so we can right click here and you can say configure click on convert to mavin project you will get a maven project and you can add that jav file using just a text again it's your choice time bin I will not convert to mavin let's use the same project so when you want to use the same project what you have to do is you have to paste that jav file in this lip folder so paste it here and you can see we got Mas connector now if you don't have the jav file with you uh what you can do is you can just go to uh you can you can use Google to search for that or you can go to Maven repository and in this Maven repository search for mask your connector okay so if you go here you can you can select any version I will prefer this stable version I will go for 4.1.4 one and you can click on download job so this is one way you can download okay but using MAV it will be very easy but okay it's your choice again so this is where you will get this job file just click on download you will get the jav file just add it in the leave folder now we have to also add that because for develop developing also we need that so we go to properties we'll go to Java build path and here as well I will add that particular Java file file so I will click on add Java files I will navigate to my desktop I will say Mas connector done okay now let's write our steps the first step is you have to create you have to load the class I will say class. forame ignore that error of eclipse so yeah class. forame and inside this you have to mention com. MySQL what's happening com. my. jdbc do driver okay we got our class oh it might throw an exception so let's everything let's put everything inside a tri block okay and let's generalize it to exception so that I don't have to write R Block once again okay so we got class. forame the next step you have to create a connection object uh we'll say connection con equal to uh driver manager. get connection again there are multiple ways of creating this connection object we can use data source we can use yeah we can use data source we can use uh different tools available right uh we can use uh dat a connection pooling concept but time and let's make it simple I will simply use a URL here for database I will say string URL and the URL for mql is jdbc colon mql colon SL localhost colon 3306 as dbms changes this link will also change and the my database name is n right so we got this thing we got uh URL here the next thing is username uh we got username we got this username and the username is root and we'll say string password is equal to zero for my case the password is zero okay so we got this URL then we got a username and we got a password right so we need these three things so we got URL we got username and we got password uh I guess I don't know why it is giving that symbol just ignore okay just ignore that okay so we got URL username and password the next step is you have to get a statement object this is JBC steps right again if you're not familiar with this do watch my videos on jdbc that will make more sense okay we'll say create statement or a prepared statement will make more sense here because because I'm passing the values right so I will say prepare statement but then how will you fire this query now that's that is important right what query you will fire now how do verify we have that username and password which is correct now there are different ways of implementing it what you can do is you can you can fetch the entire table and you can verify each and every record you can run a loop and you can navigate to The Loop you can find the username password is matching or not but then don't you think you're fetching the entire table that doesn't make sense right so what I will do do is I will create a query which will beit bit efficient than normal query we'll say select uh we'll say select star from uh from login but then where I will say where you name is equal to question mark and pass is equal to question mark now we'll replace those question marks okay first of all let me just write it SQL here we'll replace those question mark we'll say search string let let's replace the first question mark with you name and let's replace oh I just made a mistake here the problem is this password and this password is same I will make this as pass okay that will be better and this would be the password for the database which is this one so this is my password this is database password that's weird I know I will say st. set string and we'll say two and this will be pass okay what will happen now is when you file the query example if I say username is naen and password is any password so it will try to match if it will try to match this with the database and of course if I have my entry there with that username and password it will return something otherwise it will return nothing that's what we want to do we just have to file the query we'll say result set RS let's import the package and I will say st. execute query now if if I simply check if rs. next if there is a record then return true it's that simple this is what we want to do okay let me just do this once again let's say if I enter username and password okay the username which I enter now is naven and the password is 9876 so the F the query would be select star okay the query will become select star from login where you name is equal to uh naven and the password or the pass in this case is equal to 9876 if this is the data which I'm entering in the in the while login and if I run this query you can see we got a record but let's say if I make a mistake let's say if I entering a wrong password now in this scenario if I run this query you can see we got nothing that's what we want to do if uh if we got something after filing the query that means it is a valid user otherwise he's not a valid user is that simple right now once you once we have done this code let's go back here and let's create an object of login Dow again you can create that object anywhere you can create a reference up somewhere here or just for this example not a good practice again I'm creating the object inside this I will say login Dow we say Dow equal to new login Dow let's import the package as well here itself let's say import the package okay package is imported and here I will say instead of checking that thing we'll say Dow dot uh check with that username and particular password whatever username password you got and that's it your job is done if it is verified it will work otherwise it will not work let's verify let's run this code right click run as run on server I think it should work let's verify it's running it's running it's running waiting for the console waiting for the browser to pop up okay I got my Firefox browser here hey I don't want to make your default pass default browser okay so if I say welcome uh. JSP now and you can see we got a login page let's verify with telescope and L and this will not work of course because I don't have that data there the data which I have is naen and the password is 97 okay that is 9876 if I click on login you can see it is working now right if I go back if I log out and if I give a wrong data example if I say Tusk and Tusk one if I give a wrong password you can see it is not working is it it's not working can you see that but if I give a right username and right password task it's we got the login right so that's how we can verify with database so try it out and let me know if you have a better solution in the in the comment section so if you think we have we do have a better solution than this uh comment section is open for you so that if if I feel that is better I will make a I will surely make a video on that as well and yeah so we have we are expanding this project like anything we uh okay so we got the login now with the help of uh database maybe in the next video we'll talk about how to do client side validations right and in this video we'll see an example covering saet JSP mavin and jdbc in one video we are covering all these things so this video will be helpful for those people who want to revise all these topics so maybe you have learned this topics from somewhere and now you want to make a quick recap so this video will be helpful for you because we will be covering most of them uh I me the internal topic of that as well and then if you want to just see how saet JSP M and jdbc looks like you know you you see you talk to lot lots of people and they say okay you should you should learn J savet and JSP but you're not sure what exactly we see in that you know some people know code Java and they just want to see how Sav JSP looks like so this video will be helpful in that way now again this video is not for those people who want to learn s and JSP in day because we'll not be covering all those thing in detail here but then if you just want to get a gist and then if you want to if you just want to do a quick recap this video will be helpful for you so let's get started now first of all to make it work we require two softwares here the first one is Eclipse again you can go with any IDE which you which you like maybe you can go for net beans or eclipse or inj which you like in fact in my machine I already have Eclipse so I will continue with that and then the second software we need is rdbms so in this example I'm using MySQL you can opt for Oracle poist whichever you like so I'm in this in this video I will be using eclipse and uh my skill yeah so so in my Eclipse you have to open Eclipse you have to select a workspace and then let's create a project so I will say new and now you have two choice you can go for a dyn dynamic web project or you can go for mavin project now what's the advantage of going for Maven so in Dynamic what you do is you create a project and then you assign a tomcat server again you have do that so you have to assign a tomat server and then what you do is you uh when you when you want to use some external jav files example for MySQL for jdbc we need a jav file right and you have to go to Google I mean you have to go to internet you have to download the jav file and you have to add in your project now when you work with lots of dependencies it creates some issues uh example if you work with Advanced framework like spring hynet or maybe STS framework there are so many dependencies you have to work with so in downloading all those dependency by yourself I'm talking about J files okay what you can do is you can you know uh you can uh ask mavin or a build tool to give it to you so mavin will say Okay programmer you want uh you want my connector I will give it to you if you want spring framework dependencies I will give it to you so your maravin will give you all those dependencies again you you have to conate somewhere you have to inform Marin hey what what what are things I need so mavin will give you give you those things provided it's there with mavin and luckily we have all the Java files available available with mavin okay so here I will be selecting a mavin project and how to do that just click on others and you will simply say okay it's taking some time I will say mavin so search for mavin project and you can see we got mavin here click on next and we'll say next here we'll mention the project type so I'm going for a web app project again you can select quick start if you want to go for a standard on software here we are building a web application so let's select web app click on next and here we'll enter group ID so group ID is basically your company IDs so let's say if you have thousand projects on on some web services or some Play Store you have to make sure that every app is different and you can differentiate between those with the help of group IDs and the package name so I will say group ID is comco that's how I represent my group ID I can sayi ID the project name I will say saet JSP demo again this is Ser JSP jdbc and MAV demo but let's name it as Ser JSP demo and we click on finish so we got our mavin project now this project is coming from the internet you can see it is taking some time to configure now I will if I go back to my project or not this one this is the spring project so this is the project which we have created now which is uh seret JP demo again it's taking some time to make the to build the project and you can see we got our project here we got resource folder we got JRE we got M dependencies where M dependencies so this is your uh what is that where m dependenc so if I if I open Java resources you can see in this we have M dependencies and we only have one dependency what I want is I want a myql dependency as well because if you want to work with mySQL again in this application we'll be working with a database right so we require a myql connector to connect with this database in fact I also have a database ready with ready with us uh so I have installed myql preand so you can install myql or post your choice so I have myql with mq workbench so it's taking some time to open so by the time it opens what I will do is I will download the dependency now question arise how will you get the M dependency it's very simple you just need to inform mavin hey you want Mas dependency and how do we interact with mavin uh you you do that with the help of pom file so in this pom which is Project object model you have to inform what type of what type of dependency you want so we just need to add a dependency which is Mas dependency here so we already have junit now we don't have to tie by yourself okay if you're thinking you need to buy heart all the dependencies not needed you can simply go to a website and so the website is mvn repository okay so here we'll search for mvn repository and it will take you to mvn repository where you just need to search for which dependency you need so here we need a mql connector so if you can search for M connector it will give you a connector here so if I click on search and it will give you a connector and which one you select you can select any version I will select 6.0.6 just copy this and go back to your myql go back to your eclipse and paste it here the moment you paste it you can see it will download the M Conor for you and our job is done so using mavin you can download any dependency if you want if you want Mas you get it there if you want spring frame spring framework dependencies or jav I'm talking about you can get that using Maven so mavin has lots of Advantage like it's a build tool you can build the application it will package the thing for you but time me we only need a jav file yeah so we got a jav file there now the next thing is uh the next thing is I want to create a page right so using which I can send data so if I go back to my my workbench you can see I will just connect with my with my workbench and the password is star star star star which is Root in this case for my machine at least and okay so I already have a database called as nav if I explore nav you can see uh we have a datab na in which I have a table called as alien so I already have all this data ready with us so that you know when you work on a project it will not make it will not waste much of time so I have a master database which is uh naven and the data the table name is aliens we just need to fetch data from this example if a user says Hey I want data about uh user ID nine so it will print nine Maria and Hardware if someone says Hey I want user ID two so it will print two ARA Big Data if I want five PR Ando and Oracle so this is what I want to do so for this we need a page right so that a user can enter the value so if you can see we already have a index.jsp page now why do we need JSP one of HTML because when you say you want to make a web page you always go for HTML right but in this case when you say you want to create a page and that should be a dynamic page because see what happens with HTML is you can make only static content with HTML oh again we if you are using JavaScript and then if you're sending a async request we can achieve Dynamic content there but imagine if you want to make a dynamic page without using JavaScript and all the stuff if you want to write Java code inside uh insert HTML is it possible can you run Java code inside HTML not exactly right so that's why we require JSP so whenever you want to create a dynamic content you have two choice you can go for salet or JSP now when you want to process something Serv is better and if you want to get a view JSP is better so suets are used to process the request so whatever a client sent a request Ser will handle that again you might be thinking why s then we can use normal Java right sulet is Java okay suet suet is a Java file the only thing is in sulet it has a special feature where it can accept a user request from from the internet right uh it's normal Java code if if I if I if I make the code it will it will make sense but then we have saet which will accept the request and we will process some information and then and we will show the data on JSP now again we have one more thing which which is called as MVC which stands for model view controller now MVC is used let's say if a user sent a request request will go to controller now controller will generate data again uh if you want to know about if you if you're new to MVC please watch the MVC video first I do have a spring I mean I do have a video on MVC do watch it first and then uh so if you know about just to make a revise revision uh we have a controller which will accept the request we have a model where it will hold the data and we have a view where it will show the data yeah so we can Implement controller with the help of suets we can Implement model with the help of a normal Java class and we can Implement view with the help of JSP so this is our JSP here and you can see we are getting some error here the error is because we don't have Tomcat linked here so we have to make sure that I do have tomcat and fortunately I do have a tomcat here can see that let me just verify if it is working so if I say tomcat start uh so tomat is there and it is and running State I guess yes it is running State you just need to connect your project with Tomcat because your project is not connected with Tomcat yet so will right click here and I will say properties and there's option of Target one time you simply have to check this thing which will make sure that your tet is linked and click on okay now this will link your tomcat and we got a tomcat there so you can see no there's no problem with JSP file now in this file I will create a form tag and I will say form action equal to and I'll mention I want to fetch the user data so I want to specifically I want to fetch the alien data because the table name is alien right so I will say get alien so that's my user request so action is get alien and here I will specify what I want to fetch so I want to fetch uh the data based on the a ID so if I mention a ID I want the complete data of the user okay and then I also need to make sure that I have uh I have a button here to to click it so I will say input uh type equal to submit yes so I will say submit here okay now when I click on the submit it should call it should fetch the data right now question arise when you click on this submit there need to be a controller who will accept your request right and that's where we have seret so let's get a simple seret here and there's one more issue I don't have a Java folder you can see that in SSC we need uh we need Java folder as well so we have to make sure that you have Java here okay not here let's let's go to Main and say I want to get a new folder I will say new uh folder and that folder name is Java folder which should be inside main okay now in this Java folder I want to create some resources so I will right click on my project I will say oh what's that and I want I want a saet right so I will simply say I want a class how maybe I'll simply say C salet so right click I will say new suet and this subet will have a name so it will have package name first I will say com. Tesco and com. Tesco do web and this class name here will be alien or I will say get alien controller yeah so I'm specifying a controller name as get alien controller I will click on next now the URL mapping so for which request I want to call this controller so the request type is get alien so whenever I use a request for get alien I want to call this controller so this is the link L this is how you link so you have a controller which is a saet and you have a request so request is get alien right and the controller is the name of the controller is get alien controller you can see the spelling difference there right so whenever are user request for get alien you can see in the HTML we are specifying get alien you you have to request get alien controller click on okay and click on finish this will create a subet for you and where's the mapping you will be saying the mapping will be done inside web INF so web INF we have web XML file this is where you have done the mapping so the mapping is done with the help of solid tag and solid mapping tag so in the sub mapping you specify for which request you want to call which controller can you see that the controller name is here the class name com. web app.get alen controller simple right so that's how it works that's how it does the mapping now if I go back to controller here we have a class we don't need all this extra thing we just need a do get method so basically basically when when you have a get request so basically we have two type of request in fact we have five but then in Ser we use to we have get request and we have post request now get is something where so if you want to fresh data from server you use get request if you want to submit data on the server you use post request so get to fetch and post to submit data here we are to fetch data right and that's why we using get request okay now once you specify get request what to do so based on the aid first of all a user is sending a ID right so if you can see in the index page a user is sending a ID how can you fetch the a ID in this seret thanksfully we have a request object so using request object you can actually fesh data so I would say request. get parameter so request is an object of HTTP subject request so we just need a method called as get parameter in which you specify hey I want data which is coming from a ID so request. get parameter a ID it will give you the data but then we have to store it somewhere right so okay I will just delete some extra RS here I will say I want to store that in a a ID variable so I will say in a ID equal to request. get parameter now unfortunately get parameter will give you a integer oh sorry it will give you a string I want integer so I will simply say integer. pass int and here I will just try to remove this yeah so you can see I have I have uh I have passed it right so you you're converting your request data into an integer format now once you got your integer I want the object of alien right again you can fetch one one data you can fetch Aid you can fetch name and you can fetch Tech so you can see the column names we have Aid name and Tech so these are the three columns in your database now you can fetch these three values but we are working with Java right and Java is purely objectoriented that means even if you want to fet this data this should be passed in the object format how do we get the object so to create the object first you need a class so in this I will create a class and this class name will be called as alien and this alien is a model so whenever you have a class which which whose object will represent some data that's your model object yeah so I would say I have alien here and this alien will have three things it will have private in a ID it will have private string a name and it will have private string uh teex so we have these three things and for these three variables I want getter and Setters so I will simply right click here I would say Source generate geta Setters and I will select all the variables here and I will click on okay now once I got this geta seters let's also use two string miror so that if you want to print this object it should print some data right so I would say uh Source two string because what will happen is if you try to print a reference reference of of a class it will print the hash code I don't want that I want the data so over at two string so whatever possible in fact I would suggest for every class over at two string that's how we do it okay so now once we got two string here let's go back to controller and now we can create object of a ID so I would say uh Alien so first create object of alien so I would say alien A1 equal to new alien let's get the object of alien no we should not be getting object of alien right because you want to F this data from database okay that's that's a thing right how do you f data from database now remember this whenever you want to work with database you need to write database steps uh jdbc steps right now jdbc has seven steps in it now if you think about those seven steps the first step is you have to import the package you have to then second step is you have to load the driver you have to create a connection object you have to create a statement object you have to process results and then you have to close the connection there are so many steps right now where to write all the steps should we write all the steps in insert a controller which is serlet that's not a good idea in sulet it should so serlet should only fetch the request or it will accept the request it should do some processing but not JC processing and then it should return the data to the view so who will do the Dy processing or who will have the all the content all the all the code and that's where we have a special layer which is called as Dow layers again it's not compulsary to go for Dow layer but that's a good practice right if you want to be a good developer if you want to create a software which will be having less bugs go follow some standards and that's where you have a dow layer so let's get a class and I will call this class as alien da which belongs to a package com. teliscope uh. web. Dow so we are putting that in a dow class or Dow package alien da so example if you have five tables so you'll be having five classes which represent those table and you'll be having five five Dow Dow classes right so that's so every class will have its own every table will have its own Dow class and in each Dow class you'll be having those four operations uh four operations yeah uh create create data or create alien read alien update alien delete alien right so cut options normally normally that's what we do so in this in this uh Alien to I'll be having a method which will return me the alien object and I will say this is get alien so this get alien will accept a a ID and it will give me the alien object that's simple so let me just create an alien object I will say alien alien and then I would say this alien or we have to import the package for this because it belongs to different package done that and it will say a do set a ID so time I I I will specify static IDs okay static values so that let's let's see if it is working or not I would say name is naen not small Capital then a do set Tech as my my favorite technology is Java always Java and then I will return the alien object of course this data should be coming from database but then let's go step by step now once we got this class let's go back to controller now the alien data will be coming from the Dow layer right and that's how that's where you require Dow object so we'll say alien Dow and I would say da object equal to new equal to new alien da yeah and let's import the package for that so you can import the package in Eclipse by saying control shift o so control shift o is used to uh you know to import the package for required packages and using this Dow now you can specify hey da get me the alien if I pass the value which is a ID so if a user says a ID one it will pass one but that doesn't matter because even if you even if you passing one you are passing static values yeah so we are doing some know uh some thing here some weird stuff anyway so once we got this alen object I want to display it but where to display that and to display this data we need to create a JSP because JSP is a place where you can actually show the data right again you can print data from here as well but creating a HTML page in serlet is a bad thing okay it's not a bad thing but then it will be damn difficult to design a page think about it let's say if you want to make a awesome website where you have lots of Graphics so each so even if you create a small page with lots of Graphics it needs around 70 80 lines of HTML tags what do you think will you write all the HTML tags in this in this s by saying out. print that's not a good idea so that's why we we requireed a separate page where you will which where you will have design and where you can put data so let's do that so in this uh in this web for in this project or in this project I will say right click I want to get a JSP page which will show my alien so I will say hey JSP show the alien so I will say show alien. JSP I will click on finish so we got a j page here which will have some layout example I will change the color I will say BG you can design your stuff I'm very bad with HTML so you can see I'm simply saying BG color equal to C that's it that's what I know about HTML and here I will print the data about alien now how do we print it oh first of all how do we call a JSP from a subet that's the first question right so I will go back to subet now how do you call a JSP now there are two ways of calling it one is dispatcher which is request dispatcher the second way is using send direct which one to use here now we can use anything but then it is always recommended that if you if you want your client to know that you are getting redirected then go for redirect otherwise if you're using dispatcher client will not even know what is happening behind the scene and that's what we want right we don't want to confuse our clients by changing the URL every time so because a client is sending request to get alien not show alien right because if I use redirect the URL will be show alien okay so what I will do here is I would say I will use the dispatcher so for I will say dispatcher and I will say Rd equal to request dispatcher is an interface and the way you can get the object of it by saying request dot get request dispatcher and in this you have to specify so in this bracket you have to specify the name of your JSP so name of the JSP is show alien. JSP yeah and you simply have to forward the request you will say rd. forward and you will pass you will forward this two objects which is request and response because you want to send so when a user send a request right user send a request to the index page right from index page user sending request to the controller that's one request the same request object will go to JSP so user should not know that you're sending two request right so that request will go to JS spin that's why we have to forward otherwise if you don't want to forward it's your choice you can go for another way as as well okay which is dispatch which is redirect way time let's go for dispatcher and now it will forward the request to JSP it works okay well let's verify in JSP I will print in show alient just to see if the flow is working yeah so we are we will be running index.jsp index.jsp will call a controller which is your seret and ser will call JSP let's verify right click run as run on server it will I guess it will run on my Firefox maybe let me just remove this MVC from here because that's not required click on finish uh my browsers are working so very slow today yeah so we can see it is asking for the aid I would say 101 I would click on submit and you can see we it is printing a JC page remember the page color which is C color and then uh see the request request is still get alien even if you're calling a page which is get uh which is show alien the you you R is get alen because we're using dispatcher so a client don't even know which page is getting called that's the beauty yeah but if you're using redirect then you it will it will show the page name okay so I will stick with dispatcher Salvage but how do we pass this object with alien here so I want to pass this alien A1 object to JSP and the way you can do that is by using the request object because in forward you are sending request object right so what if you can simply add your data in the request object it's something like you know let's say if from us if your friend is coming to your place let's say so if I'm staying in India now so let's say one of my friend is coming from us to India and if I want to order something from us he can carry it right so he's not coming to India just to give me the uh just to give that thing to me but he's coming to India for his own work but then I'm asking him to take that thing for me that's the same thing we can do here the forward is sending the request object right so we can put that data into request object and send it so I would say request dot set attribute and I will have a name so I will say I'm sending the alien data I'm sending alien and the object is A1 so request. set attribute we are sending alien which is a A1 here and in JSP you just need to fetch it now you can write Java code here right so to write Java code inside JSP we simply use this uh this angular backet in percentage this is called as scet yeah so in JSP we have this we have declaration tag we have scriptlet tag we have different tags available but if you want to Java code inside this we can simply use scriptlet tag and I would say I'm I'm fetching the object of alien which is A1 here but how do you fetch it so you simply say request. get attribute and you specify hey I want to fetch alien now if you remember in sulet in sulet we have set we have set the attribute in JSP we are getting the attribute with the name Alien same thing right and here we'll print the A1 object oh not this way we'll say out do print Len I will say A1 okay I guess we have to import the package now this alien Pack alien is a class which belongs to package model so you simply say control space and that's the package you can see import package comes something like this this is how you import packages in JSP not like normal normal Java code where you write import space and package name you have to say import equal to because page is an uh tag for you now and this becomes a attribute okay we are getting some error here because we have to typ cast it because get attribute will give you object of object I want object of alien so we just have typ casted it it should work now right it should it should print data at least some data right so let's restart the server and let me just go back to my page here refresh and I will enter data which is 101 I will say submit can you see that we got our data we got alien a ID is equal to 101 name naven and Tech Java but hold on this is this is static data right this is not coming from from database so how do we connect to database here so if you want to connect to database you just need to use Dow layer right because this this is where you are fake you are faking this data you just you don't need to create a dumy data you want to fet it from database so let's remove this or maybe you can just have it there that's fine what I want is I want to write datab code here now to work with database we need to follow certain steps right the first step you have to import the package so I will go here I will say import java.sql do star that's how you import the package uh and then the next step is you have to create the connection object now I would say I would simply create a connection object outside the method or let's let's do that inside just just for time being I would say try catch because it might generate errors so I will say try catch and then here we'll write all those statements we'll say say uh first the first step is we have to say class. for name and here we have to mention com do mas. jdbc do driver yeah so this is how you load your driver so for my we have this string for Oracle we have a different string from po have a different string so you have to just go to Google and search for the uh the driver name for different different drivers different different dbms that's the first step we got your class. forame next is you have to get a connection object so I will say connection con equal to now connection is interface and the way you can create object of it by saying driver manager do get connection and here we have to pass three parameters so in this get connection we have to pass three parameters the first one is the URL the second one is the username and third is password so password is root and the username is also good for MySQL for my machine and the URL is jdbc colon myql colon slash Local Host colon 3306 slash database name is naen I know it's a big string right but then this is called as URL which is which is used to connect with database so jdbc colon m/ Local Host colon 3306 noven now once we have done with the connection object we need to create statement object because let's say if you want to call someone you uh you create the connection first you dial the number you connect and then you think what to say and then you say it right so that thinking is statement right so let's get a statement object I will say statement St equal to con do create statement now you are you are saying you you are you'll be saying something and how to say so you will say st. execute query and you will find the query here what's the query for us it is Select star from Alien that's the uh query you have but then this query is not complete because we have to specify the ID as well right because I don't want all data I want only one so I would say where a ID is equal to plus I'll mention a ID simple and then this execute query will return you the object of Al not this will not return object of alien right it will return the object of result set because statement object Returns the object of result set so I would say result set RS equal to so is set an interface so the moment you file a query it will not give you data right it will give you a table structure and that will be stored inside result set and now from result set you can say so we so we have to Traverse between different rows right so if you get five rows how do you specify which row to pick up and that's how you that's why we use ar. nextext so. nextt is basically to shift your pointer between different rules and we are not even sure if you have a next rowle and that's why we'll say if so that we can avoid the exceptions if you have a next row because next will also give you a return return it will return bullion value if you have a row if you have a row then what we will do is we'll fetch one one value and how do you fetch so we have so we will fetch and we will assign to the alien object we'll say a do set a ID now this data is coming from visite so we'll say rs. get in the First Column is a ID so we have to mention column name so when I say get in you have to mention the column name we'll say a do set a name I would say rs. get string because a name is string format I would say this is a name you can also use column numbers if you want 1 2 3 or you can use column names so to be on safe side always use column name yeah it is safe right you know what columns you're fetching you don't have to remember the sequence of uh columns and once we got it that's it we don't even need to specify the static value because data will be coming from database now these are the steps oh one more important step you have to close it but that's fine you can do it in your code and after doing all this thing let me just verify if it is working I will right click here I will say run I will say go back and refresh and this will say nine I will say submit you can see we got nine Maria and Hardware if I say five submit you can see we got five PR and Oracle if I say one you can say we got naven Java so it's working right so we have done with uh mavin jdbc salet and JSP in one code so this is how it works awesome right again this video is not to not for uh beginners this was for people who have already done JSP salet and just they want to revise it or uh if you just want to see this stuff you know some people have the Curiosity of how I mean what exactly it is you know so let's say if you're learning code Java and someone says hey you need to learn s JSP in future and they're not sure what they are what they're going to learn so this video will be helpful for them as I mentioned in the as I mentioned earlier so it's that awesome now once the once we once you once you watch the video check the description area where I where you have a we have a where you have a link uh so I have uploaded this project on GitHub so you will find the link in in the description area and you can add your own own code now so assignment for you after this after this after after doing this code on your machine is uh let's say I also want to create a new alien because I'm just fetching an alien right what if I want to create new alien so in this I have 101 carel Java right let's say if I want to have one more one2 um let's say Rika and some technology maybe let's say python again so in that case you need to create a alien and where do you specify the data so you have to create a page where you will it will ask you for three values when you click on submit it will go to a controller and controller will use a dow layer to save the alien right create a new method called as save alien here and write the same steps and save it in the in the uh thing in fact the moment I will when I get some time I will I will upload that as well so you can check the description you will check the uh link it may be or may not be available when you when you check this video when you check the link okay so that's it I hope you enjoyed the entire session let me just go for a quick recap what we have done till now so first you have a index page where you're asking for the uh Aid when you click on submit request goes to the subet and this is how you create a solet you create a class an extend which should be solv it you have a dog get method here yeah and then in dog get you fetching the aid which is coming from client you are using a dow layer to fet data and from Dow you're saying get alien by passing that Aid and that same data you're assigning to A1 there's one more thing we can do here you know if you if you don't want to use equid dispatcher you can also use redirect so the way you do that is by saying response do send redirect and you can specify the name which is show alien. JSP so we can specify this as well but the thing is if you if you're using this one what will happen is you cannot use request object because a client will send a new request when I say client I'm talking about browser here so in total you're sending two request one to controller and second to the JSP as well so in redirect your response goes to the client again and from client it goes to the show show alien so it is a new request object so this will not work so this request will not work in that case you have to use HTTP session so you have to work with session management so we have cookies we have uh we URL rewriting or you can also use HTP session session is the best thing I feel so I will say session uh session equal to and the way you can get the hold of session is by saying request. get session and now you have to set the attribute in session not in request object so I will say do set attribute the same thing alien and A1 you're passing A1 so nothing changed the only thing is instead of using request object using session object now because it doesn't matter how many request you send to the server now the session will be maintained and in show alien instead of fetching from request object you have to use session object cool let's run this code let's verify it is working I will as or not let's let's restart the server that will be enough let's go back to the page and refresh let's enter one again I will say submit now look at the look at the URL you can see now you're calling show show alien. JSP in the earlier case it was get alen because since we are using redirect it is chaining the URL as well so as a client now I know that I I got redirected to a different URL that's the beauty about send direct yeah so that's how you use session object in this video we'll see how to upload of how to upload files on the website so let's say if if you're if you're making a website in Java so you'll be using S JSP for the server Technologies right so in this video we'll see how to upload multiple files on the server okay so it will be useful for your website where you where you want to upload multiple files maybe you want to upload a photo maybe multiple photos so let's see how to to do that so of course when you when you say you want to build a website you need a project right so what we'll do is we'll start with the project and we'll send new and we'll go to others now of course we can make a simple application I mean we can make a simple web application or uh in this in this video I'll be using a maven uh Maven project again you can go for simple application you can go for you know you can simply search for dynamic web page or dynamic web project so you can go with this and the thing is in this project we'll be needing a a jar file so now why we need a jar file to upload a file on the server so Apache provides you a jar file a library to do that okay so you have to you have to make a project and then you have to download the jar file from the website or from the Google other instead of that what I will do is I will create a mavin project because when you make a mavin project it is easier to maintain your libraries right so I'll be going for a mavin project is not that difficult uh you can choose either Dynamic project or mavin project so we'll select next and we'll say next now here uh whenever you work with mavin you have to select the type of project you're working with the arch type uh I will select mavin Arch type web app okay we'll say next now if you don't know how to work with mavin if you want to learn about this again on my channel I have a mavin video so you can just search for M tutorial on telesco learnings you will get that uh I will create I will make a grou group ID which is our package name we'll say com do teliscope and we'll make an artifact ID as file demo or file upload demo so my project name is file upload demo and we'll say finish now once you click on finish you will get a project on the left hand side you can see we got our project here which is file upload demo and in this thing the first thing we need is the library right of course when you work with uh file upload you need a library which is Apache common file upload so there are multiple ways you can do that you can download the J from from Google you can just go to Google and search for in fact let's do that let's let me show you how how that works if we go to Google and search for common file uh what is the spelling of common so it is common file upload okay it's upload so if you can uh search for this okay this is the this is the library you need okay you can download a jar file from here okay in fact they also have a code for you that's good so you can download J file from here but I'll I will not be using uh this common file upload J file from Apache I'll be using uh from Maven so I will say so how to do that just double click on the P file which is Project object uh project object model file and you can see there's a option of dependencies here and you can say um maybe add and search for I hope it will work here otherwise we have different way so search for common file upload it should take it up if your net is working there's something wrong here indexing is disable that's weird uh Commons no that not this one file upload that's my project not this one okay this is not working here so what what we can do is we can go to P XML file and here we can add the dependency now you can get this dependency from mavin itself so you can just go to mavin website so you can see Maven repository common file upload so when you search for common file upload on Google it will give you Maven option also uh you can download any version I will prefer 1.3 which is a stable version and you can just copy this file in fact you can also download the J file from mavin I prefer mavin in fact lots of Industry people they prefer mavin project and let's paste it here when you say save you can see it is somewhere in the Java resources there should be libraries and there should be M dependencies and you got your file there can you see that we got Commons file upload and commons. iio or common hyon IO so we got this two libraries now once you got this now what next now once you got the r file let's create a web page from where you can upload the files okay so let me just go to um okay let me create a web page so we'll say right click I should already get a web page here do we have a web page buil okay we we already got a index page uh there's one more issue with the project uh it it is see whenever you create a jsv page or solet page we need something called as uh a tomcat server right again Tomcat or Joss whatever is preferable let me assign a tomcat server here how to do that how to assign a tomcat server right click on the project and say properties there should be something called as servers here you see that or server or runtime is the option Target runtime so you have search for Target one time and select Apache Tomcat 8.0 if you don't have that then you have to download Tomcat you have to click on new and then you have to select the particular version you want to work with Apache then specify the version and click on download it will download from the internet but since I already have Apache Tomcat here we can also use Joss but I have Apache lightweight click on okay and you can see all the erors are gone because it was searching for the tomcat and we got Tomcat now what else I need here so once we got this page um okay hold on so once we got this page let's create uh the page I mean the create let's create the required page here so in the body tag what I will do is I will create a form tag okay let me because I want to upload a file right so when you want to send something on the server we need to create a form tag and in form we say action this action will call something called as upload URL that's upload and they type of method we'll be using is post because you want to send data to server and in this let's create input type so when normally when you create input type uh when you want to send text we use input type text right but since we are to work with file so we have to say we have to specify input type file now when you say input type file it will give you a option of Select I mean you can browse your files here using type file we can also mention name we'll say file itself and that's it that's your input file type and we also need to send data to server so we'll say submit button so that's submit I guess that's it uh that's what that's what we need let's right click and let's see if we are getting this we are getting something which we need we say run on server I will select Tom catch next I will click let me remove the old project from here let me just go with the new project and when I say finish you can see it will run and it will run run on the server which is Tomcat and we got the output here so you can see that we got a page which says hello world and it is giving me option of choose files now why we're getting option of this choose file is because we are writing a file here right so when you say type file it will give you option of choose file and if you can you can choose the files from here you can select any file I already have Java demo do Java file if I click on open you can see it says demo. Java I can if I say submit oh there's error because we have not created this server site yet but I need one more thing here when you say choose file I want choose files it should it should it should select multiple files here and for that what we'll do is we'll simply say multiple so multiple is attribute using which you can achieve multiple files so you can see we are having choose file if I refresh this page we got option of choose files and if I select this I can actually select multiple files now uh can I do that let me select multiple files do have do I have multiple files here uh so we got this one and this one if I click on open so you can see it says two files okay that's it that's on the client side but hold on there's one little issue here when you send data when you send files on the server you have to also mention the encoding type okay so when you send files we have to mention the encoding type and inside encoding type you have to mention I'm sending a multi-art document so it is multi part so to say multiart document and multiart and form data so this is this type of of data you want to pass it's multiart form data okay so now once you click on submit it will call this URL which is upload and on that upload serlet you'll be sending all these files so clients are clients are done let's work on the server side now and on the server side we'll be creating a serlet so we'll right click we'll send new and do we have option of seret here yes we have a subet and let me mention a package name as com. Tesco itself and the class name I will say is file upload okay so we got file upload which extends HTTP servlet anything else here nothing so that's my URL mapping next do we need any method here we'll only need one method which is do post I don't need to get we click on finish now you can see once we say that we we get all the extra files we don't need extra things here I like to clip my uh C my uh you know subet clean I don't need extra things here do that let me just remove extra thing I'm not a big fan of writing cly after the method name again we have two types of people in the world right one who puts cly backets after the method and one who puts below below below the method definition okay so once we have done that let's see our web.xml file is it mapping properly or not let me just go to source so mapping is done there's no problem with the mapping uh if it is not done automatically in your machine you can type it manually you can just see this code here here and you can type it manually by pausing the video but since it is done on the my and it is done on my on my application I'm directly continue with the seress S now what will happen when you send all these files so let's say if you're selecting three files now this three files when you click on submit it will call this file upload seret right and on this on this side we have to first check uh how do we check that is it so see when what whenever you send data it send it gets in inside this request object right any any data for the matter so when you set when you send data like let's say you want to add two numbers so those two numbers can I mean you will get those data in this request object so in fact your files will be also coming in this request object now how do you verify that you're getting actual files so we can say if okay we'll do that uh you know those stuff checking stuff later we can also do that later so let's say if I'm sending multiple files to to fetch those multiple files I will fetch those data and will put that data a list list of I mean collection of files right so when you say you sending multiple files we'll be saying list and the the type of data you'll be getting is something called as file item now file item is a inbuilt class inside your uh common folder Apache Commons it's not working here uh okay we need to we to import that manually it should work let me expand the library which we have downloaded it is common file upload so it should be somewhere inside disk if I'm not wrong so if I expand this or it's not there here if expand this one okay can you see that file item so we need to import this package it is not getting imported automatically so we have to do it manually sometime it happens you know sometime your Eclipse doesn't support what you want to do so you have to do it manually so you have to say o. Apache do Commons dot file upload and inside this file upload we have uh this file which is file file item right and now you can see there's no error it was not giving any error before and now we'll say our data will be coming in we can go we can use any uh any name so we'll say file I mean list of file items so every file will will be called as file item and we have we creating list of that we a new now how do you fetch values so for sure our data is inside request object right so this is the object request object in which you have all your data but we need to fetch it we cannot directly use in that way right we cannot say request it will give you all the data so the the way you can fetch it is with the help of object of request or seret file upload so before that we have to create object of subet file upload now the awesome thing is Ser file upload also comes from Apache I mean these Commons file Commons uh file upload Library so you can see the importance of this file I mean this jar file okay so if you to do this upload part you need to use common file upload and we need to create object of this first we'll say SF for file upload equal to new sulet file upload so we'll say sulet uh my my machine is not working so fast today sub file upload and we'll say semicolon now this sub file upload in in this we have to pass the object we have to pass the object of disk file item Factory so this file item Factory that's the Constructor we have to pass item Factory yes so we have to pass the object here now once you got the object of SF okay so once we got the object of subate file upload which is required to pass your request so we'll say SF dot so in this we already have a method called as pass request can you see that we got pass request and request so what this pass request will do is it will return you so if you see the Declaration of pass request which is inside sub file upload again how to check the documentation I normally have this habit of checking the documentation of everything so if you if you want to check the documentation just click on pass request it will give you documentation which accepts the request object it will give you the list of file items and that's what we want right that's what we wanted and we got that so we got list of file items now once you got that what next now what we'll do is once we got the list of item we can run a for Loop right we can run a for Loop to fetch each item so we'll say file item I hope you're familiar with uh enhanced for Loop if you're not just go to my channel again and search for enhanced for Loop so I'm using I'm using this multip multiple files and we'll fetch one one file so it will first fetch the I mean it will first it will fetch the first item and it will be inside item class or item object and let's let's write that item so we can simply say it as item dot now how do you how do you write the item on the server side we'll say item. write and inside this you have to mention the file name now which file name I'm talking about uh I'm talking about the file you have to mention the file object here we'll say file and inside this you have to specify the path where you want to store the data uh okay good question where I where I want to store data so I want to store the data inside my where's my project this is my project uh that's my project area this is this is where I want to upload the file and how will I get this uh path so I will go to CMD that's the best way I mean the terminal if you're working with Windows you can go with command prompt since I'm working with uh let me just say clear okay so now I'm here in this command prompt I will say command document and I'm inside my project which is file or not I have to get into my uh workspace which is telescope and inside that I have to search for a project which is file upload and if I say LS now you can see we got this three files so this is this is the place I to upload the file so I will get the working directory this is the path I want to upload so copy go back to the project and simply say this is the path I want to upload my file path slash that's the path plus I want to specify the name of the file and how can you fetch the name of the file which your which a user is uploading we'll say item dot get name this should work even if it's not working we'll do the debugging part and I guess that's it if you can do that your file will be uploaded on server so take one one file and put all your files uh on the server um it should work and once we have done that let's print a message um how do you print a message now that's weird we'll say response do let me print the message on server itself we'll say file uploaded again we'll we'll do that how to upload that how to print that data on the on the client side and we I guess it everything is everything should work now if I run this code on the Tomcat server like we don't need to run this it's already running St it's already running state right anyway so I'm running this part and now it is asking asking for the files hope so it will work let me select multiple files here let me just upload first file here which is demo. Java upload submit oh it is giving you an error it is not able to find the upload part that's weird we are saying upload oh we should be saying we need to change that in the XML file right because I'm saying upload and in XML the mapping is done with something else let me check what's the mapping here oh mapping is done with file upload I have to say upload okay so now let's me just let me just restart my server I will say where's my server it's here this is Tomcat restart okay there's always some type of Errors which is coming on the way let's refresh let me select Java again uh demo. Java and if I say open submit oh now we got one more error it says class not found exception it is not able to find file upload that's weird it is there it's here and it's here let me just verify once what is the issue again go back to to the XML file I guess there's something wrong with XML file here everything seems good there's nothing wrong let me remove this extra part discom do telescope is it the package name which we are working with okay so the problem is we are putting that into a resource folder right it should not be inside resource folder it should be Java folder let me create one more folder here this sometime this is what happens when you work with mavin sometime so we need to move this into Java so it should be inside Java and you can see it is giving me some errors now let me import the package for list okay because we need to also input the package for list right and it is giving me an error because it it is it may throw an exception is that the issue yes it it it is it is trying to throw an exception let's write everything inside our try catch so we say try and catch say exception e and let me just print the exception here okay now let me just format this format this thing just to make it look better and formatting is done I think everything is done now let's refresh choose Java file it's here and says submit and we got one more error we are always getting some sort of Errors let me just I think we have to restart the server it's not able to find that again let me just restart it once again okay refresh choose the file is the file now Dem Java submit and you can see we have uh done that there's no errors now because anyway we are not getting a text because we're not printing and stuff let me just go to the console it says file uploaded let's verify let me just go to my project you can see we got demo. Java so we are able to upload a demo. Java file uh let me just upload let me just run this once again by uploading more files and let me remove this demo. jav for now I want to upload more files now delete let me choose more files uh um we'll choose this one AWS PDF we'll choose code. JSP we'll choose demo. Java demo. JSP and display contact. zip we uploading file files submit it will take some time of course and you can see we got this five files and that's how you can upload the files on the server side simple so it's that simple to work with this uh this file upload that's it that's it from this video let me know if you have any queries thanks for subscribing welcome back aliens my name is D readyy and in this course we are going to talk about hibernate now when you learn a new technology there are four things important the first one why do we need that technology second one what it is the third one how to implement it and fourth one the advanced concepts and in this course we're going to talk about everything so initially we'll start with prerequisites that will move towards why do we need it how to implement that and then we'll talk about some advanced concepts as well so just to give you a brief idea about hynet hibernate is one of the best omm framework available for Java now when you say o RM what it is it stands for object relational mapping now if you think about this software world or the IT world of course we have softwares on the other hand we have data the main thing is data right and to access that data maybe when I say access we want to fetch data we want to save data we want to modify data and to do that we write applications and that application can be return using any language let's say shop Java python your choice now if you work with this languages and if you want to connect them with help of database that's where we need connectors right example if you talk about Java and let's say my database you need a connector and that can be implemented with the help of jdbc so you as a programmer you have to write the application let's say if you using Java you have to think in object terms write everything is object in fact in python as well you do that so you write object objects we write classes and then on the other hand in databases we have tables right I'm talking about the relation databases so you have tables and in tables you have rows and columns now to connect that we write SQL queries right so from the object world or with the help of application you write SQL queries and that get fired to work with database what if you don't have to write SQL queries and that's where this orm Frameworks are quite famous So based on your classes based on your how many objects you create it will Define your tables and data and that's fascinating right and how do we do that that we'll be seeing in this course so I hope you are excited make sure that you hit the like button and you subscribe your channel so that you will get more updates on this so you should be knowing two things before starting with hynet first obviously Java right so if you don't know Java you will find it very difficult to understand what I'm talking about those object stuff those oops Concepts so C Java concept is very important for hynet uh because we'll be dealing with phos which is objects we'll be dealing with inheritance we'll be dealing with uh for Loop and all those stuff so your code Java concept should be should be good next thing you need is the SQL concept because see what we are doing in hypernet we are working with data right so we are fetching the data from the application and we are storing that data into database so so we have the application here and then we'll be having our database here so of course you should be knowing this part as well as the database part I mean not just Basics I mean let's say if you know about select query if you know about uh uh table column that's not enough you should also know what is joining table means uh how do we create primary key foreign key how to normalize a table and all those stuff so you should be good with SQL plus it should be good with 's Concept because here also in your application we'll be working with some classes and one class will be creating object in the other class we we need to pass the object from one class to other class we need to create the Constructor we have to work with interfaces right so on the Java side you should be good with code Java on the database side you should be good with SQL how your database all those thing works now if you're good with this thing you need one more thing which is the connectivity between this two you should know how jdbc works so these are some with jdbc so this this is some prerequisite for hypernet so if you know this thing you're good to go for the further to tutorial but in case if you don't know this stuff that's very easy so in the description section you can see some links one link is for Co Java so again there are there are normally even very less videos for kjva it is only 181 videos and you will learn kjva from that you don't have to watch everything for that you can watch till interface right and for my skill also I have some videos in the links in the description you can watch those videos are not everything till you know how to create praman foreign key and then for jdbc you have one you have to watch two videos again you will find that in description area Okay so these are the prerequisite for hypernet and after doing this you're good to go for hynet but in case if you know this continue with the next video thank you so much in this video we are going to talk about hypernet now when you talk about hypernet it's a orm tool which is used for persistent data now again just to understand those terms let me an example here now we are living in a world where we make different softwares right and we can build all these softwares with the help of a language right one of the language which we normally use to make applications is Java right and when you make a software that software will be working with data of course now the data can be a normal variable or it can be objects So when you say normal variable I'm talking about primitive values uh let's say int float string and string but in float uh double all those values or you might be having your data in the object format and when I say object format let's say you have a student object maybe you have an employee object and every object will have some data example let's say I have a class here so I have a class here and the class name is let's say student and in this student class I have certain variables let's say I have a role number then we'll be having name and let's say we have marks so in this student class we have these three fields we have name we have role number name and marks now if I create object of student it will be having three V it will be having three variables right which is name role number and marks so that will be one object so if I create object of student now it will be having three values I want to make sure that those values are getting stored somewhere because let's say if you talk about your bank account so when you when you deposit money in your bank and that data is stored somewhere right that's you have deposit $10,000 let's say if one day your bank says hey I don't know we lost our database we lost our dat we lost all your data I me you will not be happy right so of course when you work with data you want to show it somewhere and that somewhere is your database now this database can be anything it can be MySQL it can be Oracle it can be SQL Server maybe post maybe Derby where we have different dbms to work with right so we we use database so that we can store data and that concept of storing data is called as persistence right uh because by default when you talk about variables those are transient which means we we we normally don't store data in inside database directly so what we do is we use we we create variables we create objects and those are temporary data if you want to store that data we need to use databases right any of this now to normally to connect your Java application with database we use a concept of J DB C which stands for Java database connectivity right so we use jdbc to connect your Java application with database and the moment you say you're working with jdbc and you have a database so for sure we need to use something called as SQL language right and not everyone is comfortable with SQL in fact uh people who only learn Java they might not be comfortable with SQL and and the moment you say hey you want to work with database now you need to know SQL so we wanted a solution where you can store the data into database but without using SQL I I mean as a programmer I don't want to use SQL query that's the first concern the second one is if I have an object which is let's say if I have an object which is obj and this obj object is of student which will have three values I mean role number let's say RO number one the name let's say naven and let's say marks equal to 80 I want to store this marks into database right or this this data into database so to work with SQL I mean I hope you're familiar with jdbc what we normally do is we write an SQL query of course in jdbc we have to follow seven steps and then we have to write SQL query we have to say insert into student then values and then we we'll we'll provide the values here right so again we have to write SQL query what if what if in Java if you we have this object obj here if you can directly store the data inside database simply by saying hey uh Hey a hey framework or any framework save my object that's it the moment you say save obj your work is done what if you have a student object and if you're saying save obj and that data is getting stored inside database your job is done right but how it's possible can we store an object into database directly because normally when you say database I'm actually talking about R DB Ms type of databases which is relational databases where you have rows and columns how can you convert uh an object into table now for that you have to understand a concept of O RM which stands for object relational mapping okay so we'll be having an object we'll be having we'll be having relational which is your tables and then we have to map it now how it is possible now if you talk about the class structure now this class structure is same as table structure right example if I create a table structure here so let's say we have a table structure here and in this table what we have is so this this is your student table and in this you'll be having three three columns the First Column is the role number the second column is the name and the third column is the marks right so we have these three columns so this table this class so this class represents your table right and the object let's say we have the first object as obj1 so this obj1 will be your first row let's say we have one more object which is obj2 now let me just do that here we'll say obj2 and it will have let's say two uh then we'll be having name as let's say arti and let's say marks is 90 here so that will be your second row so if you have five objects you have five rows so one class is one table so class is the table your objects is your rowes so if you have five objects you'll be having five rows right that means we are trying to create a relationship between the object oriented programming Concepts and the relational database system and that is possible with the help of O RM which is object so that is your Java Concepts relational that is your database Concepts and we have to map it okay again it doesn't is not that simple the way it looks on the paper but this is what o RM means now the problem is O RM is just a concept how can you implement om so to implement omm we need to use some tools which is O RM tools and one of the tool of om is your hnet again hiber net is not the only tool we have we have I Bettis then we have what more we have jpa which is the standards and then we can also use one of the tool from from Oracle which is top link okay so we have IES we have top link we have hynet so these are your omm tools right so using hypernet what you can do is you can store your objects into database directly with the help of save method okay now question arise how do we use a save method it's very simple to use Save method we need to create an object of session okay so we can say session object which is let's say s so session object is s so we can say s Dove okay but hold on how do we create session for that we need to create object of session Factory because using session Factory we can we can we can create sessions and in session Factory you have to provide those details the configuration for your database example when you work with jdbc you might remember that we use we certain steps right we have to provide the driver name we have to specify the URL right so you have to configure all those things with the help of session Factory again there are multiple ways of configuration we can use XML way of configuration we can use Java configuration way right there are different ways we can do that so we can we have XML we have Java configuration right so we have to provide your driver name we have to provide your your database uh your database URL which is the connection string you have to provide username and password in the configuration file so that you can load that inside the session Factory object and once you got session Factory object we can work with session and using session we can save the object not just saving we can also use uh we can also use get method to fetch the value to fetch the data right so we have save we have get method so we can use all this concept to implement hibernate and trust me it is one of the awesome tool available which is om and again om is not just you know a technology which is which which came accidentally it was well planned uh so if you want if you are making an application where you want to purs data this is something which you can actually use okay again in the next video we'll be talking about the Practical implementation of this how exactly it works uh so I hope you have liked the session make sure you go to my Channel or subscribe the channel and like this video if you liked it and that's it that's about hynet Basics and in this video we'll talk about hibernate implementation so for to implement this in fact in the last video we have seen the theory of it right so again to implement this we can use any IDE we can use Eclipse we can use net beans we can use intell J for this video I'm using Eclipse okay now when you create project in Eclipse we have multiple ways right we can create a normal project or we can create a maven project now the advantage of using Maven project is you know it will give you the project structure and since we are using hypernet here you have to use certain libraries for hypernet and to use those libraries we need to uh we need to download those libraries from the internet and then you have to add it right instead of that if you can use Maven it will do that it will do that task for you okay now let's see how to do it so uh again if you don't know about mavin you can watch my video on my channel so you can just go to telis Learning Channel and search for mavin video and you can watch that if you're not familiar with mavin so what I will do is I will click on new and I will select where's mavin so we can see we got Maven project here and we'll send next now there are multiple ways you can do this you can select uh so you can go for internal and you can select uh Maven AR type quick start which is coj application if you're making a website you can select a web app here but let's say if you if you want all this thing to be configured automatically you can go for all cataloges so that you don't have to select uh you don't have to do any anything for hynet you will getting you'll be getting everything for which is prebuilt so I will select hynet here and you can see we are getting MVC spring hynet project so in this you'll be getting spring hybernate pre-configured and since this is your first video for hybernate will not do that we'll go for internal and we'll select quick start say quick start that's your quick start and click on next now since we are going for quick start which means we are making a code Java application so we'll say com. telescope and the artifact which is the which is your project name I will give as demo hyp we click on finish now once you do that it will give you a project you can see we got a project here which is a MAV project now what we are going to do is we will be using our mql and inside my myql I have a database here which is neon so you can see I'm using that and in this database neon I don't have I I just have one table I guess so we we have a student table here not no other table here so what I do is from my application I'll be having okay let's go back to the application here and we already have app. Java which has a main function let's see if this this app is running or not so you can see we got hello world that means this project is working but what I want to do is I want to create a class and we'll say the class name as let's say alien because I promote aliens here we'll click on finish now in this alien class I want to have some variabl so let's say I will create private in RO number or not role number alien will not have a role number right they will have alien ID then we say private in a name that's your that's the alien name and then uh as you can as you know that all this aliens have a different color if you watch Hollywood movies or or Bollywood movies you can see alien in different colors so I will say string color okay now once we got these three variables we also need uh geta sets for those things so we'll say right click and say Source we have gets and sets and we'll click all the three we'll click on okay you can see we got our Getters and Setters now this is your bean right so we got a alien Bean here or we can also call this as pojo now people are familiar with pojos right okay so once we got our alien let's get back to our app where you will create an object of alien we say alien obj equal to not alien obj we say alien okay let me just go with a name here let me create alien of myself let's say nain maybe teliscope will be better right I don't give us space for not for not promoting my thing so we'll say alien now once you got the object of alien here which is telescope I will add some values to it so say telesco and you must have seen the logo for alien right telesco Tesco logo so we'll say set a ID we'll say 101 then Tesco do set name uh we'll say this is naven and we'll say telescope. set color and our logo is in green color right so we'll say green okay now once we have set all these values now what I want is I want to persist this data when I say persist it means I want to store this data inside a database because this data is inside your code application not inside your database now how do we do that now if you're familiar with code Java things or if you're familiar with jdbc what we normally do is we write jdbc steps those seven steps which you love uh import the package then all those stuff now once you have done with the import statement once you have done with the connection once you have done with the statement object and then then you can write the query which is insert into a table which is alien then you have to specify the values which is is one1 naen and green so you have to divide those you have to isolate this values now as a Java professionals you don't feel the those things very comfortable right writing SQL queries what if someone says hey what you can do is you can use a method which is save and you can simply pass Tesco there and that's it your work is done once you do this your data which is telesco where you have one1 naen and green will be going inside database okay uh you might be feeling this that's not true right but hold on we will do this and it will work so when you say save this data will be stored inside database was it is it that simple uh not exactly because see when you when you use Save method we have to specify this method belongs to which class of interface right that means we have to create object object of it first let's do that but hold on uh which interface or class this save belongs to so this save method belongs to a interface called a session and this session interface belongs to hybernate framework so if I say control space you can see we are not getting any option because we don't have any Library you can see I have m dependen is here I don't have any Library here which which is of hypernet so we cannot do that right so first you have to add these dependencies we have to add dependency for hibernate and MySQL both so we'll open our pom.xml file and we'll move towards your P XML file here where you have your code we have to add two dependencies now I'm not a Superman here to remember all the dependencies right so the best way is to go to Maven repository the official website and search for the hibernate dependency so you have to go for uh hypernet omm functionality and the version I will working here is 4.1.6 because that is what I've tested so you can use that again we have different version latest one is 5.x you can also use that it has some new features which if you want to explore but 4.1 will will do for us so just have to paste it here that dependency and you can see we got all the dependencies here now since I'm doing this project for multiple times so you can I I was having all these libraries in my local repository if you're doing this for the first time it will take some time for you so don't be tensed if you take it is taking long long time for you we also need a myql connector right so we'll say m SQL connector and we'll select 5.1. 36 or 35 which is not 35 I'll be using 1 Point okay 5.38 does the maximum me use okay and let's copy this let's go back here and paste you can see we also got my connector here now once you got these dependencies let get back get back to our app and if I say control space now you can see we got an option of o. hypernet so that's the package we have to use which say session object as session equal to the problem is if I click on session here you can see session is a interface that means we cannot create object of session right but we have another way we have to search for the class which implements session again we don't have any class which implements session I mean I don't know any class which implements session so there's another another way of doing this what you can do is first of all let me just write session here session. save now we have to create object of session right for that we need to create object of session Factory now this is something like a factory design pattern where let's say if you want to buy a phone where do you buy a phone in a shop right and where that phone is built or created is it is inside the factory right so that is session Factory so we'll say session Factory SF equal to uh again we have a Twist we have to UT the package first that's done if I go to session Factory documentation again it's interface that means we cannot create object of session Factory okay yeah before going for before going ahead I I would like to tell you the founder of hnet which is Gavin King you can see that he is the founder of hnet here let's get back now we cannot create object of session Factory right now how do we do that so again we have to create one more object here which is of configuration because configuration class it's a class okay finally uh not that package I have to import the package with hypernet which is org. hypernet CFG doc configuration so if I go to configuration finally we got a class okay no more so no more uh interfaces right now once we got this once we got configuration let's create object of it we'll say configur configuration con equal to new configuration okay and now once we got configuration con equal to new configuration we'll say con dot uh okay so it is build session Factory now once we got the object of build s Factory uh again we you can see we are getting a line here which is which simply means that this method is deprecated okay that means we we have to use some other method but just for timeing we'll use this method in the next video we'll go for some other way now we'll say SF dot uh Open Session because Open session is a method which will give you object of session now once we got that now everything is in place and that's perfect right now you can see see there's no error and we are expecting that this should be stored inside the database let's try if we run this code we got an error it says cannot connect with B null when hybernate dialect not set hold on what is asking for what is di now the problem is your hypernet is is asking you hey you have not provided the properties what properties they want so first your hibernate wants to know which dbms to connect with because it it may happen that in your machine you have multiple dbms you have masq you have Oracle which dbms to connect with second in your database let's say when if you specify myql in myql you'll be having multiple databases right you have to also specify database you have to specify the username password as well and we have not provided any of those now how do we do that that we'll be saying in the next video right so there's some suspense how it this will work that we'll see in the next video so before going for the next video we have to make sure that we uh we have a hybd net plug-in in your system in your Eclipse especially now why we need hypernet plugin is because Eclipse provide you some some features using which you can create the configuration for hynet okay in the next video will will require hynet things so what you have to do is go to your help menu in your uh menu bar and go to uh okay so Eclipse Marketplace and this you have to search for hypernet luckily I have uh I have the that tool installed okay it will take some time and let's search for hybernate here we say hybernate and you can see you will get an option now okay it's taking again some time so now if I if I scroll here and you can see there is something called as jbos tools again this this has hynet and you just have to install this while installing it will ask you for the available options you can see it has Maven it has mobile devices JSF we don't have to check everything you have to only check you have to only only go for hynet if you've done that you will get the hynet plugin in your system so again you have to you have to install this plugin which is JBoss tools not all the tools when you when you're installing it it will it will ask you for the number of available tools you have to check the hybernate uncheck everything else and say finish that's where you will get your hybernate in your eclipse and now let's see the output once again if I run this code you can see we are getting an error right and that error is it says hybernate do di not set so the problem is we have not specified the configuration right because when you want to connect with database we have to provide certain certain details right we have to provide the the uh dbms which you're working with you have to provide the database name you have to provide username and password now how do we provide that there's one way you can do that with the help of XML file now how to do that let's right click on your project and we'll say new and if you click on others you can see we if you search for hypernet it will give you a file which is hibernate.cfg.xml so that file name is hybernate do CFG XML we click on next it will ask you for certain things uh the first one is the dialect so if you're working with mySQL I'll be selecting MySQL here and if you're selecting with Oracle you have you can select Oracle then you have to select for your driver class and I'm selecting com. my. driver now depend upon which D which uh driver you're working with so if you're working with Oracle you have to mention Oracle there if it is not there in the in this drop down you have to type it by yourself okay next you have to specify the connection URL so again can I can take the template from here okay I will replace this thing with I'm using the same machine which is Local Host so Local Host then we have to specify the port number which is 3307 and then we have to specify the database name and that database name is neon here okay and then we'll specify the username I don't have a password for MySQL so I'm not mentioning that but if you are if you you have a password for myql you have to specify that as well if I click on finish now and you can see we got our CFG file which is hypernet do CFG file with all this configuration and that's why I love IDE where where you will get all this thing by default okay and that's it we have specified our configuration file here let's go back to our application and if I run this code now and you got an error it says connection cannot be null again you saying it is not set the problem is even if you say configuration conod a new configuration it is not actually going to this file even if you have this file now how to mention that you have to go for that file is by saying configure now if you say configure it will use that file okay and in this configure you can also specify the uh the the name of the file we can specify hibernate.cfg.xml but since we are using a default name then we don't have to mention that but let's say if you want to change the name If instead of having hibernate.cfg.xml instead of that you want maybe hybernate XML then you have to mention that file name here now let's run this and okay now we got a different error not the same error this time we are getting a different error it says unknown entity which is com. telescope. demo hype. alen oh it's not an entity I don't know let's verify the problem is we are trying to save an object right which is of type alien in order to do that we need to specify that this class is allowed to store it to store its object inside the database okay so we have to use certain annotations to do that we can use addit entity we can say control space and you can say it belongs to a packet which is Java x. persistence so this is basically jpa which is Java persistence API now once you have mentioned the entity let's go back to our app and let's run this now and you can see oh now we got another error oh the same error it says unknown entity now even if you specify The annotation there why still not working it's because your build Factory or when you create this configuration you have to also specify which classes you are working with so you have to SP we have to say add annotated class in this you have to mention the class name you have to say alien docl class let me repeat to specify that you are working with alien and that's an entity you have to specify add add class because we are using anotations right let's run this once again and yes now we got another error it says no identify specified now what the what what is the issue now the problem is every table in hnet should have a primary key right in this table if if you're creating this table for alien you don't have a primary key mentioned right so I have to mention mention the primary key as well now how do you mention the primary key it's very simple we have to use one annotation called as at ID and if you say control space again you have to import the package once you do that the package is the package is imported and you got an annotation as at ID which specifies that you are getting a primary key let's run this once again now and okay something has happened I don't know what what went right or what went wrong let's go to mySQL and let's see if you got that table okay and let's say show tables let's verify how many tables we have now and run we got nothing we don't we have we have not got any alien table that means we have to create that or not exactly hynet says don't worry I will give you a table but we don't we have not we are not getting the table what's the issue there the problem the one more problem is when you say you want to save the data into database you're making some changes right and whenever you make changes you have to follow the asset properties now asset property means atomicity consistency isolation and durability so to maintain that you have to make sure that all the all these changes should be a part of the transaction now how do we do that it's very simple create an object of transaction so that you are a part of transaction so we'll say transaction from org. hnet we'll say TX equal to new session do begin transaction okay and then at the end after saving you have to say TX do commit okay so we are starting the transaction and then we are committing it now let's run this I hope it will be done now and oh we got an error it says table neon do alen does not exist now what is that problem now the table is not getting created that means you have to create a table no what we'll do is we have to specify in your CFG file that I don't have a table create a table for me now how to do that that we'll see in the next video thank you so much and do subscribe and like this video so now let's see how to create that table so in your mycle you can see we have not got that table right even if even after running this it's because I mean you have two ways now one is you can create that table by yourself or you can ask hyper to do it now the way you can do that I mean the way you can ask Hy to do that is by specifying a property so you can say property name as hbm2 dl. Auto okay again you can use control space to get that and you can specify update so when you say update in fact we have update we have create So when you say create it means whenever you run this application it will create the database it will create a tabl for you so for all the entity classes it will create tables for you if you say update if you if you if the table is not there then it will create okay it will not replace your existing table so now let's see if it is working or not and if you on this code and okay so you can see we got uh table not found alien but after some time we see it is saying that schema update complete let's verify let's go to your myql if I say show tables now you can see we got a table here which is aliens let's verify do we have a data there so we'll say select star from Alien and if I run this code you can see we got the data as one1 naven green so finally after this four videos or in the fourth video we got our output right so we are able to run run this application in which we are getting get the output in the in the database so that's how you store the data inside a database using hybernate okay but there are you know we we we also getting before before going ahead you can just have a round of applause for yourself for computing this code here okay now what I will do is I will create uh so see if if you can see we are getting a method which is replicated right so what happened in the latest latest version of the hybernate which is after 4.1 we got a new class or new interface which have which we need to Ed to work with uh session Factory now what is that method so this is a interface called as service registry not of this package we say service registry it belongs to package org. hynet do service in fact hynet provides you lots of services one of the servic which you have to use is service registry we'll say R EG equal to new service okay so now we have to use service registry Builder because that's that's how you can create an object so we'll say service registry is that service registry Builder and then we can say dot apply settings so you have to apply the settings of the con object so it's not setting it is settings you have to remember that settings and then in this you have to pass the object I mean you have to pass the properties now how do we do that we say con dot get properties because all the configurations is there inside this Con in this inside this Con object right so we'll say properties dot then you have to say build build service registry by doing this you are getting the object of registry okay so you have to say new service build service registry Builder apply settings and then you have to say build service registry now once we got the object of registry we can pass that here we can say R EG and now you can see the replicated method is not there so we can we have got build session Factory and now it should work if I run this code and we got error now what is this error you can see it says duplicate entry 101 it's because we already have one in our table right so if I have to say one2 this time let's change our alien name we'll say this is uh midula and let's say we have a color here which is blue so midula is in blue color let's run this and you can see we have okay so we got our schema update let's go to database to verify if I run this code you can see we got two entries now one2 Mula and blue so that's how you use uh hypernet to store data into database uh there are lots lot of more lot of Concepts remaining in this so that will be seeing in the subsequent tutorials in the last video what we have seen is we have created the we have created this application where when you run this code it will be so it will be storing this object in the database right now in the hy. CFG XML file we have mentioned update right so that's why whenever you run this application all this new data so if I let's say if I change this to three and if I say hsh and let's say for color hsh is let's say blue or blue I've already done right let's say see on now in this color if I if I run this code you can see in our database we'll be getting hsh as well if I run this code and you can see we got hsh but you can see we are not creating database again and again so it is using the ex existing table it's because we are using update but what will happen if I say create and if I change this value from 103 to 104 and with the same name H andan and if I run this code if I go back to my database you can see if I run this code you can see we got a new table so it has deleted the old table and it is getting a new table and that is what it does when you say create it will create a table every time you run this code now what is happening behind the scene I mean when you say when you run this code see we are able to store data into database right so that means your MySQL only work with SQL so somewhere your hybernate is convert in your code into SQL but how for that what we can do is uh we can use one more property here and we can name this properties as showcore SQL and we can say true because by default you cannot see the SQL query if you do if you do this if you say show SQ show SQL true it will make sure that you you will get your query in the console window let's try it out once again if I this code since we are using in create it will drop the old table and you can see we got the entire SQL query here it says drop table if any or if exist alien create a table with all these fails and it is saying not null and then we also specifying the primary key at the end primary key a ID and then we are inserting the values right so that means even if you are not writing SQL queries hypernet is creating an SQL query for you awesome right so if I go back here and so that is we have create and then we have true uh so these are two two properties we can use in fact let's say if I if I say update here it will not create new table now if I say update if I change this to let's say one1 with name naven and if I say color is green now in this scenario if I run this code you can see uh we are getting the SQL query but only of insert no create and no uh no drop right if I go to my database now so it's always better to use update you can see it is not creating a new table every time you run this and we don't want that right we don't want every time you Le this application it should create it it should create your database again so that's that's how you use these two properties okay in the next video we are going to talk about different annotations which we can use now what we'll do in this video is we'll talk about some more annotations which we can use in this class which is alien now what more annotations we can use now if you see we if you go back and if you say create so that you know I want to mix I I want do some experiment here and so the see the the table name which you getting is alien right but let's say I don't want to have a table name as alien I want to have a table name as something else what we can do is we can say name equal to in the entity uh it's s entity annotation itself we can say alien okay or alien alien table so if you want a table name like this you can say aliencore table and if you on this code now you can see it will okay so you can see it says create a table alien table now because this time you are specifying what table you table name you want because if you don't specify the default table name would be alien itself now what is happening here is of course again we can check that when you go here you can see for this code you can see we got this table which is alien table okay but now what happens is whenever you run this code there's something called as a layer of entity okay so there are there are three different layers here one is your class name second is your entity name and third is your table name so we have three things class name which is this entity name and table Name by default I mean your class name name is alien when you say your entity name is alien table so your table name which is there in the database will be will be with the entity name okay but let's say if I don't want to change the entity name I just want to change only the table name we have to use at table again we'll differentiate between the table name and entity name later once we start with hql but let's say if you want to change only the table name we can say table name equal to uh let's say aliencore table it will only change the table name not the entity name so entity and table are two different things it's just that table name is derived from entity name when you run this code okay now so that's one of the thing we can use the another thing is let's say I don't want this uh this this column name as color I want the column name to be alien color so what we can do is we can use one of The annotation called as column add column which is from java persistence and in this can specify the name we can say name equal to uh let's say alien maybe alien underscore name so you can do that so if you're UND this scod now you can see your column name which is color will become alien color okay can you see that we got not this one oh have I said alien name I my bad it should be alien color okay and if I onun this code now it should be alien color that and here we go yes so we can see we got alien color right so that's how you can specify the color here you can specify the column name to be specific so that's one of The annotation which we can use uh there's one more annotation which we can normally use is let's say I don't want to store the color I don't want to store or maybe I just I don't want to store the name in the table okay what you can also do that because if you see the database now and if I onun this sarey you can see we have or not alien but uh Alien table that's the table name we have if I on this you can see we are getting the alien color we also getting name I don't want this name to be here what you can do is you can mention name as at temporal or not temporal add transient because if you use transient here now this will not be getting stored okay if I R this code okay you can see it will say it will it is dropping the old table and the new table will only have have a ID and alien color we don't have alien name there let me verify that if I onun this sarey you can see we only have a ID and color we don't have name there right so we can use add transient so transient is used to store the uh temporary data I mean that will not be getting stored now what else we can use so again we have some more annotation we can uh we can use that uh so that's it from this video we have we have talked about add table we have talked about add column we have talked about at transient in the next video we'll see how to fetch data we have talked about how to save data right let's say how to see how to fet data and in this video we'll talk about how to fetch the data from the hnet or using hbnet from database so in the last video we have seen how to add values right so we can use this method which is save using which you can store the object but let's say I want to fetch the data now but before fetching we should have some data right so I already have this data in my database if you can see in my MyQ we already have one1 and green oh we are not using name is because we have made it transient let me just run this once again so that we'll get that transient object as well let's go back to our database and if I say show you can see we got that record now what I want is I want to add some more values uh the problem is if I say two and if I change this value to let's say s and if I say the color is white the problem is since we are saying create every time it will get a new table right we don't want that we want it to use the same table so we have to make it update right so we'll say Okay update now by saying update what you're doing is it will not create a new table now so if I onun this code you can see in your database in your database you'll be having two rows now one naven green and one2 S White right now if I if I go back here okay now what I what I want is I don't have this values here okay let me just remove this values in the same code and in Alien I also want one more method here which is two string so that you know when I print the object it should not print the hash code I want to print the values of it so we we'll Implement two string method here now if you're not familiar with two string method you can just watch my video on two string on YouTube so two string you have to overwrite two string so that you can fetch the values because by default two string method fetches the hash code now let's go back here and yeah so after this what I want to do is I want to print the value of telescope so I will say uh system.out.print I will print the object telescope let's not save that because we want to fetch it from database now since you can you can see I have created this object which is blank now so this object here is a blank object I mean it's an object which doesn't have any any default values and if you try to print that object it will give you some we output which is 0 0 and then null value I guess so you can see we got zero and then null for name and N null for color I want to fetch this record from database now how do we do that it's very simple we have to say that I want object of telescope equal to now there's a we can use session dot so there's a method called as get in this this get you have to pass two parameters the first one is the class name the class name is alien docl class then you have to mention the primary key Which object you want to fetch I want to fetch 101 because one1 is actually n right and we have to typ cast it with alien because it will give you so get method gives you the object of object I want object of alien so have to typ cast it right so once we have done with the type casting let's run this code if you can see the output now we got alien 101 naven and green so we are fetching the data from database in the back end you can see we are getting an SQL query so even if you are saying get it is firing a query by passing a we Clause where you mention that the ID is question mark which is actually 101 in our case if I do it for 102 of course you'll be getting the data which is which is which is s you can say one2 sarung and white so that's how you can fetch the data from the database again we have two methods to use here one is get and second is load again we'll talk about load in one of the video in in the in the subsequent tutorials but we can use get to fetch the data okay so that's how you fetch data data from database using uh using hibernate and we have to do that with the help of get method one more thing uh you don't have to create object here we can also say null because it is it will be giving you the object right the instance you don't have to do that and and if I if I run this code now you can see it still works so we don't have to create the object there you can simply specify null even that will do so that's it in this video what we'll do is let's try to understand how to use embeddable objects or embeddable Fields what we'll do for that is let me just remove all extra things here uh this one okay and okay let just see it's working yeah so we'll we'll okay so let's remove that and let me add one more property here and we'll say that property is or maybe a name a name okay so we have a name uh we have a name here right but what happens you know in the in your name if I want to take a full name okay example if if you talk about my name everyone thinks that my name is n ready but not exactly uh today let me reveal my full name on on you on YouTube so my full name is t nain Kumar uh ready Bob ready okay that's weird I know so that's my that's my that's my surname this is my first name and this is my father's name okay so this is my sname this is my first name and this is my father's name okay so we have I have this big name here so let me just close that now that's enough for my my name so what we have is when you talk about a name we have these three things right we have first name last name and middle name what I want is instead of having one field I want three Fields so that you know when I when I go with name it should be easier for me to work with right example if I want to see only the first name of the people I can use the first name if I want to see the last name of the people I can use last name and if I want to see the middle name I can see the middle name right what I will do for that is uh so we can create an array of string but instead of that we can create a class called as name okay a name class or maybe say alien name class and this alien name class will have certain variables so will have a variable like private uh string F name then private string L name and then private string M name which is middle name okay so we have these three things and of course for this we also need uh we also need get and set so we'll say Source Getters and sets Okay let's select all these three and click on okay now with get and Setter we also need a two string method for that also we'll use source and say two string method and we'll select all the three okay now we got a two string method and get an set up let's go back to my so this is my alien name uh class class in this alien instead of having string now I will go for alien name now the advantage is this alien a name will have three things a first name last name and middle name of of course we have to change the get sets as well right since we are changing that so we'll say Source uh Getters and Setters I will say for a name okay now once we got this uh once we got this a name L name we have to go back to app as well now since we want to we want to save the value we'll say new alien again because in the last video we have talked about fetching that's why we have done fetching here right but this time we'll go for again save I will say session. save and I want to save what uh I'm planning to save uh telescope here okay now what else we need here I want to set the values now for sure when you say set the values I will say telescope dot set a ID I will say this is 1 1 0 and or maybe one 1 because since I'm I'm changing the structure uh so we have to make sure we are use create here not update let's go back to alien or notan app and let's set the what we can set as we can say set color I will say color is green but if you say set name that is something which is different right when you say set a name now this is not a simple string right it's an object that means to set the value first you have to create the object I will say alien name I will say a n equal to new alien name and we'll set the values we'll say a n do set F name which is naven Kumar RI and then we'll say a and. set L name which is tly okay in fact I should change my t I should change my you know my uh I should change my channel name to this one okay so once we have said all these things let's set a n here and that's your a n right so we are creating an object of name and then we are passing that name here that sounds cool right now the real question is when I say save in the earlier scenario we were having name as a string right so when you save name as a string it will take a string but this time your name is not a string it's an object how many fields it will create since we have two classes now not one we have alien and alien name will it create two table will it f will it keep all this values in the same table now see to create table we have to make sure that we use entity right and for a name we have not mentioning anything let's see what happens if I run this code if I run this code you can see it will give you an error it says it is not able to to find the columns for name it's because it is getting confused is it an entity or something else we have to say hey don't create a new table for me what I will do is I will put this values inside the same table so that table will have multiple values and you can do that with the help of embeddable So when you say embeddable that means this name will get embedded inside a alien table itself so this alien table will have alien uh it will have Aid it will have color it will also have a name but not a name exactly it will have these three things F name L name and M name let me just run this to see you to show you the output if I run this code uh you can see it it it is working see the table structure here it is a ID then we got F name then we got L name then we got M name then we got color and then uh okay so then we got color and that's how you are inserting a value here let's see how this this how this looks inside a table if I run this query you can see this is your sequence we got a ID we got F name L name m name and color the thing is you can see the the sequence of okay so that's how it works you can see we got one1 we got naen we got my son name we got my middle name and we got our color so that's how you use this embeddable annotation to embed your your your object inside on another table of course when we use entity here instead of using embed then it will get a new table right but we don't want a new table here and that's why we'll be using edable so that's it that's it from this video in which you have used a embeddable annotation again we can we can change the column name here as well we can say add column we can we can do all those extra things now since this is not an entity we will not will not be using at ID because at ID is for uh this is for the primary key right and since you are in we are inserting this in a new table so we don't I in the existing table then we have to do we don't have to do that and in this video we'll talk about hibernate relations now you if you're coming from the database background you know what I'm talking about right so we have different relations to work with right so we we have one to one we have one to many we have many to many right so one table can have its records in the so let's imagine we have employees and we have Department table so normally what we do is uh so one I mean one department will have multiple employees so that means they have uh one Department multiple employees which means one to many and if you if you look at from the employees perspective it is many employees work for same Department that is called as many to one right then we have laptops so I own one laptop so which means one to one and one laptop can belongs to only one person right so there's one to one but but let's say if you go to a library where you have multip you have lots of books so the same book can be I mean so multiple people can use the same book I mean they can you know in rotation basis or one person can take multiple books so that is multiple people can work with multiple books that is called as many to many right so you know all this stuff from from dat from the database world right so that's not the whole topic here the the whole thing is how can you implement those things in hypernight because that we can do in SQL right and we are expert in that we can create all those primary key foreign key stuff in in SQL how about hypernet let's let's try do that here so you can see we have two examples here the first one is we have a class laptop which has these two records we have L ID and L name and then we have uh we have student class here which has role number name and marks and these are two different entities right and you can see I'm using annotation which is at entity and at entity which means we'll be getting two separate tables all together one will be laptop and one will be student right so let's imagine this is those tabl so we have laptop table where we have L ID and we have L name then we have student table where we have role number name and marks which is your columns here and again we have role number as the primary key and then we have your liid as your primary key so this column and this column are your primary keys right but let's say now I want to create a relationship so a laptop so let's say student will have a laptop object I mean can we do that and of course we can do that right so imagine this is the class and again we have some gets and sets here the way we normally do so on both the end we have gets and sets now let's say in this student class if I'm creating object of laptop because every student will have a laptop right so what we'll do is we'll say private and that will be laptop and then we'll be having laptop okay by any mean if you're thinking that we have a different font all together so this thing is written by someone else and I'm writing this this stuff here so you will find it a different fonts there so now what I'm doing is I'm creating one more object here which is laptop right so we have a laptop uh object here which is which is inside your student class now how will it impact your table so now since we have one more column there what we'll be doing again you guess it right again we have to create a foreign key and primary key relationship here so this class will have one more I mean this table will have one more column here and that will be your L right so you'll be having a laptop here let's say let's imagine we have laptop one and then laptop name is Dell and then we have RO number one or let's say this is 101 RO number one name is let's say naven and then I'm saying marks equal to 50 and then L ID which is your 101 which specifies that we have a relationship between this laptop and a student where this is your foreign key from your laptop table right so uh so yes that's how you can relate that Navin ready has has a laptop which is Del laptop right so that's how you can create one to one but will it work directly not exactly to achieve this somewhere here just about your object declaration you have to mention an annotation which is called as at 1 2 1 okay this this o is capital here so you have to use an annotation called as one to one so one student will have only one laptop so student student table will get affected and you have to mention that liid there simple right so when you have one object one laptop object in student table that's how you do it now let's say I don't want to use one laptop so in fact as a student or as a professional I have multiple laptops let's say I have one dell one HP laptop and one Macbook so so if I have three laptops here I cannot use one object here right I have to use a list of laptops so what I will do now is instead of having a normal laptop entry here let me go for list of laptops again we have uh okay that's your int marks but let's say if I create private and we'll say list of laptop and then we'll say laptops okay again when you change all these things you have you you do have to change the get and centers again we'll see that in the in the Practical how do we do that but time and what you are focusing is those foration share so when you have one student with multiple laptops and for this scenario you have to use an anotation called as one to many because one student will have multiple laptops right so you have to use annotation which is one to many again with without any space so it is act one to many that means one student will have many laptops but in this scenario how will you update your tables because say this will not work because one student will have multiple laptops let's say we have laptop 102 here which is HP and nain readyy buys I mean has both the laptops how can you how can you mention that here you cannot say one2 comma one I mean 101 comma one2 right and you can't even have two records here because role number is a primary key so you cannot say one nav 5012 because RO number is a primary key so it will give you error so in this situation what we normally do is let's not change this table so this table will be independent so student table will have its own records and laptop will have its own record there will be one more table which will be called as student underscore laptop so there will be one more table called a student score laptop in this table you'll be having two columns the First Column would be student or in fact role number because that's your primary key so it will be having role number and the second column would be L ID now the entries would be it is 1 then one1 then 1 one2 let's say if we have one more student here which is RO number two and his name is let's say Ari and then the marks is let's say 56 and then I'm using one three here which is MacBook which is let's say MacBook okay and let's say arti has only one laptop here which is Macbook so what we'll do is we'll say two and one3 so you can see we have a table here which is used for the mapping so this is a different table called as student laptop which has a which which has all the relationship entries right because we cannot do that here but hold on we have a solution for that right instead of creating a new table what if you can just change your laptop table I mean something like this what if somewhere here I don't have a place to do that let's say I'm just creating a column column here it should be at the end but let's say since we don't have a place I will I will create it here can I create a role number column here so this is your primary key okay so can I create a ro number column here and every L laptop will be defined so this this laptop belongs tole number one this laptop belongs to number one this laptop belongs to number two simple right so instead of having the entry in the another table we can have the entry in laptop itself but hold on how it will work because student table knows that it has laptop entry your laptop class don't know that it is related to student how will this column will be created so you have to mention in your laptop class that every laptop belongs to a student so for that I'll be using private student and then we'll say let's say stood again if you don't do this if you don't write this the the the output would be laptop table without this column student table and then student uncore laptop table but if you do this if you write private student I mean private student stood now this class will know that we have a column we have a field here of which you have to create a column right but again we are fetching the rle number right so this should know that we are going for for a relationship and the way you can do that is with the help of at many 2 one annotation so when we use add men to one annotation which means that this this laptop class is responsible to create a column from the student sorry from the student table and that will be RO number right so in this student you have to mention one to many you have to mention that and in laptop you have to mention many to one now if you if you do this if you run this code the expected output is laptop table with this column student table with this field there will not be any extra table right but hold on it will not work you will be getting extra table as well even if you don't want that your student student will say hey you you asked me to create one too many it is my job to create that table laptop will say you have mentioned many to one it is my job to add this column why both are doing that again ego problem Maybe so you have to mention to your student class that the mapping is done by laptop so you don't have to do it now how can you mention that you can mention here mapped by property so you can say mapped by and in double codes you have to mention the name of the field which is St so if you write stood here you're mentioning to your laptop I mean you're mentioning to your student table or student class that I'm doing one too Mone that's perfect but the mapping is done by laptop don't do it by yourself that means if you do this it will not create an extra table it will go for two tables laptop and student and will be having the entry at the end of this end end of this uh table which is role number okay so that's how you use one to many one to one one to many many to one but how about many to many how you can do that let's say uh so we were having one student will have multiple laptops and then laptop will have one student but what if you have many to many relationship so one student can use multiple laptops and multiple laptop can belong to one student that's weird right so multiple laptop can be used by multiple students so we we have many to many relationship here to achieve that instead of using stud or student we need to create a list of student so we'll say list of student and let's say this is stood ining St we say STS now this time we have laptops which has multiple students and we have students which has multiple laptops so we can use one to many or many to one here we need to use many to many and here as well you have to mention many to many now again if you write many to many and many to many without this mapped by both the classes or both the tables will be having their subordinate table so if you don't write mapped by what it will do is laptop will create laptop table student will create student table but laptop will also create laptop uncore student table and student will create student uncore laptop table so in total we'll be getting four tables we don't want that right the mapping should be done with only one table now this will not be this will not be applicable because one laptop can have multiple students so we cannot do that so we need to use a mapping table here which which will be called as let's say laptop uncore student right and you can achieve that with the help of mapped by property or attribute so that's how you use one to many many to one many to and all those stuff so in the next video we'll be talking about the Practical implementation of it and in this video we'll talk about hypernet relations but this will be a practical in the last session we have talked about the theory of it so for that what what I have done here is I have created two classes one is laptop in laptop I have a ID as L ID and then we have L name and for this we have G than Center then we have student where you have you can see we have okay so we have this student class here and then in our app class what we have done is we have created this laptop object and this student object and I'm I want to store both the objects into database and of course you have to make sure that you have annotated both the classes here which is student and laptop and then you can simply add you can Simply Save the value into database you can see we have save laptop and save the student now what uh what what what's something okay everything is perfect here let's run this so if you run this of course there will be a table for laptop and there will be a table for student and you can see I'm not using any specific name for this uh tables they will use student and laptop table as I mean only so you can see we have l l name and then we have uh marks name and role number now if I go to the go to the mql and you can see you cannot see so I have this uh neon as my database and you can see we have two tables here which is student and okay let me just say show tables we do have two tables here laptop and student and if I say Select Staff from student we'll be getting the det details for the student which is 150 naven and if I say laptop you can see we got one one and L again why I'm going for student and laptop because in the theory we have talked about these two classes again you can continue the same example for alien class as well which we which we are doing from last few videos okay so what ex what what's new here so what I want is there should be a link between a student and a laptop because every student will have a laptop right so what I will do here is I will say private let's create object of laptop here and we'll name it laptop as laptop uh thing and then for this property we have to use get and set of course so we have to say right click we'll say source which is here and we'll select gets and sets so we'll select this part and we'll click on okay okay so you can see we got get for laptop as well now so student class has a property which is laptop so whatever I mean see we have role number we have name we have marks and we also have laptop so it should have a laptop property right but then laptop is not a normal variable table it's a object so it will go to the laptop class and see okay in laptop class we have L ID as the primary key so in student table we'll be having L ID as the foreign key right let's try let's see or before that we have to change in app as well we have to also set the laptop here right so we'll say s do set laptop and we'll say laptop laptop itself so you can see we have laptop here in fact in Eclipse I'm missing this thing in fact if I if I want to change uh this student it should change the all the variables here right I have seen that in some of the ID is available when you type so if you select this s and if you if you type student all this reference should also change but I don't know how how to get that if you know let me know in the comment section which ID he supports that okay let's continue so what I'm doing here is I'm setting this laptop to student as well an icore uh okay we got an error because because yes so what what's wrong here is it is not able to define the type of laptop here so whenever you create object inside student you have to specify what is the relationship between this laptop and a student for that you have to use an annotation which is called as one two one because one student will have one laptop now let's try if you onun this code you can see it worked so one student will have one laptop that is your one to so that's your one to one relationship now if I say show tables you can see we got laptop and student this is this this is our old tables Let's ignore that we have laptop and student and if I say select start from laptop it will be only laptop entry because laptop don't know about the student but student knows about the table student knows about the laptop right because in student class we have updated with the laptop object that's one to one to one but let's see how to implement one to many here now for one to many what you have to do is let's say one student has many laptop so of course you can use normal laptop here we have to say list of laptops we'll say list and then let's create object here itself we'll say new add a list okay let's import the package for both we'll say java.util okay so we have imported that okay it is not getting imported for this or what uh okay so since I'm using I have set my project to 1.6 and that's why is not supporting this diamond here you have to specify that this feature of not mentioning was introduced in 1.7 so of course you have to mention here because my jdk has set to 1.6 okay so instead of using one to one I should be using one to many now right and for this you have to UT the package let's do that now what we'll change is we have to change the get and seter as well so instead of saying get laptop we say return the list of laptop and here we'll mention list of laptop as well okay now in App instead of setting the laptop here we say s do get laptop dot add you have to add this laptop there right so you have to add this into add list so the the changes what we have done is in the student class we have mentioned about one to many and we have created the list of laptops not one laptop we have list of laptops and in this app what you're doing is you're saying get laptop which will give you the add list and then you can say add I hope it will work uh it will uh if I run this code okay you can see we got an error is because it is not able to delete the table okay this is important because sometime because of dependency it is not able to delete that the problem here is it is not able to delete the laptop table because or student table because student table has a primary key of laptop right so you have to delete this part first I have to delete the laptop or first I have to delete the student here so that it will create a new table and it is it cannot delete because okay we already got uh so you can see we got the third table uh let's delete one by one you know this is what you have to do whenever you create whenever you run you have to make sure that you delete all this extra stuff so we'll s delete the laptop and let's drop drop and drop now let's run this let's go to eclipse and say run there should not be any issue now and you can see we got the output but the change here is if you see the output you can see we got laptop table that makes sense you got student table that makes sense why do we have student laptop because we cannot do mapping in student table now because one student will have multiple laptops so we cannot do that ENT into student that's why it is it has created a mapping table where you have mapping between student and a laptop and that's why the name is student laptop right and if I say select star there if I say select star from do that let's say select star from [Music] student laptop and if I say F this query you can see we we got rule number one got laptop 101 and if I say student table they don't know about the laptop and if you say the laptop table they don't know about the student right that's that's awesome right so that's how your one to many works but hold on I think laptop should know about the student as well because if laptop knows about the student and one laptop I mean one laptop will belong to one student only right so if you can create the student reference in laptop as student and for that of course we need get seter let's do that we'll say right click source and we'll select getter and Setter but this time it will be for student we'll click on okay we got our student there but this student should know what mapping we doing so for for one student we have multiple laptops right so in student we have student has multiple laptops that's why there one too many because one student will have multiple laptops but in case of laptop it is multiple laptop belong to one student so we have to use many to many to one now why many to one is because multiple laptop will belong to one student so with this the mapping will be done inside laptop table we don't need extra table to map that okay but before running this thing again the best thing is drop all the tables so that you know it will not create any issue while running we drop student and we we drop laptop as well now if I run this code okay so you can see it has created the laptop laptop table that makes sense because it has L name it has student R number can you see that now this laptop table knows about the student this laptop belong to what student right and then we got student which doesn't know about the laptop because we have a mapping table but when you when you have this entry in the laptop table why you need a mapping table again the same problem uh laptop thinks that he's responsible to map student thinks that he is responsible to map we have to mention we have to mention here hey student you don't have to map it because this will be mapped by and then you have to mention that it is mapped by this way table here which is student so you don't have to do it I will simply specify student so that so this time your student class will not map it but again before running this I have to drop all the tables let's drop this let's drop this one and let's drop this one as well it's not able to delete the student table because there was a laptop table and let's delete that is it working now oh it says unknown table neon la student laptop is it the table name I know what's what's wrong there let's run this thing now once again and can you see that this time we are not getting a new table it's because the mapping is done by laptop so should have don't have to care about that right so that's how we use many to one so that's one to many many to one now the last part let's say this laptop has list of students here so we don't have one student we have I mean we don't have one student so multiple student will use multiple laptops like you know let's say if you have 10 laptops in a in a in a college lab a computer lab and then any student can use any laptop so that's one that's many to many too many right let's import the package for this let's say control shift o and we'll say this is oh what's that we'll say new array list and this time we'll mention student okay uh we need to import the package for this as well so we're using a list right and then instead of saying many many to one we have to say many to many we need to import package for that as well and here we return the list and while setting also we have to set the list not one student but list of students right so that's done uh we have created this laptop where where mapping is done with the student but here in app have to change something so laptop should know about the student as well uh how do we do that we'll say laptop dot set set student uh then we have to say set student or not set it should be get student get student. add and you have to mention student but that should be done afterwards somewhere here because once you have created the student then only can you can specify that right so now student has added laptop and laptop is adding student so it should work now you can see we have list of students we list of uh laptop but you have to make sure that you write many to many and here also you have to mention many to many not one to many and let let's see if I if I not if I'm not writing this ma by let's run this let's import the package for this okay so what I'm doing is in list we are mentioning many to many so one laptop belong can belong to multiple students and one student can have multiple laptops before running this of course we have to do that uh stuff let's drop this table anyway don't we don't have that table now and let's drop this two table okay now if I say show tables you can see we have only alien there's no uh footprint of student and laptop let's run this let's see what happens if IUN this code now you can see it worked but how many tables we have we have laptop table that makes sense student table makes sense laptop student also makes sense but why we have student laptop when mapping is done by laptop why student has to do that it's because laptop thinks okay he's responsible to map it student thinks that he's responsible to map it let's specify the student hey you don't have to worry about that this is done by the laptop so why you are mapping it so let's say let's again do the same thing mapped by student okay let's drop all the tables because if I say show tables you can see we got four extra tables there uh let's drop all this table we say drop table laptop underscore student okay let's drop this one let's drop this one let's drop this one and all the tables are dropped now if I on this thing okay and you can see the number of tables we got laptop we got student and we got laptop student now we don't have anything called as student laptop and if I go to mySQL just to see how many fields we got let's remove let's say this is laptop student let's find the query for laptop we got laptop entry find the query for student we got student entry and if I say laptop student we got the mapping as well again one laptop can have multiple students and multiple laptop can have one student that's how it works right so that is one to one one to many many to one and many to many so if you have any queries let me know in the comment section and that's it in this video we'll talk about the different type of fetch techniques we have uh eager and lazy now it's it's almost same as human behavior right so let's say if you get a task what you do you wait for the deadlines that that that that means lazy but if you are kind of person who uh the moment you get a task you do it that is eager now to explain this what I will do is you know just we have some code changes here we got as different class here so you can see we have alien class so we all are aliens by default right so alien will have an ID a name and alien will have multiple laptops normally you know you work with multiple laptops in one machine you'll be having one uh one type of task running on the other machine you have some other task other type of task running so let's say one lap one alien will have multiple lap laptops and so in Alien we have a Aid a name and laptop collection and in laptop we basically have uh we basically have uh Alien a start of different example right which we are doing from from long time so uh and then what I will I want to do is I want to fetch again we have done this before we have talked about one to many relationship right so let's try to fetch the value again now what I will do is I will simply ask for alien because I already have a database created so if you can see I already have a alien database here or not alien database but test database in which I have alien and laptop so if I say alien you can see we got three aliens we got nain Rahul and mayank and if I say laptop uh so we have five laptops and alien id1 has three laptops that's great Ali 93 has two laptops and unfortunately Rahul doesn't have any laptop a poor rul right uh so uh so let's go back here and let's try to fetch the value so what I will do is I will say alien A1 and I want to fetch the data of alien from the database so what I will do is I will say session. get and then you specify hey I want data about alien so you say alien. class and then in this you have mentioned the ID uh let's say the ID is one that's I'm talking about naen here okay so if I simply F the value what do you think do you will you get all the values and of course you should get the you should get all the values right now that means if I fire the query of course you will get the SQL there let's run this code and let's see what happens you run this code and and you got you can see we got an SQL and okay I'm also Printing High here where okay let I don't want High anymore let's run this and you can see we got it is fing a query but then it is only fetching the alien ID alen name you can see nowhere it is fetching the uh collection can you see that it is not fetching the list of laptops so if I try to fetch the list of laptops I if I say let me print the value of a one uh and then or maybe I I will not print the value of A1 because if I okay let's let's use only the the laptop part uh so I will first print a1. get a name let's fetch that one and then I will fetch the Leist of uh yeah I will say collection of laptops that's what I'm using right I'm using collection here so I will say collection of laptops and I will fetch all the collections here and I will say a1. getlabs because will give you the col of laptop and then I will trate with with the help of for loop I will say laptop L colon labs and it will print I will try to print one by one laptop I just want to know will it will it fire the query now because you can see laptops on separate table right why is not fetching it and the moment you try to fetch the value you can see we are firing the query for laptop as well so when we when we were saying that we want to fetch alien it was only firing the query for alien but not for the laptops it is firing the query for laptops only when you want it so if I again comment this section so if I comment this section it will not fire the query for laptops that means even if you are fetching the value of alien you are getting only the value of alien not laptops even if alien has multiple laptops that means it is lazy but I don't want lazy I want it to be eager because see what what is happening is it is filing the query two times I mean at two different intervals so first first you're firing the query for alien the second time you're firing query for laptops so a user will feel a bit delay here now if you want to make it eager that's your choice again that's the choice of Designing the way you design your software but if you want to make it eager it's very simple actually in your collection you can specify hey I want to specify the fetch type so in one to menu you can specify hey I want to specify the fetch type and the fetch type I want is so I will say fetch type dot there's option of eager by default is lazy if the moment you make it eager and even if you are not fetching the collection for I mean even if you're not fetching the laptops here and if you run this code you can see it will fire the SQL query so you can see at least it is f in the SQL query now this time it is fetching a ID a name and also the laptop data can you see that in the select qu it is fing the uh it is firing the data for laptop as well but then wait with one difference it is not firing two different queries it is filing only one query and it is using something called as left outter join is it yeah so it is using left outer join to fetch the value so remember this by default the fetching type is lazy so when you say lazy it means it will fetch the value only when you need it otherwise it will not give it to you it's something like let's say your boss says Hey Hey naen I want to I I want uh I want uh I I want to complete a task I want there's a a task you have to complete now I will say okay that will be done and I will not complete the task and let's say one day my boss says hey you have two hours now and give me that task at that point I will start because I'm thinking maybe my boss will not ask me for to complete the task right why to waste my time and that's what happening with hynet maybe a user don't want laptops then why to fetch the value that's what hn hnet is thinking so that's why it is lazy but you can you can make it eager that's very simple just go to La I mean just go to aliens and say fetch equal to fetch type eager and it's that simple yeah so there are two type of fetching fetching techniques we have eager and lazy and that's how you do it by default is lazy you can make it eager and it will work I hope you enjoyed this video you understood about Fetch and I mean eager and lazy and in this video we'll talk about hybernate caching now if you're thinking that what is the advantage of using hynet instead of using jdbc we are lots of advantages right one of the advantage we have seen is o RM which is object raditional mapping so is it the only reason to use hibernate not exactly in hibernate we have some more features which is one I mean one of the feature is hypernet caching now what is what is caching or Cache so basically when whenever you want to access the same data multiple times instead of hitting your database every time you can use cache right so how exactly it works so let's say we have our client here and then we have our application and then we have have a database here right so whenever a client sends the request to the application server that request goes to your your database server right so let's say you have a client here and a client want to fetch the data so what we normally do is we create session object right so in hypernet we have to create session object then we have to use transactions right so when you are fetching this thing we need to use transaction which is a part of session so what happens is whenever you whenever a client signs the request to the application server that goes to database it will fetch the data let's say I'm I'm asking for select star from student so if I'm filing this query you can imagine how many data we'll be getting it it is not one or two records because we are not doing any filtering we are saying select select sta from student it might have 2,000 records maybe 3,000 records maybe 10,000 records so in this scenario when you fetch the data it will be coming to your application server and from the application server it will go to the user so that user in that particular session right uh but let's say if I'm again I'm fing a query so let's say I'm requesting the same data for the second time from the for the same user in the same session okay so in the same session if I'm asking for the same records it should not so what it will do is it will again it will again go to database server it will again hit your database server it will F data time consuming right so when you have done that query for the first time it should be stored somewhere so in your cachier memory it should be there so when you when you run this query for the first time data should be there in the cachier memory so that for the second request it will check okay we have the same query let's go for the data here right and now this cache is provided by your hibernate and this cache will be called as first level cash okay so for a particular session so this is one session let's say uh if I'm using session one for that particular session we'll be having a first level cashier but let's say if a user is changing the session maybe the same user is going for the second session or we have another user who is going for second session so we have two different session now now this two different session will be having two different first level cachier that means if S1 or in the session one if you are filing this query you have your data in the first level cache of S1 now if S2 I mean the second session is filing the same query now the the the data is not available in the first level cachier of session two so again it will hit the database right so again we have the advantage is if you are fing the same query in the in in the same session hnet provides you first level cach but if you have two different s session then hnet says okay uh you cannot use first of cachier here then we have to go for something called as second level cache so basically it has two levels one is level one and level two so level one is by default given by hibernate okay so even if you don't want to use it hnet says hey take this take this uh option of using first of cach a right again how to do that that will say in the next video but this is how your first level cach a works you know in the same session so let's say I want to use second level caches so what second level caches says uh says is it doesn't matter how many sessions you have all the sessions in the same application can share the second level cach right now hynet will not give you by default okay you have to configure this so first of all you have to use some third party library for this so again when you say third party provider so we had either we can use eh cache that is one of the option or you can use o OS then we can use uh we have one more we have swarm right so we have all this uh cachier available now which is best uh so everyone prefer to go for eh cachia so in this in this session we'll be talking about eh cache okay now when you say eh cache how it works so we have to configure certain things let's go for the first one so let's say if I'm using a eh cacher you have to configure certain fields now what fields we have to configure uh first of all since we are using now let me just create a line here so first thing the first thing we have to configure is your pom.xml file because see when you say you are using eh cache that means you need to download that library right so that Library name is eh cache you have to you have to download this cache from the maven repository you can go to m repository you can ask for even eh cache you will get first there uh next you have to also download the J file which is hnet eh cache jar file again for this is for integration so this is the this is this cache will provide the features and this will provide the integration of eh and hypernet the next thing we need here is so you have to configure your hypernet do CFG do XML file in this see by default your second level cache is disabled you have to enable it so you have to add certain properties in your hibernate.cfg.xml file which will specify that okay we we want to we want to allow or we want to give the permission so that we can use second level cache you have to configure that in hypernet cf. XML so first you have to give the permission second you have to mention who is your provider so our provider in this case is eh cache both this thing you have to provide in hypernet CFG XML file the next configuration which we need is you have to change your entity because by default not every entity is cachable so you have to provide two annotations for that the first annotation is called as at uh at cache the second one is in fact the first one is cachable the second one is caching so by by specifying that it is cachable that you're saying that uh you can use I mean this entity is eligible for caching the second one is caching because you have to Pro you have to specify the strategies okay there are different strategies available we have uh Z only again when we start with the Practical implementation you will understand what I'm talking about but we have to provide two things one is cachable and second is the cache strategy okay so if you can configure this thing you you are using the second level caching okay H how exactly we have to do that that we'll see in the pr iCal video but point to remember is we have the concept of caching in hibernate we have first level cach a we have second level cach a whenever you go for first level cache it will be in the particular session because every session will have its own first level cache for multiple sessions if you want to share data then we have to use second level cache now second level cache for that we have to use certain providers we can use eh cache we can use OS cache we can use uh swamp uh one of the best one is eh cach so for that you have to configure your pom XML file you have to configure your hybernate file configuration file and then you have to specify uh cachable on the entity okay so that's it that's how you use hypernet caching in the next video we'll talk about the Practical implementation at in this video we'll talk about hypernet caching in fact in the last video we have seen the theory of hypernet caching right how exactly it does caching so we have seen we have level cache we have level two cache let's try to implement that here and as you know that level one cache work with session so every session we have its own level one cach a and the moment you say you you have two different sessions then your first level cach a will be different right for that we use second level caching so what I'm doing now here is we have a session with name session one and then we have session one. big transaction session 1. get transaction. commit which will commit the transaction and all the transaction should be done here here so and we are using a table you can see I'm using an uh table name as alien table so if I go to my myql you can see we have alien table here let me remove this extra values here so yeah so we have alien table here so if I file the query you can see we have three records there one 1 one 02 one Z3 then we have three names no ready no one ready and no two ready and the color is C it doesn't matter so what I will do here is I will I want to fetch the value so how f a value because if I try to print uh a there if I say print a of course the value of a is null so it will print null value okay you can see it is null but I don't want null value I want to fet some value so I will say a equal to we'll cast it with alien because we know that whenever you fetch the data from the database using hybernate it returns to the object of object so we say session 1.g that's the method we use to fetch the value and we'll use a class name as alien. class that's how you specify the class name which will map with the table of course and here you have to specify the primary key so when I say one1 I know one1 is not ready and color is green so if I onun this query now you can see the output which we got is the object which we which we wanted and it has Al it has also fired a query which is a select query right so for one fetching it has fired one query but let's say in the same session if I do this once again with a different value so I'm I'm again fetching a value so after printing here if I'm again fetching the value but this time instead of going for 101 if I go to one one2 so of course it will fire two queries right you can see we got two values that's for sure one is N Ready second one one ready but the number of queries it has five is to qu is one with one1 one with one2 but what if you find the same query you want the values with 101 and 101 if I find this query now okay you can see it has F the query only once but the output there are two objects now what is happening is since you fire this query for the first time see whenever you f any query or whenever you work with hynet it will first check the value in the first level cach a and you can see this value is not there in the first level cach a so it will go to the second level cach it's not also there in second level cacher so it will go to the uh go to the database it will fire the query for the second time it will check okay do we have the data in the first hour cache and yes for this value one1 we do have value in the first hour cache so it will fetch the value from there it will fetch the value from the first of cash right so that's how it is firing the query only once and that's how you improve the performance of your system because we are not going to database for the second time right but the moment you change your session let's say uh let me just end this session here or let me just do the commit uh okay cut paste commit let's do the commit here and let's skip this part in the second session how can we do that let's copy this code uh we say copy and paste okay we cannot say copy paste we should say uh code reuse that's that's the awesome word right so we'll say session two and here we'll say session two. begin trans action and here we'll end the session we say session 1. close Okay so we are we are going for a second session here session two and here we'll say session 2.get transaction and we'll say do commit okay and we'll end the transaction as well so we'll say session two Dot close now let's see how many how many times it files the query you can see we have the same data but this time we are using two different session this is one session which is getting closed here this is the second session oh I have to change this to session two so this is the second session here and now if I file this query and you can see it is it is fing that query two times so that means it going to database for the second time as well so that means your first level cache Works only for that particular session now how do we use with the so we have to use second level session here second level cache to achieve uh to to fetch the data from the second session or the second session will use the data of the first session provided your values are there in the second level cache but hold on first level cache is provided by hyper by default right what about second level second level session second level cache so second level cache is not provided by default you have to configure it as you can see in the hibernate configuration we have not specified anywhere that I'm using second level C okay and that's why it will not use second level cach so first you have to specify that I'm going for second level cach somewhere here second you have second thing you have to specify is you have to get the provider for the second level cach because hynet will not provide you by default so you have to get the external files and one of the file which one of the cache second cache we can use here is eh cache okay so first you have to download the jar fils for your eh cache you have to download the jar files for hibernate and eh cache integration then you have to configure your hibernate config file and then you can use second level cach it seems difficult right but don't worry we'll do it in the second video how exactly we can achieve that let's continue for the second level caching in the last video we have talked about the level one cachier right and before that we have talked about the theory of it in this video we'll talk about this second level cach so let's start with it so let's if you if I go to app. Java and then we have return two sessions right this is your first session this is your second session and we know that first level session first level cache Works only for that particular session now since we are changing this changing this session that means it is not able to find the data into cach in the first level cach a so we have to implement the second level cash now how do we do that now we have to follow certain steps okay the first one is you have to download the jar files or you have to add the dependency for eh cach now how to do that just go to hibernate do no not this file go to pom.xml file this is this is where you mention your dependencies so if I go to pom.xml file you can see I don't have any dependency for eh cache so I will go to dependencies I will click on ADD and we'll search for eh cach again we can go to m reposit and we can find it but otherwise we can do it from here as well so it is eh cach from net. SF that's the domain name okay this one so you have to select E net. sf. Eh gash and you can go for the latest version no problem click on okay you got one one dependency we have to add one more for the integration of hnet andan CAS for that we'll search for hynet hyphen e HED cach and you can see you got this one here click on that and click on okay now once you have done that go to P XML file make sure you have those two things here you can see we got these two dependencies click on Save and once you do that you can see in M dependencies it will be getting added one is the eh cach it's not is still building so it is getting downloaded from the internet which will take of course it will take some time so once you have done with that once you have got got the dependency the next thing you have to mention is you have to go to your hibernate.cfg.xml file and you have to mention which uh I mean you have to set two properties first by mentioning that you are using second level cache because if you don't mention that in your in your hybrid XML file it will not get downloaded or it will not enabled to enable that you have to set one more property we'll say property it's taking some time go in the background okay so I think I should pause my video for time in because it is getting downloaded and okay so I got my dependencies there you can see we got hibernate eh cach I got uh eh cach right and if I go back to the PM file because of these two two entries here we got those dependencies right now I will go to hynet and I will set two properties first one is is you have to set hibernate dot so you can see if I say control space we got an option of cach and inside this cache there is there should be an option called as cache. use second level cach a by default this isn't this is false let's set it true because you have to use to uh the second level cach are we done with it let's try let me just go to go to my app and let's file this query once again because we have done two things and you can see still it is going for two uh is going for two queries that means we have not done with it we have to provide one more thing now whenever you use second level cacher you have to also specify which provider you are working with because we have different providers right we have eh cach we have Swan cash and all the stuff since we are working with EH cach we have to mention the property which is hybernate do cach dot so in hibernate 4 point uh from 4.0 or 3.3 we have to use instead of using provider class before that we used to use provider class but now we have to use region Factory because if you go to uh certain websites they they still use provider class because they're using hbnet 3 now since we are using hypernet 4 we have to use region Factory undor class in this you have to mention the class name now which class it is so have to type hypernet sorry org. hypernet we not getting suggestion so we have to type it complete stuff so we'll say cach uh then we have to say eh cach in fact we can we can copy that you have to just go to hibernate cache this it should be here so if you expand this you can see this is the class I want to use so I can copy this I can say copy qualify name and we can paste it here instead of typing that stuff we can paste it when that works let's remove this extra class at the end which we don't need so so this is the this is the properties you have to set two properties one is the second cache enabled and second is you have to specify the region Factory name now from where you will get the factory name you have to go to hynet cach which is or hynet eh Cas jar files you have to expand the first one and you will find your class name there now once you got that let's go to app and say run it should work now and oh it's not working it says what it says hybernate oh no class definition found for which class okay let's try okay it's not able to find the definition made any mistake here so that's oh we don't have to specify org we can simply say hybernate or we oh we need here and what else is remaining there okay let's try once again it says class not found for class name class implementation let me just change the version from is it the version issue let's try it oh I'm using the hybernate it's my hynet version so you can see I'm using the hynet version 4.1.6 here and the version which is we are using here is different you have to make sure that they both are same right and have I got that dependency yes I got it and if I run this code now it should work let's try and it worked but again the problem is we got again we got two queries now what's wrong we have we have specified in the we have got the dependencies we have specified that we want to use hnet but the problem is not all classes or not all entities are allowed to get cashed you have to you have to explicitly specify that you are using the cache for that you have to use one annotation called as cachable so your class need to have an annotation call as cashable second this concept of cache has multiple uh you know multiple strategies one is read only one is none one is read write so what is those strategies are so let just write to to change your strategy you have to say cache because by default it is none you have to say usage by I mean what way we are using that so you have to say cache concurrency strategy that's how you specify the strategy then Dot and you can see it's an enum where you can specify different strategies here we have none which means by default is none it will not use any cache concept you can specify uh nonstrict read write you can specify read only read only means it will only work for reading not for writing so if you if you don't update your update your database read only is best but if you think that you normally update your database it's best to go with read right let's use read only here and that's it I think we are good to go let's go to app and run and you can see we got only one select query now since even if you have two different sessions it is firing only one query because the both the sessions are getting shared with the second level cash okay so let me repeat what we have done you have to first update your uh eh cache dependencies two dependencies one is eh cach second one is hibernate eh cach you have to make sure that your hibernate version is I mean the hibernate and uh cach version is same as the hyet version you're working with next you have to update your hyet CFG file by mentioning these two things which is your use second level cache and region Factory class and on your entity you have to mention two annotations one is cachable which is which will allow it to get cached and second to specify the strategy which you which you are using so you can do that with the help of add cach add cach annotation so that's it that's how we use this thing in fact we can do one more thing here let's say if I go to app and if I specify uh let's say if I don't do if I don't use this if I use a query now how can we achieve caching using query in fact what we'll do is we'll do that in the next video in this video we'll talk about hypernet caching with query so in the last video what we have done is we have used hypernet caching second level caching right this time we'll go for queries because see what the data which we have fetched is with the help of get method right so when you use get method as the hnet implementation so that means hnet will use cach but what if you specify your own query let's say if you are using hql will it work let's try so before going before going forward let's do a quick revision what we have done so we have added these two dependencies one is the eh cache and the hynet and eh cache then we have specified that I want to use oh I want to use this thing which is hibernate second level cache and Factory class then we in this we have mentioned mention that we want to use cash right in the entity entity class in this let's try to implement the query now now how do we use Query so we'll say query q1 it's q1 equal to we'll say Session One Dot create query and then we'll mention a query here we'll say from uh Alien where or we'll say where a ID is equal to 101 that's the that's the data we want right and let's import the package for this we'll say org. hybert that's the package where you have your query then we'll say uh so here instead of fetching data like this we'll say q1 dot unique results that's how you fet the value so if you if you know that you'll be getting only one row you can use uh unique result and we can cast it here we'll say let's cast it with alien itself so we got our first object let's copy this and let's paste this in this second object here uh we'll say replace this so instead of using get method now we are using query and we'll say this is Q2 and session two uh it should work now let's say if I run this query you can see uh okay we got so we got an error it says session is closed or because we have not replaced this with Q2 okay let's run this once again you know bugs are everywhere so you can see we got the data not with the get method but with the help of queries and we got these two queries here we got the first select query and then we got the second select query the same data you're getting for two times now why exactly we are doing that or why exactly we getting that is because whenever you use second level Cash It Is by default of get meth it is not applicable for the configuration okay so we have specified the second level cachier right but we have to also mention one more property here because by default when you use second level cache it will it is applicable for get method not for the query for that you have to specify one more property here and the property name is it's hibernate dot cach dot so you can see if I say control space there is one more property here which is use Query cache by default this is false you have to make it true so that it will be available for query cache as well and now once we have done that let's see if it is working or not if I run this query and okay no it is still going for two times the reason is when you specify that it is your your second level cache should be available for query cache you have specified this in hnet file right but you have to also do that for the query object you have to say q1 dot there is a method called a set cachable you have to say true because you qu don't know that we are using cacher here the same thing need to be done for Q2 we say Q2 Dot set cachable and true now why we adding two times is because the q1 will mention that I want to use so this results or this quy result should be stored in the cache and second time it is fetching from the cache so you have to write it at both the times and you run this code and you can see we got only one query so it is not executing the query two times it is doing only one time right so that's how we can use Query with uh with hypernet caching hql which is which stands for hypernet query language now why we need this concept when you have when you can use the methods like get when you can use method like save update so if you can use all these methods to change the data in your database why you need another language okay just think about it now when we use a method called as save what we do we save the object to database when we use update we normally use we we change the data again with the help of objects but when we use something called as get what we specify is the class name and the primary key right so let's say if you want a object which is the fifth row of your table we will be using a get method and again we have to specify the primary key right but what if you don't want a simple row you want some you want to work with multiple tables maybe you want to fetch not the entire records but some specific records I don't want to have the entire record I don't want to get the entire row I want to get only one row how to do that for this you need hql now you might be thinking about okay we have to learn another language now uh not exactly if you know SQL hql is very very easy H provided you know SQL okay if you don't know SQL then there is something called as learning curve but if you know SQL if you know what is Select star from table if you know what is Select role number from student if you know where Clause you're good to go so in hql what we change so first of all in SQL what we mention is so let's say if I if I use this query the about query here which is Select role number from student now this role number is a column name and student is a table name in SQL right in hql what you have to mention is you have to mention select then you have to mention role number but this role number is not a column name it's a variable name or the feed field in your class or the property in your class and then you have to mention the class name not the table name but the class name and that's why you can see this is student in small s this is student with capital S right so you have to mention the class name not the table name that's that's something you have to remember this is SQL and this is SQL almost same right so that is exciting you know you have to you can you can learn a new language called SQL if you know SQL you can learn hql if you know SQL right now now what what what's new in that that I mean can we do some extra stuff and yes so let's say if you want to fetch the entire table what you do in SQL is you say select star from table name in case of hql you can simply mention from table you don't have I mean from class name you don't have to mention the select query because it is something which is obvious right when you say select star you want the entire table then why not go with this instead of saying select you can simply mention from class name it will fetch the entire table for you right okay can we do joints yes of course instead of using two uh instead of using two table name we have to use two class names can we use Group by of course can we use aut by of course everything is possible again how to do those things that we'll see in the in the Practical session but it is possible to use all those things which we do in SQL in hql as well okay now there's one last thing which I have to point to you here so if you talk about SQL let's say if you're working with Java and database and you are using jdbc now if you remember we used to use result set so when you file a query you get the data in result set and then what you do is you apply those weird while loops and then you try to FES the data in case of hql you get so when you file a query you get the list of objects which means you can fetch all the information with the help of a enhanced for it's that simple okay so hql is simpler than SQL okay can we use okay can we use SQL in hypernet and yes we can also do that that so there's something called as native query so if you want to use SQL in h in hypernet instead of using hql we can do that okay with the help of native queries in this video we'll talk about how to use hql in hibernate now if you're wondering about can we use SQL yes we can so we can use hql and we can use SQL in hypernet in fact we have seen that in theory right we have talked about why do we need hql so let's see how to implement that in this in this project so if you can see we have this project here right we have this app. Java class then we have student class and then we have uh this hypernet configuration so I made some changes here again uh I have changed my database from MySQL to postgress and then I have also changed the student class here instead of having the laptop object we have removed that we have we are simply saying role number name and Mark right and let me just go to I mean I'm just using some simple class here no alien all the stuff let's go with simple thing role number name and marks so what I want to do now is I want to save this project into database I want to save the data into database so for that first of all to use hql what I will do is I will insert some values into into database okay uh I'll be using some random class to insert the values so first of all I will use a for Loop here we say insert I I the value of I would be one and then let's go till let's say 50 values and then we'll say I ++ that's weird i++ and then here I will create object of student I will say student s equal to new student okay and now what I will do is I will set the values I will say s dot uh first thing I will set is set role number we'll set the role number as I itself so as your I value increases you have you can increase the value and then you can say this is s do set name I will say name and plus I so that it will print name one name two name three and then we'll say s do set marks now I want the marks should be random for for that I will use a random class that's a concept of cjva again so we'll simply say random r equal to new random okay and then we'll say what uh we'll we import the package for that control shift o your package is imported and here we'll say instead of marks we'll say r dot next in and we specify the value the max value should be 100 because Max should not be more than 100 right uh that's that's good and then we'll simply say session do save and we save that object so it should work right so if I run this code there should be 50 entries in the table let's run this and you can see there is no error and we got 50 entries is there can you see that insert statement 50 times let's verify let's go to our post gz and let's refresh this thing and you can see we got our oh we got all this thing it's because I have added this stuff here I don't want that stuff okay let's run this once again okay now let's refresh and okay it doesn't matter it just there the main focus here is on student right if I if I say view data and all the r that's how your uh postas works and you can see we got this table here with 50 entries what I will do now is I want to fetch the values so I want to fetch let's say I want to fetch the mass of a person with ro number seven okay I want I want this one okay how can you do that before that what we can do is we can print all the values so let's go with all the values so for that let me remove this stuff because the the thing is done and I will change this from uh Auto I mean this this will make it update so that I don't want new table every time right so now let's file the query now how do you file a query it's very simple simply create an object of query so that's a query interface which you have in persistance okay not in persistance but we'll use hynet so let's say control space and we'll say hynet and we'll say Q equal to uh now how do you create a query will say session dot create query so that's how you create a query inside this you have to specify specify the query now again we are not using SQL here we are using hql so when you say you want all the data we can simply say from and you have to mention the entity name which is student so we'll say student and that's it you got this query but hold on how how do you fetch a value now because this just this is just a query right so to fetch the value we can simply say q. list so that's the method we have to use to fet the value so we'll say q. list and we'll save this value because Q do list will give you list value you can see it gives you list so let's go back to app and let's say give me a list there so I will say list and we'll specify what type of student object we want I want I want list of students and I will say this is students oh that's weird Okay students so you can see we got a list here of course we want to import the package there let's import the package which is java. utel okay package import done and you got the list now it's see so simple we are simply saying from student it is not giving you the result set which you used to get in uh in jdbc right and you know trust me everyone no one likes result set okay it is just the way because it was there so we were not having any issue but now instead of fetching the result set we are getting a list of values and it is very easy to work with list right so we can use enhanced for Loop here and we can say student as colon students now once you got list [Music] 50 right let's say if you if you are getting a specific value let's say I'm not getting a list I will say marks equal to or role number RO number equal to 7 I want I know that it will give you only one result in that case instead of saying list what you can do is we know that it will return return only student object not the least I will say student student and here instead of saying list you can say Q dot there's a method called as unique result because unique result will give you only the unique result oh but it's giving you ER it says uh you have to typ cast it because it will give you the object you have to type cast it with with student and done and instead of instead of using for Lo we can simply print the value of student right and that's it we can also use where Clause here can you see that we got the data with it with with role number seven with name seven and 49 again you can verify that in your table as well right so that's how you can you can use hql Query here can we use some more queries yes we can we can say we can create tables we can use joint tables we can use what else we can use here uh okay we can also specify the columns okay if you don't want the entire table you know you can specify the columns you can say select star from student you can also specify select star from student run this query and okay now in this case what you would do is oh start this doesn't work let's use something else let's specify all the values or all the columns we'll say RO number uh name and marks now this will not work the problem is okay so what what what is Just Happening Here the problem is what is not working is when you specify the columns it is not sure that you're getting actual object okay so what it says is instead of using the instead of getting a student object what you will get is you will get list of string you will get an array of strings okay again how to solve this that we'll see in the next video but in this video just remember we can use hql queries in the next video we'll talk about how to solve this issue okay what else we can do here in this yeah we'll talk about that in the next video and in this video let's continue with the last thing which we have done which is hql so in the last video when we finded a query which is from from student we got the entire table right and then we saved that into a list and everything was working then what we did is we applied a w Closs where you getting a unique result it was working but the moment you specify the columns you know we have specified role number name and marks that's where the problem started right because when you use select query that means or whenever you specify the select part it means that you're not fetching the entire row you're fetching a specific columns it will not return you student object now so what it will return is it will return you the object now why object why not string integer it's because role number is integer name is string and marks an integer so if you want to have any type of data we we always go for object right and we not getting one object we are getting three objects role number name and marks right so for that we have to create an array so we'll say object array on the right hand side as well we have to say object array right we have to type c with the object array and now since we got an array we cannot use normal student print right we have to use a loop here we'll say Loop which is of type object I can use enhanced for Loop here we can say object o equal to student okay and let's print all the values let's say I want to print o and if you run this code you can see we got seven name seven and and 49 right so that's our record here we have one more way in of instead of using a loop what we can do is we can say system print and we can say o of Zer then we can use a colon here just to separate those things we can say o of one uh oh not o it is student right so we say student of zero student of one and then Plus student of two that's that's our three records right so now it will you will get one row which is uh seven name seven and 49 so you can see that that oh there was there was not a space between this we wanted a space here right so will you a colur okay that's perfect now let's run this and you can see we got the data as we want it but let's say um so that's how you can fetch the if you use a select thing but let's say if I don't use a w Clause now let me remove a w Clause from here now what will happen is the moment if I remove the w Clause it will not give you one record it will give you the entire table right now you cannot store that in one record you need to use a least of students so we'll say list here because again we are going for the entire table let's import the package for that which is java. utl and let's typ cast it with list of objects right so that object array is for one row and list is for the list of rows right okay now there's some change here we will name it as students because we are not getting one student and now we can use enhance for Loop here we can say uh object array colon students object array student colon students now what will happen is from the student students list it will fetch one one student and you can print all the values and that's what you're doing here right and let's run this code you can see we got an error oh now we cannot use the uni results right because we are getting the entire list so we have to use a list that's perfect let's run this and you got the entire table so that's how we can use a select query with uh with by using the object array uh can we do something more here uh let's let's try let's say I want to fetch okay I want to fetch only those values which are greater than greater than uh whose whose marks is greater than 60 so again we can do that we can say where role number is greater than 60 now sometime what happen oh not role number but marks uh marks is more than 60 now what happens is uh let's say if you have two tables and both these table have the same row or same column example let's say we have student and we have uh a certification person any anything and the both the table has the same column it there will be a confusion right because we can use joints here we can have two tables so we have to very specific which table marks I'm talking about so that means we have to use allies with student so I can say student s and we can say s do marks even we can do this and if you run this code you can see we got the output with all the all the students are having more than 60 marks right so that's how you can apply the we Clause uh can we do something more here yes we can uh let's say I want to fetch only one result I want to find the sum of marks we can do that we can say sum of marks now in this scenario we are not fetching one recorder we are not fetching the uh we not fetching The Columns we just fetching one list so I will remove this I will say it is just a list because we are not getting an array of objects we simply see a list and now we cannot say object array I will say simply say object and we'll just print student marks now okay in fact this column name is wrong we'll say marks and we'll print marks will it work and you can see we got the addition but hold on why do we need a list because we need we know that we will getting only one thing right so we can simply say unique result and we can store that in the type object right and we can remove this with list from object and then we can say this is total marks and you can print the total marks right so when you know that you're getting only one data you don't have to use a list here uh we can change I guess we can also do one more thing instead of saying object we say integer because we know it will be getting we will be getting integer right let's try okay it works and you can see we got the output as oh we got an error it says it will return you long that's the problem it will sum returns you long okay so you cannot type c with integer you have to say long on this side as well you have to use long and now let's run this and it works right so that's how we can use all those things the important point to remember is whatever you can what whatever you can do in SQL the same thing is possible in hql okay in a more efficient way okay so can we do one more thing let's let's try what we'll do now is let's say I don't know this value so this value will be coming afterwards so let's say I have a variable here which is int uh marks and let's say the marks is 60 I cannot say marks but let's say B so int B is 60 and in here I want to specify that variable how do you specify variable if it's very simple we can say plus b right if you don't know the value you can add it at the end and let's run this it works but hold on I'm not a big fan of concatenation right because concatenation always gives you trouble so if you remember in jdbc we have done with prepared statement can we use prepar statement here okay we can try so to use prepare statement here we can simply say colon B and done our job is done with don't have to we don't have to use that uh prepare statement stuff we can simply specify colon it will work but hold on before before running the query we have to change the value so we will say Q dot set parameter for the first position I will say the value is b or not the first position but for B let's see if this works and okay is it zero it starts with zero I guess okay uh there's one more issue parameters are based at one okay that's weird so what you can do is you can simply use B here and now it will work so B will be replaced by B okay so this B which is colon B will be replaced by this B so anybody you want to specify the value later you can actually use colon which will act as a positional parameters which we in jdbc we have done with prepare statement right the same stuff is here so this is how cool hql is okay so in the next video we'll talk about some more stuff about hql in the last video we have talked about hql right so now you know how to use hql a different language which is almost same like SQL but we all love SQL is it no exactly right but let's say let's say if you want to use if you want to use SQL in hypernet is it possible let's try let's try so what we'll do now is I will go here and instead of firing a hql query let's go for SQL query so what I want here is let's say I have a student table right we have done with that so in that student table we have role number we have name and we have marks I want to print all the students whose marks is more than 60 the same query which we have done in hql let's see how to do it so when you work with hql we we create a query object right but since we are using S SQL in hypernet we have to create object of SQL query so we'll go for SQL query and we'll say let's say let's say a query name is itself is query and then we have to say session dot now in case of SQL we have I mean hql we have used create query right here we have to use create SQL query in which you have to mention the query so I will mention a query we say select again our favorite query we'll say select star from student and we specify a we Clause we say where marks is more than 60 and we are done it's so simple we can simply specify select sta from student where marks is greater than 60 but how do you execute this to execute this query we have to again we have to use the same thing which we have done in in hql right which will say a list again the output will be list right so we'll say list and we'll say students equal to Let's import the package for list that's done and then we'll say query dot so there is a method called as list right but I have just one query here if I say query. list and if you're getting the list how can you fetch the values because this is a list right if you if you try to print see this will work I mean this you will get the output but that output is there inside student and that's a that's a least format how can you print this elements okay so let's try the problem is if you try to print this values because the type of the list is object right so if I say object O Colon students is students and if I try to print the value we'll say print o and if you run this query you can see we are getting all object reference type and all hash codes we don't want that what we want is okay what we want we want exact object right for that what we have to do is we have to specify that we are using a list of students right so when you say uh list of student how it will work okay let me just think about it so what we can do is we can say query dot so we can specify add entity because what you're getting is not normal thing right we are getting an object of student we'll say student. class right so we get student. class now we now we will be getting the students object and we can typ cast it here so we can say this is not a normal thing this is a student and we can change here itself we can say what we are getting is not a normal list but it's a list of students I hope this will work now let's run this and here we go can you see that we got the list of student okay so that's how you can fetch the you can you can work with SQL in hypernet so we can specify a select query which is your uh which which is your normal SQL query we can specify add entity by by for of specifying what type of values you are getting and you're getting a student object so that you can use typ cast you can use generics here which is student and we can print all the values this is one way now let's say you don't want to print entire table you want to specify specific columns so instead of using star we can specify the columns here so let's say I just want RO number and I want marks I don't want names okay or just to change it let's say I want name but I don't want role number in this scenario since we are not fetching the entire student we cannot typ cast it with student right let's remove that okay but how will it work now so I cannot even say identity student because this is this by by specifying this you are saying I will be getting the entire row we are not getting that for that if you want to specify okay before that I have one more thing so this will be not student this will be object and here I can say okay how can you print it now uh so what you have to do is when you say you're fetching name and marks you have to specify that we are getting two things that means so when you execute this name will have a value and marks will have a value right so name and the value so it will be a key value pair so it will be name as a label as a key and then the name actual name will be the value right and marks is will be the key and the actual marks will be the value for that have what you have have to do is you have to say query dot so there's a method called as set result uh Transformer in this you have to specify criteria dot allies to entity map what it will do is it will convert your output into map format okay so that you can use it okay uh how do you do that so we say from student every object will get a map uh first you have to typ cast it with map right so once we got an object here we have to typ cast it uh okay we'll say map m equal to we'll say o but hold on O is a normal object right so you have to type cast here itself you have to say map let's import the package for map okay now once you got the map we can actually fet the values we'll say o dot get and you can specify the key here the key is name then we'll use a colon and then we'll say O.G okay not o but M I made a mistake here so it should be m m. get and you can specify marks quite simple right so you got map and you are getting the output will it work let's try let's try and if I run this query here it works right so that's how you can use SQL in hql in hybernate SQL in hybernate so that is your yeah one more thing this SQL actually called as native queries so this is called as native queries okay so that's it we have talked about SQL how to use SQL in hynet and in this video we'll talk about hybernate object States or the persistent life cycle now when you work with hybernate we use session now in session we use different methods right we use we use Save we use uh update we use delete now when you use these methods there's something which is changing behind the scene which is your states of the object now it is applicable for JP and hybernate as well so what happens is we have different states so let's imagine we have a start state or we're going to say start state but then we have two things in Java for sure right it doesn't matter is it hibernate is included or not when you create an object that's a new key that's a new object for you right so we got a new object and then once you've done with your object work you you normally nfy it or you you make it eligible for garbage collection Maybe by uh completing the scope or by writing equal to null so we have these two states for sure what for every project or for any every object we have new and we have the end state or you can say destroy state but in between this if you can put hibernate so hibernate has its own state right the first one is let's say you got the object and I my favorite class is alien so I will say I will I have an object of alien here now this alien will have certain features let's say alien ID alen alen name and alien Tech or the favorite technology for that particular alien example if I get my object that will be Java so that I love Java right so let's say I have I have set all these values the moment I do that the moment I create the object in hybernate that becomes a transient state so your object in a is in a transient State now what exactly it means so transient State means if you do any modification with the object and if you set any values and the moment you close the application or the moment you destroy the object that data I mean you you will lose that data you cannot get it back so if you want that data back again you have to make sure that you should persist it right and that's why we have the second state so let's say if you want to save this data so in hynet we do that we say save or we say persist so the moment you say save or persist your object get into persistent State that's the second state you have the first state is transient by default all the objects are transient the moment you try to save it that becomes persistent now in persistent whatever you do with the object it is it is there in in the database in fact there's a direct linkage between your object the Java object and the database database row so every time you modify if you if you modify anything in the object that will be applicable in the database as well yeah example let's say if you got the object okay you got the object and you say Okay I want to update some I I want to so I want to assign the data and then you say save but what happens when you try to uh change the value of the alien let's say if you want to change the technology for an alien let's say I love Java now but maybe in future I would love something else let's say a blockchain maybe let's say python so if I change that value after saving still it will be applicable it will it will get updated in dat database as well because we have not uh we have not removed the object from the persistence state so whatever you do with the object will be applicable in database as well yeah so we have two states first we have transient and we have persistent the next one is actually detached so let's say you you are working with the object and the object is is there in the persistent persistent State and you want to modify something every time you modify it will affect database and you don't don't want to do that now maybe you want to per you want to perform some operation and that operation will should not be there in in the DAT database so before doing that you have to make sure either you commit your session or you close your session or you have to detach the object from the session or from the person context so we can use method like detach to do that and the moment you do that it will so your object goes from persistent state to the detach State okay we have talked about three states now we have transient we have persistent and we have detached in some ter you can imagine detach and transient as the same state because in both the state it is not related to database right there's no there's no persistent between your object and database but yeah we have this three state we have transient we have uh we have persistent and we have detached but let's say you have removed the data from the database so you got the object and that is in position date and now you want to remove the data Maybe by delete or by remove method the moment you say delete it goes into a removed state so basically so it is there with the Java so that object is there in Java but is not there in database because you have removed it or because you have deleted it so you have three you have four states now you got transient you have persistent you have detached and you have removed yeah so when you create the object let's say new state from New it goes to transient from transient when you say save it goes to uh persistent from persistent it goes to detach when you detach it so this is like you have a database or you have a position context you have the object you have detaching it that's detach State and then when you delete the data or when you when you remove the data that becomes your remove state but the question arise what if you don't want to create a new object or you don't want to save the data in the database when you want to fetch it now what happens when you fetch it let's say if you if you had a new state and you want to fetch data in that scenario you don't have to make it transient first what you can simply do is you can say find or you have different methods like you have get or you have find So when you say get your object by default get into persistent State because you're fetching from database right so it is it is in the persistent state which means after doing get and if you if you modify the object it will affect database as well yeah again after this video I do have the implementation of this so you will see that implementation later but then you so we have from from the news so when you say find or get it will it will get into persistent state right so there's a shortcut as well uh but let's say after in in transient itself if you don't want to continue with the object if you don't want to modify or something you can directly destroy it so all the object will be eligible for garbage collection after removing it or after DET detaching it or after after the transient State itself yeah so that makes sense right so yeah that's it how that's how you make it work in fact you can go back from detach to persistent state with the help of save method or or update method yeah so those are things available uh in hybernate yeah so those are the object States available again how to implement this that we'll see in the Practical implementation in the last video we have seen the theory of hybernate object States or which is also called as persistent life cycle in this video we'll try to implement that now before going ahead let me show you a class here which is laptop so basically working with this laptop class which is actually an entity which we are using here and it has three properties we have L ID brand and price and we already have a table with this name in the in the database and again that is done with the help of hnet itself so if I open this MySQL and if I say this is my database basically which is Alpha and then in this we have uh this laptop so if I select start from laptop you can see we have we have all this uh brand and then price and L ID the last one is one 112 Del India and zero let's focus on 51 so let's say after 50 we have 51 because there's a huge gap between 50 and 111 so what I will do is I will add a new object which is I will get a new object which is which will have 51 now uh let's go back to app and let's create the object of laptop so I will say laptop L equal to new laptop and this this laptop will have certain values I will say l do set L ID which is let's say 51 as this first and then we'll say l. set brand I will say the brand name is Sony I don't know anyone buy Sony laptop nowadays but then we have a brand Sony and then let's say the price as uh maybe uh maybe $700 so that's the cost you can you're ready to pay for Sony laptops so we have all this we have all this data now what state it is by default in Java when you say you are creating an object that's a new state but the moment you assign the values and if you take take this into a hybernate uh consideration it it is now in transient State now why transient is because we we got this object but then this object is not getting stored in the DAT database uh so the the moment I run this application the data will be there in the application and the moment I stop the application so when I stop it will I will lose this data I will lose 51 I will lose Sony I will lose 700 I will not be having this data so in order to have this data somewhere I have to persist it or you can say I have to save it and and that's where we say we have we we so that object goes into persistent state so I will say session do save and if I pass the object L now this goes into persistent State yeah so this goes into persistent State and this data will be stor inside database but only when you say commit otherwise it will not getting stored so let's say I have set the value which is said brand is Sony but after after saving it if I change the value if I say hey I want to change let's I want to change the price not the brand I want to change the price from uh $700 to $650 I want to just give some discount and then then I want to reduce the value so I will make it 650 now what do you think if I run this scod what will getting stored What will What what will be stored in storing in the database will it be 700 or 650 if on this code now logically if you if you think in normal world when you say save it should get stored right and that should be the value which is 700 and after saving we are changing the price which is 6 so we are not updating again right we are not trying to save that data again so it should be 700 but the moment I run this code and if I go back to Mel workbench if I select star from laptop if I refresh if I go up can you see that we got Sony and we got 650 that's weird we were expecting 700 here but why 650 is because the moment you say save your object is still in is in the persistent state so whatever whatever changes you make with the uh with the I mean with with the object it is reflected in the database as well and if you look at the console here initially it is it is saving it is saying that you are storing the data so insert query but the moment you update the data it becomes uh it it FES the update query yeah now so this is your this is what you say a transient so from transient you're going into persistent state but let's say I want to make one more change first of all I will I will create a new object now just since I'm making some changes I will say let's go for a new uh new brand I will say Lenovo and then the price for Lenovo is let's say $800 and after some time I'm making the changes to $750 and then uh so I want this to be persisted so of course I want to change the database value as well which is 750 but due to certain operation let's say I have done some statements here and these are some operations and after all this operations I want to make some changes because I just want to change the business model I don't want to change the data and database so I will say customer comes to me and say so I will inform customer okay I'm I'm giving you a special discount but then that will not be getting stored in the database so for database for for those people it will be 7 750 but for you I want to make it 600 but the moment I say l. set price what do you think what will be the final price uh so it will be 600 of course because it is still in the persistent State what I want is I want the changes till 750 I don't want to make the changes in the database so in that point what you have to do is you have to say session dot there's a meth called detach the moment you say detach this object L now your object L which is a laptop object is not in a persistent state it is in detached state so whatever changes you make here it doesn't matter to database and let me show you the output so if I on this code uh you can see it's firing the update query where is that it's not showing the it's not firing the update query that's weird uh what's wrong okay let me just go back there and let's see what happens oh I'm detaching it before the comit that's weird Okay so let's let's see what happens so if I say thing okay so you can see this 650 oh it's not even is not even getting stored because I'm detaching it before commit that's my bad yeah but then uh so do so let's let's do that after commit uh I will say cut and I should be doing this after commit and if onun this code uh you can see is updating the query only once and now if I uh if I go back here and you can see we got 750 we have not we have not got 600 so that's the power of detach in fact after commit we don't even need detach but then let's say if you're using a transactional things or if you're using some other type of uh maybe some other framework to implement this maybe you have to use detach at that point but anyway you got the point right so after commit or after detach it will come into detach state that means whatever make changes you make with the object that doesn't matter right so that's how you implement these things in fact we have one more which is removed uh so you can when you say uh session okay when you say session. remove and if you pass the entity L from the from the database it will remove data that will take this object into a removed state so that's that's how it works uh so that's how you can use all the states we have transient we have persistent we have detached and we have removed in this video we'll talk about the difference between get and load method in hypernet now if you want to fetch some data from database what we normally do is let's say we have this class called Laptop and I want to fetch the data from from database again I do have a table which in which we have so many rows we have uh so if if I can show you you can see we have data which is L ID one brand brand one and then price so let's say if I pass a brand let's say six and based on and based on the idea I will get the get the laptop object right so let's do that here so what I will do is I will go to app and I will say laptop and I will say lab now basically there are multiple ways of fetching data you can use hql you can use criteria or uh you can simply say session dot so there are two methods we can using which you can do that in fact we also have find uh but then let that comes from jpa perspective but then we have get uh using get if you pass the laptop I mean if you pass the class name so that you will get the you don't have to do the casting manually and I will I will mention the ID as let's say six and then I will get the object so if I try to print laptop you can see it it it works you can see if IUN the code you will get the object here and that that works right but let's say if I don't use get if I use load so load is another method using which using using which you can fetch right and the moment I run this code and let's see if you're getting some other output we are getting the same output so two methods doing the same thing in both the cases we are getting the same output but what's the main difference now the main difference comes in the perspective of performance or the type of exceptions throws now what happens is let's say if I use get okay if I use get and if I if I don't use this object so I'm not using this laptop object I'm just fetching it okay I'm not I'm not printing it that means I maybe not just printing but then I'm not using that object so the moment I run this code and let's see if it is uh if it's filing the query you can see it is filing the query so the query is fired and it is hitting database so every time you use get it will hit database but what about load now what load does is load will actually not hit the database let me show you what happens when you say load now when you say load it will not fire a query can you see that it is not filing a query but yes when you try to use the object then it will then it will file the query so if I try to use it and if I run this qu if I run this code you can satisfying the query in fact if I if I apply some thread here which is which will ask it to wait for some time if I say a thread dot sleep for let's say 3 seconds so that we can see the difference there and if I say surround with try catch maybe with TR catch applying a sleep here for 3 seconds basically and if run this code you can see is not fing a query and the moment you say use it and after 3 seconds it is filing the query that means load will not give you the object at the same time so what it gives you is a proxy object so get will give you the object but load will give you the proxy object now this proxy object will be a blank object it will it will simply specify hey we got the object but it's not an object okay it is just faking it's not there's no there's no actual object there it's a proxy object now what happen so this is this is the difference between get and uh load so get will give you the object load will not give you the object load will give you object only only when you want to use it so it will basically give you a proxy object when you say load yeah now this is helpful when you have uh when you when your object is depend upon some other object let's say uh to create an object of let's say alien you want object of laptop so every alien will have a laptop in that scenario if you want to make an object of alien you can you can pass a proxy object of laptop yeah so we don't normally use load we always use get for when you want to fetch data but there are certain scenarios where load make more sense right something where you want to pass a proxy object you don't want to pass the object object you just want to run the code and that's it okay so that's where you can use load now there's one more difference between get and load in terms of exceptions or in terms of data which which is returned example let's say if I pass an ID to 2001 and I don't have any ID which is 2001 and if I use get method okay so if I in my database I don't have any row which has 2011 as a row number so if I run this code and you can see it it prints null of course I don't have a data there so it will print null and yes if you try to fetch a specific data it will it will throw null Point exception yeah so if on this query you can say it throws null Point exception because I'm trying to access the data from null object or null value but if I use load what will happen now load will not give you null let's see what it gives so the moment you run this code it throws an exception and that is object not found exception yeah so get will give you null and load will give you the exception it throws this object not for exception so you can actually handle it and you can and make it work so some some way uh you are getting an exception so you can you you can use strike catch you can write in the catch block what to do when you get the exception you can handle those things so that's how this uh load and get works and this is the difference so normally we use get but there are certain certain specific res specific times for example if you want to uh create if you want to use an object as a fake object or just use a proxy object we can use load there so that's how you can use get and load in this video we'll talk about what is jpa and how do we Implement jpa now before going for jpa there are some prerequisites so so to understand this concept you need to understand what you need to know what is Java you need to know what is how to work with databases and how do we connect this two with the help of jdbc because if you don't know how to work with jdbc then this video will not make any sense to you and having knowledge of hibernate will make will be added Advantage but that's fine even if you can if any you don't know hynet that's fine we can still try to understand what is happening here so Java database andits right and now we can jump into the concept so before implementing it we have to understand the theory of jpa right so what jpa is so jpa stands for Java persistence API so what exactly this means so let's get back to the world of it so when you say you are working in this it world or technological world we are working with information right we are working with data so whatever we use it's all about data right if you if you're using Facebook if you're using Whatsapp if you're using any Enterprise application as well it's all about data right so your company is spending so much of money why because they want to work with data so if you want to work with data you use softwares right in software we process data now those data are by default temporary data because your application is working with some data the moment you close the application you will lose all the data what you want is even if your application is shut down you still want to purist that data somewhere where do we our data so nor we can data in a file or we can Pur data in database which is your dbms or rdbms you can say maybe Mas Oracle there so many RBS available yeah so question arise how do we save the data so we have one way you have a Java application you have a database you can JBC but again there's so many issues with jdbc you know uh you have to fetch the data so normally in Java world we work with objects and then in the database world you have rows and columns how can you save the object in data in the rows and column format and that's where we have this concept of omm so what om is om basically means it stands for object relation mapping where you have a class so you have a class structure so then that class will be having some variables and then you have a table now if you can compare your class name will be your table name and your class properties will be your table columns so we can do that m there okay so we have to use now is a concept which is applicable for different technology it is there with net we have for Java so there are different languages or Technologies where we we can use but the problem is to implement in Java we need some tools right so when you say tools we have hibernate is one of the tool we have IP is one of the tool we have toping so there are so many tools available so you can use any of this tool and you can make it work so using hynet using I you can build a om application now the advantage would be if you're using hnet or any tool let's say you have an object yeah so let's say that so you have alen object alen object you have alen ID alien name and alien technology or maybe you can also talk about the salary of alien and then which laptop alien use or which technology is working on okay we discuss about this but then let's say you have alen object and you want to store that alen object database so in jdbc what you do is you fetch one one value and you store it that's a good that's a good way right so what om says is take object and save into database directly that will be awesome so to implement we have different tools so every company they want to use now but the problem is we have different tools right so let's say today if you're using hynet and entire application is buil using hynet now let's say after two years you thought okay IB has some extra features and we want to use those features so you need to switch your application from hiber to IUS is it that easy maybe your application is built on top link and you want to move it to hiet is it that easy to switch between different tool you can switch if they are if if they using the same type of structure of same specification unfortunately all these tools they were having their own way of working they they were independent right and that's where this sun micros system they realize you know we to have some common standards and that's where they introduced jpa which is Java persistence API it is basically a specific so all these tools will be hnet I top they will all be they all will be implementing jpa standards or jpa specification so that if you want to switch from itis to hypernet it it will be easier because you're just changing the tool the implementation but the specification is still same yeah so that's the advantage of using jpa in your project but yes if you are building an application purely on hynet you will face the issue of Shifting but if you build the application based on jpa it will be easier for you to switch between hypernet to I but hold on when we have jpa then why we need hynet as I mentioned jpa is just a specification so if you want to work with jpa you do need hibernate or IES or any of this for the implementation so if you building a hibernate project then you are working on hnet if you are building a jpa project then either you're using hynet for the implementation or I you cannot simply make a jpa application without any of this tool yeah so hynet is important even if you're using jpa the importance of jpa is if you want to switch from one technology to other tools you can do it very easily in fact in future if you're working on Spring boot applications and then if you want to work with database we normally go for spring boot jpa so this will be your first step towards jpa in this video so let's try to implement whatever we have talked about now what I will do is I will create a new project so you can see my screen there and let get a new project here and I will say this is a mavin project uh okay where's mavin it's not here so let me say mavin I want to get a MAV project and in this MAV project I will mention I want to create a quick start project because I'm not building a web application here we'll say next okay so it does takes some time to download from the internet yeah so it is asking for the group ID I will say group ID is com. telescope and the project name I will mention is jpa demo again you can have any project name doesn't matter click on finish and you can see we got our project here now in this project what I want to want what I want to achieve so it's very simple I just want a main method yeah so I want a class which will have main method so that I can run the application so even before with anything just want to verify if this code works or this Eclipse works that that should be your first step so I will as uh Java application and it worked you can see we got hello world but then I want to implement a typ of thing now of course to save data somewhere because jpa will be helpful for saving data right so we need a database unfortunately I do have mq here installed so you can see in my machine I'm using mq you can use any dbms which you want it's completely fine uh you can use post you can use Oracle there will be some modification we can handle that so I'm using Mas specifically I'm using my workbench to handle my again you can work on command line as well if you want it but I'm I'm a good big fan of GUI so I have a workbench here and the database name which I've created is na in that database I have a table alien so I've already done this beforehand so that you know I Will Not Waste much of time in the video so in this alien table I have those five records you can see we got one na Java two ARA Big Data three David net and for an PL m not ML and then turn Oracle so we have this five entries there now I want to fetch this data using application or maybe I want to store some more data so what I will do is I will go back to my eclipse and I want to create so first of all to fetch that data since we are using om tool you need to also have a class which will represent the table so let me create a class for you here so I right click here I will say new class and this class will be alien and we'll click on finish so we got our class and then in this class we just need to mention those three properties so if you compare we have a ID we have a name and we have tech let's do that so I will say private in a ID then private uh what string a name a name and then private string Tech so I hope we only have three variables and that's right we have three variables we have a ID a name and Tech so your columns will become your variables in the Java Java class and then these three variables need get sets I right click here I say Source generate get sets and okay so we got our class and we got our get we also need a two string method so I will say SCE generate two string so that if you want to print an object you can print the values of it in printing the hash code okay so we got our class here that's that's cool right now what I want to do is I want to create that object of alien or maybe I will try to fetch data from the database so what I want to achieve here is we already have those five records right I want to fetch the data from database is it possible let's try so what I will do is I will say alien a equal to so when I fetch I want to specify some ID right so I say hey I want the alien with name or with ID which is three yeah so when I say three it should print David andet but will it work so I would say hey jpa please give me a alien object with ID four now or no not four three now will this work of course not right we are we are working on Java technology this is not Java this is normal English right how can we Implement that here so to work with jpa we have to do some settings right jpa is not a Superman right we need to we need to make it understand what you're talking about so how will you use jpa here so there for that have to do some settings the first thing is uh jpa has this special method as find now in this find you simply have to mention four and then you have to also mention the class name so alien. class and then you have to mention four so find is a method inside jpa you have to pass two things the first one is the class name the type of data you're fetching and the uh the ID which is for but can you simply call a method without a class name of course not right what we have learned ja in Java till now is if you have a method if you want to call it from some other class you need to create object of it or if St static method you can simply use the class name how do we access find here now to use this find we have a special class or the interface which is called as entity manager now basically all the classes or all the classes which represents a database table in jpa is called as entities now in fact you have worked on databases we have e diagrams right so all the tables are also represented by entities so alien class is an entity for us right so to manage those entities we have entity manager because in your application you might not have one table or one class you might have 10 classes so 10 entities so we need a manager to manage all those entities so yes we got one manager but question arise how do we instantiate it unfortunately this that's an interf space uh we cannot even say new new entity manager that's the first problem the second problem is if I say control space it's not working you can see I'm not getting the uh the the uh the package now the package is not there is because in the M dependencies I have not mentioned that I want to work with jpa or hynet so as I mentioned jpa is only a specification right you need to implement that with the help of hnet or or I bet or top whichever you like but hnet is one of the top container in this list so I want to implement jpa with the help of hnet so we have to make sure that we do have hibernate dependencies in this project and how do we do that so I will open Firefox browser and where is that it's get getting open and in this Firefox I will download the dependencies for hynet now we don't have to actually download one by one J file thanks to M it gives you a concept of M repository where you can actually search for hynet so when you search for hnet it will give you a hibern repository in fact you can just go to the website of me repository and here you can search for hibernate that will give you the same same list oh which is not giving the same list let's search for H again uh it is giving a different list so you can see the first link which is core hybernate functionality again if you have seen my hibernate entire series uh we have already done the steps yeah so if you know hnet this is very easy for you you can skip this skip this some steps and then go ahead but then if you have if you're new to orm Technologies just watch the entire video so that it will make sense so yeah let's pick up the latest one here let's pick up the 5.2.8 and I will say copy in fact uh yeah let's say copy let's go back to our file and paste so we got one dependency for hibernate and the moment I do that can see that in dependencies it will download all the dep dependencies for you now it's also depend upon your internet speed how fast should be mine is very slow today uh it's taking some time now by the by the time it it downloaded the hyper dependency we want one more now see if you're working with masle you need a Mas connector right for working with Oracle you need Oracle connectors different dbms have different connectors to work with and for this one we need to use MySQL connector as well so by the time it gets downloaded I will search for mnor the first one and I will download which version I always prefer the stable one this looks stable there's no problem with this let's copy this one and go back here paste and you can see we got high dependencies and in some time we'll also get myql one because I just saved it it does take some time to get downloaded and you can see we got Mas Conor as well now once you got this to two things we can simply say control space now and you can see we got we got the uh the UTS and now if I say enter we got entity manager belongs to persistence package y so that's the power of of this dependencies okay now how do we create object of entity manager now unfortunately we have to write one more step to implement that to create object of entity manager we need to create object of entity manager Factory so this follows a Factory design patterns you know all the all the latest Frameworks they all follow design patterns it's all about design patterns you know uh so we need to use Factory design pattern here you don't have to implement it okay hypernet or JP has done it for you if you want to create object of En manager you need entity manag Factory so we'll say EMF any object will do but then question arise how can I create object of enti manager Factory good point uh okay so what we will do is there's a there's a method called as there's a class called as persistence persistence dot is it a class or interface is it a class it's a class so persistence dot the method name is it's what get entity manager Factory no not this one is it create enti manager Factory yeah so the method name is create entity manager Factory and we have something here again what is that something I will I will I will tell you that in some time but you have to pass something here now once you got object of entity man Factory you can create you can use that object manager Factory and you can say create manager uh okay so just to repeat what we have done we have created two objects enti manager en manager Factory and now using Em which is the entity manager Factory you can say em doind and you will get an object so what do you think if I if I've done this will this work of course it should not work because something is missing now nowhere we have specified which dbm you want to work with I mean we have specified it in a form file which is this this form file here but then uh how do your project knows that the app knows which D you want to work with or which database you want to work with or what's your username and password we have to specify those properties somewhere right but where now normally when you work on different Frameworks you know when you work with hynet jpa jpa with hynet or spring framework or St framework we do specify the configuration file or the property files we can say so here as well we have to specify some properties where now we can specify those properties inside SRC so inside SRC we need a special folder uh inside Java is it inside Java let's verify uh no inside main I want a special folder called resources so I will say folder the folder name is resources slash and inside that the folder name should be meta NF this is the exact path you have to follow so it is resources meta and inside this folder you need to create a XML file you can you can specify the properties I will say new and this should be an XML file yeah so let's say XML because XML is a fancy thing right in fact we have properties files we have XML files thanks to Spring framework we also we have Java configuration so I will say persistence dot XML and here we have to paste that we have to use that XML XML code now in XML you can specify your own tags I mean the framework will have their own tags so in jpa we have a tag called as persistence and in that persistence tag you can mention a persistent unit and you can mention all the properties what I'm talking about this thing so you have to specify Properties or persistence and inside this POS you have a POS unit now why unit is because let's say if you have multiple settings you might want to work with different settings for different applications or different Behavior or different time so you can specify different POS unit and you can select one at a time example this properties they have a unit name as pu you can create another one as pu1 pu2 pu3 you can create multiple person unit and then here you have to mention these properties so we have a driver name we have a URL username and password now once you haveed all these things let's go back to app now how do we link it how do we link this code with our XML and that's where this double quotes comes into picture this is where you will mention your persistent unit name pu can be anything it can be ABC it can be my jpa pu it can be my PO it can be P doesn't matter right but you should have you should have a same name at both the side now will this work everything looks cool right let's verify so what I will do is I click here I will say run as jav application and you can see we got error now what error we got is it says unable to locate persister which is alien it's there the class is available here why it's not able to find it it's because when you work with jpa jpa basically works with entities what is entity uh so we have a class and we have a table so somewhere in between we have entity how do we convert this class into entity how do we mention that this class is not nor class enti so for that we need to use a special annotation here which is called as ADD entity this this is what will make this class as entity and then we have to use at ID which will be your primary key because every table needs a primary key so two things the first thing uh the first thing is the entity and second one is at ID things that's it now will this work I don't know let's verify I will go back to my app and I will say run and oh something has happened and we I don't know will it is it successful the way you can verify is by printing the object right so if you this if you printed the object it will give you the output right and you got it can see that we got our alien object there we got a I mean four is four Anna and ml you can change the value from four in fact you can also value from the user I will say I would say two and two I don't know who is that let's run this and two is AR it's that simple yeah so this is how you can save you can fetch the value how can you save the value because I want to also want to see how do we save it so what I will do is I will go back here I will say alien AAL to new alien and then I will specify some values I will say a do set a ID as let's say six maybe I will go for N I would say a do set a name as uh maybe uh Maria and I will say a do set Tech okay which Tech is the meing now I would say Tech is Hardware yeah and then uh once I got this details how do I save it now to save this it's very simple you can simply say EF now to fetch we are using find to save the method name is persist you're persisting it and you can simply pass a and this should work let's verify let's run this code and you can see it is pending here but then is it affecting database let's go back to database here myql and okay so there was some issues I just I was just solving the issues that's why I insert this inserted a new ignore that R and I would say run this code let's verify is it Maria there no we don't have Maria there that's weird now the thing is when want to change data in a database we use something called as you know we have transactions to manage asset asset properties so here we before saving we have to start the transaction and before and after saving we have to commit the transaction and the way you can do that is by saying Em which is the entity manager get transaction. begin this is how it will start the transaction for you and here we'll say em do get transaction and we'll say commit is that simple yeah I know the video is being getting very ly now but then that's how it works so yeah so now will it work let's verify it should it should it should yeah so you can see we got the output but then let's verify in the database here we go we got nine Maria Maria and we got Hardware simple yeah so uh now let's see what are what the things we have done we have simply created a m project uh we we created this app class we already get we already had that class here in this we have Creed the alien object and then two objects are important one is entity manager and the en manager Factory so using Factory we can create object of entity manager we just need to specify the the persistent unit name which we defined in the XML file again this XML file should be there in the resources folder or don't worry I will push this project on GitHub so that you can access this project you will find the link in description description area so that you can you can just pull it and you can use it for yourself make sure that you make those changes here the the URL and the username and password depend on on your machine and inste alien you can use add entity at ID and then we have this property we have this hybernate dependencies m dependency now there's one thing how do we know okay let's see if you want to understand everything about jpa because this is only this is only one video right so what you can do is I do have a playlist on hypernet so if you want to understand each and every step why I mean what are the other possibilities we have with entities can change the column name can change the or what if you don't have a table can you create table by yourself you will understand that those stuffs when you start when you start learning hnet Series so after watching this video If you enjoyed it on the like button but then also watch hnet enti series and then you will get the idea or the power of om tool the main idea behind this video is if you want to understand jpa this is how you implement but then we can also do it with hnet right why you have to use jpa here it's because in future if you think okay let's change I don't want to work with maybe let's say if you don't create a JP project or if you create a hnet project from start the problem is in hnet everything what you do is hnet code example these two objects will be based on hnet framework or hnet packages the thing is in future if you want to change it you need to change the so let's say from hnet if you want to move to I or maybe top link you have to change the source code as well but if you look at this project project which we have built here even if you're changing from hnet to I you just have to change one thing and that is your dependency that's it everything remains same that's the beauty okay maybe some other changes as well but then that's so it does it require only minimum changes when you want to move from different tool so that's the beauty about jpa I hope you enjoyed it do click on the like button and do subscribe for for the [Music] videos if you have been in this web world or web development world you might have heard about rest API right doesn't matter which technology you're working with maybe you're working on Java asp.net maybe uh PHP Ruby you must have heard this term called as Rest full API so what exactly this rest means right is it very different from the web world or is it very magical things so we'll discuss about that in this video and if you want to implement that we have the entire series on that so let's let's get started welcome back in you have lots of data right and this data will be coming from hold on from where you will get this data so you can give static data right you can just assign the data there and it will never change example the temperature in Mumbai is 28° Celsius you can set it that way the temperature in Delhi maybe 31° celsi you can set it that way so it is permanent right but then weather keep changes changing right according to the time so let's say it is 6: p.m. now so again the time the weather will change the the climate or the temperature will change uh if it is 12 noon of course the temperature will be different so how can you change the data now of course right your mobile phone doesn't have a sensor even if you have a sensor you can sense the current climate right if I'm sitting in an AC room so of course I will get a current temperature I will not get temperature outside of my house or I will not get a temperature of Delhi so how can I how can I get all these details so the idea is what you have to do is if you want all those reports what you have to do is all over the Mumbai you have to put sensors so just around above the building just put sensors a weather sensor around the around the Globe everywhere you have to put sensor because wherever you don't have a sensor of course you will not have a data so if you want data about Delhi you have to put Delhi Delhi in in Delhi you have to put a sensor you have to go to us you have to go to New York to get the New York climate and you have to put your sensors everywhere and that will cost you billions of dollars of course right you will not do that so that means you need this data from somewhere but question arise from where so what happens is luckily we are so fortunate that we already have some web services available on the internet when I say web services it simply means that there is some servers or some Cloud somewhere in the world and what you are doing you are trying to access that cloud right imagine I have a mobile phone here and that's my app that's right not actually my app it is already app in my mobile phone and on this side you have a server right now this server will give you a data but then question arise how can you generate this data I mean which technology you will use here so basically this is a normal web server you can take any Dynamic web server not a static servers Dynamic servers where you can generate data so this is your Dynamic server and that's your mobile phone so what you will do is you will request your server now this server belongs to some company maybe uh maybe there are different uh different different providers of weather reports right so what you can do is they these people who who has all the sensors or they have the data they are ready to give you the data right and what you have to do is you just have to call them so there is a API so what is API now so API is something which will connect you and that server right so how would you know that how many services that server provide and how a server will know who are you right so if you want to access that data we have to use somewhere API somewhere in between but if you keep that API part aside let's talk about server what do you think how will you generate data now I'm from java world so I will take a Java example here so you have to create a suet right that's how we do that's how we data right so you have to get a subet a client will send you a request and then this subet will give you the data but then question arise what Ser returns now if you think about normal World okay now if you're not familiar with Java that's fine you can imagine a p PHP application you can imagine a ruby application or ASP application all these applications they return something normally that something is web page now when I say web page it will be a HTML page now when you request from from a browser so when let's say if you have a browser that makes sense right because browser is a blank thing and you will get the entire web page but hold on if you talk about my application I already have a design right I don't want design I just want data so when I request to the serlet now sulet should not return uh HTML page because I don't want HTML page right and even if I get HTML page how will I pass it so what what I will ask is hey hey salet I don't want a data I mean I don't want a page I want data salet will say okay you want data I will give you data in maybe two formats you will get data either in Json format or XML format now you might be thinking why Json y XML it's because whenever you return some data data should have some structure right you cannot just randomly send any data so when you send data it will be a complex data you have to follow a particular structure now if you follow XML in XML we have this hierarchical structure right so in exml we have this hierarchical structure where you can have a a a c a country name inside the country you'll be having city name inside city you'll be having different different values right we have humidity level we have temperat you'll be having all these values right so you can return XML format but the problem is there are lots of people they don't like XML especially I don't like XML but Json is something you can can you can actually work with because if you know JavaScript in JavaScript we create array objects so those are basically your Jason objects right so what we can do is we can instead of using XML we can also use Json now again you can use XML you can use Json right so your sulet returns you XML or just format and your job is done right you are getting data but the only thing is when you talk about this data if you talk about this solet it's it it creates lots of overhead right right we have a server technology where you have to create a post request I mean you have to write do do get do Post request object response object a bulky thing right so to replace this we have a simple solution and that simple solution is using rest API in any technology maybe Java PHP so rest says if you want to return some data what if you can simply create an object of that on the server side and just return the object itself uh not object but the values of the object right so you have a object you want to send the state of an object right you don't want to send the object but the state of an object imagine if I want to send a weather report something like this you have a city name then you have a uh uh uh temperature then you have humidity level so you will send all this data to the client side right so if you can create an object of this you can simply send an object in Json format or XML format right so that will be converting an object into a a state right so that is called as representational state you're transferring the state right so you're you're sending the state of an object that's why you call as rest which is representational State transfer so what you are sending is not object but the state of an object now that state of an object can be sent using Json or XML so that's the first thing then what if so I mean think about this web world right what we do doesn't matter which website you're working with which uh what requirement you have we do one simple thing we do Cod operations right we we create we create the resource we read the resource we update the resource and we delete the resource now okay now if you observed I said resource right now why resource is because in this world technology we we want something from the server right so we we example if I want to add two numbers whatever so what you say hey server just add these two numbers for Me 2 + 2 it will give me four so when you send a request so if you send a normal HTTP request it look something like this right we have HTTP www.co.com let's let's looker my website and then if you want to add two numbers you will say add and then you will give a question mark which will which will send the values so you will say question mark and then uh two I mean query parameters right you have to send question mark and then values instead of that what you can send the what if you can send the value something like this okay instead of going for ad what will make more sense is you're fetching some data example if you go to my website uh we have this awesome feature you can get your you can get questions right so if I say uh HTTP I mean the website name slash questions SL Java now this will make more sense instead of saying uh questions question mark subject equal to Java right it looks more like a resource so what you're fetching is actually a resource right so instead of fetching in of instead of thinking that you have a server where you're doing some processing instead of thinking like that what you can think about is you are fetching data as it is as a client you don't even know what is happening on the server what you know is you are getting your resource back and to get to do this to do cud which is create read read update and delete we can actually use rest I mean HTTP methods right in HTTP we have post method we have put method we have uh what we have else we have get method and we have delete right so using these four methods of HTP we can achieve our code operation and that's how your rest API works again before rest we were also having soap request which is which is soap soap web services but nowadays everyone uses restful apis in fact all all the companies they are moving from soap to rest but then question arise how can you implement rest so we have two choice I mean we have multiple choice U most one of the famous implementation for rest is Jersey and the second one which I love is spring implementation of Jersey or spring implementation of restful API so in this course uh we'll be we'll be focusing on both we'll see some examples on Jersey and most of the example we'll see on Spring rest so I hope you likeed this video you got something about the API again we we'll talk about the complement implementation in the subsequent videos we have talked about rest API which is also restful web services and it is booming everywhere so what exactly we are going to start with I mean what type of example we are going to start again we'll be going for a complex example once you go ahead but initially we'll be talking about aliens so if you want to understand rest API to understand the overall picture let's talk about rest API with an example which is one one of my favorite example called as aliens I mean see we all are aliens right of course you can treat yourself as human in this case so when I say alien you can imagine yourself as human so what is alien alien is someone who who belong to this software world or the virtual world what you say and everyone in this virtual world maybe they are very technical in one of the field maybe database field or programming field or maybe testing field or hacking field so all these are aliens right and every field so let's say if if I'm a programmer if I'm a Java programmer so I'll be having some points in that example when I start with Java as a fresher I'll be having maybe two or three points the moment I get experience I get 10 points 50 points maybe uh as as experience of six years now I can say I'm I'm 70 I got 60 points out of 100 in Java maybe you might be having more or less doesn't matter so we all are alien so we have a name we have an ID and we have uh points so if you want to create a class of this this is how your class will look like right you'll be having a Class A simple class called as alien in which you'll be having three variables alien ID then you have a name and a points right so if I create object of the first alien let's say the first alien name is nav itself I will say alien ID 101 or name nain and then the points would be let's say 60 so that's my value let's say if you have one more object so now you have two or three objects right now if you want to send this data on the client side you have to convert this data into XML or Jason again how will you do that that's a different thing we'll be we'll be seeing in the Practical session but I want to send this data in XML format even before sending this data we have to accept the request right and to accept the request first you have to create the application now question arise how will you create an application for rest API again to the prerequisite for this would be you have to understand mavin because using Maven you will be building this project and luckily I have a I have a video on that as well on YouTube so first you have to understand how mavin Works uh second we have to create a jersey project now why Jersey it's because rest is just a concept right and to implement that we need some implementation you can use Jersey or we can use spring as we discussed in the earlier video so you can use one of this now if you are using Jersey of course you have to use Jersey libraries you have to create a project structure which will be working with Jersey now you might be thinking okay we have to create that structure don't worry uh we have a mavin video right and in in the mavin what you can do is you can ask mavin hey mavin can you just give me a jersey project mavin will say okay you got your jersey project there so don't have to worry about the project creation it will be taken care by mavin now once you got your project there will be some project structure there will be some uh dependencies which you have to add now you don't have to add any dependencies right because JY will be given by mavin and but then we have to create some resources right as we have discussed uh in R API we have to create a resource alien is our resource right but but then it is simply a pojo class right I mean what what is pojo it's a plain old Java object so this is a plain old Java object what we want is when you send a request there should be someone there who will respond who will send you a response now how will you do that uh again if you're thinking in subet technology in subet we have to use we have to get a class then we have to extend our class with the help of HP seret do we have to do that here uh not exactly we don't have to do all those things here in fact I I believe that rest tap is much simpler than learning Ser technology and you might be knowing about Ser technology even if you don't that's okay uh because anyway learning rest is easier than learning Sate so if you know the rest API thing there so using rest API we can send a request and you will get the response so there should be someone who will request who will accept your request and who will send your response so once we have seen the theory about rest API let's try to implement that but then before implementation of course we should be knowing which tool we are going to use right so in this video we'll discuss about the tools we require so welcome back aliens I'm N Ready and let's get started so what we are going to do now is we'll try to understand what are the tools we require and as you can see I have already using I have already opened Eclipse here now question arise which are the available options you have again you can go with any one any IDE which you prefer maybe if you are in love with net beans if you're in love with intell J you can go with that the same example which we are doing here will work there as well right so we have Eclipse we have net beans we have intell J so if you can just just go to the website uh so just search for Eclipse download it will take you to the website and you can download the eclipse from here right so if you can search for Eclipse IDE for Java e developers and yeah so you have to make sure that when you download Eclipse you have to make sure that you are going for Java e version uh it's because when you talk about about neon itself neon has multiple version as you can see we have Java developers uh we have Java e developers the difference here would be uh since we want to work with West API which is a part of web development so we need we need some servers right and to add those servers this Java e developers version will give you the option so I will go with this one which is Java e developers and depend upon which platform you're working with maybe Windows Mac or Linux you can download particular version now once you download it you have to just have to unzip it and you are good to go so once you open Eclipse this is how it looks like so this is how your Eclipse looks like now once you have um you know once you have downloaded the eclipse and when you open the eclipse it will ask you for the workspace you can mention any folder which you like I just mention the workspace as YouTube and I'm good to go and then we have to also make sure that you add a server now again I have a separate video on this on YouTube just search for uh telescope eclipse and Tomcat so it will give you the video where you can watch how do you connect this Tomcat with Eclipse but still if you want to check it out so what what I will do is I will just delete this part I mean okay what what you can do is it will not give you this option there so it will not be having this Tomcat 8 by default so you have to click on this so there will be option of no Ser available click here so just click on that uh so same thing I can get here right click new and server now most of the time you know you don't get this server tab here so for that you have have to make sure that you are in Java e perspective if you're not there just click on this button which is perspective and select Java e now luckily for my machine It Is by default in your machine also it will be by default but in case if it is not there just make sure that you are selecting a Java e perspective and then go to window Show view there is a option of servers now when you click on this servers it will give you this window okay the current window which you working with so right click and say new click on server and now out of all the servers you can see when you open that it will give you list of servers here and depend upon what type of plugins you have installed in eclipse this is not the new Eclipse I'm using I'm using this eclipse from a long time so I'm getting option of red hat and all those all the things so if I open E Apache uh you can see we can have Tomcat 8 version but unfortunately okay uh I mean before doing all those things you have to download Tomcat as well uh so you have to go to Google and download Tomcat and then you can select and say next it will ask you for the browse where is the folder just select that and you're good to go okay so once you got Tomcat once you got Eclipse it's time to create a project now to work with this project what we what we'll be doing is we'll be creating a maven project now for this of course you should be knowing about Maven and if you have not seen that make sure that you have seen my Maven video it's there on YouTube uh so search for teliscope mavin uh you will get some idea how to get a MAV project but if you already know know that that's great so say new and go for others and let's say I want to go for a maven project and say next uh we'll say next now which type of what type of arch type I have to use here now mavin is famous for that those Arch types right in fact uh if you want to create different kind of project mavin will say okay you want a project I will give you the project don't worry so I will go to filter now I know that I want to build a jersey project now why Jersey because to implement rest we have different implementation right and Jersey is using is Jersey project is one of the implementation again we'll be doing this in two steps I mean first we'll focus on Jersey and then we'll also see how to do rest API using spring uh using spring MVC so here what I will do is I will search for Jersey and you can see we have lots of arch types already available for Jersey okay as you can see we are getting lots of option here but it is possible that these options are not available in your machine okay it is possible so in that scenario what you can do is okay when you search for this it may happen that your mavin is not able to interact with the internet okay it it may happen in that scenario what you can do is first you have to make sure go to Eclipse go to preference and in fact it was not working for me so I did some changes so go to Maven click on Maven here and you have to select this four text you have to make this four text you have say download AR type Source download AR type documentation download repository index uh I mean this is the main thing and then update M project on Startup so select all these things click on okay and you have to make sure that those four ticks are there and now create a project it should be working so I will say again a mavin project I will say mavin project and then I will say next and I I know I mean you know right we have to search for Jersey here okay we'll search for Jersey and okay so it is coming very fast on my machine because I've already done this in my machine if you're doing this for the first time it may take some time so I want to go for a web project right so I will say JY quick start web project web app and click on next now it will ask you for the group ID as you know mavin so we'll say com. telescope and the artifact ID for me is let's say demo uh demo rest so that's my web application which I'm I'm working here once you do that just click on okay and your job is done can you see that you got your jersey project and if I expand that not not this our tab if I expand this project we got SRC and we got a Java folder inside the Java folder we have com we have teliscope okay this this project is still in building phase as you can see there are still creating this project and this project is coming from the internet as you can see it is downloading everything here okay so it is downloading the just the dependencies whatever we need uh and that's the beauty about mavin you know if you are doing this in a normal project you have to add all the jav files by yourself you have to uh create uh you have to do some settings by yourself right thanks to mavin it will give you the project so yeah that's how you create a project in the next video we'll talk about how we can write our first code in Jersey and let's see let's do some uh settings there so that's it guys that's our project building and as you can see the project is already built here I was supposed to stop this video but then it is already buil uh let's expand this Java resources as you can see okay let's have a look on these things okay uh so let's expand this and okay where is that so if I expanding it it just take some time because it is downloading the source code as we have checked everything so let me just click on that once again it is taking some time as you can see by the time it gets open you can focus on your palm XML file which is the most important file for your uh for any uh any MAV project right so if I double click that and my M machine is working very slow now as you can see because it is downloading lots of things from the internet and it makes a system very slow and luckily I got a good machine otherwise if you are working on normal machine does take lots of time okay so yeah so we'll talk about this we'll see how this works we'll so as you can see we got this project here it's working slow let me just check it out here what's going on behind the scene okay so it's taking some time so what I will do is I will stop the video here in the next video we'll discuss how this thing ex actually works so now let's try to understand this project structure now if you remember in the last video we have talked about how to create a jersey project and we have done that right you can see we we got this project here now this is a simple web Project based on mavin and again you if you open this form XML file you you'll be getting all the J just dependency there now the most important thing is let's try to understand this structure here so if you expand this SRC folder and if you expand this Main and if you have ever worked on S JSP before you you know this structure right so we have this Java folder where you'll be having all your Java files of course right when you talk about s when you talk about uh your model classes everything will will belong to this Java folder and all your pages like example uh JC Pages then you'll be having your JavaScript pages will be belong to this uh this we web web app and if you expand this web app we we already have a index.jsp now you might be thinking this is a this is a rest project right where you will return data in a text format or Json format why do you even need this index.jsp uh I guess uh when you create this project is in a jersey format they give you this page just to test your application if your if everything is working or fine or not this is not exactly what you will be using so this is our just this this is just a test file for you so if you open that you can see it says uh the resources you will find everywhere you will find it my resources and it belongs to web API again those are things are there and this is only for testing okay and then it prints project Jersey website and all those things but if you can see we are getting an except we are getting some error here it says the superclass Java x.l. HTP doset HTTP serlet is not is not found uh the problem here is When anybody create a web web project you have to make sure that you have Tomcat assigned or any web server assigned here so in the Target run time you have to take this you have to say check mark which is apach Tom 8.0 Target connected and now if you click on okay you can see the error is gone so you have to make sure that you are assigning some web server to your project so that it will understand that it it will get that HTTP solet uh class now everything seems good so let's let's try to run this application oh before running I have just one more thing if you open this Java folder inside the we have com folder we have the entire package list here right we we got demo rest and inside that we have a file a Java file called as my resources now inside this my resource you can see we have a me get it okay so we have get it which has two things which is add it get add produces what it means that we we we'll talk about this later in detail time wi we don't want that let I mean let's keep it there so the thing is if I run this application if I if I click on this button as you can see you will getting you'll be getting this page when you click on this web resource you will be getting something right so even before clicking on that what I will do is I will just focus here I want to I want to print get got it doesn't matter where I print it maybe in the console or maybe in the page so let's try to understand that how that works now to run this application what I will do is I will right click this I will say run as run on server so I already have a tomcat server in the running state so if I click on that it will restart my server of course and if you run this um okay now you got this page right that means everything is working now that's and that's what you say uh a delusion now if you if you open this console you can see we have an error do doesn't matter if you got the output but still we we got an error right the error is injection manager Factory was not found again uh due to some update they have change they have made some changes uh so this thing is not working in the new version uh so what I will do is I will do I will make some changes here so in my pom XL file if you just go down you can see we have Jersey version which is 2.26 hyphone b06 you have to make sure that you you make it as b03 okay it is working in this version I guess there's some problem with the new version so if I say save uh it will change my resources if I expand this Java resources and if I expand libraries inside which you have M dependencies you can see the Jersey now changed to 03 uh I guess it will work now uh let's run this once again uh okay let me just restart my route restart my um thing where is my server it's here and let's right click and restart I hope it will work this time so it's running it's running as you can see there is no problem now there's no error and let's get back there and if I say enter okay you got the same output in fact I don't want this output to be to be used in the internal browser I hate internal browser of eclipse I want to use my own browser I will use Firefox so just go to window go to web browser and click on Firefox uh now if you go to Firefox again next time when you done this it will it will open in Firefox but this time I will open the Firefox by myself and it will say enter oh what was that ignore that let me just copy this I guess that this is some number I copied from somewhere let's enter not here what's happening okay so if I go to browser you can see we are getting this page now of course when you click on this it will call for resource but let me just type it by myself I will say my resource and if I say enter oh we got an error we I was expecting got it right which is a text which which should have printed maybe it is printing on Console let's go back to console let's see if you got if you got got it there oh it's not there so that that means it is not able to find the resource why it is not able to find the resource to understand that let me just go to one of the most important file in your project which is if you go to web you have web XML file now this is the most important file as you can see it says uh to work with Jersey this is the class which you have to use this is is your serlet container so whenever you send any request this is a class which will handle your request for Jersey but hold on it will accept only those request which are coming with this URL which is web API slash that means when you request for my resource you have to also mention web API SL my resource and now when you run this can you see that we got got it right so that's how your rest thing works again this is a simple text okay this is not a page this is a simple Le text again you can type it here in the address bar or uh what you could what we could have done is we can simply say uh resource or just a resource and it it sends the request to the same same thing and that's why I've not clicked on that before I wanted to show you how to access your resources with the help of web API again if you can change that from your uh URL pattern if you remove this it will work perfectly fine if you remove from here you don't have to put web web API there right so that's how that that's how your first web service works so let me just repeat uh we have not done any changes yet we are working with the same project which which Jersey gives you because I want to I want to make I want to make you understand what basics of Jersey here so that we can continue in our easy Pace okay so what is happening here so when you send a request from my resource this is a path which is linking So when you say when you when you go to your browser what was that let just remove that yeah so when you you go to your browser when you send my resource request the the request goes to web XML file and it sends the request to the server container which says I have to search for this path my resource and when once you get this path connected of course you have you have to use this class inside which you have only one method call as get it and on that we are mentioning get now what is this get get specifies whenever to send a get request this is the method I want to access is it a get request as of now by default all the request are get right so to specify it's a get request we are saying get now what is produced uh again we'll talk about this produce in in the next video but then we are printing got it right which is wring and it is going on the on the page so yeah that's it of this is our first jersey project so that's it from this video in the next video we'll we'll do some changes and we'll make our own stuff now let's try to create our own resource now if you can see we what we have done here is we have used my resource right and when you request for my resource we are getting got it but to make you I mean let's if you want to create your own resource now what I will do is first of all I will create a model object because see it's always better to return an object format right because that's what rest actually means which is representational State transfer so you are transferring the state of an object so for that you need object and to get an object first of all we have to create a class right so let's create a class here and we'll go for alien because I love aliens and I'll click on finish okay when uh okay just if you don't know what is alien means I call all my it it uh traines or students or colleagues as aliens because we are we we live in virtual world right we are not we don't live in real world so yeah so we have a alien here and then I got let's let's let's let me just use some variables here so again uh we'll go for some typical examples uh so we'll go for private uh string name every alien will be having a name and every alien will be having some points maybe okay when I say points it means for a particular so let's say if I'm I'm going to Java so I'm as Al I'm I'm an alien I'll be having some points right maybe 50 points out of 100 maybe 80 points out of 100 maybe 10 points out of 100 so every every alien will have a name and a point and for this what I will do is I will say Getters and sets we'll say Source generate getting Setters let's select all and click on okay so we got our class here right this is our simple pojo or you can call it as now what I want is when I request for aliens so let's say if anyone says Hey I want aliens I want to return the objects of aliens maybe one or two objects whatever you want to return initially we'll return only one and let's see how to return multiple multiple objects and if you want to return that in which format you'll be returning that that's that's one of the question so what we will do here is let me create our own resource again uh normally when you work on Ser application maybe some other framework like spring we always create a controll right because normally we follow MVC pattern there now here we are not following MVC pattern what we are trying to achieve is when you send a request you want a response as a Json format or XML format right now we can call it as my own resource so we can do it at here itself but instead of touching the I mean instead of changing the existing code what I would do is I will create my own resource and now it's not that difficult to do that right we'll create a new resource we'll say new class and I will call this as alien resources again you could we can go with alien controller or maybe only maybe uh some other other name but then this makes more sense right you should always create a project which when you should always use a name in the project which actually makes some sense so I will say alien resource which will return the alien object now since you want it to work with with uh a request of course you have to use one of our favorite annotations now if you remember in the earlier example where we have talked about uh this one which is your my resource so we have worked on my resource and my resource is first of all Let Me Close all this Textra thing so this my resource having an anation which is at path right this defines in which on I mean at what request you have to call this so I will say add path I will say control space so whenever I send this request uh with with uh URL okay so I will say whenever I call for aliens I want to call this resource right but again we need some method to work with it right so I will create a method here okay let me just say method name as okay so I will say method name okay so say public I will say method name as uh maybe it will return okay okay we'll say get alien but then it will return alien object right so we'll say alien and the method name would be okay the me name would be get alien now this get alien will give you an alien a single alien right okay so we have to create alien object we say alien A1 again you can go with any name doesn't matter we'll say new alien and let's assign some values there so I will say A1 dot uh set name I will assign the name of myself I will say naven and I will say A1 dot set points I will assign the points to myself let's say I'm assigning a point 60 out of 100 okay so I want to return this object and and the way you can do that is by simply saying return A1 and that's it your job is done here but hold on uh how this method will be called is it it will it be called automatically I don't think so but still let's run this code and let's see what's the error we are getting because that's how you understand what's going wrong right so let me restart my restart my server and let's get back to my browser which is Firefox here and instead of calling resources this time I will be calling slash aliens that's how that's how my [Music] enter and enter okay now if I go back to my browser and I will request for the same thing aliens okay this time we got a different can you see that we are not getting 44 anymore we are getting 500 that means when you go back here it says get alien call that's perfect everything is working perfectly but then it says message body writer not found okay so what you have to do is whenever you return something you have to specify what type of data you're returning so you have to show your intention right so I will say at theate produces okay uh atate produces we have to mention what it produce so we'll say produces and in this we will mention a bracket square bracket now there are some constants we can use we can say Media or we can simply say a application Json so we have different uh not Json application _ XML but that belongs to a class which is called as media type media type dot it is application XML so we'll say application underscore X what's happening XML right so you have to mention media type do XM application XML let's me import the package so that's your media type okay now I guess it will return what we are expecting here it says application underscore oh I've have made a mistake here it should be in capital letters is it capital yes it is capital so let's go back to my XML it's here this one yeah I and this should work now let's try okay when you say it should work sometime it doesn't work okay because of something is missing let's try this and let's run this application let's get back to our browser which is here and the same request oh again we are getting 500 it says get called now what is wrong the thing is whenever you send a request it is it is I mean alien is ready to get converted but then you know when you talk about conversion this object will be return in XML format right so your alien should know that which is the root element because in XML we have this hierarchy structure right we have root element then we have element inside that and we have element inside that so what I want is this alien should be the root element and then this name and point should be elements inside that and to achieve that we have to use one of the anation called as addage XML root is that XML root element so we have to make sure that we are using this annotation because I want to return a XML file okay now I think it should work let me start the server from a long time I'm saying this will work this will work but let's let's try once again let me go back to my browser and let's say enter oh can you see that it is working we got our XML data and you can it's so beautiful we got this alien name Alien tag here inside this is your root element by the way and then we are saying names and name and points right so this is how you create your own resource and I mean can you see that it's not that difficult I don't know why people think rest AP is damn difficult you can see it is so simple it is easier than sols I guess you know we just have to create a method and just return the resource just return return the create an object and return it again you might be thinking we are creating our own resource the own object what if you want to fetch from database we can do that very easily just write a jdbc code and fetch it from there or maybe you can use hibernate hnet framework here return the get get the object from the database and return it I it's it's that simple okay no nothing nothing to do extra but maybe in future we'll talk about how do we merge uh spring jpa and rest together so by the time uh this is this is how it works let's let's keep it simple now we have just Creed the alen alien class on on top of that we have WR added XML root element to Define that we are returning this in exml format and we are creating this alien resource where you are creating a method call as get alen with the with annotation get at get because we are sending a get request at produces to mention that what we are producing it and we got this object we are returning that object and everything is so simple so that's it from this video in the next video we'll discuss how do we return multiple objects right and yeah that's it so in the last video we have seen how to return this list right in fact let me just run this code once again just to show you what we have done in the last session so let me just run it as run on server uh let's say next and click on finish so by the time it runs uh already in running State let me just say uh okay demo rest we have to mention web API if remember right and then we have to request for the aliens so I will say aliens and you can see we are getting one alien one what I'm expecting now is it should return multiple aliens right so let's see if I create one more object here so the way we have navine here and let me just copy that let's paste it here and this time we'll go for A2 and let's assign this is A2 and A2 let's let's go for a different name let's say the name is Ari here and let's say the value is let's say 70 so we got naven 60 point Ary 70 points and now I want to return a list I want to return a list so what I will do here is I will say list of alien I will say aliens equal to and I will say new AR list or maybe I can simply say arrays dot as list we do we do have these two objects wi we can say A1 comma A2 and we got our list let's let me import the package for list and let me just do that what's happening just building automatically let's say control shift o and yeah we got our list right now once we got a list let's return a list so instead of returning a A1 okay I should be taking this window somewhere down right it will be better at least I will not be getting that popup every time okay so instead of returning uh a A1 I want to return uh aliens right so I want to return a list of aliens but the problem is here also we have to mention it is a list of alien right and now everything is set up I I think it will work now uh okay so I have to restart my server let me just do that and I don't have a server tab here let's say oh it's getting restarted automatically let's go back to Firefox and now if I say aliens now that makes sense right you can see we are fetching aliens and we got two aliens we got alien and we got alien so we got the first alien as naen second alien as Ai and that's the beauty about XML right so we are only seeing now how to fetch least of aliens but what about if you want to uh fetch only one one object now maybe I don't want to fetch all the aliens and the better name for this would be get aliens okay so let's say I don't want to fetch the all the aliens I want to be very specific now I want to fetch the first alien with uh name naven I want to fetch alien with name AI something like this if I say aliens SL naven it should return naven if I say a should return a but is not working now how exactly it will work that will see in the coming video we'll also see how to create a resource I want to create a new resource and there's one more thing you know normally what happens is you get this data from database and as you can see we are creating our own resource here statically so we'll also see how to create a separate class which will give me this object maybe a demo what you say a dummy database or maybe a mock database we can call it as so yeah we'll see that in the coming videos in the last session we have talked about resources how do you fetch resources here right so we have we were returning the list of aliens right so we cre these two aliens here and the moment someone sends you a request for aliens you will return uh list of aliens where you will return this two aliens right and then we have also discussed I mean we have talked about this if normally when you return data of course the data will be coming from database right what we are doing here is we are returning data by ourself by creating the static resource and that's not a good practice now so what we should be doing here is we should be creating a separate class which will be dealing with database and of course we will we have to fetch data from database but and in fact we can mock it up right we can create a separate class which will return data to us again we can imagine that that class is connecting with database I mean we don't we don't care about that we are not concerned about which database you are working with what we are concerned is this data should be coming from database and to mock it up what I will do is I will create a separate class now and I will name that class as okay let just create a class and we can call this class as something alien so we'll say alien alien what uh normally if you are coming from web development we normally refer our class name as da right so a class which deals with database is called as da but then in rest API instead of calling it as da we can use a good name called as repository again you can imagine you can you you can still use da that's fine you can use any class name doesn't matter but then repository sounds good because you might be fetching data from database maybe from from some files maybe from Network you are fetching data from a repository right again uh having said that you can use any class name doesn't matter now so we have the class name which I'm using here is alien repository and this alien repository will work with uh certain methods now again there's one more reason why I have taken this name as repository you will you will understand there's one we start with spring rest so after after this jersey we'll be talking about spring rest where we will implement the same thing there this alien repository will make more sense right so time in let's say we have a class name as alien repository you can take any class name the way you want okay so we have this class right and this class will work with a database now when we talk about Resource as a user you can fetch all the resource you can fetch all the aliens in the database uh you can uh save a new alien maybe you want to add a new alien object maybe you want to fetch one particular alien maybe you want to update one particular alien so you will do all those operation with this alien repository right so this alien repository need to have all the methods right so what I will do is I will first of all let me create a array okay maybe a list and I will I will use that list will be accessed by all the objects okay now how do I do that so what I will do is I will simply create a list of objects here so I will say list of aliens and I will name this alien as um okay let me just name this as aliens equal to uh okay what next now now we have to say new adist we have to create the object as well right in fact we can create this object somewhere else so let's let's say we are declaring the object object here now once you have declared it we need a Constructor where you will create the object of it so I will say public alien repository where you will create an object of that so we'll say alien repository and now here we we we will create an object of aliens right and that's how you do it that's the one of the advantage of using Constructors we can initialize our variables and then so we have initialized our reference variable now which is aliens in this case now I want to have some static data okay of course you can imagine this data is coming from database but then uh we we don't really I mean we are not really creating a database here right so let's marck it up so what I will do is the thing which you are doing here these aliens uh we will cut this part and let's put it here in the alien repository right so we already have two aliens here and let's add these two aliens in our in our list so we'll say aliens. add and let's add A1 and let's say aliens do add we'll say A2 now what will happen is if you want to fetch this aliens of course you will be getting these two aliens or maybe you want to add uh some more you can do that right it is available for you okay that sounds good now once you got this Constructor where you're adding this values now we need a method which will return all the aliens so what I will do is I will say public uh which will return you the least of aliens see in this video we we are going to only talk about this repository how do I create all the methods so I will say aliens. getet aliens which will return return you the number of list of aliens and now we can uh we can simply return whatever alien object you have so we'll say aliens return alien right so whenever someone ask you for get aliens you will return this aliens here right is that simple uh then you need a method which will okay so you need a method now which will return only okay maybe you want to fetch only one object it is possible right if someone says hey I don't want all the aliens I want an alien object with ID 102 that's possible again we'll be seeing that how do you send a request from the client okay let let me I mean just to let you know okay first of of all let me remove this thing here okay so first of all when you talk about this list here when you talk about uh request so request will be for any type right a request will be for only aliens which will return all the aliens maybe in future we'll be working on how to return one object by ID so let's say if I specify ID 102 you will return that particular object maybe you want to delete alien so you will do all those things with the help of this repository again how to implement that in this resource that we'll see later but time in let's focus on this Repository so maybe you want to fetch one particular object so in that case what you will do is you will say public again this will make more sense after some time when you talk about uh how do you f how do you use that resource so we'll say public alien and we'll say this is get alien if you want only one alien and the thing is if you want only one alien how do you specify which alien you want so of course in this method you have to specify a primary key which is ID in this case I hope it's yeah it's ID now based on the you will fetch that particular object right uh how do we do that here how do we Implement that concept here so what I will do is I will say I want an object of alien so we'll say alien A1 equal to uh but how do I return that particular alien from the list um okay that's a that's a difficult thing now let's say by default we'll say this null or maybe uh yeah let's let's say null and let's return that A1 but then how do you fetch that particular object from a list so what you have to do is you have have to say uh we have to run a for Loop we'll say uh Alien a colon and we will refer this this is this is to the alien list which is aliens and you will run to the loop and you will check if if the alien a. get ID matches with uh is equal to ID in that case return a right so now what will happen is it doesn't matter what we are returning here in fact we don't even need this thing uh but yeah we have to return something right we'll say new alien we don't want to return a null value because that will generate a null poter exception if you're not able to find an alien it will return a a blank data for alien but at least you're not returning null so what we are returning is yeah so we have uh in fact you know what uh returning null will be a good thing because in future we we'll be looking at that as well so if you send a particular ID we will return that ID and we will check if the ID matches with the ID which you which is then the list you return that particular object otherwise it will return null right this is for one ID uh this one alien maybe you want to have some more maybe you want to have methods like uh Delete update so you can you can write this all those things here so again we'll be I will I will be completing this U and then we will see how to work with resource but then how do you how do you work with at least this resource so what I will do here is I will say let me create a object of alien resource here so I will say uh Alien repository not the resource alien repository and we'll say this is repo which works for the database and we'll say new alen repository okay that's how we create the object and then from here instead of saying return aliens we'll say repo do get aliens right so now you're fetching data from database now it feels that you're fetching from data from database right so this if you look at this alien resource now we don't know from where you will get the data but we are getting data that's important right now to make it work what I will do is I will write my application I hope there's no other server running on this this machine let's run as run on run on server you know sometime it does happen that I might be having some other server working and let's let's see let's say click on finish and I cannot see any error there okay everything seem seems good so let's say let's send a request for web web API and let's say aliens and you got your data now trust me this data is coming from the repository because there's no other way you can this right so yeah this data is coming from repository which is here so again we'll we'll see uh how do you do other stuff as well producing the resource because in the next video we'll talk about how do you produce some more resources and then how to update delete all the stuff on the way in the last video we have seen how do you fetch data and then we have also created this repository right in which you are saying we want if you want to uh fetch repositories if you want to fetch all the data but now my concern is how do I create a new resource let's say if you have these two objects that's fine maybe I want to create my own resource there and we are assuming that this data are coming from database maybe you want to add data into database how do we do that how do we add data in a database so for that of course you have to send a request right and when you send a request again we will not be using this like okay so when you send a request of course there should be a method here who will accept your request now question arise for what type of request you you specify that you are creating a new resource so what I'll be doing is I'll be creating a method here which is public which returns maybe if you want to return you can return let's return an alien object and let's say this is this it the name is create an alien okay and now this alien takes a alien object and that's the amazing part about this right you can directly send data from the client and you can accept it here let's name it alien I mean A1 and let's return the same object okay let's return the alien object itself but then I want to add this data to database right so we'll write that code here okay now when I say database I'm talking about repository again we have a fake database okay we have to feel that guilty every time we we we work with this so This should work with not get request because because whenever you send data we as we have discussed in the theory video whenever you send data or whenever you create a new resource on the server we always use a post request so post is basically basically for creating a resource right so get is for fetching the fetching the resource uh post is for creating a resource put is for updating a resource and delete is for uh deleting the resource so I want to use post because I'm creating a new resource here and then I will say at theate but then okay now I want to change this link what I want to do is I want to say create only when when I say alien alien so I want to change my URL whatever URL you have now I want to say aliens SL alien for that URL now so aliens are already here and we'll again go for a path variable because I want to create a new resource here and I will say that resource is in this case um I will say alien okay I mean the path is alien and let's say okay everything is working fine now it it sounds good so we are accepting we are fetching an alien and everything seems good now let's accept that data so first of all I will print the data on A1 let's see before adding that into database let's see if it is working I want to I want to print that data here okay it should work right so what I will do is I will say repo dot uh there should be a methus create on the repository where you which will accept your A1 and it will add the data into Repository okay but hold on we don't have any method inside this agent repository so we can directly click that uh error here and we can create a method there so we'll do that we'll say create method uh Alien right okay so we got this method which will accept your value okay so how we should create that uh thing here it's very easy we got this alien A1 here right now we'll fetch our list which is aliens and we'll say dot add and let let's add whatever alien object we have so we'll simply say aliens. add A1 our job is done okay again you can imagine the data is going into dat database because we are not doing that yet but it is going in database we can imagine that the important part here is not to work with database the important thing is how do you handle rest request right now we have done all these things but the question arise how do you send this data now when you are fetching data we are directly using the address bar right if you can see my Firefox when I say enter I'm getting the data how about sending data how do I do that we are not getting any form here that's not our intention right we are not making a website here or we are not creating a design for that how do you send this data normally when you talk about sending data fetching data for wrist API we need a rest we need some client application which will do this when you make a mobile application when you use all these things in mobile application your mobile application becomes a client uh when we use angularjs to work with this uh data we use use angularjs as a client but unfortunately I don't have anything here we don't have a mobile application we don't have a angularjs script so we have to use a test we have to use a tool using which you can do all those things but which tool we have to use now luckily we have lots of add-on tools available on on the market so example if you open okay let me just go back to my Eclipse okay so if you right click this and if you click on this new and Okay so if you go here there is uh there are lots of plugins available example if I click on apps I do have lots of plugins installed in my system as you can see we have hangout but one of the most one important one is this one which is called as Postman now Postman is one of the tool which I use to to it it works as a client uh it works as a rest client for me okay this is what I was doing in the uh somewhere else let's close this so this is how your Postman looks like but then how exactly it works how do we send request using this how do you fetch the response using this that's a different point right again this the same add-on plugins are available for uh for your uh Firefox as well but then I'm using Chrome so let's use Postman there but then how to install Postman how to use Postman and how do that how that resource will be created that will discuss in the upcoming videos it's time to explore what is Postman now as you can see Postman is a tool here which is a app of Google Chrome again for Firefox also we do have this type of tools so I'm using Postman here watch for Chrome now you might be thinking what's the advantage of this so what I will do is I will just copy this link and let me just post I mean paste that in Postman and as you can see if I say okay where's that oh we already got the oh past I've copied the wrong link or what okay we'll say copy and let's say delete this part and paste as you can see I'm copying the list the moment I say click on send you got your response right so the same response which you got on the client on the browser you got the same response here now you might be thinking what's new in this we can do that on on on browser as well right why you have to install a separate app it's because if you expand this list you can see we can send multiple requests we can send a get request we can send a post request we can send put request patch delete copy so we can use all these options of HTTP which is not possible in browser in browser we normally use get and post only right otherwise if you want to use extra to features like put and delete which we are going to in this course of course we need some client right again we can create our own client if you uh if you want to explore that we can create our own Java client but then we don't want to do that as of now we are focusing on uh inbuild tool so we got a postman where you can send a request if you want to send a post request just simply click on post and say send you don't even change you don't have to touch your you URL if you want to do that you can but then not required so you can you can fetch data in Json format you can fetch data in XML format the way you want right but then how do we how do you find this tool and where to from where to download this so to download this what you do is you have to go to Chrome and you have to open a new window and what I will do here is in this chrome you can see when you open a new tab there's an option of apps so you have to click on this apps and okay from there if you don't find Post here you have to create a new you have to click on this web resource and in this web resource you have to search for which app you want again if you simply search for a rest client there are multiple apps available as you can see if I search for this I don't know how many uh how many options we'll be getting now you can see we have lots of rest client available we have yes yet another rest client okay we can we can try this all this things which is available but then we'll be using Postman so we'll say search for Postman search for this and okay so this is the F not this one this one so this is the app so we don't have to focus on extension we have to focus on apps so in this apps this is the postman this is how it looks like so I have already added into the Chrome so you have to click on this add to Chrome where you will get the option and you you have to click on that and you can see how many reviews we have for this it's 8,286 reviews right this is very very very famous uh client it's not just uh which I prefer the lot lots of people they do prefer this type this client here so if I click on this there are lots of description available you can read it out if you want and you can also read the terms and conditions if you want but then once you install this your Postman will be up and running I guess you have to restart your uh you have to restart your uh browser if you after installing this uh it will it will notify that but then after doing that when you open when you open your post M this is how it looks like and how to use this so you will you will understand how to use this in the entire course but then if you to just to go go for a quick Glimpse you to select on this get request paste your url here and click on send it will send a a get request if you want to send a post request just click on this post and then when you click on this uh send you will it will send a post request but then when you send a post request you have to also send the data right how do you send a data here to the server in that scenario we ask we have to specify the body now inside this body you can enter your key value pair whatever data you want to send or you can type your own XML or uh you can you can you can type your XML or Json format you can if you want if you're sending XML click on XML and type XML if you're sending Json type Json and you can send it so this is how you can use Postman and so once we understood how to use Postman how to install Postman the next video we'll see how do we send that data to the server so that uh this this method will be called okay so that's it from this video guys we I hope you understood about the postman uh we'll talk about this thing in the next video till this point we have seen how to create a get request and a post request now you can achieve a get request from your browser right you can directly go and you can paste this link the problem starts when you have a post request because this is where you have to mention uh the post because see when you talk about your browser we have your browser supports get request your brother support post request but maybe in future you'll be working on put request maybe delete request and that scenario we have to use some sort of client which will send all the type of request again we can use JavaScript here but then we don't want to write any of the code so I will be using a a tool as Postman again we have seen how to use this tool and how these tools is made or I mean how to download this tool and how to make it open so let's try to run this code so what I will do is I first of all run this code just to make sure that everything is working fine so I will click on this and click on run button so the server uh again it will take some time to start the server and set up the things okay so as you can see our server started and this is your request but again I don't want to use a web request here I mean I don't want to use browser I want to use Postman and in here I will send a request for aliens this is a get request I will say get request and send as you can see we got the data but now this time I want to I want to go for a post request right now let me let me just verify with of whatever request I'm sending so you can see I'm sending a XML request now if you want to send a post request again we have to use uh XML right because if you can see we have an alien where we have written XML root element so that means we are expecting uh XML resource let me go back to my Postman and I want to create a new object so I will I will just copy this so that I will be having some sort of template with me so I will copy this and now what I will do is I will go for a post request I will say post but this time you have to send a request to for aliens I mean Al aliens SL alen and if I click on send you can see we got an error it says unsupported media type and then all those things that for that you have to mention the header whatever data you are sending so the data you are sending is not Json but XML because you are supporting only XML and when you send send now uh again it says bad request because see you are sending a request but you're not sending data right you should be sending data as well so in that case you have to go to body and you have to mention XML and that page XML what I have to send now is I'll be sending the ID as 105 then I'll be using a name here called as hsh and let me just use a points as let's say 40 points now with this value let me send a request and you can see we are getting the this data back and if you can verify on the server oh on the server side we are getting this weird output because I forgot to put two string method here my bad because to print the values we should be having two string meth as well right because otherwise it will print the hash code so let me say don't done generate two string and click on okay now as you can see we got our two string method there and let's get back uh first of all you have to rerun the code uh it's already done let's go to postman once again and click on send now you can see we got our data which is coming from the client right again you can write JPC code here and you can save this data in the database the important thing is now you know how to send a post request now just to make a quick recap what we have done if I open that and where's the postman so if I open a postman here as you can see we have we are sending data so you have to make sure that when you send a post data on the server you to select post from here you have to mention the URL again you have to mention the alien as well then you have to say click Send but before sending you have to also send the data right so I'm using a raw data here in raw data you have to type your XML and then you can specify what type of data you're sending so you're sending an XML type data not the Json and in headers you have to also mention what type of data you're sending so that's XML and click on send you can see you're sending your data back to the server so that's how you send a post request but then how do we use uh how maybe you maybe you want to do something else now maybe you want to uh send a put request how to update data how to delete data again we'll be saying everything in the subsequent tutorials till this point we have seen how to create a resource how to fetch all the resources when I say all the resource I'm talking about the aliens right as you can see in the code we have we do have two aliens here we got alien one and A2 and we are returning those values we are also creating a new alien but then as you can see we have created one more method here which is public alien get alien and we are trying to return this object again we if if you find that object example since we have two aliens here we got uh 101 naen and one2 Ary if I pass an ID as 101 I need to return the object of r with 101 if I if I pass one2 of course I have to return one2 object now otherwise if it don't if it doesn't matches we will return the empty object so that we'll not get any exception now that sounds good right but then we have we don't have any resource here which accepts one which returns one alien as you can see if you remember in pman we have seen if I run this if I if I fetch all the aliens it will it will fetch all the aliens it is working I don't want to fetch all the aliens what I want is I will specify if I'm fetching an alien whose ID we say ID is equal to 101 now that this is this is how we execute in server technology right what we do is we specify question mark and then we say ID equal to 101 so that ID represents your that ID represents your query parameter now this doesn't look good right uh in this API what you can do is you can say alien sl101 now when I send a request for 101 if I click on send I want data for that particular alien and as you can see it is not working we are getting exception here which is one of our favorite which is 44 in this situation what we need to do is we have to get get method of course we have to use get which returns XML which will be having get alien names and let's copy this so that I will not have to type that once again and this time I don't want to return a list of aliens I want to return a alien and we'll say get alien which takes a ID we'll do that in some time and let's return let's return repo doget alien when you pass an ID time being I will pass one1 just to verify if it is working or not right but we have to make sure one we have to check one more thing when you say get request you can see we have two methods here who who taking get request and both this will work for aliens what you have to specify is inside your aliens we have to also specify alien so I'll specify alien here so I will say get request not not this one I don't want to touch this one now here we have to we have to use one more which is ADD path in this add path we have to mention uh the path so we'll say if if I send a request for alien sl101 so if I send a request for alien sl101 I have to return 101 right uh this sounds good right and there's nothing wrong here let's get back to our our post Mana if I send a request now can you see that we got an object now this is for 101 awesome but then if I send a request for 102 this is where the problem starts because if I click on send oh we can you see that we got an error which is 404 how to solve this issue now of course you something might have struck your mind that we can create this method once again uh this time we'll change the path from one 1 to one2 and that perfectly makes sense you can create one more one more method here but imagine if you have 50 aliens in your database or maybe you have 100 aliens is it a good idea to create 00 methods not exactly right so this one1 might change maybe I want to go for one2 so in this case what I will do is I will say ID okay uh but then this even this will not work how can you replace this ID with with a number now if you want to replace this number if you want to replace this this ID with a number you have to use a cudly brackets now this cudly brackets means this ID is just a placeholder which means you can replace this ID with anything you want example if you write one2 this ID becomes one2 if you say1 this ID becomes 101 but then how to store that value because if you want to use that ID here of course it should be a Java variable right in this case I will say int ID and here also I will specify ID so whatever data I'm getting here the same data need to be assigned in this ID but how we can do that for that we have to use one more anation called as at I mean at PA param in this you have to mention whenever you get a whenever you get a request and if that request has a ID in that that ID becomes this ID I mean this ID which is here in the rly brackets that represent this ID here and then the value from this will be assigned to this particular ID and that makes sense right this is this is how it works let's run this let's verify if it is working for 101 let's verify for 101 first and click on send a request oh it's working let's go with one to send oh it's working can you see that it's working for both the variables or both the values so for a particular ID you can mention uh you can change the value to 101 02 maybe you can you can have any other value let's say if I go for 105 I know we don't even have that let's click on send and you can see we got blank values that doesn't matter what matters is whatever data you have in the database you're getting that that particular data right this is what we wanted so yeah that's how we work with this thing in fact you instead of sending a new object you can return null and here you can check if it is null you can send a error or something like that what time it is working right we restricted does okay so this looks awesome now think about this we are living in a world where we don't actually use XML I mean we we do use XML but XML is not the only format using which you can send response maybe you want to send a response in Json format and we have not seen Jason yet right as you can see in this code when when whenever you fesh data you are getting uh XML XML data what if you want to send a Json request I want I want I want to say I'm I'm expecting adjacent data now I'm asking my server hey server you are rning XML that's great but I want Jason the moment you say Jason uh not here you have to specify okay is fetching in Jason format what's wrong can you see that it is not giving you Json so even spe even if you specify oh we have to specify the except parameter is it except parameter application SL Json okay so you can see we are getting error it says for not SE not acceptable so this is not accepting a Json data right what we want is I want to return a Json request now okay so you might be saying okay instead of using XML we will be using Json but then if you specify Json there your application will only return Json it will not return XML so I want to have uh produce annotation this produce actually takes an array of uh XM I mean the array of met type we are specifying only one right if you want to specify multiple media type what you can say is in fact first of all let's say I don't want to use XML I just want to fet Json okay we'll see how to do both but then time let's focus on Json so I'm not producing an XML I'm producing a Json now will it work if I'm if I'm sending a Json and if I go back to my if I go back to my Postman let's verify I'm sending a Json request and we got an error see there's no error with the response okay the problem is with the with the server itself on the server side we are getting error which is 500 So when you say 500 it means so there's there's something wrong with the server and you can see it says message body writer not found of metat type J application Json now when you want to send a response for Json format your jersey says hey hey user I mean hey developer uh as a jersey I normally work with XML because that's what is default in Jersey if you want to send Jason then you have to use external dependencies oh now that's the thing we have to download some external dependencies for that oh that's lots of work right but don't worry Jesse also says I have done done it for you don't worry what you to do is in fact if you go to the P XML file if you just go down and down you can see we have dependencies for Jersey container we have a uh dependency for this jersey we are we have one more dependency which is called as Jersey Media Moxy and that is in commented state if you can just uncomment it you will be getting this dependency in your in your m in your M dependencies where you can find it here if you go to library if you go to mavin as you and you can see we don't have Mo as of of we got it I guess we have saved this file okay so you can see we got media Moxy this is what we want and the Ser is also restarted we just have to add that particular dependency and if you send a request okay it's sending it's sending waiting for the response and can you see that we got adjacent response and Json response looks much much cool I mean cooler than uh what you say XML right so this looks good so this is how you return ajason but what if I want to send a request for XML now example if I go to my uh get request if I again so go for get request here and if I send a request so this time I'm sending a request for Jason but if I send a request for XML and if I say send you can see we are getting eror which is for not6 so you have to mention what Tye of data you are sending so for that you have to again go back and you have to make it XML but that's an issue right what if you want to provide both so depend upon users some people will say I want XML some people will say I will I want Jason what you can do is you can just put them put them in an array and in this aray you can specify two types one is Json and the second media type would be second media type dot it will be XML so that depend upon what user ask for if user ask for Json if user ask for XML will return whatever user ask right this is what you call as content negotiation you are so this client and a server they are negotiating the the content what Tye of content they want let's go back to the postman and now I'm sending an XML request it works now let me send a Json request it is Jason and send it works right so this is accepting both now can we do that with our this thing I mean this this here let's try let's go with the get aliens I mean for all the aliens let's produce both Json and XML and let's see if it is working or not uh let me just go back to my okay is it server loaded I don't think so you're done let's go back to my Postman and if I send I want to request for aliens I want okay first of all let me just go with Json if I send request it works can you see that we got a Json so we got an array here we got a Json array and if I specify XML if I specify XML you can say send it works again you can do the same thing for the create alien instead of sending a data in XML format we can send data in just format and I would I would leave it to leave it up to you so you have to do you have to complete this one okay so yeah that's how you use Json format that's how we you that's how we use this Json I mean Jersey Media Moxy so that's it from this video where you return the Json format and for the further further to further videos I'll be focusing more on Json Less on XML because I love Json so once we have talked about how do you send a Json request and I mean how do you fetch Json data from this server and if you can observe we are fetching all the values we are even fetching one particular alien and then we are even creating a new object now the thing is uh till this point whenever we talked about repository we were having this weird repository right where you are storing data in a static way can you see that we are getting this object by ourself now logically what we should be doing is we should be storing this data in database right so when you fetch when you fetch data this data should be coming from database and when you save data example when I say create this should be stored inside database and unfortunately I have not written any code for that now let's make it real I mean why to work with a fake database here or a fake repository let's work with a real database so that you can actually feel that thing you know when you save data in a database when you fetch it and all those things now question arise which dbms you going to work here uh first of all we we have to think about two things the first one is which dbms you are going to work and the second one are we going to use any framework for that the first one the first question is which dbms again you can choose any dbms which you like you can go for my skill so write it here you can go for masc you can go for post and we can go for Oracle it's your choice you know the code will not change I mean of course the database code will change but then your resources will not get affected this will be same for all the different dbms now which one I will choose here so I will go for myql because I already I have already configured my my system everything is working there you can use po you can use Ral which which one is which you prefer the only thing is as dbms changes you have to change some settings again uh if you have learned jdbc before you know what I'm talking about okay so that's the first thing so I'll be using mql now will be using will I be using any framework here like hibernate or maybe some omm Tool uh not exactly you can if you want you can Implement that we will we will be purely using jdbc here no fancy stuff let's use jdbc because we are focusing more on rest API not on the odam Tool uh if you once you learn about rest API I do have a video on hypernet just watch those videos also and you can combine by yourself I would love to see your implementation for that but time B let's focus on jdbc again I not I will not be using any connection pool in this in this example we'll be going with a simple jdbc concept because I don't want to confuse you with JB stuff here so let's keep it simple so first of all I'll be using mascle and I'll be using jdbc now to understand how okay I'm assuming that you know jdbc beforehand but in case okay if you are not familiar with jdbc I would suggest you to watch my jdbc video on YouTube so even if you search for teliscope jdbc it will give you a jdbc video and so do do search this on YouTube and once you watch that video you will be having enough understanding to watch I mean to continue with the series and if you know jdbc that's fine let's continue with this now how do we Implement jdbc so we have to implement those seven steps right so I will make it quick so first step would be I want to import java.sql dostar in fact the amazing thing is we will replace this by our own stuff in future time let's say Java java.sql dostar then I also need a myql connector of course right when you talk about your datab B uh we need to use a masol connector right now question arise from where I will get a masol connector now to do that we have to go to our favorite website which is MySQL I mean the maven repository so we'll go to MAV repository this is a place where you will get your MySQL connector so let's search for my connector the first link click on that and just go down uh this works perfectly fine 5.1. 38 I'm using this from a long time so I I already have a what is say a a local jav file for this so it will not take much time to get downloaded as you can see it is it is done uh in my dependencies you can you can you can find my SQL connector now now once we have done with that let's get back to repository and let's continue with the steps we need we need object of connection of course we'll say connection con and we'll initially we'll set it as null now I also need to do some more things uh let's not use this uh array as of now because we don't need any any add here right let's remove all this stuff in the Constructor I want to initialize my my con object so I will say con equal to driver manager. getet connection and you can pass those three parameters three beautiful parameters again uh you can do in your own way okay there's no nothing that you have to follow my step for jdbc if you know something better just go with that go go ahead with that we say jdbc colon uh myql colon and Local Host where is local host then the port number for MySQL is 3306 oh that's six and then the database name oh which database I'm going to use because uh we have not yet created a database yet right so let me open my uh myql I was already working on some database here let me remove all this thing say create database and we'll say this is for rest data or rest DB again you can name it anything you want let me just create a database here and let me create a table for that so let say create table will what we'll say table name as alien and this alien will have alien ID um we'll keep it as ID itself so we say ID as in comma name as vcat uh let's say there a size of 20 provided it's not my name uh okay because my name is very big you know if you don't know that that's fine okay so I say in ID int name Vare and I also need a points which of type end I got my table which is that I got my my table and if I select star from Alien now you can see oh what's wrong I have to first say this then I have to say this and then I have to say select oh what's happening what's wrong it says cannot create a table rest DB [Music] because database exist is it Let me refresh and we okay we we got this database and we have to say rest use rest DB and now is it working yeah that's fine let's create this table and let's create let's fetch the alien as you can see it is empty again you you could have insert this data from here but just to make it real let's get back to our code and let's insert data from here not from here actually from the user So currently my database name is rest DB right once we got our database we have to mention the username so we'll say username equal to for for uh mask or username is root and for for for MySQL my password is password is zero I know we should not be putting that as a password but that's fine once we got this three U three things let's specify username and the password right so once we have assigned all these values and I can see there's a error there let me say control shift o it will import all the packages what's wrong why oh oh okay so it says uh there might there is an unhandled exception so of course whenever whenever do these things you have to make sure that you use try catch we'll say try catch exception exception e and in case there if there's any error we'll print system do print and error we can also maintain the log file we can also uh do the print Bas but then let's system. is fine for for for me at least okay so once we got this connection object we have to also say class. forame if you want to load your driver so I will say class do for name and you have to mention the driver name which is com. MySQL jdbc do driver as I've already mention I'm assuming that you know uh ddbc beforehand so if if you still don't know just go back to my go back to YouTube and search for um telescope jdbc and watch that video okay once we got these two steps now we have to replace this thing which is your list uh your G alien with a Java query right you have to file this query to get this element but then we'll be doing that in the next video let's go ahead so what we have done is we have simply created this Constructor where you have assigned these two things right we have uh loaded a driver and then we got the connection object the next thing would be uh when I say I want to fetch all the elements this this is not what I want to return I don't want to return something which is not defined right so let's create a list of values now so I will say a list of aliens and I will name this as aliens and that's what I want to return here so I will say add a list and we'll say add a list again if if we can use this hybernate framework here this will make our work very easy right but then since we are not using that let's use normal jdbc and if you know how do you fetch values of course we have to file a query so I will say string SQL where I will find the query and the query would be it's very simple so we select star from Alien and okay once we do that let's file this query now the way you can do that is by creating a statement object so we'll say statement St equal to con do create statement okay now once we got a statement object let's import the statement object there or statement interface uh what's wrong with this so statement from java.sql and we got an error now it says it might throw an exception so again we have to use a try and catch block block here so I will say this will be included in the tri block and it will be getting over inside the catch block so let's assign that let's say exception e exception e and let's print a error whatever error you'll be getting there let's print that here so we'll say e okay now once we got the statement object it's time to execute it so we'll say st. execute query now as you know whenever you execute a query it will return you a result set so what what I will do is I will say result set I will say RS equal to st. execute query and we have to import this package yes which is from java.sql now once we got results set we we have to run a while loop because we don't have one object right we'll be having multiple objects so we'll say rs. next so it depends upon how many values you have it will iterate to all the values and every time you get a value let's create alien object we'll say alien a equal to new alien and we'll set the value so we'll say a do set alien ID and this would be coming from the from the database so we'll say set Al ID we'll say rs. get ID for the from the First Column we'll say a do set name it will be second column rs. get string we'll say two we'll say a do uh set points which is rs. get int it will will be from three right so we got this object and we have assigned the value now we'll assign this value in a list so we'll say aliens dot add and let's pass a so doesn't matter how many rows you have if you have 50 rows it will add your 50 aliens and once you do that you will get all the aliens now this is what this is how you fetch the values right maybe you want to fetch only one value now so in this scenario I don't want to return all the values I want to return only one value again we can do the same thing but this time we will not return uh Alien I mean multiple aliens but only one alien right so for that we can even fetch this we can copy this code and let's paste it here again we can call it as code reuse that's what we are doing here right so we'll say instead of having select star or instead of yeah we have to say select star where the ID is equal to whatever ID you have defined here maybe if your ID is 101 it will fetch 101 right okay so same thing the only thing is we don't have to use a while loop here we have to use use a if because we will not be running a loop here because we don't have I mean when you run this s it will return only one value so we'll say if others. next uh we'll set all this okay we we we create an object and will not add it we'll return the same object and to return the same object of course we have to create it outside somewhere here so that we can actually return it and at the end we'll say return return a right that's the only change we have to do right so we have created this we have five this query and you will you will get a result set we just have to fetch you have to say. next and then you can fetch your values and you can return that particular object it's that simple right and now you have to create right so for create again we can use the same stuff uh we can just paste it not this one uh we can just copy this code in fact the catch block as well okay just to just to make it fast I'm just copying all this thing you can you can do your stuff there okay so the query is okay now when you say in when you create a new thing we have to create a string SQL equal to you have to mention a query here we'll say create uh not not create we have to say insert insert into alien uh we'll say values now question arise for values we have to specify each and every value right now instead of using a normal statement I'll be using a prepared statement here so that I can use question marks which is placeholder in this case and instead of saying statement let me go for prepared statement and this would be prepare statement right let me import the package for this is imported uh from java.sql package anything else yeah so we have to pass the query here itself we don't pass the query inside execute query we pass the query here itself and we don't say execute query when you insert we only say St do execute update where is that execute update right so we have to execute update and uh we don't actually use if LCA because we are not fetching data we are inserting data right but where is my values so have to say St do set int the first question mark will will be replaced by a1. get ID St do set string the second question mark will be replaced by a1. getet name and st. set in the third question mark will be replaced by a1. get points that's how you assign the values and that's how you insert the data now you think it will work let's try let's try this the finally we are running something which will be saving into database right and this would be exciting let's go back here and let's restart my server I guess it is done already no okay let me just yeah so I guess restart is done let me just go back to my Postman just to verify it is working and here I will say I want to fetch all the values first of all if I send a request you can see we are getting blank because of course we don't have anything in database let's insert some data so I will say post and I will go to body and I will say the I want to send a request for alien and the value is one one0 I mean 101 H 40 and if I click on send can you see it worked but then is it inserting data into database let's verify now there are two ways of verifying it the first way is you can go to mycle workbench and you can verify there you can say select star from Alien can you see that we have our data here and there's one more way of verifying it let's go back to my Postman and let's send a get request for all the aliens and you got your alien likewise we can insert some more data just verify it let's let's say alien and I want to send a post request and also send a also want to send a body so we'll say hsh I mean so we'll say naen and let's add it as 70 right click on send now we got 17 database let's go for three and let's say this is uh Ry and let's say this is okay uh let's number 60 and send uh let's verify how can you verify let's say Select Staff from I mean let's fetch all the value from database we'll say we'll send a get request and send you can see we got all the data here again you can send a post you can send a Json request as well you will get your data back so finally we are working with database and rest in the subsequent tutorials we we we'll see how to update data and delete data so now we have database we have all the request response things now if you go back to your Postman and if you try to send data so let's say what I will do now is let let's create a new resource and to do that we have to use a URL which is alien right and then we have to send data as well now to send the data we have to go to go to body and you have to insert a new object right let me go for 104 and we'll say this is Myan and let's say Myan has 90 points here now let's say if you have this data now see I'm sending this data in XML format right what if you want to send this data in Json format is it possible so what I will do here is I will just convert this into Json because I want to send Json not not XML so of course this will not work so we have to send data something like this we have to say uh in double codes you have to the ID then you will give a colon uh the ID I want to send is 104 right then oh I forgot to put uh comma there and the second thing I have to send is name and let's say the name is Myan and we'll give a comma and then we we have a points here and the points would be let's say 90 so I want to send this data now the moment I click on send okay so you can see it is getting stored so in this which is getting stored and it is working right so that means you can send a request in Json format or you can send a request in an XML format right now how exactly it's working so what we what if I do okay what if I change this thing now let's say I don't want to I mean I want to specify something example uh if you want to specify what type of data you want to accept you can also use consumes so you can say consumes and now produce is something which will send from server to client that is produ so from this server I'm sending data to client that is producers consumes is something where your client sends the data and your your server will will will be accepting it so we'll say consumes and so it only consumes let's say U maybe if I if I mention the meta type as medat type. application.xml so I will make it XML and let's see if it is working with Json or not so let me just make it XML and I need to restart the server I guess it will be happening automatically but in another case it's not happening automatically let's right click and say run as on on server okay so now if I go back to my Postman to send this request this time I'll go for 105 because we already have 10 104 there and we'll go for a name called as Puja and let's have our points which is 80 even those point doesn't matter in this case okay let's run this and let's see what happens now if you click on this uh send button can you that we got an error it says unsupported media type because what we are doing is we are specifying that it will accept only XML and that's the problem when you specify it will support only XML it will only support XML that means if I send data in XML format it will surely work so I will go back here and say I want to undo so that I will get the XML which we have written before okay so let's set this as 105 and let's say name is Puja and let's say this this is 80 and if I click on uh I have to make sure that I select XML and if I click on send now you can see it is working so that means when you specify the type of data you want to accept it will accept only that particular thing I it's better to have both right a client can send ajent data or they can send an XML data that's their choice if you want to assign both if you if you want to accept only this two not anything else uh you can specify those two things here and that did this would be a good thing you know specify both so that when a client try to send something weird it will not accept so it's better to mention the consume anation as well so consume says what type of data you are going to accept XML Json plain text what else right so once we have mentioned this uh okay it say some memory leak issue but it's fine let's go back and let's go to postman and okay so we'll say send oh okay we have this two times now now I want to go for Json this time so let's enter Json data so we'll say Json again we have to type the same thing right I should have copied it somewhere to reuse but let's make it fast so I will say id id is let's say 106 and we'll say name is equal to let's say in double codes let's mention the name or we'll send this 106 in double codes as well okay now this will be let's say um provin let's say PA points and let's have a point which is let's say 85 okay we'll keep that in double quotes Okay now if I send uh Json format let's see if it is working or not if I click on send it is working you see that again you are getting data in XML format because we have not mention whatever data you want to send right you can also specify by going to headers and say hey I don't want uh XML I want Json in return so if I say send now uh okay where is our data is it coming but don't worry this is giving you just for this is giving you just format so that's how you send request that's how you accept request and yeah that's how you use at consumes annotation so in this video we have talked about at consumes so yeah that's it we have talked about lots of stuff right we have seen how to how to send a get request how to send a post request in fact we have also included database now where you can fesh data using uh using XML or Json on the client side the thing is I want to achieve some more features now so when I say more featur let me just go back to my database which is MyQ here and I have done some changes in my code I mean in database especially I have deleted this ID 105 just to check something and I was trying to update this name so initially it was mayank now I just renamed it to May Mayu with the help of this this update query so can we can we achieve this too with the help of restful services I mean a client will send a delete request a client may send a update request how can we do that now if you remember when you talk about get request now get is very simple if you want to fetch data of course you will be using get uh if you want to create a new resource on the server you'll be using a a post request now for update we have a special type of request method which is called as put again there's a main difference between post and put again we'll discuss that in one of the theory video but then put means put is something using which you can update values and post is something with with the help of with the help of which you can create a new resource we have already used post right now it's time to go for put in fact even before starting with the uh resource what I will do is I will go back to my eclipse and here what I will do is I will create in the repository especially I will create one method for update of course right we want to also go for update as well so what I will do is I will copy this now this time we have to go for update so of course the method name would be update here so I will say update and the the query here would be uh it will be different it will be update uh in fact we can I can just copy this code so copy and paste again we can call this as code reuse right so we'll say update alien set name now I don't know the name so this will be question mark so you can put this question mark here I don't know the point so it will be question mark here and I don't know that r i mean the ID here that will be question mark So now here we have to replace those question marks right the first question mark here is a name so this makes more sense if you have a name so set string question number one it will be name the second question mark is point so we'll name we'll make this as two this will be points and the third question mark is the ID so this should be three right again we could have written that in sequence so let me do that here so let me c cut this part and paste it here so we are going in sequence 1 2 three 1 two and three we got all these question marks and that's it this is how you update the data but then how will you send data from the client uh okay to do that we have to of course create a resource here now how do I create a resource now I'm creting a resource for uh getting an alien oh sorry updating an alien so of course we have to create a put request so I will copy this code and paste it here now instead of using a post request we have to go for put request because we are updating data so should put and the method name would be let's say update alien so I will say update it will update the data but then update need to be done only when you have that particular data in the system right example if I try to update something and if if you if you don't find data there of course you cannot update it right uh in fact I will I will check that after some time the main motor here is I I don't want to create the thing I want to update the thing okay so I don't want to I don't want to create I want to update again there are some there are some restrictions which you have to follow that will see bit in some time but time being what what I'm doing is I'm using a put request Now using put request you actually achieve updating okay now there are some people they do this debate that using put you can create a new resource and using post you can update resource uh first of all to be specific post is for creation of resource put is for updating but then you can use it you can use it anywhere right it will not make any sense because for you it will work the only thing is if you have someone else and if they are checking your code it will be difficult for them to understand what you have done so let's let's all follow the same standards right so let's use put for update and let's use post for creation of data okay now this sounds good let me just go back to my I have to restart the server it's already done let me just go back to my Postman or Postman now here I want to go for a let's let's go for post first I will go for a post request where I will create a body again this time I will go for 105 we'll go for name PR and 85 so these are my values and I don't want to send this request I want to send a request for alien right that's my URL for that and let's say send the moment you do do that you can see in our database we got 105 and we got okay let me refresh this code you can see we got 105 we got PR and we got 85 now I want to update this data so I want to make it I want to change this 104 to May again how do I do that so to update of course we have to use a put request now when you send a put request of course for aliens you will say for 104 I want to put name as my myang so we'll say this is my and we'll we'll have a point okay what should with the points let's let's go for 90 points so the initial value is uh Mayu and 80 let's go for 90 points and let's go for my uh will this work let's try it out the moment I click on send uh and if I go to my database you can see there's nothing wrong there if I select star and alien you can see we got 104 May and 90 that's how you can update data now just have one query what if you don't have a ID which you specifying so the request which I'm sending now is okay where's my post man it is here so the the data which I'm sending is let's say 104 what if you try to update 108 or 107 now what will happen because we don't have any data with 107 right so let's say for 107 I want to change this name from Mayu to mayang to chiku that's his ni nickname he's my nephew actually and let's say points is 100 because he's my nephew he has more knowledge than me so of course this should work so the moment I run this code okay you can see it worked something something happened you are sending a put request and let me just go back to my database and if I select start oh this is not working on the server side you got the data on the on the server side you got you can see we got data on the server side and we have also tried to update but the problem is while you're updating it it is not affected because we don't have any data which with with 107 Now what is my intention here is see if it is not there if the data is not there it should create a new resource so using put I have two intention here the first intention is either okay you can also return the data you can also return the response to the client hey we have not got any data as 107 that's one way you can do that again we'll do one of in one of the video how to send a response code but then let's say we have a scenario here where uh if the resource is not available I want to create a new resource in that way what you can do is you can check so while running the resource you can check if uh first you have to fetch the value right so how do I fetch value so we can use this get uh Alien method can you see that we have this uh get where is that method now we have get alien so what I will do I will I will check if repo do get alien and if I pass the ID I will say a1. get ID and if I if I fetch an object using this if I compare this with if it is equal to equal to null okay so if this is equal to equal to null we'll we'll say okay so in case if it is equal to equal to null what is that okay so if the object is not available so when I say get alien bypassing that ID that means the object is not available in the database in that scenario it should create a new resource so I will say repo do create a new resource right and it should work now let's try it out let's try let's try uh I I'm very hopeful for this now let me go to post man and if I click on send okay something is happening on the server side it's saying it is updated but then let's verify if I go back to my database and let's select star oh it's not working what just happened uh okay so it says get oh it will not return you null right that's that's an issue it will not return you null because when I say get alien I'm actually passing a new object oh how do I verify uh okay so what I will do is okay let's let's try to apply a a programmer mind here so when I when I try to update this data and for before okay how do I verify if the data is available or not so if I say okay so okay okay that makes sense so if I say Get alen By by passing that ID if this get alien ID we'll say get ID is equal to equal to zero because it will be it will be a ID right it will be zero so if I don't have the data in the database it will return me a a see we have one more method which is delete again uh using rest you can use this for you can also also use other the thing as well but then this four are very useful so we'll use delete now now of course you know why we be why will be using delete because we want to delete the resource so what I will do is if I say hey I want to I want I want to delete the alien with an ID which is 101 so when I pass when I send a delete delete request for alien 101 if I click on send it should delete the resource okay it should return the Resource as well it should also delete the resource but as you can see it is not working of course it will not work because method not allowed right is because we don't have that method on the server let's go back to server now and let's create a method which will use for deleting so I will say public and I also want to return what we have deleted so I will say alien and I will say delete uh Delete alien you know the name should be kill alien that will make more sense right so let's go for kill alien here I know that's violence but then it will do here so we'll say kill alien and in this you have you just have to pass an ID uh the way we have done in this one which is uh get alien so we were using this at ID right the same thing we have to do there so I will copy this and I will say delete a kill alien by accepting this value which is ID and uh you have to also specify uh the request type so okay we have to also return the value right we'll do that we'll do that sometime but then we have to return data right so we'll say I'm going for a delete I'm going for delete request with a path alien so we'll say path alien and in this path alien we have to say slash and we have to also mention the ID so whatever ID you mentioned the same thing we have we must have done for get right you can see for get alien also we are using the same stuff the only difference would be instead of using a get request you have to go for a delete request so we'll say delete so add the delete and here we have to return the alien object now so how can we do that so first of all you have to fire a a get method right so I will use a get repository so I I will simply use that get method here so is that get code it's here it's here it's here yeah so I will do the same thing I will say report. get alen ID okay in fact we could have we have we could have type that right uh let's go back there and say this let will say alien a equal to and let's return the Val value let's say return a our job is done but then this is not the only thing we want to do we just want we just don't want to fetch it we also want to delete it now deleting is very easy you can simply say repo do delete and you can pass the ID but unfortunately uh will this work now before even before even deleting we should verify if do if do we we have that data so we'll say if a do get ID is not not equal to zero then only we have to delete otherwise we don't have to delete that so only when you have the data in the database then only we should delete that but question arise do we have this meth do we have this method there unfortunately we don't have delete method inside the repository so let's create one so I click on this and let me create a delete thing okay now what should be the code and I I mean you know right we have to write SQL query so again I'm I'm damn lazy so what I will do is I will just copy this code and I will paste that stuff here now the query for me would be delete okay what's the delete query so we say delete uh okay delete from Alien where ID is equal to question mark I don't know the ID so I just say ID is okay do this one as well so I will say A1 uh ID would be ID okay I just need to remove this Extra Spaces here so what we are doing is we are firing a delete query and then we are specifying that ID which is whatever ID you pass and then it will it will delete the particular ID of yours uh okay I hope it will work now let's go back to my resources everything seems good I want to delete a particular ID right so I will go back to my Postman uh so I hope it would work I will delete one1 and it worked can you see that it is fetching the data oh but the important thing is is it working on the database let's go back and say select sta from Alien for this and you can see alien hsh gone now we can delete 102 one3 we can delete any anything you want uh if I try to delete the 109 which is not available there and if I run you can see it is not ring data of course it will not return because we don't have anything with 109 uh we should have sent the uh the response code right maybe the data is not available okay we we'll TR to do that so this is how you delete data again how to send Response Code that's a different thing we'll talk about that in one of the video but then time being you can you have seen all the methods of HTTP we have worked with uh get we have worked with post delete and update or we just put time to make a quick recap so till this point what we have done is we have worked with different methods of HTTP right we have worked with uh get request we have work with post put and delete and you can see we have used we also we have also used a postman as a tool to send the request to the server and everything was working fine right and we have enjoyed the videos till this point now if You observe the scenario here what we are doing is uh so in in terms of recap what we are going to do now is let's try to understand what are things we have done so let's let's do with the get request so let's say if a user sends a get request and a user user intention is to fetch all the aliens here for that we have written this method which returns all the aliens now as you can see we have get aliens method now this method will be called whenever you request for aliens so this path annotation will map your request right so when you request for aliens this will map it and you can return all the aliens but again in which format you're returning them so we are returning them in a least format now you might be thinking can you can you really send a least format to the user because user will be a client right it may be a browser it may be a Android application so of course we have to convert this Java object into some format which is understandable with all the application and that's where XML and Json comes into picture okay but the question arise how will it convert your uh J uh your object into Json format so we don't have to worry about that now if you remember in your libraries we have added one of the dependency okay in instead of opening this we should be opening our uh XML not this XML the pom XML file now in this XML we have seen there are some dependencies using which you can convert and luckily uh we have Jersey here now Jersey says Hey user or hey programmer you just have to give the object it is my job to convert that object into XML format but then what if you want Json so for Json we need one we need one more dependency which is Moxy right again we can also use uh Jon we can use Jackson if you love love it but JY says hey why you have to add extra libraries when I can provide you those libraries so we are using JY Library here but you have a choice right we can also choose between Jason and Jackson and also this one which is Moxy okay so we have added these dependencies using which you can convert your object into either XML or Json it's just that when you return you have to mention what it produce now your application says hey I can produce a Json data or I can produce a XML data now depend upon what a user request for if a user is requesting for XML you will return XML if a user is requesting for Json you will return Json right now that is called as content negotiation so you are negotiating a content with the user and that is perfectly fine so that is about your get request right now we also need a post request uh where is that this is here's a post request let's say if you want want to create a new alien in fact what we have also done is we have connected this thing with database right so if you create a new alien that alien will be stored in the database and to do that we have used a repository here and in this we have done with all the configuration again uh you could have use data source here if you have a different implementation for database you can do that our application is so this Cod this file here is not concerned about what of database code you have written there you can also fake it in fact if you remember the first few videos we were faking that we have a database right we were using some dummy data dummy storage but now we we actually have myle there right everything was working you know we have a post request we can use get request we can use put and delete so we have to do the same thing is just that you have to change this annotation from put uh Delete we have uh post and we also mentioned path now why we need need a path we we have already specified path here right now to get that link example if I go to my post man and you can see we have aliens here right if I say I want one particular alien or if I want a alien to be updated or if I want Al to be deleted let's say I want to delete this one two we just have to go here just say uh Delete request and say send it will be deleting it will it will get deleted right that's how it works so everything was working so why I mean what next we are going to do see we have Jersey as one of the implementation right and if you are working for a company there's a chance that you might be implementing Jersey or maybe some other implementation now one of my favorite implementation for restful web services is uh spring spring rest because spring framework is a new framework which provides you lots of features right if you implement spring MVC spring code it's an awesome it's it's an awesome framework to work with so what we are going to do now is we will be working with the same stuff the same get request same post request put delete all the same thing with database of course but with the help of rest spring rest [Music] Advanced one especially for spring framework we have STS as a as a as IDE you can see we have STS here uh STS stands for spring tool Suite which is specially designed for spring based applications again you can also use Eclipse here okay it's your choice but then I will go for SS and you can also learn that right so why don't you uh let's expand our knowledge uh box and let's also learn SS now question arise from where you will download that uh so I will show you the downlo okay let me show you that if you go to a browser let's go to okay let's go to STS this is the official it will take you to official website of stts which is here spring tool site you just have to download SS from here there's option of uh all versions you can you can click on this uh if you depend upon which machine you use it will automatically detect your OS and it will download that particular version for you you don't have to worry about this so once you click on this it will get downloaded you have to just you just have to unzip it and you can use it now if you might be thinking oh we are using a new IDE now we have to learn lots of things you don't have to worry about that because STS itself is based on Eclipse uh Eclipse platform so if you're using STS internally you are using Eclipse right so let's let's start with it in the next video we'll talk about the first code with spring rest now when you open SS it will ask you for the workspace you want to you want to work with again you can can create a you can create a new workspace or you can work with the same workspace which which we were using for Eclipse ultimately spring SS or STS itself is Eclipse right now you can verify that when you go to STS and if you click on STS you will get an opt you will get that this works on Eclipse platform specifically Eclipse neon 3 so the version which I'm working on maybe if you're using a new version it will be having a latest version of neon or maybe Eclipse okay so uh the if you remember this is the project which we have worked earlier right now since uh I have this project in Eclipse I have worked on this uh project in the existing workspace I got this project here now what I will do is I will create a new project so to create create a new project now this time we are going for spring project okay there's an option of spring boot in Spring uh it's a new thing again you can go with normal spring MVC or you can go with spring boot now just to make these things easy for you I'm going for spring boot because spring boot says you don't have to do much of the configuration in fact if you're going for spring MVC there are lots of confusion which you have to take care spring boot says hey don't worry you can directly start with the project and the way you can do that is by saying new so I click on your project Explorer and say new and create uh oh okay so I'm not getting the option here so I will right click I will say oh can you see that I'm not getting the option of spring project here so if I if I see I don't have a spring project option we are using STS right so we have to make sure that you are using a spring perspective so if I click on here you can see we have a spring perspective you have you have to click on that and say okay now you are in Spring perspective now if you right click and if you say new and if you click on Spring startup project now you can see we we getting option of spring startup project now this is basically Ally your spring boot project so if you click on Spring startup project um okay it will give you the service Ur again we'll talk about this in a in in some time here I will mention my rest I will mention my project name as spring boot rest because this is not just a rest project this is a spring boot rest project and we'll mention the group group ID uh we'll say com. telescope and we'll mention here com. telescope okay everything is set let's click on next now here we have to select web because we are working with web right so we select web and nothing else just click on next and then click on finish the moment you do that you can see we got a project now this is a spring boot project and you can also see s there s simply means a spring project and at the end you have boot this is a spring boot project if I expand this and if I go to my pom.xml file let's let's see what exactly this file is if I go to Pal XML file you can see we have uh we have spring boot dependency here and then we have spring boot startup stter web so this is a web project basically this is a spring boot web project you can also Implement MVC here where we are going for spring rest let's close this and anything else if I expand this main if I expand Java com example if I open the spring boot rest app you can see we have a main method here and you can if you run this code it this will this will work now one of the awesome thing about spring boot project is when you select web and if you expand your M dependencies of course you'll be getting the some web libraries there but with those web libraries as you can see we have spring we we have spring MVC here with this we have one important thing which is this one can you see that we have an option of Tom Cat embedded now what exactly it means let's say if you build a project on your machine and you your machine in your in your machine you have Eclipse you have Tomcat installed everything is set up let's say if you're going to your friend's house or if you have a different machine now if you run this project on on the other machine what you have to do is you have to install Tomcat as well you have to download Tomcat you have to configure Tomcat but spring boot says hey don't worry why you have why we need a external Tomcat where I can give you embedded Tomcat that simply means you don't need a external server what I will do is I will stop the external server you can delete Del this delete this if you want I will simply stop this now what I want is I want to create uh okay I want to create a URL using which I can access some data now we have a choice of creating a web page here from which you can access data but that's not a good thing right we are building a restful web service where you don't have to mention a page we can request the data using Postman so what I will do is I will first of all I run this code and of course this will not work uh this will work maybe let's try so I will say run as run as oh now how do you run this do we have to run this on server no this is a spring boot project right so anybody want to run a spring boot project you have to select spring boot app just click on this now that's your spring boot app which is running and you can see in the console here it is running let me just minimize all this extra stuff which we never use anywhere and we got an error it says the port is already busy oh that's bad is it the issue yeah that's the because I already have my Eclipse running in running State here let me close my eclipse and let me just make sure that this is ex uh this is stopped or maybe I will delete this Tomcat server from here to be on safe side anyway so I will right click again I will say run as run as spring boot app it should work now and you can see oh again we got an error what's wrong who is using the port number 8080 that's bad really bad okay so what I will do is this will not be initial of course you will not get you will get nothing right but what I want is I want to fetch all the aliens so say I get request I will say I want to fetch all the aliens if I send a send if I click on the send request it should return the data okay now how do I do that it's very easy we just have to create a class and that class will is responsible to return the data the way we have done in Spring uh the rest Jersey right I will mention this is my alien controller now technically this should not be a controller this should be a resource that makes more sense right this is a resource and click on enter now how do you mention that this is a spring project I mean how do you mention that this is a spring a controller or a source now to do that we have we just have to use an annotation which is called as rest controller so anybody want to implement restful API you just have to annotate it with rest controller the way we have done in uh in just say we have used at path we have to use at rest controller here in this you just have to mention a method so I will say method name is public uh this return a list of aliens again we can write database code but that will do after some time we'll say list of aliens and I I will say this is get aliens and this will return a list now to return a list we have to get a list first right so I will create a list of aliens here I will say list of aliens and I will name this as aliens equal to new aliens but the problem is we don't have a alien class here that means first you have to create this alien class oh now I'm bit lazy I will instead of creating the class I will just simply copy paste this again you can call it as code reuse right the same alien class let me paste it here now problem solved is it the problem solved yeah problem solved we we don't have any issue with aliens now and I will simply return the aliens but unfortunately this aliens is empty so I will say alien I will say alien A1 equal to new alien and I will insert some data for this I will say a1. set ID we mention the ID as one1 I will say a1. set name I will set the name as naen and I will say A1 do set points I will set the points as 70 okay that's my points let me just use one more object I will say copy this and create one one more object for me this will be A2 A2 A2 A2 and this will one2 I will name this as Pria uh let's say points is 80 once I got this two objects two alien objects let me add that in a in that list I will say aliens do add let's add A1 I will say aliens do add let's add a two okay so we have added these two objects everything should work now now okay so before before running this code let's let's I have this question for you will this work think about it and let's see the answer now so let's restart the application now there is one thing you have to you have to remember when you work with spring boot don't start your project once again if you run this project once again it might give you an issue because springbot project has a embedded tom cat so it will not restart when you run this project you have to click on restart to restart the project or restart the Tomcat server let's go back to our Postman and say hey Postman send oh it's still not working of course it will not work because we forgot one of The annotation here which will map it right remember the path annotation there here we have to use request mapping because you are mapping with request right so we have to say request mapping and I will say request mapping with uh Slash aliens oh we don't even need slash that so I will say aliens so whenever you get a request for aliens this is what you have to run let's justart the application and let's go back to our tom cat or sorry Postman and let's say run it worked can you see that we got Json data we got your 101 the first alien we got the second alien as well it's so simple right so instead of using Jersey we can also use spring boot now the thing which you have observed here we have added no dependency nothing right in fact we got the data in ajent format and that's an awesome thing what Jason we we don't have to actually add any dependency everything is there but who is doing that in in your in your jersey we have Jersey converters right which is Moxy here if you expand this m dependencies you can see we have something called Jackson now Jackson is pre-built in in in Spring rest project or spring web project so this Jackson ja J Jackson is responsible to convert your list of objects into Json right so it's an exciting tool to work with again in the in the subsequent tutorial we'll talk about how do you use post request put request and Del request so the get request works perfectly right but then think about this if you remember your your rest with Jersey we have we have specified The annotation which is at at uh at get right here we can mention that in the in the method itself we can specify a comma here you can say method and that get or you can simply use at theate get mapping So when you say get mapping it means you are using a get request you can also use post mapping put mapping and delete mapping for other thing again we'll see that once we continue with the videos in this video we have the most important part is uh the database connection now think about this if you want to work with database because I want to fetch all this data here from database so of course we have to uh we have to connect with database we have to create a repository right so we have to say alien repository right and we have to specify all this thing example we have to specify all these properties and of course somewhere we have to specify all these properties uh where you have you have to mention your url your username password the URL and you have to mention this methods we need a method to to to find all the aliens have to write you have to mention the entire stuff here you have to mention the entire uh this entire code right for one alien you want the entire code for create alien this is the code for delete alien you have a code right for for everything we have a code there but what if I say you have a easier way now how do I Implement that now first of all I'm not using normal jdbc here of course we'll be using jdbc but then we will be using a layer of it which is called as a spring omm now we also have a concept of jpa again if you're not familiar with jpa Concept I would recommend you to learn about that as well I do have a hibernate playlist if you if you follow follow that playlist you will understand how exactly jpa works or how exactly hyate works again you will find that in description area but even if you don't know that's fine I will uh we can ignore that internal part of it we can understand the overall structure what is happening what I will do here is first of all I will add a jpa support in this project and as you can see in my P XL file I don't have a jpa support so we have to add a jpa support how do I get that how do I get a spring jpa support for that you have to go to my repository and search for spring spring boot oh what's that spring boot jpa now if you search for spring boot jpa your your first result in this you have to make sure that you're selecting 1.5.4 because that's our project version if you see that's the project uh spring boot version we are working with so let's use 1.5.4 copy that and paste in your dependencies now you got a spring boot jpa support now once you have added that and the moment you run this project even if you are not changing any stuff we have just added dependency right let's try to run this code let's say run as uh spring or we'll restart the application the moment you restart the application you will get an error okay before solving that error I want to tell you one more thing Let let's say if you want to create a project and you're sure that the you you will be building a project which will be having a rest and jpa both so while while you're getting the project at that time itself you can mention that you want to work with jpa so you don't have to manually add this dependency again uh we can it's very easy you can just right click here and say new spring startup project and you can mention the default stuff here you can mention I want a web stuff so you can mention web and you can mention jpa so you don't have to manually uh manually do the manually add a dependency but let's say we have added this dependency right anyhow but what is this error now it says cannot determine the embedded driver class for database type none what what is this eror now what is what happens you know your spring boot project is very intelligent or you can say very smart it says Hey programmer I can see that you have added this spring JP dependency but you have not specified the properties oh spring understand everything things so what you have to do now is you have to specify those properties here but which property you have to mention now what are settings we have to do here so it's very easy actually you can mention URL and it will give you a list here right and inside this list you have to be you have to select spring. datas source. URL because we are using a data source concept here and in this we'll mention jdbc colon MySQL colon SL localhost the same the same property you have to mention in normal jdbc right so we mention 2306 slash the database name I forgot database name which we are working with so if I go to my alien repository uh the database is rest DB right let's use the same thing we's say rest DB the next thing you have to mention is username so I will say spring. dat source. username we'll mention the username as root and I will say again spring. dat source. password I'll mention the password as zero and the last thing is the DV class we say spring dot the same property we have to mention in any jdbc okay don't there's nothing new which is spring data source dot it is driver dot driver hyphone class hphone name you don't have to byard this thing okay it will be pop up it will pop up automatically so we'll say com. MySQL jdbc do driver right so we have specified all these properties when you work with agent repository here the same stuff you see that the same stuff and that's it our job is done I think let's restart the application again this will not be fetching from but at least the application should work oh we got an error now it says it is not able to find com. M.J driver because we forgot to add MySQL dependency let's add that as well let's go to my repository here and let's say hey I want a MyQ connector in fact you don't even have to do this if you know that you are working with uh spring boot jpa web and myql while cutting the project you can also search for my it will give you the dependency but since since we have not done that there let's do it manually here I will say I want I want 39 version which is 4.3.9 again you can go with any version which you love okay we have added the Mas dependency as well let's let's restart the application now it should work and you can see there's no error right there's no error now but again it will not work from database because we are not fetching from database how do I do that here so in this project if you want to make it work with database you have to go to your package and say Hey I want to create a new class now what if I say you don't have to create a class you have to create an interface you might be thinking okay we have to first get the interface then we have to get a class and then we'll Implement uh let's see how it works we'll say alien repository okay and we just have to extend or we click on finish we got the interface here right we just have to extend with one of the one of the internal one of the internal interface called as C reposit three where you have to mention what Ty of class you're working with and the primary key whatever primary key you're working with so I'm working with a primary key integer now in your M database you have a primary key which is uh this is not our table let's use rest DB let's verify what you're working with and select star from Alien okay so this is our data right now this is your primary key which is integer so we have to specify integer here and and now you might be thinking okay we have to Define all the methods here create uh get update and delete and then we have to create one more class which will Implement in this interface right that's how Java works but what if I say you don't have to do any of this stuff let's give this interface empty and let's not create any other class and in here in this resources we have to create object of alien repository we say alien repository and we'll say repo that's it and here we'll simply write at autowire now this is a spring core feature if you want object you don't have to create the object by saying new or alien repository you can simply write at autowire it will search for the implementation it will give you the object you don't have to worry about this and here let's not create the object by ourself simply say the data will be coming from the repo we'll say repo dot repo dot uh find all so we have to use internal method which is find all okay this works okay so we can typ cast it uh if it if it will not work let's change something let's let's let's run this code let's see what happens the moment you run this code you can see I'm using a repo now and oh we got an error oh it says it is not a manag bin uh if you're using database we have to also make sure that this is an entity because we are using a concept of hnet and in hibernate we have to mention this as at entity and in not just hynet we're talking about in general jpa right so in jpa we have to mention at entity and at ID so at ID will give you at ID will make this as a primary key and this class as entity class okay so we are learning from errors now let's go back to our Postman and let's say hey Postman fetched the data and you can see that we are getting all this data and these things are coming from database right so that's how this that's how this stuff works here you just have to say find all just type cast it with with list and everything will work fine you don't have to use any TR stuff everything will work fine right awesome thing right so this is how you use a spring rest with database with the help of jpa so let me recap what is happening here the magic thing here is we are not creating any class which implements this interface and it still works right so this thing is done by spring for you now there are lots of people they say hey Spring is not that good now what is this spring is awesome spring is not good it is awesome right it will give you all those extra features which which you need right so that's the magic of spring boot again we once we continue with the example in the next videos will you will get more idea about it but I would recommend you that before going for the next video do practice this because this is something which you will understand only when you do it because it's a magic right and Magic when I show the magic you will be thinking oh this is awesome but the real thing you will understand when you try it by yourself so try it by yourself then you will see the actual uh then you will understand the actual [Music] stuff now when you say spring it basically means spring framework right we're talking about a framework here and spring is one of the best framework available for Java yes we had different Frameworks before example for web development we were using STS in the Enterprise Market we were happy with egyp and then on the other hand we have database side where we were happy with jpa but then now with the help of spring you can achieve everything the one thing which is amazing about spring is it focus on pojos now remember when you talk about ajbs it was all about entities right they were good you know you can build huge applications which are stable scalable using ejb but then still the components which we are using is very heavy in terms of the size or the services it uses but what if I say you can do everything with the help of poos now if you don't know what this pojo means you know if you don't know what services means that's completely fine just for this course you will get everything but then if you know about poos yes we can use pojos in spring now spring is also the combination of multiple projects it's not just one project which you'll be using example let's say if you want to achieve a very common feature of spring which is dependency injection we have a separate module for it if you want to work on web if you want to make a web application we have a separate module for it and of course one of our favorite if you want to build a rest application we have a seate module what if I say spring also can be used for security that's awesome right as a developer we are focusing more on application development we are not focusing on the security part of it and spring will help you there as well if you want to connect database with the help of some jpa spring has a different module so spring is basically a collection of module it has right and then we'll be going through most of this model so we'll talking about spring code which will help you for a dependency injection we'll talk about spring which will help you for web development we'll talk about spring rest to make rest apis and this goes [Music] on so before going for the actual modules let's talk about one important thing which is the official website of Springs now if you go to official website let's go to Google and search for spring and the moment you search for spring it will give you this official website which is spring. iio just click on that so you can see it say Spring by pyot so pyot is basically a company who has designed spring and you can use it for free uh that's great that's an open source project which you have now if you click on Spring projects you can see we have lot of projects here which we have talked about before we have spring boot again that's a surprise for you what is spring boot will not reveal that as of now we have spring framework if you want to work with data flow we have that as well if you want to work on Spring Cloud we have spring Cloud we have spring data and then this is very important Spring Security and then you can also build spring web services right which is your soap and Le goes on there are so many features which we can use here of course we we cannot be covering everything in this course we'll be focusing on most important part which is spring framework so of course we have our focus is this one with this we'll also cover spring Boot and spring data will be also there and then one of my favorite which is Spring Security okay that's the official website and the amazing thing about spring is you know not just spring it's about Java we have amazing documentation same goes for Java docs we have in fact you sure of referring any book you can refer for Java docs and you can solve your queries we have huge community in the same way we have spring documents as well so if you can just go to the guides do we have here uh so you can see you will get a lot of help from this otherwise you can just click on this and search for spring docks and you can see it gives you spring documentation as well you can click on guides and this is where you will get the reference documentation just click on this in fact they will also give you a performance so let's say if you want to read your book in a mobile so you can also use EPUB uh in fact if you want to get that you can simply search for spring framework ePub and this will get to the official website you can just go here okay they giving you 4.3 5. is also also available if you click on the first one you can see that we have the reference document dat which is ePub of spring five that's amazing right in fact one of I'm a big fan of this documentation at least for spring you can refer everything on book uh the ebook which I'm talking about otherwise you can just refer online you can see we have online documentation let's say if you want to know about spring Cloud just click on Spring cloud and you can see it gives you all the options you can click on documentation and you will get it so you can see I'm clicking on the documentation you can see we got single HTML open that and you can read the entire documentation so that's how you can refer documentation yes there are some books available as well on Amazon on different website of course so you can just go there and you can buy any book I will also try to releast my favorite books available for spring but then as of now the spring documentations is awesome and with this videos of course now what are the three requisites for spraying the first thing you should know is Java of course right so if you don't know Java how can you work on Spring framework in fact any framework on Java so the code Java part is very important you should know what is oops you should know what is exception handling what is threads and all those basic concept of course Loops as well the next thing you should know is database connectivity So when you say database it can be Mas with Java it should be maybe post with Java so what database you have worked on with Java that jdbc is very important so if you know that that will be amazing the next two is very important the first one is salet and JSP now of course when you work on sping MVC it's a MVC model now you'll be thinking okay we have done with s JSP why you have to learn old Technologies what if I say in Java framework you have we normally work on Tomcat right so Tomcat is Serv container which simply means even if you are running spring framework on Tomcat ultimately everything will be converted into servlets so having their knowledge of seret and JSP will be awesome so not everything but Basics are very important example you should know how to send data from client to server then what is HTTP request object HTTP response object all those basic concept that's it the next thing you should be knowing is omm framework now it can be hybernate I betes or simple GPA everything will be fine so I would suggest if you have not seen hnet videos it is available on YouTube for free just go there and you can watch hnet videos so that's the basic thing you'll be required here that's it nothing else uh yes there are some optional concept if you know that will be amazing you know how internet works the protocol works then the next thing is you know we have for sping security we should be knowing about ldap or or the basics of it of course that will be advantaged but again but once we start with a particular module in every module I'll be talking about some basics of it what are things required to for you to learn so you can just pause the videos and can go back and Google it right it's not that difficult even if you don't know KJ that's fine you can just pause this video and know pause the series go back to my code Java videos watch some of them which is fre available on this website just go there and you can learn basic code Java and you can come back here that's completely fine so that's the prerequisite you should be having and that's it that's it everyone now it's time to talk about the requirement in terms of software of course right if you want to write your spring code where will write it in fact we have the simple way right you can open notepad type your code and you can run it in command line we can do that but hold on will be building big projects right so of course if you're learning spring framework in future you will be building amazing projects and that's where if you have a good ide with you it will be an awesome thing right and that's where we have lot of idees available you can use net beans which is everyone's favorite you can use Eclipse which is industrywide famous you can also use one of the best ID available for Java which is intell J the ultimate version of course because in community version I guess spring will not be supported so you have to go for the ultimate version and it is is awesome the only thing is you have to it's not a free version but then on the other hand what if I say spring officially gives you a separate IDE it's not compulsory to use that you can still use net beans or Eclipse so the so software name is spring tool switch so if you search for it you can see it will take you to the official website just click on that and you can download this spring STS for free now you'll be thinking oh we have to learn a new IDE now not exactly if you have worked on Eclipse before spring is Eclipse so it's a Eclipse IDE on top of that we have a spring layer in fact if you just go here it says based on Eclipse can you see that so yes ultimately you are using Eclipse you're not using something else is just that you don't have to add a spring plugin Eclipse to use it right you can simply use HTS so you can download any version which you like so let me for this machine I will download the windows 64 bit because this is a Windows machine now the only thing is of course it will take some time for download so if you want to install this version you also need to have a jdk available so jdk is something which is Prett it but then which one so the suggested one is the minimum requirement for this is jdk 8 because we'll be working on Spring framework 5 for which jdk 8 is The Benchmark the reason is in Spring 5 we got reactive programming it supports spring reactive programming now and reactive programming can be achieved with the help of lambra expression which is only available in Java 8 but then spring framework 5 also supports Java 9 so that's great if you have Java 99 with you you can also Implement Java 9 features in Spring 5 but we'll not be focusing on that will be only focusing on Java 8 features with spring framework 5 uh so once it gets downloaded you can install it you can unzip it and you can use it but how to do that that will see in the next [Music] video now once you have your SS with you of course you have to unzip it because that's a zip version the only tip is when you unzip it make sure that you unzip with the help of a small name because if you unzip that with with a big name it may give you issue the issue which I was facing from a long time uh so make sure that you give a small name May remove that version number there now once you got the unzip version you can see it says STS bundle let's double click on that now it will give you this thing which is awesome you know because when you work with Eclipse you have to download Tomcat separately that's what you do and of course Tomcat is not that heavy to download but then pyot says hey when you are using SS we will give you Tomer as well so it will give you pyot TC server which is pre configured for this and then we have STS as well so let's double click on STS and we got STS here and you can see that beautiful logo of it double click on this it will open just like Eclipse because behind the scene is Eclipse right it will take some time for you when you open it for the first time because you know it will a lot of files now it will ask you for the workspace name as we do in in Eclipse so here I will give a name as spring HSE again you can give any name doesn't matter it will simply get a folder in your document so that in which you'll be having all your projects simply click on launch and it will take some time to launch it since we're doing it for the first time so fast forward you can see it is still opening and so that's my friend is STS for you yes it looks like Eclipse now I will close this dashboard of course you can see we have reference documentation available here as well simply click this and you can see this is so if you are new to any ID if you have not worked on net beans E before on the left hand side you can see we have a project section because this is where you will be having all your projects in the center you have a area where you write a code and on the in the below section you can see we have console here so all your logs will be printed that if you want to print something on a console that's where you will get the output now let me create my first project now if you can see we can get multiple type of projects here we can create a spring startup project which we are not going to see now that's a surprise we'll see how we can you know make spring easy the second thing is we have spring Legacy project we'll not do that now we can create a jav project but there's one issue with this the issue is when you work with spring framework we need to add a spring dependencies right and when you say you want to add spring dependencies you have to go to Google you have to search for all the dependencies which you need and then you have to download it manually but what if I say you can do that with the help of mavin because mavin will help you here so Maven is basically a buil tool which will help you to get dependencies which will help you to create a package for your project which will help you to compile your codes as well but at this point let me use M only for downloading the dependencies let me click on M project okay so you can see it will say uh you want to create a project yes I want to get a new project now Maven has multiple types I want you to also know about mavin as I mentioned you can just go to YouTube or Google you can search how Maven works but just to give you a brief using Maven you can create multiple type of projects you can create a simple standard on Project you can create a web project let me create a quick start project which will have nothing a simple project uh click on next now you can give a special name to it now we have to also mention group ID time I will mention com. Tesco as my group ID the artifact ID will say this is demo project and then click on finish now once you click on finish it will download the MAV dependency it will create a project for you with the all require dependencies but what if you want some extra dependency because if you see this project will have nothing it will not have spring dependencies it will not have hypernet of course we have not mentioned that as of now so how do you add dependencies what if you need a jar file for spring it's actually very easy you can just open your pom.xml now mavin works with this file in which you have to mention the dependencies you can see this is where you have to mention what dependency you need now if you think mer is the only option not exactly we can also achieve this with the help of so you can use griddle or you can use mavin that's your choice I guess lot of people nowadays they are moving towards griddle but again mavin also works now question arise from where you will get this dependency so you have to type it you by yourself you have to just go here now the thing is it is downloading some artifacts for us from the internet as of now let's keep it on hold this is where you have to put your dependency tags but we don't know what TP you have to type right we don't know the artifact name on for the spring framework in that case let's go to the official website again now for this we will go to Google and search for mvn so we search for mvn repository now this is where you can download all the me dependencies so let's click on this website and this is where you will get for spring so just go here and search for spring and you have to download spring context okay so not spring spring context because spring context will also have spring code uh let's click on this now you can go with any version my preferred version is always where you have maximum usage so you can see we have a usage of 419 which is which looks stable so let's go for five . 1.3 let's copy this in fact when you click on that it will automatically copy it so just go back to your application and paste you just have to specify that and if you go to my dependencies now on the left hand side you can see by default we have J unit but hold on very spring if you can see it says building workspace so if you click on that it is downloading all the dependencies yes it takes some time depend upon internet speed unfortunately M speed is not that good today so it takes some time and you can see we got dependencies which we wanted right so if you don't have to download this Jal file one by one it will get downloaded automatically the moment you paste that code so this is working you got your dependencies and now you can write your first [Music] application in the last video we have seen how to create a simple spring boot application but now let's understand how exactly we can use it so let's say we have this main file here in which we have nothing but then here I want to create the object now if you remember if you go back to the theory video which you have talked about which is dependencies or dependency injection normally what happens you know when you work on complex projects that's where you need to add some extra objects so your application will need some extra objects maybe those objects needs extra objects example let's say if you build a laptop you need a hard drive and to build a hard drive you need some other parts as well so you don't have to build so as a laptop manufacturer you don't have to build hard drive you can inject it or someone else will inject the hard drive in your laptop you just have to ask for it and even you don't even have to mention what company hard drive you need you simp you can simply Say Hey I want an hard drive now based on your consultant or based on your team they will say okay let's use seate hard drive let's use maybe Hitachi hard drive that's their choice so here what I want is I can create the object of alien so I will say alien obj but hold on I'm not creating the object I'm just creating the reference first to create the reference of alien we need a alien class right unfortunately we don't have this class okay let's create one okay when I say alien I'm only talking about so normally I refer alien so all the programmers for me are aliens in fact not just programmers the tech professionals are aliens because we don't think in real world right we think in virtual world so let's create the class I will simply click here and you can see I'm creating a class the class name is alien and a very simple class now in this class I want to have a simple method okay like the meth name would be let's say public void code because normally as a programmer we write code so we got code and in this I'm printing I'm coding that's it nothing much we got a alien class in which you have a method which is code and then we are saying I'm coding let's go back to our application here now I want to call that method which is code of course right you can simply say obj do code but unfortunately this will not work you know why it will not work because we have not initialized this object here and that's what it's saying you can see it says the local variable obj may not have been initialized now of course this obj local variable inside the main method how can we initialize it the best way we can do that is by saying new alien but that's where problem starts you are saying new which means you as a programmer you are creating the object and that's where problem starts because when you create the object you are responsible to maintain it we don't want to do that right that's we are using spring framework come on we can inject the object but who will create the object for us and that's where spring says hey if you want object I will give you the object you just have to ask for it how will you ask for it it's very simple you can just go here and say hey Spring give me a bean spring will say okay I will give you a bean but tell me which class object you need do you need object of alien class do you need object of object class do you need object of adct class there are so many classes available right Which object do you need and that's why you will say okay you're getting confused that means I have to mention so I will simply say alien. class so you are saying hey Spring give me the object of alien class okay spring will say okay but then there's one issue right in Java if you want to call a method we have to use the object of course right you can't call the method without the object so here what I will do is I will simply say which class object do you need so this get bean belongs to an interface which is application context so let me repeat get bean method belongs to an interface named AS application context so if you want to call get bean we need object of application context the amazing thing is if you click on this run method so you can see we have a line here which is Spring application. Run which takes the class name itself if you click on this run you can see we got documentation of course so how do you get the documentation you just have to press control and click on the method name which you want so you can see this run method returns an object of configurable application context this class or this interface is it interface yes this interface extends application context so that means this run is actually giving you the object of application context so we can simply say application context let me import the package I will say context equal to we got the object now once you got the context object I can simply come here and you can say context. getb our job is done will this work let's try let's go back here and say right click run as spring boot app I hope this will work let's try you normally learn by it as right so let's let's see and and we got error the error it says no qualifying being of type com. telescope. first. alien but we have a class there right it's just that see who is responsible to create the object spring framework right so spring responsibility to create the object of alien and you can mention that to Spring by saying add component So when you say add component on top of your class it simply means spring framework will know okay now it says add component which means I'm respons possible to create the object of alien now it should work let's go back and let's relaunch the application and it worked and you see that we got the output as I am coding it's that simple right and that's the beauty about spring framework you are not responsible to create the [Music] object welcome back everyone till this point we have seen what is frame framework and then we have also done this setup for stf now of course right spring is one of the best framework available for Java there are some other Frameworks as well example we have STS for web development for we have hibernate for database and yes we can use all those Frameworks right now Frameworks are awesome but there's only one issue when you talk about Frameworks and that is we have to do a lot of configuration to achieve the thing which we want in fact even if you building a small application we need to do a lot of configuration but think about this if you have so so let's say in this world we have so many people building different projects but all this project will have something in common so all the configuration will have some configuration which is common for all the projects don't you think the basic configuration should be given by Spring framework itself uh from a long time you know when spring was introduced everyone loved it but then there was still the problem with the configuration but now we have a concept of spring boot Now using spring boot spring boot basically means you know uh spring boot believes that you should focus on convention instead of focusing on configuration so it's normally States convention over configuration so you have to focus on your logic don't focus on the configuration okay when I say don't focus on configuration I'm talking about the basic configuration what if you want to have something customized that's where you have to do some changes right but for basic spring will give you or spring boot will give you all the basic configuration okay now to understand that let me create a simple project on Spring boot but then don't worry we'll be also focusing on Spring framework so this video we talk about spring boot how do we create a project and how do we uh use some features there but then the further videos we focus only on Spring framework now here first of all let's cre a project now first of all you need STS here which is which we have already done and then we had this project as well which is demo project in the last video but now let's do something different let's say new and let's create a spring stter project as of now it's not important to understand what is this service URL stuff the only thing which is important here is the project name I will say this is my first project of course you can give any name and then I will give a group ID which is my company name itself I will say com. telescope and then here the package name I want is telescope DOT first that's my project name and then package name let me click on next and here you can see it will give you so many options right now this is different okay this the project which we are building now is a spring boot project so when you say spring start a project it means spring boot it will give you so many features right based on whatever project you building let's say if you're building a web project you will be selecting web here if you want to build a project with database of course you have to search for jdbc in fact if you search for jdbc you can see you have an option here what if you want to work with mq you can choose that you can choose post there are so many options here what if you want to implement security and that's why we have security features as well but as of now we don't need anything and there's one more thing important which is bring boot version now of course in new version we get new features as of now 2.1.2 is stable so let's go with that click on next nothing much specific here click on finish now you can see we got a project here okay is it so fast not actually you know when you're doing this for the first time it will download all the project structure and the dependencies from the internet right so it will take some time but on this machine I've already did this so I have a local copy of the jar files and if you can see if I open this we already have a Java file here which is first project application. Java and the amazing thing is it already had some code in it because okay at this point we don't understand what that code is but let's keep it there you can see if I open this me dependencies we have so many dependencies here by default and that's the magic right spring boot will give you everything which you need all the basic stuff of course if you want to have some extra features we have to add those dependencies but as of now it will give you everything which is required okay and then the next important file here is the spa. XML so if you have worked on Maven projects before for you know what this P file is right so this is where you will add required dependencies I'm talking about the jar files in this case we wanted to work with spring boot so you can see we have added two dependencies one for spring Boot and for testing purpose as well nothing much here let's create our first stuff now what exactly I want to create that we'll see in the next video e [Music] in the last video we have seen how to create a simple spring boot application but now let's understand how exactly we can use it so let's say we have this main file here and which we have nothing but then here I want to create the object now if you remember if you go back to the theory video which we have talked about which is dependencies or dependency injection normally what happens you know when you work on complex projects that's where you need to add some some extra objects so your application will need some extra objects maybe those objects needs extra objects example let's say if you build a laptop you need a hard drive and to build a hard drive you need some other parts as well so you don't have to build so as a laptop manufacturer you don't have to build hard drive you can inject it or someone else will inject the hard drive in your laptop you just have to ask for it and even you don't even have to mention what company hard drive you need you simp you can simply Say Hey I want an hard drive now based on your consult or based on your team they will say okay let's use Seagate hard drive let's use maybe Hitachi hard drive that's their choice so here what I want is I can create the object of alien so I will say alien obj but hold on I'm not creating the object I'm just creating the reference first to create the reference of alien we need a alien class right unfortunately we don't have this class okay let's create one okay when I say alien I'm only talking about so normally I refer alien so all the programmers for me are aliens in fact not just programmers the tech professionals are aliens because we don't think in real world right we think in virtual world so let's create the class I will simply click here and you can see I'm creating a class the class name is alien and a very simple class now in this class I want to have a simple method okay like the method name would be let's say public void code because normally as a programmer we write code so we got code and in this I'm printing I'm coding that's it nothing much we got a alien class in which you have method which is code and then we are saying I'm coding let's go back to our application here now I want to call that method which is code of course you can simply say obj do code but unfortunately this will not work you know why it will not work because we have not initialized this object here and that's what it's saying you can see it says the local variable obj may not have been initialized now of course right this obj local variable inside the main method how can we initialize it the best way we can do that is by saying new alien but that's the problem starts you are saying new which means you as a programmer you are creating the object and that's where problem starts because when you create the object you are responsible to maintain it we don't want to do that right that's we are using spring framework come on we can inject the object but who will create the object for us and that's where spring says hey if you want object I will give you the object you just have to ask for it how will you ask for it it's very simple you can just go here and say hey Spring give me a bean spring will say okay I will give you a bean but tell me which class object you need do you need object of alien class do you need object of object class do you need object of add a class there are so many classes available right Which object do you need and that's why you will say okay you're getting confused that means I have to mention so I will simply say alien. class so you're saying hey Spring give me the object of alien class okay spring will say okay but then there's one issue right in Java if you want to call a method we have to use the object of course right you can't call the method without the object so here what I will do is I will simply say which class object do you need so this get bean belongs to an interface which is application context so let me repeat getb method belongs to an interface named AS application context so if you want to call get bean we need object of application context the amazing thing is if you click on this run method so you can see we have a line here which is Spring application. Run which takes the class name itself if you click on this run you can see we got documentation of course so how do you get the documentation you just have to press control and click on the method name which you want so you can see this run method returns an object of configurable application context this class or this interface is it interface yes this interface extends application context so that means this run is actually giving you the object of application context so we can simply say application context let me import the package I will say context equal to we got the object now once you got the context object I can simply come here and you can say context. getb our job is done will this work let's try let's go back here and say right click run as spring boot app I hope this will work let's try you normally learn by erors right so let's let's see and and we got eror the eror it says no qualifying being of type com. telescope. first. alen but we have a class there right is just that see who is responsible to create the object spring framework right so spring responsibility to create the object of alien and you can mention that to Spring by saying add component So when you say add component on top of your class it simply means spring framework will know okay now it says add component which means I'm responsible to create the object of alien now it should work let's go back and let's relaunch the application and it worked and you see that we got the output as I am coding it's that simple right and that's the beauty about spring framework you are not responsible to create the object so once we have done with alien think about this when you say a alien can quote so I'm talking about programmers here so when you can code of course you need a laptop right or desktop so if you want to code something of course we we go in multiple steps we have to compile our code and that's where you need object of laptop so let's create one get the object of laptop so of course right if you want to make it work you have to instantiate laptop as you can see we are not doing it and the moment you run this application now we are saying code of course code will call uh this method and then it will call compile but to call compile first we have to instantiate laptop let me just run this application as it is I will say run as spring boot app and now you can see we got okay it's asking for permission again and you can see we got an exception which is null Point exception in this case of course right this object here is not instantiated so of course you can use new a laptop here but then that's the idea right we don't want to create the object by ourself we are not done for the alien as well and that's where we can use a special annotation here which is named as autowire so when we use autowire it simply means that this object this laptop object will be created by Spring oh hold on we're still missing something we have to mention that component here as well right you have to say add component so you have to make sure all the classes has ADD component on top of it the same thing we have done for alien now what will happen is the moment of this application of course we have a detailed explanation for this in the next module uh this is just the introduction for spring boot how it can be awesome so you can see the moment you load this application it will give you two objects the object of alien the object of laptop right and in the alien class we are asking for the laptop object so it will check hey do we have object of laptop aail available in the memory and the answer is yes so it will give you the laptop object here and that's the beauty now since you are not creating the object it will be managed by Spring framework and trust me it will give you a lot of advantage in making big projects as of now yes it doesn't make sense because we have a very small code but hold on now the more we learn about spring framework with different modules different projects this will surely make sense so that's about Auto wire okay so you can see oh we have not ran this code let's do that so so right cck run as spring boot app and you can see we got the output it says compiling that means we are able to call this code method and cod is able to call this compile without any issue let me repeat we are not creating the object of laptop it is managed by Spring frame work so that's it from this video in the next module we'll talk about the fancy stuff I mean the when say fancy it simply means what is happening behind the [Music] scene welcome back everyone one till this point we have seen some amazing stuff in spring boot right we have seen how to create a project in Spring boot we have seen how to use dependency injection there and then the important concept Auto wire but then still we have not understood how spring is doing everything behind the scene and that's what we have to understand now question arise how will you understand what is happening behind the scene because in Spring boot you cannot do anything else right because everything is done by Spring Boot and that's why next few videos will not be based on Spring boot it will be based on simple spring framework now when you say simple spring framework that's where you are responsible to specify everything what is the scope of your bin is it single turn or prototype again we'll talk about that in detail don't worry but we have so many things which is happening behind the scene that's what we are going to see now so let's get started so step one we will be creating a project in Spring okay not a spring boot project a simple spring project but then question arise how will you manage depend depencies I'm okay I'm not talking about object dependencies I'm talking about jar files and that's where we can use Maven right so let's create a project here I will create a m project and we'll name this project as okay let's say next I'm not making a web application I'm simply making code application audio what you say a standalone software so we'll select quick start here click on next this is where you have to mention a group ID so let's say com. telescope and the artifact ID will be spring demo of course you can go for your name it doesn't matter right and click on finish now once you got your project now this is not a spring project okay the name is only spring but then it's a normal mavin project what we want is we want to have spring features here but why so to understand that let's go back to app and this is where I want to create an object okay first of all Which object if you remember in the the spring boot videos we have talked about the class as alien let's stick to that here so let me remove this hello world oh first of all let me just verify if everything is working that's what we do right when we create a new project we just have to verify if the thing which you are working with is working so let's try so let's right click here and say run as Java application you should see some output in the console and it says hello world that quit at least this is working so what next let's create an object of alien the way you do that is by going here and say Hey I want to say alien object so I would say alien obj but unfortunately we don't have this class one right that's what is saying here we don't have that class as alien so let's create one so of course you can just go there and you can create a separate class as alien otherwise you can simply use shortcuts here so let's click here and say hey create a class for me and now we'll be getting a alien class click on finish and you can see we got a very simple alien class now this is where I want a very simple method which is let's say public void show or maybe public void code the same meth which we have used earlier and here we'll print PR I'm coding nothing fancy we got a simple alien and then we got a method which is code and we are printing I'm coding okay so the idea is I want to instantiate this alien first so I will simply say equal to new alien oh hold on that's what we don't want to do right we don't want to instantiate this object by ourself that's where spring comes to picture so what spring will do now now spring provides you an amazing feature of dependency injection or you can call it as ioc which is inversion of control where you don't have to create the object it will be done by Spring framework okay so let's ask spring framework here so we'll say hey Spring give me the object of alien okay we have used that method name right now if you remember we have a very special method here which is get bean now get bean is responsible to give you the object but we have a Twist here the twist is get bean belongs to some interface right that's the first thing second So when you say it belongs to some interface it should belong to libraries right of course in code Java or the jdk which we normally use here we don't have this as a inil class or a inil method right inside jvm or inside J so we have to get it from somewhere and that's where we have to add dependencies for spring framework if you go to your M dependencies you can see we don't have any spring dependency here so that should be our first step we have to add dependency and to achieve that let's go to browser so we will go to m repository this is where you will get all dependencies right and here we will be searching for spring context now when you search for spring context you can see we got it here let's click on that now you can see we have so many versions the latest version is 5.1.4 now depend upon when you watch this video if you're watching this in Jan Fab of course you'll be working on 5.1.4 uh if you're watching this later of course you'll be having some new versions but you know every minor version there will not be any big updates so we can select any of this in fact I prefer the dependencies which have more usage you can see we have 504 here so let's pick up 5.1.3 okay let's copy this code from here go back and oh where to paste it and if you are working on Maven before you know we have to just go to pal. XML file and this is where we have to add a dependency so simply paste it and the moment you say save now if you're doing this for the first time it will take some time because all these dependencies will be coming from the MAV repository the remote one uh luckily on this laptop I've already used it so we have a local copy of it and that's why it took around 1 second to do that okay so once we got dependency let's use them where to use it go back to your app and this is where you'll be using a interface in which you have this method which is get bean Okay now what's the interface name okay you guess it right we have a concept of application context but then before the application context came into picture we had something else think about this if you want to make a car you have to open a car factory if you want to make a mobile phone you have to make make a mobile phone factory if you want to make a bean you need a you guess it right it's bean factory so we have an interface named as bean factory let's simply say control space here and you can see this bean factory interface belongs to Spring framework Library so let's click that let's instantiate it we'll simply say Factory equal to so we'll say B factory factory equal to oh we have a problem here be Factory is a interface uh we don't have a class so let's instantiate it with the help of a class CL and the class name or the class which implements bean factory is XML bean factory that's it you just have to mention that so I have to mention bean factory factory equal to new XML bean factory and you can see we got a strike there now this strike simply means that we should not be using this class here because this is dicated of course we have some new features we got some new classes so you should not be using this but time in since we are into learning State we'll be learning everything from start right so this is the first class which came into picture to implement bean factory time just ignore that deated strike there okay now we have a issue the issue is when you say get bean so let's say if I say Factory here I would simply say factory. get bean we still have an issue here the issue is how do you know which class object do you need so that's what you have to specify in this bracket I will simply Say Hey I want object of alien but see spring framework is a framework right it's not a Superman it's not a magician to know everything right so So when you say alien spring should know what this alien means and that's where we have to do some configuration of course right whenever you work with Frameworks there is a part of configuration that's one of the reason why people love spring boot right we have less configuration there but since we are not using spring board we're using normal spring framework we have to configure it but how there are different ways you can configure this there's a XML based configuration we have a Java based configuration or we can also use annotations time being since we are into learning State let's use XML that means if you want to customize it if you want to specify configuration we have to create XML files and then you have to specify that file here but then unfortunately we cannot simply pass a file name here if you want to specify the file we have to create one more object here which is new file system resource is it there file system resource yes and in this you have to mention the file name so file name is we'll be creating that file I will simply name it as spring do XML of course you can go with any name but let's go with this okay once we got this Factory object once you have specified hey I want an alien but still we are getting error see it's not because of we don't have that file it's just that when you say get B it will give you object right example if you see it says it will give you object we want object of alien in that case in this bracket I simply specify alien that's it you can see there's no error yes we have some warning but that's fine let's right click here at least let's see see of course this will not work because we don't have spring.xml file as of now let's see the error as well so we simply say run as Java application yes I'm expecting error here and we got an error and that's why it is saying file not found exception which is it's not able to find spring.xml okay let's create it then so let's right click on your project and I will say new and this is where you'll be saying Hey I want to create a XML file that's one way of doing it otherwise you can simply say see the problem is when you say XML we have to add the DDD as well if you don't want to do that you can simply say new there's the option of spring Bean configuration file and that's why we are using STS here you could have done the same thing in Eclipse as well but in Eclipse you don't get this option which is spring Bean configuration file let's click here and let's mention the file name the file name here would be spring.xml the same name which you have mentioned in the code let's click on enter and we got our XML file this is what we wanted right and you can see everything is mentioned let's enter say enter here this is where you have to say hey see if you go back to your app it says find I mean get bean of alien so we just have to mention hey Spring framework whenever a user says alien give the object of alien class now alien class belongs to a package which is com dood do spring demo. alen whenever in a code says alien you have to create object of alien I mean this alien the class alien but we cannot simply use this syntax right we have to use syntax of spring and that's where spring says hey if you want to specify this create a bean as let's say Bean every Bean will have an ID so we'll say ID equal to and ID is alien so whatever ID you want whatever name you want to use in the code which is alien in this case mention that and also mention the class of which you want an object I will simply say I want object of com in fact we can use alien because it will will give you the class as it is say control space you can see it will autoc complete it and that's it that's the only thing you have to mention it's Beauty right you just have to configure it by saying hey when a user says alien give the object of alien go back to your code and now let's run this application so right click say run as Java application and oh no error but we have not also got the output the reason is we are not calling coder so you'll be saying obj Dot code because as a alien as a programmer you have to code right so let's run this application now I hope it will work and it worked can you see that we got I am coding and that's it it's so simple so what we have done we are trying to use spring framework to get the object as you can see the object of alien is not created by you it's created by Spring framework I know for this example it doesn't make sense because to create one object we are doing so much of stuff right but think about the complex of application what if you building an application where you have maybe 15 20 classes and every class will have let say two three objects you don't have to manage those objects right you to manage by Spring framework but how do we do that that we see in the upcoming videos so till this point we have seen how to create a simple spring project where you can introduce a concept of Bin Factory which will give you the object right it works but still we have an issue here the issue is you can see the code we are getting uh it says deprecated right so this CL use that now this class implements application context example if I click on click here you can see it's a class which extends abstract XML application context if I click on that it says extends abstract refreshable config application context if I click on that somewhere if you go up and up and up you will find everything extends from application context are we getting there today yes we got it so you can see we have the whole hierarchy there and unfortunately by doing that I've opened so many classes here let me just close them quickly okay let's get back here so instead of using bin Factory now we are using application context the amazing thing is here itself you can mention spring.xml you don't have to mention new file system resource or something like that you can simply mention the thing and you can see we are not getting any dicated now because we will be using application context a lot so now let's right click and say run as Java application we have not changed much we have only changed from bin Factory to appliation context let's click on run and we got an error it says file not found exception that's Weir we have that file here right the problem is since you have mentioned class path right it will look for this file in class path so you have to move this file in a class path in a Java folder of course let's keep it there now and let's relaunch the application and now it worked we got I'm coding that's great so that's how we use application context in your spring [Music] project in the last video we have seen how do we use application context but but still we don't know how everything is working behind the scene that's important right so let's understand what happens when you say application context and then when you say alien get bean to understand that what I will do is I will go back to my alien and let's create a Constructor here so let's say a Constructor so you can see we have a Constructor here a simple Constructor which simply says alien object Creator nothing fancy we got a simple Constructor there what I want to understand is if I run this application of course it will call the Constructor first it will say a ion object Creator and it says I'm coding that's great that means spring framework will create the object and of course when you create the object Constructor will be called but my question is what happens when you comment this because now if when I'm commenting this line that means I'm not asking for alien object so the question is will Spring framework create that object for me even if I'm not using it let's run this the moment run this code you can see we are not getting I'm coding because we are not calling code but still your spring framework is creating the alen object for you it's because spring believes even if you don't want the object it is my job to create the object but how spring knows that we will be needing alien object later it's because when you run this application the first line of execution is main method right so it will come to main it will check hey the next line is we have to create object of application context Now application context will simply create a spring container for you now what is a spring container all the objects or all the process which you do in Java happens on jvm so inside your jvm we'll be having a special container which will which we will call as spring container which will have spring beans oh what is Bean now any class which has certain variables and every variable will have gets and Setters is normally referred as Bean now in this class we don't have any variables and that's why we don't have any methods but then this is also a bean uh so any you can imagine any class as a bean as of now so we want spring to create the object of all these classes but how spring will know that because you can see we have specified a file name here which is spring.xml so your spring container will check this file and it will say okay so it's my responsibility okay so I'm spring container here it's my responsibility to create the object of alien because as a developer you have mentioned that here what if you mention some more objects if you create object of object class if you create object of some other classes if you write five means here a five being tag it will create five objects for you that's what spring is doing even if you don't need the object so if I create object of some V class which I don't even know in JRE it will create the object for you okay just have to mention that there so the moment you run this line it will create the object for you in the spring container it is your responsibility now or it is your decision now to use that object or not in this case I'm not using it but still we got the object so that what is happening behind the scene the moment you say get being alien it will go to Spring container and say hey Spring container I want the object which whose ID is alien and spring container will say okay we have the object here just take it and that's your that's the object you're getting and that's why it is working but then with this we have some more questions okay example let's say what if you have certain variables here example let's say uh let me create a simple variable here or maybe I will not be using a private variable I will be using a simple variable int uh I will be naming this variable as age of course you can go with anything every alien will have a different age right okay it can be same but normally it is different so let's say if I have a age there and I want to print the age of a particular alien so I will say obj do AG so when you print age what do you think what value you will get so of course you can specify the age by default you can specify the age here you can say by default the age is 18 whatever age you want to specify maybe 12 otherwise for every object for this obj time I will say this is obj 1 just let just check what is happening so I will say obj 1 and then I'm printing the age and you can see I have not mentioned the age and by default the age will be zero so let's run this code and you can see the age is by default zero that's great so before printing age I want to specify the age as let's say 15 that's the age which I'm mentioning here let's run this code and you can see we got 15 that's great but what if if I create one more object or maybe if I want to create one more reference let me just copy paste the code as it is let's past it here and this time I will say this is obj 2 not one uh let's go back here I want to call code by obj 2 I want to specify the age I don't want to specify the age for two let's keep it blank and now let's say this is obj 2. AG now what do you think will be the age of obj 1 of course 15 right we have specified that but what will be the age of obj 2 that's the question let's run this application and you can see both the object has the same age so either we got two object with the same age or we got only one object and both the reference are pointing to the same object that's what is happening here go back to the previous example when you say line number 18 when you say hey Spring framework I want an object whose ID is alien spring will say okay we have the object take it when you go for the second time as well on line number 24 you're saying hey Spring container I want the object with ID alien spring will say we have the object just take it it will not create a new object for you that means both the reference which is obj 1 and obj2 are referring to the same object and that's why we normally call all the spring beans as Singleton beans because it will the object will be created only once that's awesome right but what if you want different object what if you don't want to follow single T pattern what if you want to have a different object every time you call for the object and that's what we'll see in the next video [Music] in the last video we have created this alien object and then we were trying to create two objects but unfortunately we got only one object now is it a feature and the answer is yes by default spring follows a single turn design pattern which simply means by default spring container will give you one object but what if you want multiple objects and that's where spring gives you a choice and that is the scope let's try so let's go back here in spring.xml and if you see this Bean tag there is one more attribute here which is missing the moment you say control space you can see we have a attribute known as scope by default the scope is Singleton which simply means that spring container will give you only one instance of this class even if you ask for 10 times it will give you only one instance example if I go back to app and let me create obj 3 obj 4 still you will get one object because by default it's a single ton what if you want multiple objects and that's where we have to use a different scope so apart from singl ton we have one more scope to use and that is prototype now what it means prototype simply means that whenever you ask for a bean spring container will give you or it will create an object at that point example when you say get bean once with alien spring container will give you one object again when you say get bean with alien spring container will give you second object so if you ask for 10 times spring will give you 10 instance 10 different instance of that particular class let me prove my point so you can see I have made it Singleton now I've made it prototype now so if I go back to app let's run this application one so say right click and say run as Java application and look at the output now so you got 15 for the first object and you got zero for the second object because we have two different objects it's that simple right so Singleton is you will get only one object protype you will get multiple objects based on the number of time you're requesting for it but what if you're not requesting even ones you can see I'm creating the object of application context which which is your spring container but I'm not asking for any object do you think it will create object for you let's try the moment you run this code you can see it's not saying object created even once that means in single T even if you don't ask for the object spring container will give you that object but in case of prototype you have to ask for it otherwise it will not create the object right now which one to use it depend upon your situation right so if you think there's an object you need only one object of course you will go for Singleton apart from that if you think you know we will be using multiple objects so you have to go for Prototype so that's the scope in spring so we have single turn we have prototype we have some other scope as well but that's that's not applicable in Spring code that's for some other modules so the the last video we have seen the scope in spring right we have Singleton and we have prototype now let's go back to our alien and you can see we have one variable here which is age now logically this age variable should be private variable right that's what we do we have to make sure that all your variables are private in order to access these variables what we need is Getters and Setters right and we don't have it here let's go back and let's create gets and sets for this so let's right click here say Source generate getter and Setters select this click on generate and you can see we got Getters and Setters if I go back to my app now and you can see I don't want to assign the values now of course you cannot do you cannot even use the age variable directly you have to use set age and get age but time in I will not do anything and let me also remove this second object we don't need that in fact one more change I don't want to use prototype I want to use single ton and the amazing thing is when you don't mention any type it is by default single ton let's go back to app we don't even need this age the thing is oh we need age but then it should print using get age right so I will say obj1 do getet age the thing is I want to assign a default age because if you don't if you don't Define the default age by default it will be zero I don't want zero at least the age should be 12 or maybe 10 how do I mention that 10 here of course there's one way you can just go here and say 10 right by default the age value will be 10 10 that's one option otherwise you can just go to app when you create the object you can assign it manually you can pass it to the Constructor what if you want to assign the value in the spring Bean itself because remember the object will be created by Spring framework so the Constructor will be called by Spring framework right so I want to assign the value for age here in this XML file can we do that let's try so let me just expand this bin now in this bin tag we can have one more tag here which is named as property now what is property so every variable in your class if your class is be every variable will be known as property you can have multiple properties so that's the property here you have to mention the name of your property in this case it is age so we have name and we have to also specify value so to specify the name and the value if your property is a primitive value or primitive type you can assign value but what if your property is of type reference that we see later but time let's only focus on value I want to assign the value as 10 by default the age should be 10 that's what we want here but will this work let's try go back to app and you can see I have not assigned the value for age anywhere else apart from that your XML file let's run this code and you can see we got 10 so what happens here the moment you run this application on this line you will get that particular container in your jvm in that container it will check hey we have a springl file where you have mentioned mentioned about alien object so it is my job to create that alien object the moment you create the object it will also call the set age method you know why because we are saying property since you are specifying property it will call set age oh okay I know you don't trust me here let's go back to alien and oh we don't have a Constructor as well oh we have a Constructor here so let me just put this construct on top so that it will be visible right I also want to go to set age and by saying here age assign that's it just to see if that set meth is getting called let's go back to app and now let's relaunch the application and you can see it says alien object Creator that's step one second step is age assigned so first it will create the object second it will set the age because we are saying property right just to prove my point once more what I will do is I will go back here and let me just change the method name from set age to set age one now what will happen is if I do that if I go back to app and let's relaunch the application you can see we got an error it's compulsory to have your method name suitable for your variable so if your variable name is age it should be age right it should not be one that's what it's it's saying here you can see it says Ed creating Bean with name Alien because if you go you know if you go there it says invalid property age because we don't have it there the property we are using here is set age one right make sure that you just set age yeah if you want to keep it set age one in XML as well you have to say age one then it will work right okay so let's go back and you can see if I say age it will it is giving me error so let's go back to our code and it set age so that that's why this set methods and get methods are so important in spring so that's about your how do you assign a default value but what if you have a reference type now when you say Age It's A Primitive right say int but what if a alien needs a laptop now of course laptop will be a class then the laptop will have certain methods and then we have to create the reference of laptop in this alien okay when you say reference how will you assign the particular value to it because we cannot use value there that will see in the next video so now it's time to have a reference type here because what we have done in Alien is we have a variable which is of type A but what if you have a reference type because what it will affect is this spring.xml file because here you can see we have a property which is age and then we are giving a value with an attribute which is value itself now to explain that what I will do here is I will create a simple class let's say because every area needs a laptop to code right so let me create a laptop class here a very simple class laptop and in this laptop class let's say we have a very simple method which is compile so we got a laptop class and then the method name is compile here and then in this compile you will simply print code compiled that's it so we got a compil method and then we are printing code compiled and in this alien of course if you want to use laptop you need to do that in Alien right so from Alien you will say now alien has a laptop type in it so we'll say laptop laptop and then uh we will need a getter setup for this as well so let's come back here and say right cck Source generate gets and sets but this time it will be for laptop because we already have it for age right now this thing is very simple right we have not done anything fancy just just the normal stuff and then from the code you will use a laptop object and you will say compile of course right when you code something you have to compile as well these things are simple even if you run this code let's see what happens the moment you run this code you will get an error and that is nullpo exception but why we got nullo exception is quite simple right because we don't have the object of alien anywhere because we are saying we are using dependency injection where the dependencies will be added by Spring framework so of course if you want to work with a laptop you need to create a bean for that as well so that's a golden rule if you have a class just create a bean for that so let's get a simple bean and this will be for laptop so I will say ID laptop and then we have a class and of course we know the class name now it's a laptop right and then this laptop don't have any variable so we don't have to specify any property in this case but the question is how this alien will use this laptop object or the ID so of course in the alien we have a property which is the of type laptop but what you will give the value there because if you say value you have to assign A Primitive value or maybe a string type now that will work for Primitives and string but if you have a reference object you can't use a value here because now you want to inform spring hey this alien object needs a laptop object in that case you will you cannot use a value you have to use a reference so you have to say ref and you have to say laptop so that's how you can refer it right so now let's verify if this working let's go back to app let's relaunch the application and you can see it is working so basically if you want to use a reference type the way we are doing here in this alien class we have a object of laptop if you want to achieve that you have in your spring XML file we have to mention that your property which is laptop is referring to the ID which is laptop here right now you can have multiple dependencies you maybe if you have your class aliens depend upon some other object maybe let's say a pen object so in that case you have to create be for pen as well provided you have a pen class right so that's how you implement this reference stuff in spring so till this point we have seen how can we create a bean and then how can you set the properties now when I say property it is simply a variable which you can access with the help of Setters and Getters now here you can see we have assigned two properties one is a primitive property and second one is a reference property okay but what if if I go back to my alien you know one of the way you can assign the value to a particular variable is with the help of Constructor of course right there have two ways you can assign the values one with the help of Setters and second with the help of Constructor so what if if I want to assign the value to H but with the help of Constructor so let me create a Constructor here with variable of course so here I will simply right click and I will say Source generate a Constructor with Fields I only want to assign ages at this point let me click on generate of course you can also create a Constructor which takes laptop or you can create a Constructor which takes both but as of now let's stick to one particular Constructor so you can see we got age here we don't want this super method there just to reduce the number of lines we have in the code so you can see I'm saying this. AG is equal to age but how can you pass this value because if you're not passing this value and by running this code let me just run this code of course it will take the value from the setup so in this case we are passing the value from setup but what if you don't do this so let me remove the property of course it will not be calling set age now let me restart the application you can see we got the value of age as zero okay so I want to pass the value the way you can pass a value here is with the help of tag which is Constructor argument but then what will happen so even before doing that let me just remove this default Constructor here you can you see that we have a default Constructor as well so time being I will simply comment it so I will say comment save and let me just relaunch the application now this time you can see we got an exception but why so it says no such method exception so we are not able to find alien in it okay but why we are not okay so let's try this it says no default Constructor found oh since we are creating this Bean we are getting the object of alien and by default it will call the default Constructor right and as you can see in Alien we don't have a default Constructor The Constructor which we have is a parameterized one so in order to achieve in order to run this code of of course you need a default Constructor here otherwise you can mention here hey don't use the default Constructor use the parameter wise one and the way you can pass the value is by saying construct the argument in which you can pass the value so how do you pass the value it's very simple actually you can simply say value equal to and you can assign any value here so let me say 12 of course you can use Setter or you can use Constructor let me just run this code now let me relaunch the application can you see that we got 12 now now this time we are not calling default Constructor we are calling a parameterized Constructor by passing this value but what if you want to assign for laptop as well what if you have a Constructor which takes laptop in that case you can specify one more Constructor argument here and that will work the only consideration is instead of having this value here we have to use ref that's how inject the value with the help of Constructor so basically we have two concepts one is a set injection and second one is a Constructor injection so seter injection simply means when we use property this is a set injection and when you say Constructor that is a Constructor injection now which one you should be using now whenever you have a variable example when you go back to this alien whenever you have these properties whichever property is compulsory to be assigned or which is required you can go for Constructor but if you think you have a property which is optional you don't want to assign the values to it if you want to keep it optional you can go for Setter don't use Constructor when your variable assign assignment is optional because Constructor will have will give you error in this case if you don't assign the value for the age if you don't pass this Constructor age it will give you error right so that's how it works that's how we work with Constructor and Setter injection so before continuing let me just do some changes the example I don't want to use construct injection anymore let me go back to my property and for that we also need a Constructor here and then in the spring XML file of course I want to go back to the properties I will say undo undo undo and we got it okay now what other things we need example let's say if I go back to alien every alien needs a laptop so let's say if you're a programmer and if you want to a code you need a machine where you can write a code and you should be able to compile it now don't you think to write a code you don't need a laptop it can be desktop as well it can be tablet as well so why you are saying a alien needs a laptop it can be a computer example let create an interface here and we'll say interface so we have can be laptop so I will say computer here and then here I will say com okay of course for those changes we have to change something here as well so I will say right click Source generate Getters and sets for this particular variable com done okay and one more change instead of having laptop here we have to say comp that's it nothing much is all about computer now so if I go back to spring.xml what do you think will it work of course not right here we have to mention the property name as Comm so I will say Comm now okay now will this work let's go back to our application and let's relaunch it so the moment you relaunch it you can see it is working and okay just want to do one more change it says code compiled in a laptop and in desktop I will say code compiled in desktop okay so you can see we have one computer interface and two classes let's go back here and let's relaunch it and you can see it says code compiled in a laptop that's great so how it is connecting so it is connecting based on the property the property is comp and the reference is lap top and we got it now what do you think about desktops of course I will talk about desktop in some time but as of now it is working because in the bean tag we have specified laptop laptop since we have laptop here it will work with laptop if you want to use desktop of course you can create a bean for desktop and mention desktop here so I will copy this and this will be desktop and here as well the class would be desktop whatever you mention here in the reference if you just laptop it will create object of laptop you can see that it's working for laptop and and the moment you set desktop here now it will work for desktop and you can see we got desktop so code compile in desktop okay so I want to show you a magic here the magic is what if the ID which we are using here for laptop is also comp so let's say I'm not specifying as a laptop I'm saying it's a Comm and here as well I want to use laptop so I will say Comm can we have same name of course we can have right we have seen that before so you can see we have Comm here and we have Comm here Let's ignore desktop as for time imagine we don't have any desktop stuff we have a laptop here which whose ID is com and we have Comm here the moment you run this code you can see it is still working because of course you can have a same name but don't you think when your name and your reference is of same name why to mention that the amazing thing is you can skip it you can just remove this part and you can say hey doesn't matter what the name is I still want to get it connected but how so I want to inform spring hey spring when you create object of alien search for the laptop object or search for the computer object you will find it but how so what you will do is in the bean property that you can mention one important aspect which is so let's go back here and say autowire so can you see that we have a concept of autowire and what is autowire now autowire simply means you are informing spring framework hey Spring if you want to set some property if you're looking for some property if the bean is available connect automatically only when okay only when when the bean is available then only connect so you can see it says Auto wire it is searching for the object now so here I will say in fact we have Choice when you say this so if I say control space you can see we have by name or by type so we have two choice so it will try to search a particular element based on the name example here you can see the property name is com so the property name is com and the bean name is also com so it will directly connected with the help of Auto Wire by name let me just relaunch it and you can see it is still working so I have not specified any property here but still it is working and that's the beauty of Auto wire trust me auto wire is amazing the moment we start working on Spring boot uh when we start working on GP and other stuff you will love Auto wire okay that works so let me show you one more now let's say I don't have this particular Bean here so I will just comment it right so it is commented now and now you can see we have desktop here so as a alien you need a computer and yes we do have a desktop here so let me just real launched the application and we got an error the error is because we have not specified the property but you will say hey we have specified Auto wire right in case of Auto wi you don't have to mention the property it will search for a particular be automatically but unfortunately it was by name by name simply means it will search by name oh that how do we Sol it now so what we will do is instead of going for by name we have another way of achieving this concept now think about this when you say you want the object of of alien and alien is depend on what so let's go back to alien and here you can see alien is actually dependent on the computer type so we are not dependent on the name actually we are dependent on the type as well so what if you can go back to Spring XML and you can say hey when you say I want to get object of alien alien is dependent on the object of computer so what if you can just don't search by name search by type so we can do that as well so here we can say hey search by by type now here it will not look for the name example uh if you go back to alien it will not look for Comm it will look for the object of computer and in this case you can see we have it we have it here so we have the object of desktop with the name desk of course this desktop name is not important what is important is this desktop class implements computer that will be done let's try this so let me just save this code and let's relaunch the application can you see that we got the output and the output says code compiled in a desktop that's what we want right we wanted it to work and this time I don't want it to search by name I want it to search by type so let me repeat we have two options here autowire can go by name or by type so when you say by name it will try to match the name of the ID or name of the bean when you say by type it will try to find the type of The Bean that works but then what if you have this object as well so let's imagine we got two objects one is laptop and one is desktop and when you say by type see of course when you say by name the problem is solved right of course it will search by name but when you say by type don't you think it will get confused now we have laptop and desktop both so what do you think which one will get preference of course this should get first preference right because this that's a sequence this is the first one the second one but then in terms of spring everything is object right so when you say everything is object of course it should be independent of the sequence and that's where it will get confused the moment you run this code you can see it is giving you eror it says no qualifying Bean of type blah blah blah it says I was expecting one but then we found two so your spring framework is getting confused which one to assign is it laptop or desktop how do you solve this that will see in the next video how do we solve this error now you can see we have two candidates here we got laptop object we got desktop object object and both are computers and then we are saying hey go by type so that's where your spring is getting confused hey should I go for a laptop or desktop now sequence will not be considered now one way to solve this is by going here and going back to the property the moment you have a property here the way we did it before so if you say reference to desktop now this will solve your issue if you want to go with desktop you can see the you mention it will not consider by type or by name it will simply go with the property in fact it will avoid Auto wire so if you run this code you can see problem solved we got desktop and then if you say comp now this will be giving preference to comp right so problem will be solved so when I say comp it will give preference to laptop so this is one way but what if you don't want to mention this and still you want to go for autowire by type yes so we have by name we can go with that or we can mention the property otherwise by type if you want to solve this see even if you have both this objects you have have to give preference to at least one and the way you can do that is by using a primary attribute so you can say primary equal to true the moment you do that this particular Bean becomes a primary Bean so in case of confusion it will give preference to this one so example if I on this code now you can see it is giving preference to a laptop so even if you don't have anything assigned here even if you don't have by name it will go for by type and it will prefer this particular Bean you can see we got laptop now if you write primary equal to true in desktop it will be getting the first preference right so let's run this code again and you can see we got desktop so that's how you can solve this particular confusion so this is where you can use this Primary in fact the moment you start using annotations so we also have a concept there again we'll use annotation there so that's about this code we have solved the issue here welcome back aliens my name is EV ready and in this video we'll talk about spring boot jdbc the thing is if you want to connect your application with database in Java you have to use something called jdbc which stands for Java database connectivity and you will say we know that right so basically you can use JBC few steps and then you can connect your application with database easily but the problem is as a developer you want to reduce your work right and also you have to make sure that the application is optimized so let's talk about the optimization part here see the thing is every time you send a request from your application to database it creates a new request and then if you have multiple connections imagine you already have few connections and now you are submitting a new request now you do have this connections right can't we just reuse the same connection instead of getting a new one now that's something you have to manually do or you can use something called a data source here now data source will have a configuration and also it provides you something called a connection pooling so basically it will already have few connections and you can use those connections and again you can do this by using any external uh data source or connection pulling concept and you can make it work so that's one thing the second is about developers work so you have to write those beautiful seven or six steps to achieve jdbc and you can do that but the only problem is the more code you write you are basically increasing the number of lines which will have a bug so you know every new line is a potential bug right so reduce the number of lines which you write and use certain tools or Frameworks which will do it for you and these Frameworks and tools are been tested by everyone so if it works it works for you as well now that's where we have to use a template now there's something called jdbc template now jdbc template will help you with data source that means the connection pulling concept will be done and it will also help you to write your code in an efficient way and all this things are provided in Spring boot jdbc now why spring boot is because if you want to build Enterprise application and if you want to follow all the design patterns best practices and a framework where you can get the libraries and everything working together spring is one of the best framework and that's why we're going to use spring boot jdbc is because under the spring umbrella we have lot of projects and this is one of it so let's get started with the code where we're going to talk about how do you build an application and we'll learn by building something right so it will make much more sense so let's start the code the thing is if you want to build this project you need a i d e now you can choose any ID which you want maybe you can use Eclipse intellig vs code your choice I'm going to use intj idea and that to a community version because that's for free and it works so we'll be using that so to get the project for spring if you're using a spring tools on vs code or Eclipse you can directly get the project from there and if you're using the ultimate version of intell idea you can create the project directly from your IDE but since we are using a community version I'm going to use the spring in visor so you can head to the start. spring.io and you can get your project from here the base skeleton of the project so the first thing you will mention is a project type I'm going to go with mavin uh language I will choose Java of course that's what we're doing now and then the spring boot version I'm selecting 3.2 next you have to mention the group ID so I'm going to select I'm going to type here com. telescope the project since I'm working with spring jdbc demo that's the project name I will give and will not change anything here the packaging I want it to be type of jar and the version for Java I'm using in my machine is 21 now based on which version you have you can either just choose 21 or you can select 17 uh dependencies so basically we have to add uh two things only one is the jdbc API now this is your springboard jdbc and you have to add a data now which database we going to use of course you can choose any dbms which you want now we will be using a embedded one so if you have post mql Oracle mssql you can use that the only thing is you have to do some additional configuration and I will tell you where to do that configuration but we are going to use a inmemory database here and by default when you use inmemory like H2 it will be pre-configured so you don't have to do any configuration for that so this these are the two libraries we need and that's it you can click on on generate it will give you a project so as you can see I got the project downloaded which is spring jdbc demo I will just unzip it and open in any ID which you want I'm going to use intj so let me do that quickly okay so basically have opened that project in my IDE and it will take some time for it to resolve the dependencies now we have added two which is jdbc and the H2 and we don't have to manually download the H2 driver because the moment you add H2 it will also have a driver installed so if I open the pom.xml you can see we do have H2 here and we got spring boot starter jdpc this is what we want and now we can get the work started so we will go back here to main Java and spring jdbc demo application and we can start the coding here okay so one thing I mentioned before right if you want to use po or Mas or any other dbms veryable to the configuration so if you can see we have a resources folder here and if you open application. properties this is where you have to specify those properties so what properties I'm talking about you have to specify the URL you have to specify the username and password and Driver now basically this variable belongs to spring. dat source. URL and you can set the value for the URL whichever you want so that's how you do it but now we are not going to use M or pus we are going to use H2 which is in memory reason I'm doing using H2 here is because you can directly take this project and run on your machine if I use poqu or MySQL then you have to also install those softwares you have to configure it and there will be some issues with that so let's go stick to H2 okay so what we want to do we want to basically store some data in the database and fetch it from database so we do have database ready so the moment you run this application you will be having edge2 database behind the scene running for you but then what you want to store so in that case what I will do is I will create a simple class which will represent a particular table so normally you can have a relation between a table and your class now you will say how is is it possible see the thing is if you think about a table table will have a table name table will have a columns let's say we are working with aliens so every programmer is alien right so a programmer or alien need an ID a alien needs a name and a alien needs a favorite technology so let's say for me my favorite technology is Java blockchain AI for you it might be something different so basically every alien will need a technology as well so three columns in the table and then this columns or this table will also have some data let's say one1 which is ID and naven Technology Java let's say two Gan technology blockchain so basically you can have this data in the same way if if you try to relate this with a class now in a class basically what you'll have is you will have a class of course a class will have a name a class will have properties right variables now now if you try to match the class name and the table name look similar properties of this class which is ID name and Tech matches with the column names right and the objects for this alien class will be the rows each object will be one row now that's how basically you map between the class and the object and then if you're using any spring orm technology let's say hibernate in that case the table creation happens automatically now since we're not using hybernate here we have to create the table by ourself yeah that's something you have to do okay let's create a class here and I want to create a class for the let's say alien okay so when I say alien class what I want to do is I want to basically create few variables so I will say private in ID and then private string name and private string Tech so basically I need these three variables and for these three variables I also need gets and Setters so we will right click here and we say generate Getters and Setters I will just choose the three variables done I also need a two string method if I want to print the data itself so I will just right click here say generate uh two string method and done I will do one more thing I will move this particular class the alien class to a different package so this should be into a package of model so every class should belong to their own packages so alien is a model class I will just move that to a model package and since we don't have a model package I will say hey move to model package in this RC yes and you can see it is moved now so that's done but if I want my spring framework to manage this particular bean I have to use add component now if you're not sure how spring boot Works uh you you can check out the video link in the description I will make sure that you have your spring boot video there okay so basically what we are doing with ADD component is we are asking spring framework to handle this object creation object managing and object destroying by Spring framework I don't want to do that okay so basically when I say I don't have to create the object how spring will do it so basically what I will do is this run method so I will go back to the main class so main class is your spring jdbc demo application and in this the you can see we are calling a run method run Returns the object of application context so I will say application context context now why I'm doing this is because I want to get the hold on a spring container again I'm assuming that you know spring boot here because if I start teaching you spring boot then it will take it will make the video length of fir hours I don't want to do that so again you'll find the link in description you can check it out now let me create the object for alien so I will say alien I will say alien one is equal to okay we have to basically import the package for alien which is in the model package done and now I want to create the object the way you can create the object by saying alien right but I don't want to create this object by myself I just want this to be done by Spring framework so I will say context. get bean I want the object for alien. class right uh but the thing is by default the type of this alien component is Singleton so basically the ban is Singleton I want to make sure this is prototype so I will say scope prototype so what will happen is for different request it will create new objects for you otherwise you will have multiple alien object or multiple alien references with the same object so you will have the same data doesn't matter how many data you have in your table okay so that's done and for this alien what I will do is I will add some values so let's say the ID I want to add is 11 one and I want to say not get ID I want to say set ID alien do set name I want to set the name as let's say naven and alien do alien 1. set Tech I want to set the tech as let's say Java now basically I'm adding this values and now I got my object ready I want to store this object in database how will I do that maybe I can simply say save this data in database of course this will not work so what you have to do and you know why this will not work is because you have not subscribed the channel yet so if this works on your machine that means you have already subscribed okay I'm just kidding so this will not work right you have to write Java code there now how will you make this work now basically this is where you have to write extra classes see what happens is in normal application you'll be having multiple layers you'll be having a controller so if you're building a web application you'll be having a controller layer you'll be having a service layer you'll be having a repository layer now this repository layer is responsible to connect with the database of course I can write my JB steps here in the main because that's a normal main method I can write all the beautiful jdbc steps here but then we don't want to do that that's why we are doing springboard jdbc here and this is not a good idea to write your jdbc code inside main so this should be a part of a separate file now instead of creating a service layer I will be skipping that and I will directly jump into a reposit layer so I will just create here deposite layer so I will say new class and I will this I will say this is a alien repo or you can also say da if you want now repo is responsible or repository is responsible to connect with database okay now this is where you will do this stuff but first of all uh we need to have some methods here maybe I want to have a method like public vo save so it will save the object Which object the alien object alien alien into the database okay so we have to import the package I will also make sure that this belongs to a service uh the repo package move to repo yeah okay so here what I will do is I will say at this point I'm not writing all the codes I will say added and then I also need a method which will return the least of aliens so let's say if I want to fetch all the aliens from the database now in this case I have I need a method called maybe find all method name you can change as you want I'm just saying find all here import the package and here let me return a new at list of aliens okay and I'm I'm just wrting an empty object but we'll we'll Cote this in some time the idea is I want this to work first of all and since I want spring to manage it I will write add component so instead of writing add component what makes sense is to use at repository for different layers we have different stereotype annotations for repository use repository go back here now if you want to store this data in the database I need object of a repo I will say repo is equal to instead of saying new repo or new alien repo we will be using context. get bean the alien repo do class this is I want now once we got the object I can simply say repo dot I want to save the object let's say I want to save alien object alien one and maybe I want to print all the objects so get all the objects from the table or all this all the aliens from the table and print it here we have not done any changes yet and we have not written any code to connect database yet yet but let's see if this works right click here and say run okay taking some time because I'm running this for the first time and you can see it says uh empty why is not calling oh I said added I wanted to print added that's wrong with me okay so let's run this and now you know why it has not worked before why I said SL slash added is because I realized you don't like the videos for me anyway so you can see we got added here and and then we got the square brackets which is empty basically empty at least now let's make this work so basically we have to make sure that we write a code in the alien repo this is the only file you have to work with now we have to write a code where we can store this data in the database so how exactly we going to do that it's very simple basically we have to use a jdbc template now this is a part of your spring boot jdbc so what you to do is you have to get hold on say something called jdbc template you just have to create the object of this I will say this is template okay so you have to get hold on the jdbc template for this this is a property right and this is the inbuilt stuff if you can see jdbc template belongs to springf framework. jdbc Doc okay and and you will see how easy it is to connect and all the all the stuff right you don't have to write connection con driver manager remember in jdbc we do that and we don't have to prepare this statement all this thing will be happening behind the scene but for this I need data sets I can just right click here say generate data sets for this JBC template that's done and I want this object to be injected so because see we are just creating a reference of it right where is the object so spring will take care of it you don't have to worry because if you go to JBC template we can create object of this class but again for that you have to do all the configuration you can see we have to pass the object object of data source I don't want to do that I just want my spring framework to manage it and the way you can do that is by just writing autowire your job is done you got the jdbc template object Now using that object let's say I want to I want to add this I want to add theen object it's very simple now you can simply use the template now this template which is a jdbc template object you have multiple methods now in normal jdbc if you want to execute a statement now based on which statement it is is it a select query or is it other queries like delete update now if you're working with select query which is the reading you basically use execute query as a method if you want to change something let's say update insert delete you use execute update here we want to save it right so we'll be using execute update execute update takes two parameters in fact multiple parameters depend upon how you're doing it the first one is the query of course right you need a query with you so I say string St Str or string SQL is equal to what is a query now here I'm going to use a query like insert into what is my table name nowhere we have mentioned a table name right let me make it similar to the class name let's say alien and then how many fields I have so I have let's say ID I got name and I got Tech okay and then I want to specify values as well now where are the values so you tell me where are the values values are here inside this alien object you got the values we just have to pass this values here now if you remember the prepared statement we don't directly pass the values what we do is we put question marks there there so that we can replace this question mark later so we are using repair statement by default just pass the query here or pass the SQL here after passing the SQL you have to also replace this question marks with the actual values so those are the remaining parameters you have so after mentioning the query just say alien. get ID comma alien. getet name comma alien. getet Tech that's it this will'll basically save it but will it work that's a question right so let's see what happens if you run this and we'll try to solve that later right click here and say run I'm expecting error but let's see what errors you get okay we got the error and the error is very simple it says the table alien not found of course right H2 will give you database not the table table is something you have to create by yourself now how will I create the table and that's where we have to add a schema in the resource folder so you have to add two things actually one is schema for the structure and what if you want some default values maybe you know every time you create a table you want certain values there to test it for that you have to create a file called data tosql and we'll do that in this so let's make it work so what we are missing here is we are missing this table and we have I've told solution we have to create a schema file because hed2 when it loads we don't have a table there right now to create that file it's very simple you can right click on resources and say new and create a file called schema.sql and say enter now this is where you will write your script now the script to create your table so the script for creating a table or the SQL query is create table and you'll give the table name as alien and then you have to mention the properties or the the the columns so the column which I want here is the first one is ID now the ID will be of type uh int and we'll say this is a primary key uh then you'll be having a name name will be of type Ward and I will say the Vare is 20 in fact you know maybe we have done this before so normally in 20 characters you will name will be there but then if you talk about South Indian names I'm a South Indian so I have a very big name now nain is not my full name my full name is tumar B I know that's huge so maybe I will need 50 characters and then here I will say marks and again marks will be of type in and here we'll be having Tech and Tech will be of again of my Ware and I will say Ware 20 because Technologies can be shorten up if you even if you say artificial intelligence it is AI so let's keep it there so basically this is the file for the schema now we also need a file for the data again not compulsory but then I want to have some data so I will have some insert statement here so I will say insert into alien and I want to insert three columns ID name and Tech and what are the values I'm going to have here so I will say value is one1 then name in single code let's say kid and let's say technology is blockchain that's one and maybe I want two more so I will just copy and paste it here so this is one2 and this is hsh and let technology is AI 103 and this is social let's say iot okay so basically we got this uh three rows already created and now if you run this code so we have created two files schema and data SQL I will just go back here to my main application and run this and let's see what happens now so when you run this let's see in the taking some time again okay so you can see there's no problem there's no error and we got uh m here so so the reason it got empty is because if you see the repo we are still wring the empty data so we are not rning data from the table okay so the data is created how do I know that data is created what I will do is uh you know this update returns you the number of rows affected so you can see we got int so what I will do here is I will say rows and I want to see how many rows are affected and then I will print the number of rows affected so I will say rows plus space rows affected okay so it will print how many rows got affected now if I run this code of course we'll still return the empty at list but at least I want to see is it getting stored in the database that's important so it says one row or rows affected here that's good so that means it is we are able to insert but now I want to fetch it how do I fetch it now to fetch the data again we can use our jdb template because if you see the template dot there's one more method here which is called query now query returns you the data from table right so I will say query but what is my query so I will say string SQL basically I want to fetch all the records I will say select star from Alien so what it will do is it will return all the rows and then okay let's see what happens so if I say SQL it will return me the rows right but in which format now basically the query you can see we have multiple methods of query to fetch the data so we can use string which is the SQL and then you can use result set extractor or you can say row call back Handler or you can use we can use R mapper for our example R mapper makes sense now what R mapper will do is I I will show you one more time so we have to basically pass the object of row mapper so what happens is when you get all the data in your result set you want to fetch one row at a time right so row mapper will help you to give you one row at a time to do that we have to create the object of R mapper so let's create one so I will say row mapper and R mapper will be working with a type alien let's say this is mapper equal to now how do we create object of row mapper so we can say new row mapper but the problem is if you go to row mapper it is a interface this is a functional interface which has only one method called map row and which takes two parameters one is a result set now this result set will be given by your query and then it also gives you a row number so in result set you might be having multiple rows right and you want to work with one row at a time so this row number will take care of it okay so basically I have to create object of row mapper which has a method called map row and then I have two things here which is result set and row number now basically this is the interface right so we have to create a Anonymous in a class to do that or we can also create a separate class which implements row mapper then create the object of it again that's your choice CH but here I'm using Anonymous in a class and slowly we'll convert that into Lambda as well so that it will be short of code but let's say we have this object here which is mapper and this is our Anonymous in our class so basically uh we have to do something here so what we have to do is this result set will have all the records right we have to take one record at a time so what map row does is it takes the entire result set and it executes map row multiple times depend upon how many rows you have so if you have four rows then the map row will execute four times and it will give you one object at a time so basically we'll take that we'll create object of alen here I will say alien a is equal to new alien and then this alien object is empty right as of now and I return the same object so you can see R mapper needs object of alien so I'm getting the object here and then I'm returning it but then this is an empty object right so it will not have values the values are there in result set so I can simply say a DOT set ID and now get the value from result set so you can say get int so how do you get data from result set you have to say get int and pass the column number so either you can say column number or you can say column name both work so here you can also mention ID a do set name this will be coming from the second row so get string I will say two here and then a do set Tech rs. get string 3 as I mentioned before you can also use a number or you can use a name of the column and that's it once you got the data from result set you are giving it to the aan object and then you are returning it so basically R mapper will take care of the data one by one now the same object which is of mapper you have to pass it here so you're not just passing a query you are also specifying what to do with the result set so if you click on query it returns you the object of list of aliens so the type is alien so we have to accept the list of alien I will say aliens is equal to this so basically this query will return the list of aliens and that's what you are going to return not the new at least so what we did is we fired the query we got the data it's just that we have to map it we have to convert that data from result set into the list of aliens and that's what we have done with the of mapper and that's it I hope this will work let's see let me remove some extra spaces there there so with this if I run this code and again I hope it will work let's run okay can you see that it worked so basically alien 101 name Kiran technology blockchain 102 hsh AI 103 Su iot and then the new entry which we have added 111 naven and Java this is what we have added right so basically these things are working out that's how you work with the spring boot with your jdbc and yeah that's it in fact you can do some more changes here if you want uh since this is a function interface we can use Lambda expression here so we can remove from here to here cut we can also remove one cly brackets here and you have you just have to put the arrow now let's remove the SQL exception let's put the arrow and then in Lambda we don't even need the type so we can remove both the types here and you know one more thing the row number is not not getting used right so we can even use R there or any other variable if you want let's say a again it will not make it readable right because a is already there let's say row number or we can say row okay so this is the shorter code now you can also replace this mapper with this code again this is optional if you want to do it you can try to use Lambda not compulsory so we don't need to create the reference here we can directly use this right so that's how we can do it in fact you can also remove this part you can directly write this particular statement in return even that works so so if I run this code once again so you can see it works so yeah that's your spring Boot jdbc and if you observed everything is happening with the help of JC template so you don't have to basically do all this configuration by yourself writing the connection statement creating the statement everything will be taken care by the template you just have to specify the query and file the query welcome back everyone in this module we'll talk about spring MVC now when you create a spring MVC project there are two ways of doing it so in the earlier days we were having only one way which was spring MVC configuration but now we also got spring boot so the idea of spring boot is just to make thing easy okay so in Spring MVC as well we can achieve all the thing but just that we have to do a lot of configuration and to solve that we have spring boot okay but what is spring MVC to understand spring MVC we have to understand what is MVC and to understand MVC we have to understand why we have MVC so let's go back to the old days when you talk about normal servet and JSP or let's talk about serlet so whenever a client sends the request to the server it is server's responsibility to process the request and give it back to the client but as a client what a client will get so of course client need two things the first thing is client needs data of course right if you go to any website the first thing you need is data but you want that data in a well formatted way example when you go to Facebook on Facebook you see data plus you also see a proper layout of course everyone will get the same layout so if I go to Facebook and you go to Facebook we'll be seeing the same screen the same colors right what will change this data so your friend list different from my friend list your feeds will be different from my feeds what will remain common is design right so we thought as a s instead of creating everything because subet on the server side will create the layout and will create data yes you can do that but you it becomes clumsy we feel that you know you should be have a separate task or we we should be having separate things for separate task example we need someone who will accept the request we need someone who will give the design okay uh it's like a normal template so design will remain common what will change is data and to hold that data we want something so basically we want something which will accept the request we want something which will be viewed to the client and we want something where you will hold the data so we got a controller who will accept the request who will send the response we want a view technology and then we want a model so model basically will hold the data and that's where we got this concept of MVC model view controller so if you have model view controller it will almost work the same way as normal seret without using MVC but then the advantage would be we are separating the concerns okay we are separating the task so the controller will only accept the request in the earlier days we used to use salvet for controllers and GSP for View and normal poos or normal Java classes for model right it's it's that simple but we got some issues here the issues were you know whenever a client sends a request request goes to a controller right and we are assuming that you only have one controller but what if you have multiple controllers oh that's an issue so let's say in application we have user controller we have accounts controller we have laptop controller we have so many controllers right as a website as web service you will provide so many services how do we create a multiple controls very simple create multiple servlets one seret can handle only one request or one Serv will be known as a single action controller okay so one Serv can handle only one request so we wanted something where you can have one class or One controller can handle multiple requests that's one second we wanted something in front so we wanted something as a front controller what I'm saying is when you send a request instead of sending that request to the actual controller first you will send the request to the front controller now based on what request you are sending it will check and then it will send it to the actual controller then now you will think okay we have to create a controller plus we have to also create front controller don't worry the moment you start using framework like spring where you have spring MVC in Spring MVC you don't have to create front controller spring MVC will give it to you of course the normal controllers you have to build bu it uh so example normal controller is your user controller accounts controller laptop controller and this front controller will be your dispatch servet and dispar will be given by Spring framework so now what will happen is the moment you send a request request goes to dispar servlet now let's say you want all the users you want to fetch all the users of course this can be done using user controller but who will call user controller and that's why disp says hey don't worry I'm there so disp seret will send your request to the user controller but disp Serv is something which will given by Spring framework right so how would disp Serv knows that you want to call user controller and that's where you have to create a file where you will configure all this thing oh now you have to also create the file it's not that difficult it's damn easy to work with but then again based on what you're using if you're using normal spring MVC then you have to do some sort of configuration if you're using spring boot this configuration goes down we will see that but we have to configure it we have to config [Music] spring boot application I want to say spring stter project now this is where you have to mention group ID here we'll say Tesco and have the artifact ID as spring MVC Boot and of course the package name I will say teliscope do spring MVC boot click on next okay now what to select from here now since we are building a web application you have to say web now when you search for web you can see we have an option of web here you can select this one you only need this one if you are going for reactive web you can select this one which supports which uses web flux otherwise if you want to create a web service you can go for Jersey but as of now let's go with web and click on next we don't need anything else now we are not working with database or something let's click on next and that's it click on finish the moment you click on finish it will give you a project so once you got your spring boot project and you can see we got our project okay still getting downloaded you can see it takes some time to get it from the internet so let's wait for it okay so you can see we got our project here which is demo I guess I forgot to mention the name for the project we have assigned the artifact now to verify that we can go to pom.xml and here you can see we have the artifact ID as spring MVC boot okay that's great we forgot to change the name from demo to Spring boot MVC but that's fine you can see we got one dependency which is very important here which is spring boot starter web the moment you add this dependency in your project this is your springing MVC now yes with the help of spring boot now first of all why do we create a web application of course there will be a client and client will send you a request now based on the request you will process the request you will uh generate some data and you will send it back that's what we do right okay but in this scenario since we have not created anything which will accept the request normally we call them as controllers uh so we have a concept of MVC where we have model view controller now it is a responsibility of the controller to accept the request from the client and then it will do some operation it will give the data back to the user normally we save that data in the model object and then we have a view which is actually your the layout which you want to send to the client now that view can be JSP it can be other template as well at the start at least we'll work only on JSP and then we'll can see later okay so we got this demo here now what I want to do is I just want to create a controller which will accept the request let's say I want to call a homepage and I want to return a JSP now first of all we don't have a JSP you can see that I have not created any JSP here so let's open this SRC and here in this main I want one more folder so normally if you want to keep your JSC Pages you have to keep that in a web app folder so right click here we'll send new I want to create a folder now and we'll name this folder as web app okay now once you got your folder we want to create a JSP page let me do that quickly so I will right click here I will send new I want to create this uh I want to create a JSP ajs file click on next okay now let's name this I will simply call it as index.jsp that what makes much more sense right and you can see in the web app we got our JSP in this JSP I will say welcome to telescope that's it I just want to say welcome to telesco and so whenever I call a homepage I want to just display this page but will this work okay let's we'll try that later now there's one thing you have to observe the first thing is uh we got JSP that's awesome that we have to put that in web app normally when you create a web application you always add the Tomcat server in your Eclipse right so in fact this is STS but you can imagine that is it is your eclipse and you can see in the server tab you can see we don't have any server here oh that means we have to also do this setup okay we'll see that later but that means if you want to this application we need a Tom getet otherwise will give you error what if I say you can simply run this application and it will not even ask you for the Tom Cat let's try so when you right click on this application and when you say run as the only thing is you have to run it as a spring boot app not on server because if you say run on server it will ask you for the external server we don't have it let's click on Spring boot app the moment you do that you can see it is doing something it will take some time so you can see if I jump to console it says spring boot app started it is doing some processing now okay now the amazing thing is it says Tomcat started on port number 8080 that's weird because we don't have any Tomcat server here right from where you're getting it the amazing thing is if you expand this me dependencies here spring boot application has a embedded Tomcat so you can see we have embedded Tomcat here the advantage is the moment you move your application to some other server some other machine you don't have to configure your own tomcat and that's the the benefit here so in the application itself you have your tomat server so whenever you car your application you are also getting toat server that's great right okay so that's something amazing but how do we access this now of course it says you have to use the port number which is 8080 so what I will do is I will open my browser so that's my browser I will simply search for local host colon 880 and the you say enter you can see we got something okay we got this error that's fine at least we are not getting that weird full page 404 error we got this 44 but but then this is getting returned by the spring MVC framework it says there's no error page because we have not built it yet uh it shows the timing and there's no message that's it at least something is working we have not got the output it is not calling index.jsp but why it is not calling index.jsp of course right when you send a request the request goes to the controller that's how spring MVC work so the first request goes to dispatcher seret and from dispatch Ser it will call a particular controller we don't have a controller yet how do we do that that we'll see in the next video so let's solve that issue the issue was we don't have a controller so let's create one now of course right when you say a controller we are working on Spring framework it should be a Java file let's do that so let's go back here and here I want to create a controller oh how do we do that let's right click here and say new I want to create Java class let's select that and this will be of course you can name anything right from from ABCD to some logical name but here I will say this is my home controller so logically when you say you have an application your application will have multiple Services let's say you have a Accounting Service uh let's say you have a user service you have uh let's say some some other service so when you say you have a user service and you have certain request which a server will handle what makes most sense is to have a controller named as user controller if you're working for accounting so it should be accounting controller now since we are working with homepage let's say home controller and that's it nothing to specify much here and click on finish and that's your controller okay now you will say hey this will not work how can you just simply create a controller and you're getting the I mean the normal class as a controller kind of you don't have to extend or Implement any interface here the only thing you have to add to this controller is a annotation which is ADD controller this is what makes it as a controller now as we discussed your request will first go to dispatch a serlet and dispatch a serlet will send it to a particular controller okay now yes in theory we have despire subet needs a configuration file as of now you can see we have not created any configuration file and that's why spring boot says hey uh you don't have to worry about the default configuration I will take care of it yes if you want to provide some extra configuration that's where you have to use some configuration file as of now it will work work so we got a controller now this controller whenever you call this controller you have to perform certain operation oh when you say operation that means we need a method let's create one so I will say public and this method name will be let's say home of course uh you can have a different method name but that's fine let's give it home I want to call this method home which will simply print as of now I'm not calling any JSP let me just check if this method is getting called when I call the homepage uh I will say homepage requested that's it nothing much I'm just printing homepage requested okay but will this work let's restart the application let's say restart the application it will take some time so let's keep our page ready and let's refresh and you can see after refreshing the only thing we changed is the timing okay we have not got the output yet but of course the output you'll be getting is in the console right because you're saying system.out.println and if you can see the console we have no way it says homepage requested that means it's not working the reason it is not working is you know you might be be having different kind of request you're requesting for homepage you might be requesting for some particular Page by saying slash accounts or slash users or baby slash laptops here I want to specify Hey whenever I use a request for the homepage then only you have to call this method and the way you specify that is with the help of annotation here which is request mapping so after to say request mapping in bracket you have to mention the path first of all you have to import the package let's say control space package imported and here you have to say slash so that's our homepage right when you say the slash okay I guess it will work now let's relaunch the application okay let's refresh it and you can see oh we got a different type of error this time we got internal server error we got 500 okay we'll talk about that later what is happening but let's see the console here are we getting homepage requested and the answer is yes can you see that we got the output it says homepage requested that's what we wanted right yes we got some error we'll talk about that error in some time but it says homepage requested so yes we got a controller we got a JSP the only thing is this controller is not able to call the GSP page that's what we have to solve now now how do we solve it when you send a request for the home okay if you check your url it says Local Host colon 8080 and then we are saying slash okay that's how you request the homepage so this we is getting called so we have to inform here whenever you call home method we have to call index.jsp now now if you're coming from Ser background we used to uh create a request dispatcher using which we used to call the GSP here we don't have to do all those thing we simply have to return the name of the JSP which you want to call so you have to say index.jsp so what happens is see who is calling this home controller it is called by dispatcher serlet so if dispatcher Ser is calling this method so it is the job of dispatch serlet to call the actual page so we just have to inform heyy which page you want to call so we are saying hey thisp Ser I want to call index.jsp that's it okay the only thing is we have to say string here and our job is done let's relaunch it so as you can see it is relaunched let me just open my browser and say refresh oh okay something happened you can see okay I saw some animation there something happened uh we got the same error but if I expand this if you can see the downloads I just got a download here just relaunch it again and you can see we got another download what is happening the thing is okay so let me just go back again and let me just click on this I don't know what this file is it is says unknown DMS just open that in a finder so as you can see if I open this it is my JSP page oh that's weird I'm downloading the JSP page what is happening so let's try to solve this problem in the next video so the problem is we are not able to run the GSP we are downloading it let's solve this issue in the next video [Music] so what's wrong here everything is working except one thing which is instead of running JSP You are downloading it we don't want that we want to run that JSP on Tom Cat so what happens you know when you talk about JSP JSP get converted into sulet and that sulet runs on Tomcat here nothing is happening we don't have something which will convert your JSP into salet now if you talk about the external Tomcat it has that power the tomat which we are using it normally don't convert JSP into suret okay in fact the spring boot is famous for rest Services you don't actually create websites in Spring boot that was not the main idea the main idea was to build rest Services if you want to achieve that if you want to use JSP yes we can use them Leaf here that perfectly works but if you want to use JSP and if you want to convert JSP into svet we need a special Library which you have to download and that is Tomcat Jasper so let's do that so let's go to our browser and let's go to the favorite website of us which is MAV repository and here we have to search for Tomcat Jasper so this is what you need so I will just download this one okay now which version of Tomcat Jasper we need so let's go back to our application and here let's open the M dependency so you can see we have the Tomcat version it's 9.0.4 so let's stick to that so let's download the 9.0.4 uh let's copy this dependency I guess it is copied let's go back where you will add it of course you have to add that in a pal. XML file so open that and here let me add that particular dependency the moment you say paste and save it will download Tomcat Jasper from the internet it will take some time now since I don't have the local copy of it so you will get the repository from the official website so as you can see we got dependency here we got Tomcat Jasper now once you have added that let's see let's run this application once more and let's see the output so let's relaunch the application okay let's go to browser and refresh and now you can see we got the output so this time we are not downloading it but we are running the JSP so that's the thing if you want to run JSP you need a tomcat Jasper library to convert that so it is working we got welcome Tesco our job is done so whatever we have done till now is very simple we have created a page which is index.jsp which is actually our web page and then we have this home controller where you are it's a controller basically and you can have a method which will accept the request for Slash and then it will simply call index.jsp but what if you want to do something extra let's say I want to have a page where user will enter some values so let's say I want to say I want to add two numbers yes uh we can do something big as well but let's stick to simple because we are working on we are learning spring framework right we are not just learning logic here so let's keep the logic simple and let's focus on the on the concept so in the next video we'll talk about how do we add two numbers using spring MVC [Music] so let's get the application so of course if you want to add two numbers after calling the web page it will instead of printing welcome to teliscope we'll also print the form where a user will enter two values so let's do that quickly so here I will say create a form for me and the from will have uh the action and I I will say the action is ADD so I want to call Simply add and then form ends and here I want to create two text Fields so I will say okay so that's our first field and we want one more so I will just copy this and paste and now you can see we got the second one as well so we simply say second number and here we'll have it as num two now what next I want to also have a button here right which will when you click on that it will call add uh let's do that so I will say okay that's done so we got our two input text for Num one num two and we also got buttons everything is done yes we don't have a perfect UI here but doesn't matter right we are learning spring uh so UI doesn't matter at this point yes it matters when you give it to your client as of now it should work now once you got that page of course when you click on this button submit it will call add but don't you think we don't have anything here which will accept the request for add now think about this what can be done of course you need a page where you will show the output but before showing the output I want to process that information so let's say if a user says 2 + 3 I want to give it five as answer where will do that so of course you need to create a controller so if you're coming from server background so what we do is we create controllers but in not printing the actual output because we don't know the actual output let's go back here and in home controller you will simply say result do JSP that's it nothing much we have created two pages one is index.jsp and result. JSP okay so this looks fun okay I'm not sure if this is fun let me just try oh we're still missing something how do we know that you want to call this add method for the add request of course it will not match the name you have to use a request mapping as The annotation So when you say request mapping in this have to say add so whenever you call add request it will call add method it's that simple so let's run this so I will just restart the application once so let's go back to our code and let's refresh once again so when you say refresh now you can see not just we got welcome with teliscope but we also got to text fi here let's say two and three as of now we have not done any coding for that let's click on submit okay you can see it is actually calling result page okay not in the address bar because if you see the address bar we have still have uh num 1 equal to two and num equal 3 okay uh we are not specifying which way is getting called here but result. JSP is getting called because we got the output as result is okay that looks cool but uh we want to perform the operation right so here I want to add those two numbers now how will you do it now let's go back to our salet and GSP stuff uh if you have learned it before so in salet what we do is if you want to take the input from the user uh we use this very special interface which is known as sttp solet request so using that you can fetch values so what I will do I will just go to this ad and I will say hey I want to use those methods if you remember it's not just one parameter we us to pass two parameters so in this service method we us to pass the request object and the response object we don't have anything here so we have to create them right so let's do that so I will say HTTP subet request and you can say req and we got that object so we'll say we we got this object now you'll be thinking how spring will know that you want want the instance of this interface here that's the magic of spring dependencies injection right if you remember we have talked about spring ioc or spring depend injection where it will inject the object which you need now thankfully this object is available with Tomcat so spring will say okay we have that object in Tomcat let's use it that's great now once you got that object we can fetch the values now so I will say int I where you will have the first variable value I know it's not a logical name to use but that's fine so we'll simply say request do get parameter you have to also convert this into integer right so you have to say integer. parent okay that's how you do it in fact for second variable as well we do the same thing let's copy this and paste and this time we'll name it as J of course and here I will say num two now once you got those two values you can simply add them right how do we add it uh it's very simple you can say int I will have the variable name as maybe num three I don't know if it makes sense I would say I Plus plus J so the value of I and J will be getting added to give you num3 now I want to print this num three because this is my output in the result page oh how would I do that so we have to send this num three to the result page and to do that it's very simple actually you can simply say Hey how do I do that okay so in solet we used to use a concept of HTTP session uh so when we use HTTP session we can simply add this num three in that object and in the JSP you can fetch it that's one way you can also use send disp Ser where you can pass this value here you can say result. JSP question mark num three equal to and whatever value of num three is you can say num three here that's URL rewriting right that's one thing you can do we have multiple options right as of now what I will do I will simply say uh I will add it in a session so I will say HTTP session session is equal to we let's create the object of it oh but how do we do that so we oh okay okay first of all we have to say we have to say control space let's import the package and here I will say request. get session so we have this method which is get session which will give you object of session okay once you have that you can simply say session dot you can add not you can okay what's the meth name I forgot the me name you have to say set attribute and the attribute name we can have num three name can be anything right you can change it the only thing is whatever name you use here you have to use same name in the GSP page oh okay so I will say num three in this case Okay once you got this attribute num three we are calling result. JSP in result. JSP I just have to say uh we can use Java code here we can use session object and we can fetch the value or we can use gstl so as for the jstl syntax we simply have to say dollar and the cly packets and here you will say num three that's it so whatever name you have in the subet or in the Java code need to have it here okay it makes sense uh but but sometime sometime your gstl may not work so maybe your code is ignoring it so we will simply say is e ignored false sometime it ignores e language which is expression language okay uh let's keep it just to be on safe side I'm saying false everything looks cool I think it will work let's restart the application so let's go back to the page uh in fact I want to call the first page let's refresh in fact you can see the the address is Local Host colon 880 and then here I will say num one as three and num two as four or five uh so when you click on submit you want output as eight and we got it can you see that we got the output as eight so what is happening behind the scene uh so when you send a request the request is getting call for the homepage right so when you say homepage this method is getting executed which is calling index.jsp and that's why we got that page which we have where we have welcome to telescope and then we have a form when you click on submit it is calling add request is again going to to dispatch a sulet where dispat subet sees hey for the ad we do have a mapping so this me is getting called so not just meth is getting called you also getting the object of request that's the job of spring dependency injection now once you got the object using that object you can fetch the values of I and J where we got three and five then we are adding them we are putting that data in a session okay so once you have dat a session we can fetch it any time in fact provided that session is running okay this is working but I still have some issues don't you think we are not using seret we are working with spring and this code looks bit ugly trust me it looks ugly can we clean it can we make it more effective and the answer is yes and that will see in the next [Music] video okay so let's make this code look good now first issue is we don't want this sdp request object here don't you think spring should automatically give you this values in the parameter itself okay what I'm talking about is if I remove this part I want this two up two values of int I and J here int I comma int J so we don't need to write these two lines okay so we areed in the code right okay I'm just trying it out I'm not sure will it work okay the second thing is we don't need object of HTTP session here so yes we can actually pass it here so we can say HTTP session session so we don't have to say request. get session so our job is done right because if spring can give you HTTP request object it will also give you session object yes it it it does that okay but will this work of course it will not work because you are not sending I and J you are sending num one and num two but how will you assign it here so we have to use an annotation here which is at request param and here in this request Pam you have to mention whenever you send a value for number one assign the value for Num one to I is that simple okay let me import the package for request param and I guess done and the same thing has to be done for J so let me just copy this code and paste it here okay and this will name it as num two so we got num one and num two so value of num one is going to I the value of num is going to J so that's how you fetch the value okay this is actually coming from the address bar we'll see that and then let's try this let's restart the application refresh the URL and you can see we got this page here I will say three and four now you can see this data is going to the server so let me click on submit in the address bar you can see the values of three and four are going in the URL so it is switching from there and that's how that request param works that's great this is working yes you can also use post request here and it will still work so that's the benefit of using request param so we are reducing the number of lines we are going for we don't want request object we don't need to assign we don't even have to pass it that's awesome right right it it is working but still but still we don't want this HTTP session object here can we remove that we'll see that in the next [Music] video let's try to solve that let's remove that HTTP session here the way you can do that is with the help of a concept known as model and view okay how do we use that now if you talk about model model will have data right we have discussed that in MVC so m in MVC model is data so your data here is this num3 so we have to just we just have to put this num3 in the model object and we also have to specify the view right and the view name is is. JSP okay how do we do that so we have a inbuilt class model and view right we'll say MV we can have any name doesn't matter and we'll say new model and view now once you got this object here instead of using session We'll add this in a model view so I will say MV dot there's the option of add object you just have to say add object Which object you want to add so I want to add num3 as num3 itself the way you do that with session right so we got model object the amazing thing is you don't even have to return the string format you can return MV here because MV will not just have the data but also the view name oh we have not assigned that let's do that so I will say mv. set view name and the view name is result. JSP remember it is view name because most of the time people make mistakes here they say they they write set view it's actually set view name okay uh let me just remove the extra lines from here yeah so you can see we have added the value we have assigned that to a model object it's just that the return return type here would be model and view that's it but then we don't need this thing here so we don't need model session object there our job is done okay let's try let's relaunch the application so let's go back to the page and here let's refresh it it should work there's nothing which we have done extra click on submit and you can see we got eight so the power of model view is it will take two things the data which is your num three so we don't need HTTP session object and it will also have the view name so you don't have to return string format so this works and we are happy whatever we have done till now okay but we have some issues what if what if you want to have this page in different folder what if you don't want to mention JSP in the code because you might want to change your view technology in future now you're using JSP in future you want to use something else can we make it Dynamic let's see that in the next video so till this point everything is working right we are able to add two numbers as well so if I say four and five I click on submit you got nine the only thing here is if you look at this code we are mentioning the extension as well right that's something we want to avoid because maybe in future I might be going for some other view technology maybe velocity or free marker in that case I don't want to mention JSP here that's one thing the second thing is if you look at the pages here all these pages are there in the web app folder now web app folder is actually a public folder which means anyone can access this page throughout the world you can they can simply go to web page and you can then they can mention the website name slash the page name it will not even call the controller it will directly call the page and it will work we don't want that right we want these pages to be secure that's one thing or maybe you want to keep it somewhere so that no one will know the path so if you want to make it secured one of the folder we can use here is web INF so you can keep these files in web INF they are private the only way to call them is through the controller and that's how you can make it private but what if you want to make it public but in a different folder that's what we do right we put all our pages in different folders so let's say I will say new and I will create a folder here and the folder name is view or views and I want to put this file inside this view so if I drag it here that's done so we got that into views now and second thing I don't want to maintain the mention the extension here so let me remove that and remove this this one as well so you can see the the extension is removed and the path is also changed and after doing that if you restart the application and if you go back to browser let's let's see if that works now if I click on submit you can see we got an error it says 44 so maybe it is not able to find something but what is that something if I go back here so that something is the page right so of course you're not able to find this z. JSP now in this case you have to mention to your framework hey uh we are using spring framework and then spring framework is using dispar solet so we have to inform dispar solet Hey whenever you are looking for the view you have to find that in this folder which is views and then you have to also add the exchange of which. JSP of course it should be configurable to configure this we have a very special file here which is application. properties this is where you will do the configuration but how because when you say property files we have to mention the key and a value so of course values we know it will be the part of the JSP which is views and the exchange which is JSP but we don't know the the property of it right and so normally when you work on the project you do remember those things but if what if you are doing for this for the first time in that case you can simply go to this website which is the first one if you search for spring boot applic properties go to this website and this is where you will find all the properties if you can just go down you can see we have this properties for logging we have aop we have identity then admin banners you can also change the banner right using this what we want is spring MVC so if you can just scroll down so we got spring M you can see that and then here if you want to do that so if you want to assign the path and the extension we have to use these two things the prefix and suffix now prefix is what what before result which is the path and suffix is what after result okay so which result I'm talking about so if you go back to the code uh you can see here we are mentioning result right so before that we have to mention views after that we have to mention extension and to achieve that we have to put those two properties here so we can go back there we can copy these two properties and we can paste it here of course we have to change this so what should be the prefix so prefix would be views of course right that makes sense uh so when you search for the file now it will be views slash and the file name we have to also mention the extension so you have to say dot JSP that's it once you have done that I hope this time this will work let's restart the application and let's go back to the browser and here if I refresh this and if I click on submit oh it's working can you see that so that's how you can achieve you achieve this thing which is removing the extension which is JSP from the code from the Java code and putting that into in the property files the second thing is we have to also change the views in fact if you want to make it private you can also say views belongs to web infm folder and that will make it private so that's how you achieve prefix and suffix see in the next video so till this point everything is working right so we have added these two numbers in fact the numbers which I'm talking about is this so if I say if I pass let's say three and four and if I click on submit you can see we got the output as result is seven so that's what we wanted to achieve that what we are doing is is we have created a controller so whenever you request for homepage it will return the index.jsp and then when you request for so in fact in the index.jsp if you go there what we are doing is when you click on submit it is sending a request for add oh so where we are handling it so you can see in the controller itself we have ADD method which accept the request for ADD and everything is working you can see it says request Pam we' got num one num two and things are working okay but there's one which we have done in the last video which is the introduction of model and view so the reason we went for this is uh when you want to call a view of course right you want to send some data to a client and client will expect a HTML page so what we are doing is we have created the JSP which will get converted into HTML after rendering but then when we say you want to call a JSP JSP also need a data and data in this case is this n three so basically we have to set up two things the first one is the view name and the second one is the data which you want to pass so data in this case is there in Num three okay so for that we are using this model and view because it can hold two things model the data and the view name which is uh result in this case result. JSP so what I want to achieve now is do we have some alternate option example let's say this is best model view is best we can set the name in fact there's one more thing about model view you can uh actually set the view name here itself so you don't have to uh use another method so you can simply pass the view name in The Constructor and that works okay so the point is can we use something else apart from model and view example let's say I don't want to return model view object here I I want to stick to the string part here okay uh that means in the return we have to still mention the view name so I will a result what I have to change is I don't want to use model view so then the question arise how will you add the object that's what we want to do right we want to add this data in the model in that case if you simply want a model you can create an object of model and it will still work but where so the idea is I want to put this data in the model object so model is something which will be available for the view as well so in the parameter in the bracket itself you can say you can specify the reference of model M okay now what this model is so model is basically an interface which will behave same as model view except that we don't have a view here so you can see we are saying model and here instead of saying MV dot we will be saying m dot but you can see we got an error the thing is in M we don't have ADD object method we have to use another method which is quite similar you can see the method name is ADD attribute so instead of saying add object we have to say add attribute and then we can pass the two stuff the same thing as model view uh it's just that we are not specifying The View separately now which one to use it depend upon you you can still go with model or you can go with model view it's just that when you send a request and if you want to have that data in a model doesn't matter if it will be used for view or not or you want to call a specific View later you can still use model but if you want to specify model view both I would say preferably you can go with model and view okay you still have a choice that's great that's how we can use model here now other than this model we also have an option of model map so you can say model map here even that works you can say control space and enter you can see the this the code will still work so let me just run this code and let we also see the difference between these two so let me first run the code with model map uh let me relaunch the application and it is still launching so let's refresh and you can see we got the same page there's no error that's great this time we'll add some different numbers we'll say 6 and 8 when you click on sub you got the answer so you can see we have not done any changes and at the output and the only change we have done is in the controller so instead of using model and view we are using model map then what's the difference between model and model map so if you click on this you can see this is documentation for model map model map as name suggest supports a feature of map so by default when you add the attribute will get added in the map format so you are adding data in a map which is getting added in the model object so it depend upon you which want to use if you want to use Simple object I would say go for model if you want to add map if you want to have the data in the map format of course the best option would be model and map I model map okay let's go back here so this still works with model and model map both at least this code so try it out uh so it will it will of course work so that's it from this video where we have talked about model and model [Music] map now once we have talked about model and model map it's time to look at another annotation which is model attribute now before doing that I want to make some changes the first change is instead of adding two numbers let's do something else now in Java everything is object right so if you want to represent data we represent data in the object format example if you want to represent a laptop if you want to represent a mobile phone or maybe Account Details or user details everything will become a class and then we instantiate them right that's what we do in Java so here as well I want to create an object of alien okay now what is alien basically so alien is a programmer so I refer programmers as alien so what I will do here is I will go back here and I will say new I want to create a model class so let's say class and we'll name this class as a alien and the only thing is I just want to make this into a different package okay so here I say the package is model it's better to have all your model classes in model package now this alien will have a certain variables right so let's say I want to go only for two variables here so I'll say private in a ID and private string a name of course in future we'll add some more attributes once we start talking about Advance concept as of now these two are enough and now I want to have gets and sets for this I will say Source generate gets and sets select all and generate so once we got the class once we got the variables and data sets done let's go back to index. J SP now what I want is as a user or as a user you will add your own records so you'll be having the option of adding the aliens so of course the I want to have the action as ADD itself but then the data which you will ask is different so we will'll say enter enter your ID so we're asking for the ID and of course this name will change I will say this is Aid and then here I will say enter your name so we're asking for two things a ID and name name let's have it a name so you can see we got these two details so now when you click on submit it will send the add request in fact just to make it more interactive I will say add alien it makes much more sense right so now when you click on submit it will call add alien but in the home controller you can see we don't have anything as ADD alien so we'll modify this and we'll make it alien in fact you know what I will do instead of changing the existing stuff let me add another method here and say okay let me just say this is so I will say public string and the method name would be add alien of course you can go with any method name and now to call this I have to map it a request mapping right so I will say request mapping and here I will say add alien now we have to create this method now of course we have to accept some parameters right so the parameter which we are passing from the index.jsp is these two details we have a ID and a name so whatever we have donear till now is uh if you want to accept the values we have to use this two annotation here in fact two variables with two annotations we have to say request param so we can accept that with the help of request param the only thing is here it will say a ID and the variable name is also a ID and here I will say instead of num two we'll say a name and here as well I will say a name okay there's one change a name is not of type int it's of type string so they have to take care of that as well so that's done now this method will return something right I want to call a page I want to call the same result page I will say result because I'm lazy to create one more page so let's stick with that of course you can create your own Pages later but as of now I want to call result but this result page need to have something right I want to print something here so what I will do is I will not print num three I will print the object of alien I want to print alien here but will this work of course if you go back to home controller we are calling result that will work but then it will not print alien because we don't have that object in the model as you can see see we don't have any model in this method so the first step is you need a model object so I will simply give a comma here and I will say model M so that at least I will be having the model object with me and in that model object I want to pass the object of alien the amazing thing is we don't have the object yet so let's create one and the way you create object you you can say alien a equal to new alien and then here you will say let import the package and here you will say a DOT we have to assign the values as well right whatever is coming from the client so as a client you will be sending data right so you will say submit by passing let's say a ID as 101 and name as naen so this two details will be received here I mean here in these two variables we have to assign that to the object so we'll say a DOT set a name I will set a name as a name in fact once you got the object we have to add this object in the model object so I will say m do add attribute and here you will simply add the attribute as a but if you add the attribute as a it will not work because in the result. JSP we are asking for the alien so the name we have to refer here is alien so I would say alien so whatever data is there in uh in a will be represented as alien in the result. JSP that's what we want now will this work see till this point we have not done anything extra it's just that we got a class in fact whatever we have done till now we know about this stuff let's relaunch the application just to verify if everything is working so application has been launched let's go back to the page let's refresh so yes of course we need to see some changes instead of having uh num one num two we'll be getting ID and name so I will say the ID is 101 and name is naven I guess I missed something in the code let's see that what I've missed when you click on submit you can see we are getting the the default uh package name class name and the hash code we don't want this we want to print the data right to achieve that we do one thing right we always create we override the two string method if remember in normal code Jaa we do that so I will simply run generate a two string method here which will print data this is what I missed let me just save it and let's relaunch the application again so when you click on submit here you can see we got the output so this is what we wanted right so whatever user has entered will be coming here now you will say hey everything is working then what's new here the thing is if you look at this code which is request param and request param something is not matching right we are accepting individual values and then we are assigning them to the object here it doesn't look good right what I want is instead of having request but I'm here what if I can simply mention the alien object here itself this is what we wanted that means I don't have to mention all this thing so the moment you pass the object once the moment you pass the values you know in a ID and a name now since the names are same right it's in a ID and a name don't you think it should be assigned automatically to the alien object is it possible yes and the way you can do that is with the help of model attribute so once you have model attribute with you you can directly assign the values here but how to do that that we'll see in the next video so let's make it work to make it work we have to use another annotation which is model attribute so that means here instead of this all this request param this stuff we don't need this Aid and a separately what I want is I want to say alien a right this is this is what we wanted right and then to achieve that we have to we don't need this stuff here we need to remove that the only thing is we want this object to be received from the client to achieve that we have to use an annotation here which is ADD model attribute that's it simply save this so whatever is coming from the client will be assigned to this a and our job is done let's verify let's relaunch the application so this time I'm sending one2 let let's click on submit and you can see we got the data this is what we wanted right and the amazing thing is on the client side I mean in the code we are not using two request Pam annotation and then checking it one by one getting the object of area nothing like that it's very simple right the amazing thing is model attribute is also responsible to add the data in the model that means we don't have to do this manually whatever object you got is by default there in the model and that's why it says model attribute right so we don't even need that model object it's called quite simple okay but what if if I go back to result you can see here we are saying alien right and here we are saying a what I will do now is let's go back here and here I will say A1 will this will this work so I'm not using alien I'm not using a because a is the name of the attribute and alien is the class name itself or you can say the decapitalized class name but here we have a different name we have A1 as a name in this case let's try what will happen so let's click on submit the moment you do that you can see we have not received the data there the reason is we have a different name here which is a and then here we are saying A1 okay how do we solve that so model attribute says okay if you want a different name that's fine in the braet you can mention A1 so now whatever data is coming from this a will be assigned to A1 is that simple let's relaunch so when you click on submit here you can see we got the data so that's the beauty about model attribute so it's an annotation which also accept the parameters so if you pass the parameters it will take that name otherwise it will take the name of the class which is alien okay but what if I have alien here okay let's stick to alien and here in the home controller what if I what if I don't write model attribute will this work the answer is yes if you don't have a different name if you still want to go with alien you don't have to mention model attribute but it's a it's a convention that you should write it otherwise it will still work but then you know there will be a confusion because here we are saying alien uh so it is working what if you change it what if you say A1 it will not work so let's relaunch it without using model attribute it will still work so when you click on submit you can see it is still working so without even without mod attribute it works because we are using the same name in as alien here okay now this is mod attribute which we have used in the as a parameter to the method can we use mod attribute at the method level we'll we'll see that in the next video so let's say here in the alien I want one more data so let me create one more class and I want some data about it or maybe not even class let's say I want to print the message let's say welcome message I will say welcome back aliens so I want to print this message the one way you can do that is by printing as it is or maybe I want to change this alien to something else every time I maybe maybe I want to I want to do that in the code so what we can do is we can say welcome back and here say name so by default you can see we don't have any name here right so name is just an attribute which we are using here that means before calling result I have to assign name where should I do that of course you will say you have to do that in this method because this is what is calling result but what if you have multiple methods which is calling result in this case you can see even this method is calling result so of course we should not be assigning the name here what you can do is you can create a separate method a separate method which was responsible to assign the value to the name so let's say I will say public void I will say model data of course you can have any name doesn't matter and here you will simply assign the data to the model okay so how do we do that so we have to use a model M now in this model you can say m dot and you can say add attribute of course with the name uh so we'll say the name here is name and then the actual name which I want to pass is aliens of course you can pass anything here but let me just pass aliens now what will happen is in the code in the output I want to welcome back aliens right so the name will be replaced by aliens but will this work of course not if you want to make it work we have to use and annotation here which is model attribute the moment you say model attribute what will happen is your spring framework before calling any request mapping it will first call model attribute this model attribute will create a model object and then in that model object you are adding the attribute which is name with the name aliens now it doesn't matter whatever control you call whatever method you call with the request mapping everywhere this name will be available so in the result page you will get that name right so we can use mot attribute in the method parameter or on the method level so let's relaunch it so let's click on submit here and you can see we got result is okay we have not received the result I don't know why what went wrong there but at least it is working you can see we got welcome back aliens so let's figure it out what went wrong there why we have not received the alien okay because we are saying alien here and the attribute here is A1 that's bad uh let's remove that I guess that was the issue let's save that okay but at least that this is getting right we are getting welcome back aliens that is that is working uh let's relaunch it okay so let's click on submit and so that that is working we got Al result is this so we got this data and then we also got welcome back aliens so that's how we can use mot attribute on method [Music] level so whatever we have done till now is awesome right uh we have created this project and it works like charm so we can send data to the server and server will give you the response the amazing thing is instead of using normal server JSP we are using a spring MVC framework and that to we are not using normal spring MVC we are using spring boot here the reason we are using spring boot is when you work on Spring MVC project or any type of spring projects we need to do a lot of configuration and spring boot says hey you don't have to worry about the configuration so if you look at the project we have not done much of the configuration we were focusing only on the logic yes we did some configuration in the property file so if I open that file for you so you can see this is where we are mentioned that we want to work with this folder views and then the extension for the uh page is JSP other than that everything is quite simple right we are doing everything in the code but then before spring board we should still work on Spring MVC I mean a normal MAV project where you have to do a lot of configuration and until this point we have not seen that so what if you are working on a project of spring MVC but without spring boot so in this video Let's create a project with the same features I want to have alien I want to have homepage so with the same feature but with the help of normal spring MVC configuration so yes we will be using Maven here we have to add dependencies we have to do some configuration so what configuration I'm talking about now first of all when you say spring boot project so this project knows that this is spring boot MVC so whenever you send a request request directly goes to dispatch s but when you create a normal Maven project or normal web project we have to navigate it we have to mention hey if you want send your request first to diset so we have to configure that second thing you have to configure is we have to mention that this is a spring MVC project where all the configuration I mean all the controller have annotations okay that's we have to mention second we have to do this configuration as well but then what type of file we'll be using to do this configuration so in general when you talk about configuration one thing come to mind is XML so we'll be using XML to configure this okay it looks lot of work right and technically yes we have to do a lot of work but it's it's worth learning so what I will do is I will go back to my application I mean go back to the project and instead of using the same project I be creating my own I mean I'll be getting a new project here so let me right click here and we say new and of course in the new project I will be just copy pasting the existing code to save our time so here I will create a maven project okay so not a spring project or something let's create a maven project and in this Maven project click on next I want to create a web application select that click on next so it is asking for the group ID here so I will say com. telesco artifact ID would be spring MVC okay nothing much specific here click on finish now once you click on finish it will create a project a simple mavin web project where you have to add dependencies okay so we have to add dependencies first right so we'll do that or maybe I will simply first copy P I want to understand what errors you get and there's one more issue here the issue is yes it is giving you index.jsp but unfortunately it is not giving you a internal Tomcat server so you have to add the Tomcat server externally okay that is something we are remaining with so what I will do is I will simply close all this thing code now and I want to add the external server so the way you do that is by clicking on this no server available I want a a tomcat server and in this I will select 9.0 click on next okay so it is saying do you have this setup need to check so browse and let me search for the location of Tomcat so yes I have the Tomcat 9 available in my downloads I will select that and click on open I don't remember when have downloaded it but in case if you don't don't have the opposite tomat you can just go to Google and search for it it's not that big deal and then click on next just shift your project here so that you don't have to add the Tomcat in your project manually so click on finish and that's done so you have added the external Tomcat to your project and soon this error will be gone I don't know why it's not going there uh let me just check once I will go to properties uh code build path okay there are some Library issues uh it says what is missing something is missing here the job is missing okay we'll do that and I just want to make one more change instead of having it 1.5 I will change it to 1.8 click on finish okay that's done and I need to create so you can see it is giving you error because you don't have the Java folder okay even the Tom kit is not set up here so I will just go here and say add external server there there should be a server option add Library I want to add a server run time Tom Cat yes finish click on apply and at a can you see that everything gone okay so once we have solved that issue I want to copy all this controller in fact I will just copy paste the packages here of course I want a different name we can change just change it change them later so I just copy that and paste in my main folder or Java folder okay so you can see we got two projects here and then the change I don't want the same package name I want to have a different package name here so I will say refactor rename I want the package name to be spring MVC not boot click on okay and the same thing here okay done so once we have done those two changes it is still giving you error okay can you see that we got error here now first of all when you talk about spring MVC we don't need this main file because this is used only for the spring boot let's remove that and we are getting eror for controller as well now what is the error here the thing is whenever you work with spring framework you have to add spring dependencies and you can see we don't have it here oh from where you will get it you guess it right we have to add dependency in the p .xml file so here we have to add spring MV dependency so save uh from where you will get it just go to the maven repository mvn repository and here you have to search for spring MVC and you can see we got spring web MVC just select that okay which version I will go with this table one which is 5.1.4 uh click on that and copy this just go back to application and paste now once you say paste it will take some time to download okay it was quick that's great uh because maybe I've used the same version before so you can see we got all the dependencies here the moment you got dependencies I will go back to home controller and can you see that gone all the errors gone okay that's working so what do you think if I run this application oh how can I run this application because we don't have the uh pages so I want the pages as well so these two pages I will just go back here and index in fact I want them in views so let me just copy the entire folder and let me paste inside the web app folder okay so when I say web app inside this we already have index.jsp we don't need it so I will just right click and say delete I don't want that folder or the file so in the view we have two pages index.jsp and result. JSP okay this looks cool what else what else we can do here I guess this is the configuration which I wanted let me just run this code so what do you think if I run this code will it work so the answer is no actually because the moment you run this application it will search for the index.jsp oh before that I have do one more thing I want to shut down the internal server which we were using for spring boot otherwise it will give you the conflict for 880 port number so I will go back here index.jsp let's run this code or in fact I I can run the entire project right so I will say run as run on server because this time we are using external server click on next and click on finish yes it takes some time I guess it will use the internal browser and can see that we got error it is not able to find the index. JC page how do we solve it yes we have to do a lot of configuration but that we'll see in the next video so it's time to solve the issue so the issue is we are not getting the page right because the moment you done this application and when you say you are calling for the homepage it will look for the index.js page in the web app folder as you can see we don't have it there that's one issue the second issue is I want the the request should be sent to the controller not directly to the page to achieve that we have to configure our XML files so now normally when you create a web application you get this amazing file here which is web.xml and in this if you go to Source this is where you have to configure that Hey whenever I send a request the request should go to dispatch a salet if you remember in Spring MVC all the request should go to dispatcher solet first okay how do we do that so it's quite simple you just have to go here in web app we don't need that and we need to create a subet tag that's how you provide the subet mapping right so we'll say subet tag and we also need a subet mapping okay once we got these two things in subet tag we have to specify two things as we do in all the subet projects the first one is a subate name uh let time me let me just have the name asisco and we need one more thing which is your servit class okay now what is the class we'll talk about that in sometime but before that I want to map with this servit mapping here okay so first of all when you say Serv mapping again we need to things here we need a Serv name in this case you have to make sure that this name is same as this name because that is how you link this to uh outer tags here and then you have to also map with the URL now why do we need URL so to mention for what type of request you want to call this particular class which you have not mentioned till now so I want to call dispatch seret for all the request so for all you can use simply use slash that okay so but then we have to mention the class name right so what is a class name so as I mentioned when you walk with spring MVC request should go to dispatch a salet okay so that's a class so we'll simply mention that here so we'll say dispatcher solet oh it's not coming directly here dispatch Sol declaration oh it's giving enti stuff we don't need that okay that's awesome that's awesome we don't need to actually create all this thing it is giving you everything but time me let me just type it here and then we can get it from the uh control space I simply type it here so it belongs to Spring framework o. spring framework. web. saet do dispatcher sulet so dispat belongs to this particular packet that's how you map it in fact the other way is you can simply if you don't want to type all this thing by yourself you can simply say disp seret you can see it says you can auto complete click on enter it will give you almost the same thing which we have done here you see that that you can handle uh but I I would like to go with the same text which I have written let's let's stick to that now we are send request should go to dispar salet but then it is dispar salet who will send the request to the controller here right so how would disp sub knows that it has to send the request to the controller and that's where we have to configure the disp sub as well because if it don't do that it will give you an error okay what error it will give you let's see let me shut it down and let's relaunch the application r on server yes click on next finish okay so you can see there's no error as of now but but then the moment you try to call the homepage we got a different error this time and the error is file not F exception and the file it is trying to find is telescope hyphone sub letter XML or that's weird why only this file name and what this file will do okay so first of all this file is responsible to configure your disp servet okay that's one second why the name is the lco hyen servet so basically the name should be something hyphen suet now what is that something the name of your subet name so the since it is the here and that's why in the error itself it is saying telescope that's the reason we we got telescope hyph Sol so if you give a different name here if you say a it will be ayen suet XML okay so we have to get that file but where in the web INF so we'll right cck here we'll say new okay so we need to create a XML file so set XML here click on next and okay what should the name as we discussed the name should be TCO hyph salet do XML click on finish and you can see we got this file here and here we have to add a bean tag instead of typing that let me just copy paste it this is where you will specify the the DTD for the beans we'll be creating and we'll say beans okay that's done we have assigned the beans tag and now we have to we have to do some configuration the first thing is we have to mention to the dispay saet where are my controllers uh second we have to mention hey we are using annotation for the controllers and third is the path so let's go step by step the first one is we have to specify the context we have to specify the package in fact the base package so for that I'll be using component scan it is CDX colon component scan and you can see it is asking for the base package so you just have to mention in which package you have all the controllers okay so in general I can specify com. telescope you should be very specific here but that's fine this should work the next thing you have to mention is you have to mention that we are using an ation configuration so you have to say CDX annotation config that's it okay will this work let's try so after doing all this configuration let's try what let's see what happens yes restart the server okay just calling that page oh this time we got a different eror okay okay uh maybe we are running very wrong file here let me just run the application yeah so you can see we got a different eror this time uh it says something not found okay 44 now what is not found it says there's no mapping for get index oh there's no mapping for the index it's because we are calling the controller you can see it is calling the controller it is no mapping for the index it is there okay it is there the only thing is it is not able to find the extension and the path because if you move this index.jsp to web app and if you write JSP here it will work but I want to specify the path as well and to do that we have to create a bean and then this will Bean will have a class so the class who is this responsible to do that is internal resource view resolver now this belongs to a particular package which you have to mention so you have to mention the entire path here that's bad okay this is the entire path we have to mention and then we have to close this that's your bean tag now in this Bean tag now this is this here which is internal resource view resolver is responsible to search for The View and to give the extension and to specify the path and to achieve that we have to specify two properties the first one is prefix as we are talking about prefix the prefix I want to have is we'll say value so there are in the view right so will say slash views slash we have to specify the path the only thing you have to mention now is the extension and to do that you guessed it right we have to use suffix so we'll just paste it here and this will be suffix okay the value will change so the value here will be JSP that's it so whatever you have done in the configuration spring boot the same thing is done here we might have to add one more property here for the gstl now since in our code we are using gstl so let's make it supportive so I we say property and we'll say view class so the view class I want to go for is gstl View and to achieve that we'll say org. springframework so this is the path so it is or. springf framework. web. s.v. jstl view okay I'm tired now I guess it is done let's let's try I hope this should work now let's relaunch the application okay something worked let's go back to the page here and say Hey I want to call the homepage and oh oh oh now we got another error it says class not found gstl is not there that means we have to also add the jstl now okay so let's try this let's go back to let's go back here and let me just remove the property for time bin relaunch go back here okay it's working without that let's try uh so go back here and say for is my ID and my name is naven click on submit oh it is working can see that we got a id4 and name is naven so we got welcome back alien as well so everything is working and this time we are using normal MVC we are not using spring boot so let me just go for a quick recap what we have done till now so basically we have this home controller but as we are working with spring MVC the first request in fact we are working with web project the first request will go to the web.xml this is where we are saying whenever you send a request request will go to disp salet and to configure that we have this file which is telescope hyphone salet so this is the configuration for disp seret where you are mentioning two things the component scan annotation and the most important is the view resolver in which this part is not required if you want to write that you have to also add dependency for gstl let's not do that here and you can see we are specifying prefix as views and suffix as JSP and everything is working it's do simple so that's it from this video now once we have seen how to create a simple spring MVC application let's get back to Spring boot because that's where we want to learn some more advanced concept now this is a project which we were talking about before right the demo project which is spring boot application basically so you can see it also says it's a boot application so what I want here is I want to have some more features so before that let me just run this code once again just to verify what we have here so I will say run as spring boot app okay so let's run this and you can see uh it is asking you for two things the name and the ID so I will say name is I mean ID is 101 uh let's say rows and if you click on submit you can see it's working we got one1 we got rows It also says welcome back aliens okay what I want now is we are only sending data and that if you see the data has been sent using a get method so normally when you send data from HTML page to the server it goes in you you can call it with the help of get method or a post method so in the get method you actually pass the data in the URL okay that's something we don't want because whenever you send data to the server you should always use post whenever you fetch data from a server you should use get so here in this scenario if you look at the pages which we built the index.jsp here it is asking you I mean it is sending the data to the server so in this case instead of using get we should be using post and the way you can do that is by mentioning method is equal to post so now you will be sending data in a post format so let me relaunch the application so get back to the browser and here you will say refresh okay where is that refresh and now I'll be sending let's say 101 and rows and if you click on submit now you can see in the address bar we don't have it we don't have those values so is get is going there with the help of post request but what is happening on the server side so if I open my home controller this method which is ADD alien accepts both the request the get and the post both okay now the amazing thing is request mapping does both right but then what if you want to support only one you can do that you can mention that so what I will do is I will say instead of using request mapping in fact there's one more way you can have one more attribute here so you can say value is equal to the value which you have and then you can give a comma here by specifying the method and you can see the method is so you can mention method equal to request method. getet now when you say Get it will only support get now you send a post request this will not work and if you want to use post just mention post it will only work with post now so that's how you can restrict so you can say post or get you can be very specific in this case this is one way the another way of doing that is instead of using method stuff here we can actually change this request mapping to we have one more annotation here which is known as get mapping now get mapping is same as request mapping with method get so it will only work with get now if you want to use post we can also use post mapping so basically we have three things we can use request mapping which supports both we can use get mapping which only supports get and we can use post mapping which supports post so that's how we can be very specific in this case so that's how we use get and a post in the scenario in fact we'll also use get in the next video so till this point we are only using post mapping right we were sending data but what if you want to receive data so what I'm talking about is let's say if I go back to the index.jsp and this is where we have this button which will send data but I also want a button which will receive data okay or you know instead of having a button let's go to URL and say uh I want to fetch a data so let's example if I go to Safari again and here instead of having the colon 880 I will say I want a data about an alien so I will say get alien now when you say get alien or maybe I can say get aliens which will give you all the aliens so I will say get aliens in this case so this get aliens oh just reting to me to Google you don't want that but that's fine uh so I want want to return the alien names the list of aliens of course I can use static uh list here uh so let's do that so I will go back to my home controller and let's create a method which will accept get aliens uh in fact I don't need this add anymore with just to reduce the number of lines we have in the code so I will go back here and I will say public string and the method name would be get aliens again as I mentioned you can use any name doesn't matter and we don't want to return string do you want to return string yeah let's return a string so let's return the list of aliens in a string format and there's one more thing if I go back to alen we want to do one more change here the change is inside this alien I want a Constructor because I don't want to create object and assign the value using set as gets uh let's use Constructor here so I will say Source generate Constructor using field both the fields okay so once we got the Constructor let's go back to the code now here I want to create certain aliens before that I just want to return some blank string so that you will can you can just avoid the error and I want to call this whenever someone request with a get request and the URL which I want it to work here is get aliens so here let's create a list of aliens so I would say list of aliens I will say this is aliens equal to okay let's import the package for list I will say control space the list and here let me create arrays do as list because I don't want to say uh new at a least this is shortcut way of getting Multiple List here and you will say new new alien and let's say 101 comma naven that's the first alien we got then we give a comma and again we will say alien we can have one more alien here which is one2 and I will say Rose so you can see we got two aliens we got a list of two aliens and let's return this list here so I will say return aliens dot two string because I sending that data in a string format so we have to convert that into string format again I'm not even converting this into Jason okay my bad I know I'm just converting that into a string okay so uh let if this works let me just relaunch the application okay it says restarted let me just go back to the thing here okay now I want to request for get aliens so slash getet aliens enter and oh we got an error it says oh there's a problem here can you see that if you look at the error properly it says it is not not able to find so there's a 44 error it says not able to find View and we got the answer you can see that we got a list in the text format and it is searching for that page. JSP you know why because by default in Spring MVC whatever string you return it gets returned in the format of name whatever you return it will be trying to search for the name of it we don't want that we want to call a page we did that mistake we should not be rning a list here we should be R the page which will display this uh so let me just return a page I will say show aliens we don't have this page as of now we need to create one so let's quickly create a page of JSP so I will say new and I want to create a JSP file with a name show aliens. JSP and here I want to display that result so I will say dollar aliens I want to print aliens here that's it or maybe I will just simply print result because I don't care what we are sending I just want to print whatever we are sending from the client side I want to have one more thing I just wanted to say is e ignore just so that in case if my tom cat says Hey I will not be supporting e let's do that so let's say is e ignored false okay everything looks good now I hope this time oh it will still give you eror because we have not added that in a model okay let's do that so I will say model M and here I will say model do add attribute as I mentioned you can also use model map here we discussed that before right and I will say this is result and this is aliens that's it let's relaunch the application so let's go back here and say enter can you see that we got the output so we got the data from the server and this time we are sending a get request so you can see this is your post request and this is your get request so if you want to work with get we say get mapping okay but let me just add one more twist what if you want to fetch a particular alien not just all the aliens you want only one alien let's say one1 or one too how can we do that and that to I want this data to be coming from database oh we are so much of restrictions here right okay so first of all let's try to get one data and we'll see the datab byas part later so that we see in the next video how do we fetch one particular data spring project has lot of modules right we have talked about spring code which talks about dependency injection we have spring MVC which talks about how can we Implement m see pattern with the help of front controller in Spring and then we have also work with spring rest that we'll see in in the future videos but what about the database what if you want to connect your spring application with database now even it doesn't matter as it spring we normally if you want to connect with Java a normal Java application with database we have to use jdbc right now we all know the pain of jdbc because in Java world we work with objects wi so we have to convert our objects into data which will be stored in a table format to solve this problem we got o RM which is object relational mapping so imagine a class here and a table here so we can map it right because the class name will be your table name we have some fields in your class you'll be having of variables so you can map those variable with the table column right but what about the data what about the rows there now for one class we can have multiple objects in the same way one table can have multiple rows and each object here will represent one row there think about this let's say we have alien object and alien will have three variables a ID a name and let's say age so we can have all this in a table columns so if you create the first object for alien with three different values that will be one row there if you create second object with three different values that will be second row there and that's how we can do the mapping and that's where omm is very famous but how do we Implement omm and that's where we have jpa which is Java persistent API now the actual implementation for jpa maybe you can use hybernate there so that means we have to build the application we have to work with hybernate and lot of configuration can we use all those things in Spring and the answer is yes because spring provides you dependency injection so the instance for hnet can be created in Spring framework and that can be done with the help of spring orm so basically you are using a spring application you are using hynet which connects with myql which connects with database so we have a huge chain there so we have spring we have hynet myql and database in between you will also have spring om which will connect this two right now the advantage would be so the instance creation will be done by spring omm for hynet the configuration will be done in the spring configuration next what about Transaction what about begin transaction commit transaction will be handled by Spring transaction uh so how exactly that works will see in the Practical video so let's try to implement the database connection here so what you're trying to do is when you run this application and that we are doing it in normal spring MVC we are not doing it in Spring boot now so yes when you talk about normal spring MVC on normal spring we have to do a lot of configuration so just be with me because in this video we are going to do a lot of configuration so let's get started the first thing is when you say you want to connect with database we have two option you can use normal jdbc or we can work with hybernate now since since we work on big applications is better to go with hynet because it can handle your transactions in a good way you can achieve o RM here so let's do that so what I will do is the first thing we need is we have to go to the pom.xml file and we need to add certain dependencies so basically we have to add approximately four more dependencies here in fact five I guess so let's let's get started the first one we have to add is let's see when you work with spring MVC and if you want to work with hypernet you need hypernet code dependency that's very important now when you say you need hypernet code where you will go so you have to go to the maven repository so you have to go to this website which is Maven repository. comom and here you have to search for hibernate code so in search for hibernate code you can see the first link is hibernate code you can select that and you can pick up any version I preferably go with the latest one because it has a good number of usage uh let's copy that and go back to your application and paste that's the first dependency we have have added we have to add some more because when you say you want to connect spring and hibernate now hibernate being a omm framework you need to add a dependency for to connect spring and hybernate here which in this case is spring omm so or will be sitting between hibernate and spring so let's open that I'll go with 5.1.4 because that's the spring version we are working on copy this and go back and paste when this is done now what next now when you say you are working on Spring and hybernate whenever you work with hybernate we have to handle transactions so you have to begin transaction you have to commit transaction but what if you want spring to handle it and that's where we can use spring transaction so we just have to add one more uh okay we have to add few more but let's go step by step so we have to add spring transaction because this will handle the transactions for you so let's paste it we have added hibernate because we want to use hibernate here uh we added spring orm because we want to connect spring and hibernate we want spring to handle the transactions and that's why we are saying spring transaction okay now we need two more okay last two okay last two uh the first thing is uh when you say you have a database of course you need to install some database right now in this machine we have already installed MySQL again we'll see this setup how to install MySQL and how to configure it so we'll see how what is MyQ and how to open MyQ workbench and where from where you can download it but then in this machine I have already configured it so the thing we need here is the mql connector so the the setup we are using is mql so we have to use mql connector and for that we have to download mql connector so in case if you're using some other dbms let's say p orle uh make sure you use that particular connector so I'm downloading myql connector let's copy that I will go with this version which looks stable copy and go back here and paste okay when this is done now all this dependency takes time to get downloaded from the MAV repository but since in this machine I've already done that before so it is getting from the local repository now when you work with hnet when you work with databases we need to create some connection pool maybe 10 connections at a time five connection at a time and we have to configure them uh so to configure this type of pooling and to use pooling in the in the project we need to use certain external jar files or maybe a third party pulling service so to use that to to use third party service here we will be using C3 P0 again you can choose whichever service you need this is my favorite C3P0 because it works uh let's use this one I will prefer the latest one copy and go back and paste so you can see we have added all the dependencies and job is done just to give you a summary we have added these dependencies the hbnet code spring orm spring transaction myc connector and C3P0 okay once we have added all the dependencies what next now we have to configure hypernet because just by saying we are using hypernet it will not automatically connect with database right we have to configure it and to do that where where do we configure this and we have done that before right so if you want to do any configuration we have this very favorite file which is telesco hyphone salet this is the dispatch salet configuration file you can add the configuration here itself now first of all we are using spring transaction right so in the in this beans we have to also add TX you can see we have MVC we have context but nowhere we have TX so let's add that now this TX we can simply copy this line and paste it here MVC will become TX and here as well we have to say TX that's done we need to add the thing here as well so we have to we will be copying this one and let's do that here itself in fact we can add it anywhere we want but let's do it here uh so instead of MVC again we have to say TX X instead of MVC we have to say TX I could have easily copied from somewhere and pasted it here but I wanted to show you how can you do it by yourself without copying so this is done next we have to add certain configuration now when you say certain there are a lot of configurations so what I will do is in of typing it typing everything now it's time to copy paste so you can see I have copy pasted a lot of stuff don't worry I will explain each and every line here now the first thing you need is we are saying let's go bottom up so the first thing we need is we want to to handle transactions right so we want someone to take care of transaction in this case we are saying hey Spring you will take care of the transaction spring will say okay I will do it but I need some configuration so you're saying okay if you want to handle transaction I will create an object of my transaction manager in fact uh this is a object of hibernate transaction manager so how do we create the object of this so it's very simple you just have to create a bean uh so we are creating object of hybernate transaction manager and then we are saying the ID name is my transaction manager so this one is dependent on this Bean but if you want to create this Bean here we need to create object of session Factory now when you learn hibernate we have a concept of session Factory right so we work with multiple sessions and all these sessions are created by session Factory that means we have to create object of session Factory here so let's set the property ass Factory but unfortunately we don't have the object yet so we are creating the object of session Factory here so you can see it says be session Factory now this session Factory is the object of local session Factory Bean the amazing thing is as a programmer we are not instantiating them I mean we are not creating object of them it will be created by Spring framework thanks to dependency injection so you can see we got the object of this one but it needs certain properties the first property is data source now this data source will have all the configuration of database what is a database I mean which DB PMS you're working with uh what is a username what is a password how many size pool you need all those stuff will be done in this my data source that's one the next thing you have to mention is in which package you have all the entities because normally work with hnet you have to specify entities every table need one entity next you have to set some hybernate properties one of them is M SK dialect So based on which dbms you're working with you have to set up this one so if you're using Oracle uh you have to use Oracle direct if you're using post you have to use post dialect we are using MySQL so let's say myql dialect and we also want to see the SQL query in the console so once you run the code I want to the query in the console and for that we have to set this particular property great but then we have still not created the object for my data source so it is still missing and you can see that's we have done here so we are saying we need my data source this is the object of combo pool data source now from where you getting this remember we have added this 3p0 in this we have to do certain configuration the first one is we have to set up the driver name jdbc URL the way you work with normal jdbc and the username and password I know I should not be revealing password on the internet but that's fine uh this is the local machine password so that's fine that's my MySQL password in this machine okay so this is the configuration which you have to do I know it's clumsy but that's how you work on Spring right now this is not something you'll be doing every day right so this is something you'll be doing only once the moment you work on Project every day you will add some new features to the project but this thing will remain same yes you will be doing some minor changes in future but as of now this works okay but then what else we have here so will this work of course not this will not work let's go back to home controller and we need to do certain configuration so if you remember we were supposed to accept the request for get alien and then get aliens we need to add that but how will you do that that we'll see in the next video [Music] now when you say you want to work with database of course you need M for that right of course depend upon which dbms you work with so in this course we are working on mascle so how do you install masc it's very simple actually just search for MySQL download this will take you to the official website of MySQL now based on which OS you are working on maybe Windows Linux or Mac it will give you those options here okay so you can see we are to download and here we have option of Community Edition so basically if you are using it for free so make sure that you use Community version in the community version you can install two things in fact we need two things here the first thing you need is a community server and the second one you need is the mascle workbench now mascle workbench will behave like uh what do you say the GUI for for your application so I will say download on this one and download on this one as well so two downloads this is the community server now based on which OS you're working on maybe Mac OS or Linux you can select that now which one to choose of course based on which OS you use you can select that click on download now this will download the masol server so you can see it is downloading the second setup we need is masol workbench and this will be your what you say the GUI so now click on download so you can see both this will take some MBS so it will take some time okay so you can see we got both the setup the first thing you have to set up is the mql server when you click C on this one now depend upon which OS you use we will have different steps but it's quite simple you just have to say next next next nothing fancy uh so you can click on continue you can click on continue continue agree continue install the same steps and you have to enter the password here for your machine if you're using Mac okay setup done now once you have done this setup it will also prompt you for the configuration now since in my machine already have the configuration it will it is not prompting me so in the configuration the only thing it it is important here is the password so make sure that you give a proper password now once you have done with the setup you have to install M workbench so you can see uh if you're using Mac it will give you this step but again if you're using Windows it's quite simple right so you can see I have installed Mas let me open that now so I'm opening my Mas called workbench this is the first screen you will see and then you have to click on this one if this it is available otherwise you have to click on plus connection here where you have to mention in the host name as Local Host and whatever port number you have configured during the configuration by default it is 3306 and then you have to enter the password click on okay and test the connection if it is successful that's great you can go ahead click on okay I have to give a proper name I will say this is try okay so you can just click on this and you will see your my schol workbench now here we have to create a database right so the query you have to type here to create a database it's very simple you have to say create database teliscope the amazing thing is I already have the database here so I will not be executing this the next thing you need is the table so I will say create table alien okay before this we have to also say use teliscope because that's how you specify that you want to use database so we'll say create alien this will have two Fields one is a ID of type integer and second one is a name which is of type vat 2 or V 20 which is the size and then we have to also insert data so we'll say insert into alien values uh you will insert let's say insert two values one Z 1 comma naven and the second one you can simply give a comma here you can say one2 comma and you can say this is let's say R so we got this two data here and then you can also hit this select query so you can select Stu from Alien the thing is I already have this thing in my database if you want to execute this click on this symbol I just want to say select star from Alien because I know it is there so I will first say use and execute you can see we have one1 naine and one2 rows in this database now once you have your mask ready let's fetch this data in your code once we have added the dependencies which is uh all the dependencies here and then we have also on the configuration and then uh on the other side we also have a back end ready with us which is your mask your database in fact mask your dbms in which we have created a table which is aliens so how do we connected now first of all if you want to connect with a database you need a layer so in fact it's not compulsory but then that's a general thing we use whenever you want to connect your database we normally create a dow layer so the way you can do that is very simple you just right click on your project in fact right click on your package here and say new I want to create a new class and this will be a alien da now for every particular table so let's say if you have multiple entities in your database let's say you have alien table you have accounts table you have user table what you can do is you can create a separate layer separate Dow for each of the tables there in fact you'll be having different entities as well so the moment you talk about hnet we have to put entities as well so that's your Al Dow here which is a dow layer in fact Dow stands for data access object so this is a class where you will have all the methods using which you can access the database normally I'm talking about the C operation create read update and delete okay I want this to be in a package name as da again not compulsory you can use any package name there in fact the moment you start using spring boot uh in spring board we don't refer them as Dow we refer them as repositories your choice you can still use Dow or you can name it as repository let's continue with da and click on finish and you can see we got a dow class here now what we want here now we want certain methods right the first method we need is in fact what I'm trying to fetch I'm trying to fetch all the aliens you can see here we are trying to in the home controller we have this method which is get alien in case if it is not there in your code make sure that you copy this from the code which we have done in the earlier sessions you know the spring boot videos so you can copy that the only thing is in you in the attribute we have not assigned anything because we don't have the values so what I want is I want to fetch values I want to fetch the aliens with the help of get alien method so here as well inside the Dow let me create a method as get aliens so I will say public which will return me the least of alien and then this will be method name as get aliens of course you can have any method name but uh this makes much more sense let me import the package for both so list and alien done and here we have to return the list of aliens now first of all where from where you will get it now since we are working with hypernet in hynet we have this amazing feature called as session Factory because to fetch data we have to use sessions and to use sessions we have to use session Factory so let's use it here so I will say private session Factory and here will be session Factory okay that's done now once you got session Factory here we have to import the package for that package imported now once you got s Factory here let's use S Factory to fetch the data so before using session Factory we have to create session object equal to now how do you create a session now first of all you don't have to create the session you just have to use the existing session created for you so in the container we have the session available just use it and the way you can do that is by saying session Factory the object of session Factory Dot there is a method which is get current session so it will not create the session for you it will use the existing session okay once you got the session object I can simply fetch the values but how do you fetch the values in fact this session object here which is if I search if I type session here and the moment I say dot you can see it has so many methods one of the method we have is get now you if you want to fetch one particular element you can use get but what if you want to fetch all the elements and that's where we will be creating creting a query so I will say create query in this create query you have to type the query I will say from Alien this is jpql or you can say hql and you can specify the alien docl class the type of data you're trying to fetch so what this will do is this will create a query which will return you which will talk to the database which is telescope and will fetch all the aliens now if you want to convert that data into list you can simply say Do List here so it will give you the list from the table and then since it is is giving you the list we have to save the list somewhere so I will say list of alien let me call it as aliens equal to so whatever data we are receiving from here will be saved in least alien and once you got it once you got your alien you can simply return it that's what we wanted to do right we want to return all the aliens okay that looks fun I mean I hope it will work but there's one more thing whenever you say you are creting object of s Factory we have to provide Auto wire here but you want be thinking how spring will create object of it automatically actually if you see the configuration in the configuration we have created a be named as session Factory So when you say autowire you will get this particular object okay so nothing is happening automatically here uh things you are doing some stuff let's go back to alien Dow I guess everything is everything will work actually no everything will not work the thing is okay in fact first of all let's run this code and let's see what errors you get now to run this code first of all you need object of uh Alien da here because you want to fetch all the aliens so I will say alien da and then I will name this object as da I don't want to say new alien da here because I want to use spring feature and to achieve that we have to say add component here if you want spring to create object of it and here we can use our favorite autowired so that it will give you the object existing object oh we already have it here I guess I've done that before so this is what you need you need object of alien da and once you got it you can just go back here and say da dot the method name is get alien it will give you all the aliens and your job is done hopefully let me just relaunch the application and let's see what happens in fact I should run this once okay something is happening it is trying to connect to database I guess that's why there's nothing wrong here now if I try to fetch if I say get aliens uh the method the request I will say enter oh we got an error it says could not obtain transaction synchronized session for current trade what went wrong here the thing is when you say you are trying trying to work with session you're trying to fetch data the thing is when you work with database we need to handle transactions okay it simply means uh before anything doing any stuff you need to begin the transaction and after you have done everything you have to commit the transaction so in hybernate we do that manually we say begin transaction we say commit but since we are using spring here spring orm you don't have to do that by yourself you can simply use annotation here so the anation name is transactional so the moment you say transactional it will say hey I will take care of all the transaction beginning and comit that's great our job is done let's relaunch it let's see what other different dat we get this time and let's say enter so as expected I'm expecting an error here but let's see and we got an error it says can you see that we got a different error this time it says alien is not mapped the thing is when you work with hibernate or omm tool we have to make sure that your model classes are actually entities that's how you can connect your table with your class and the way you do that is by using at entity on top of it and here as well you have to say at ID which defines your primary key so you have to specify two things the table as entity the class as entity and the primary key as ID that's done I hope this will work this time I'm not expecting any eror but you never know let's go back here and say enter and we got got what uh nothing is coming yeah so we got the output can you see that we got all the aliens now in database we only have two rows and that's why we got two data here but if you have more rows it will give you that the amazing thing is in the console you can actually see the select query so we have done that right on the properties we have mentioned that we want to see the SQL if you don't want to see that simply say false it will not show the SQL in in the console let's verify the data database do we have it there so you can see that these two records are there so that's how we can fetch all the aliens but what if you want to fetch one alien what if you want to add a new alien that we'll see in the upcoming videos now once we know how to fetch all the aliens we want to fetch one particular alien can we do that in fact we also want to add a new alien to the database so if you see database now we only have two records we have one1 Na and we have one2 Rose so in this video first we'll try to add the alien and then we'll see how to fetch one particular alien the amazing thing is we already have this add alien method here we just need to say hey Spring orm just add one particular alien for me now in fact if you see the homepage as well which we have it here when you say submit will call this particular method here and then which also accepts the alien object okay that simply means we need to save it somewhere in fact before that I just want to do one more thing instead of calling page I want to call show aliens page because let's stick to it let's use this one JSP file here in this JSP we are simply printing result that means if you want to call it you also have to assign to a result in fact we'll be using the same stuff uh maybe will not need it that we just need to add the model object which we have it here but that will be with name a right I want to have the name as result so what I will do here is in the bracket in double quotes I will say this is result okay that's done but then I also want to save that in database right so of course for that we have to call Dow do save so you have to say Dow dot there should be a method named as ADD alien unfortunately we don't have any method which is ADD alien here so that means we have to go to our alien Dow and this is where we have to create a method named as ADD alien let's do that so let's go back here and say public are we expecting something here no so I will simply say void and I will say add alien this add alien will accept a particular alien so I will say alien now since we are accepting here we'll also have to send it and we have done that here let's go back to our code and now how do we set it so of course we have to repeat this statement so we have to fetch the session object let's copy and paste and then with the help of this session object we can save it now the amazing thing is this session has a method named as save you just need to pass the object your job is done uh in fact before saving we have to start the transaction and we have to end the transaction and that can be handled with the help of add transactional annotation and job done right it's so simple we are simply adding one extra method with this feature and job is done let's go back to our controller just to verify everything looks cool let's run this code let's see what happens so I guess server got restarted so what I will do is I will try to call the homepage refresh and that's our homepage here I will try to add one more alien which is 103 and this time the name would be let's say Kon and if I click on submit okay so there's no error you can see we got the alien as 103 and name is Kiran now if you look at the console it says insert into alien that means we are able to fire the insert query just a final confirmation let's go back to database because we are still not sure right if you refresh this code uh you can see we got 103 K that means it is working so it is so simple to add the element in the record so we have have done with all fetching all the values we done with adding one value or one alien how can you fetch one particular value so let's do that so we'll go back to home controller and here I want to have one mode using which you can ask for one particular alien now for that I just want to do one more thing in the index.jsp we forgot to add uh the line which was there earlier so if you remember we added this in the uh spring boot project the same thing we need here paste so it will ask you for one particular alien and when you click on submit it will call get alien okay so that means in the controller we need one mode which will be get alien so I just copy this code paste it here and this one will be get alien and this is also get alien now when you say get alien you have to also accept a particular ID right so what I will do here is I will say add request Pam in a ID now since they have the same name so we don't have to do it multiple times I mean we don't have to mention that name here okay once you got Aid here in da it's the method name should not be get aliens it should be get alien by passing one particular ID so we're not passing all we are passing only one ID unfortunately we don't have this method in the Dow yes of course we can go there we can create the method or you can use a shortcut here you can click on this error and say hey create a method name as get alien so you can see we got the method name as get alien this method returns particular alien so I will say one particular alien but what should be the code here and you can see we got ruls so it's that easy to work with database you don't have to write complex queries you don't have to do jdb setup everything is done right and that's the power of spring with hybernate and the combination of these two is spring omm so I hope you are enjoying this there's so much you can try it out you can fetch one particular alien you can add alien you can also use load okay so that's great so that's it from this [Music] video so till this point we have seen how to connect spring and hybernate with the help of spring omm and everything is awesome right but the only problem is the number of configuration which you have to do first of all we have to add dependency that's fine that actually makes sense to add all those dependency but still look at the number of dependencies which we have added we have added this all this dependency by ourself that's one issue the second issue which I have is the configuration look at the configuration and that to in XML yes you can convert this into Java configur ation but still as a programmer we are focusing on configuration we don't want that we are programmers we want to focus on logic and that's where spring boot says hey you know you focus on convention which is logic I will focus on configuration so let's do the same thing whatever we have done here in Spring boot now of course we need to specify all these properties because this is not something spring boot can guess right spring boot don't know that we are using masc uh spring boot don't know that what is your IP address and the database name spring boot don't know your username and password so in this case we have to configure all this thing but not in XML in springbot we have this awesome file which is property files right it actually makes sense to write everything there and it is bit easy so let's go there and let's do all this stuff you don't need to do that because spring boot will take care of it so let's close this and we don't need this anymore let's go back to Spring boot application which is demo in this case and you can see we have home controller from where you are you will be accepting request the only thing is till this point we were returning the static content we don't want that we want to return the dynamic content coming from database and then uh we have a model class here which is alien. Java now what is missing here is the repository right the Dow layer that's something we have to create so we have to get a dow layer what else we have to add dependencies as well but do we have to add all the dependencies technically we don't need all first of all we need MySQL because you are connecting the application with mySQL so let's do that first so let's go to Safari and now we want a MySQL connector so just go to MAV repository. comom and search for M connector you can pick up any version you want of course we are doing this from a long time now in this series so now we know which one to copy so let's copy this version and paste it here so myql done now we need one more not hypernet not spring transaction not all those dependency but one which is spring data now spring data is something using which you can connect to database using spring application and it actually makes sense to use spring data when you work with spring boot again I will tell you why so what I will do now is let me add that dependency first so I will go to again Safari and here I will search for in fact you can open any browser uh let's search for spring boot data now when you search for spring boot data this is the dependency which you have to add spring boot data JPS starter so let's click that now which version select so I will pick up any version because anyway we'll be removing the version number it is mentioned globally uh so let's paste it here after spring web I don't want to mention the version number here because that's something we have assigned here so not have to mention in every spring dependency so you can see I have added two dependencies here the first one is data jpa and second one is myql connector now once you have done with that what I will do is let's go back to application you can see nowhere I have configured anything for database okay but still what do you think will this work we just added two dependency normally in normal project even if you are dependency it is not compulsory for you to do the configuration if you want to use them then only you have to do the configuration but that's not the case with spring boot example if I run this application now so let me say run as let me just stop the other ex executions I will say run as a spring boot app so you can see we are getting an error it says fail to configure data source URL oh that's weird because nowhere in the application you can check the entire source code here nowhere I'm talking about the database connection we just added dependency and that's where problem starts because the moment you say spring data jpa in your project it is compulsory for you to add I mean to configure the the database connection otherwise it will not work oh that's a compulsory let's do it then so let's go back to the application of properties and here we have to provide the configuration now what configuration you ask the first one you have to mention the URL in fact first of all We'll add the dialect so when you work with hnet see behind the scene hibernate is running it right so or jpa hynet is running it so we have to specify the dialect here so let's say spring. jpa do properties why I'm typing all this thing when I can do the shortcut so you don't have to type it you simply have to say hibernate do dial that's weird I'm not getting the entire stuff let me just type it here spring. jpa do properties. hybernate dialect now once you got the dialect you have to specify the dialect name which dialect you actually working with so I'm working with myql but that belongs to org. hybernate do dialect. mysql5 dialect okay so as your dialect changes this will change so if you're using post this becomes POG dialect that's the conf to do now the next three important the first one is URL now when you work with jdbc you know what we need we need URL we need username and we need password so let's assign that here so I will say data source. URL the first configuration uh so that is your jdbc colon mql colon sl/ localhost colon 3306 slash database name is scope the next one we need is username and password so I will say username so that's for data source. username where is that it's here so username we want is root and data source. password in this case is 1 2 3 4 5 6 7 8 that's my database password that's done we have provided the configuration we just want to solve this eror first so let's run this L let's see what happens the moment you relaunch the application now you can see we have not got any eror so the only thing we have done is we have added dependencies and we have configured the application. properties Next Step would be we need to create a dow layer then we have to go to Home controller we have to use Dow layer uh we have to also make sure that your alien class has entity annotation and at ID but that will do in the next video now once we got our dependencies and then we have all also configured our application. Properties by specifying this four properties here let's create our Dow layer now if you remember in Spring MVC as well we were supposed to create a alien Dow class but what we will do here is instead of creating a class let me create a interface I'll right click here I will say let me get an interface and we'll name this interface as alien rappo now you might be thinking okay so we got the interface here we'll be writing some methods here you know the Cod operations and alien get alien maybe you can also try removing the alien and then we will create some class which will implement this particular interface that's what we are thinking about let's see that later but what I will do is I will simply extend this class or this interface with another interface named as jpa repository so this jpa repository will ask you for two things one is it will ask you for the class which is Al in this case and the second one it will ask you for the type of primary key in this casee we are using integer let me import the package so you can see we have created the repo here okay so the point is if you got the repo which is your Dow in fact so once you got the repo you have to create a class which will implement this interface we'll do that in some time uh let me just go back to home controller and now I want to instantiate the repo and the way you do that is very simple you say alien repo uh let's name it as repo itself the way we have have done for da and let's s autowired now one thing which is very important if you want to instantiate this Alien report you need a class and we have not done that till now okay let's keep it for later now once we got the repo we can simply perform certain operations here right we can call those methods we'll do that but before that I want to go back to alien and in Alien we have to specify that this is an entity right and then we have to also mention the at ID uh here so that's done let's go back to home controller now once you got the repo let's first try to convert this get aliens here so I don't want the static names so let me remove that so how will you get the aliens it's very simple you simply say repo dot now of course in the interface you can see we don't have or not this one if you see the interface here we don't have any method but see the magic the moment you say repo dot can you see that how many methods you got in fact we also have a method which is find all find all is a method which we have not declared and we are not even defining it okay so definition part is later but how it is coming we have not declared it now that's where this jpa repository is there so when you say your interface extends jpa repository jpa repository is a interface belongs to the spring data jpa Library which we have added and if you go there you can see it has so many methods all are declared you can see we have find all we have find all uh we have find all by ID we have it list goes on you know we have uh get methods we have remove there are so many methods available here if you can't find methods in this particular interface even this interface extends another interface here some methods belong here there's no method which is available here some methods are available in code cude repository and you can see we have some methods here okay that's fine uh you can just check them out later so there are methods which are there in JP repository which you are getting in Alien repository you will say okay but all of them are declared right none of them is defined that means we have to Define them now is it a good thing or a bad thing to Define all the methods okay let's see that so without defining the class what do you think will this work of course not right in Java we have learned if you have not defined stuff of course this will not work let's try let's relaunch the application and let's see what may go wrong I will say run as in fact I just have to restart the application so you can see we have not got any error maybe we will get an anic error let's go back to application refresh there's no error till now let me fetch all the aliens so I will just go back to the address bar and I will say get aliens I will say enter oh we got the error okay it's not something we have not got the error but then this is not the error because of that alien repository this is the error because it says no default Constructor for entity the thing is when you have this class alien which has a parameterized Constructor it also needs a default Constructor since you are making it entity we forgot that point so let's create a Constructor here I'll just type it if we don't want to write anything in that that's why I'm just specifying this let me restart the application once again go back to the safari and hit refresh and can you see that we got the the output I know that's weird right because nowhere we have written the code for fetching the data we have not implemented the interface which is alien repo and that's the magic the library which we are working with here is the data jpa now data jpa says see everyone works on C repository right everyone works on create read update and delete so when you have those operations what if I can it for you so spring data jpa says Hey developer you don't have to worry about those operations I will do it so it has certain classes which implements jpa repository and it will take care of all the operations the basic operations and that's why we specify JP repository which will work with alien so all the operations finding uh deleting saving everything you will get just because of this line which is JP repository and that's the beauty so yes there is a class which implements this repository but we are not defining it it is done by Spring framework and that's why I'm in love with springboard so just let me go for a quick recap what we have done we have created this layer which is alien repository you can imagine this is a dow which extends the jpa repository here now jpa repository is responsible to to give you all the basic operations in the application. properties we have specified four properties one of the URL username and password and the dialect and that's it we have one more in Alien we have to make sure that this is entity this is at ID and we got a default Constructor now if you compare this code with the code which we have written here look at the Dow layer code look at the code how many lines are there in fact we just have three methods here what if you have save method what if you want to have remove method or save is there what if you want to have update so this code will go up in fact the idea is the more code you write the more bugs you are adding in the software right and that's why spring board is so awesome you write less code and you get less errors so that's it from this video in which we have talked about spring data [Music] jpa so now let's implement the get alien which is fetching only one particular alien and how to add a new alien so we already have those methods here in the controller right we have get alien and then we have ADD alien now first of all we don't need this list anymore let's remove that and here instead of returning the static code which we have done let's return the data from the repository now we don't have to add any method in the repo because everything is coming from jpa repository so what we'll do here is we'll say repo dot now which method we use to fetch that so we have a method which is get so the method name is get one in which you have to pass the ID which is a ID in this case and that's it your job is done it will give you that particular element let's also implement the same thing or maybe let's run this code first so let's relaunch the application and in the Safari let's go back to the homepage say refresh now to fetch let's say one3 let's click on submit and you can see we got K that's what we have in database right so this is working and just look at the code we are not doing much of the code but everything is given by Spring data jpa so let's Implement add as well now what do you think what should be done here it's very simple actually you just have to say session not you have to say repo dot the method name okay we have to search for it now what if you don't know the method name so technically it should be somewhere around save or add or something uh so if you see we don't have any method like with a so let's try save so it saves there's a method name as save we have we have to pass a so now we are trying to save the alien object uh let me repeat we are not doing any commit and roll back part here everything is done by Spring uh so let's relaunch the application in fact in normal spring MVC as well we were using add transactional so we were not doing those uh begin transaction commit transaction part so let's see in the Safari let's refresh and okay this time we adding it right so let's say 104 and this time we'll add let's say mayure now if you click on submit uh you can see we got 104 mayure it is displayed in the page but what about database so let's go back to our myql and let's refresh can you see that we got 104 and mayure I guess I entered the wrong spreading for mayure that's fine but we got the data right that's important so that's how we can add element and that's how we can fetch a particular element in fact you can also try with remove that will be assignment how can you fire the query to remove something now what if you want to fetch something based on some other parameter so what I will do is let me just go back to the index.jsp and here what if you want to fetch not based on the ID but let's say based on the name or maybe a table has a different parameter when you say a ID that's a primary key right so it is very easy to fetch something with the help of primary key and that's what your repo also suggests in the repo itself you specified the type of the ID you have which is integer but what if you want to fetch based on the name normally what happens is in a table you can have multiple aliens with the same name how can you fetch something with name because if you go back to home controller and if you let's try this here so if I say let's say I want to fetch something and we have have this repo the moment you say get you can see it is asking you for the ID it is not asking you for the name can we do that can we fetch something with the help of name that we'll say in the next video so let's try to find a particular alien with the help of name now if you go back to your alien repository we know that this extends jpa repository and this jpa repository by extending some other interface or it has certain methods in it all the methods are related to the ID because that's how you find the element from the database right with the help of primary key but now we want to fet something not with the help of primary so what we are trying to do is if I search for a particular alien I'm not passing the aid I'm passing a name so what I'm saying is let me just copy this code let me create another method and this method name would be get alien by name of course you can have any name doesn't matter but let me just go with this name so get alien by name and here as well the request would be get alien by name I know it doesn't make sense to have in the URL but that's fine uh we can use the same thing but you can have any URL it doesn't matter here okay so it's not that you have to follow this rule at this point now here instead of having param as int I will be having string and this will be a name right so we are fetching with the help of a name now here with this repo when you say get one unfortunately we cannot pass a name here because a name is not a primary key so what to do so what if I say you can simply remove this part and you can say repo dot you can see we have a method which is find by ID so when you have an ID you can find by ID but now we have to say hey I'm not finding by ID I'm finding by name just type it there so you can say find by a name that's it and just pass a name here but you can see we got an error of course right we don't have this method anywhere and this is your own variable even jpa doesn't know that you want a name there okay so what to do now now since this method is not available in jpa what if you create this method in your own repository let's create that let me just click on this and say create method find by name in the alien repo you can see we got the method and this will return you uh okay now the thing is it is possible that in a database you might might be having multiple aliens not one so in this case let me just go with the list of aliens and here as well we need to return the list okay let's go back here and now will this work now of course right if you talk about all these methods which are available in jpa repository if jpa says I will Implement all the methods that's fine because it is its own uh declaration they have their own definition but now this is our declaration right so that means now we have to get a class which implements alien repository to Define this particular method okay let's not do that let's run this code let's see what happens we maybe we'll get some errors let's run this code and let's go back to Safari so this time I'm fetching an alien not by ID but by name or but we don't have a form there we also need a form so what I will do is I just copy this code and paste it here this time I will say enter your name and this will be get alien by name is it the same thing we have yeah get in by name let's go back here and anything mention yeah so we have to mention this a name in this case let's go back to the controller just to verify yes we have a name we have a name let's run this now so let's relaunch the application go back to the homepage refresh okay oh if you still not got the form why oh I'm in the wrong form my bad let me just copy this and now since we have multile project that's why they confusion starts right so let's go back to index.jsp here and paste so now let's relaunch the application so refresh and you can see we got enter by name so I will enter name naven let's see if that works click on submit oh it worked maybe it will not work with the second one let me just type row okay it worked let me type some weird name I will say ready we don't have that in the database okay we got empty Le that means it is fetching from database and the amazing thing is it is working and that's the magic of spring jpa or spring data jpa now this thing here it has a special feature and it is known as the query DSL now DSL stands for domain specific language which means uh depend upon what variables you have depend upon what properties you have jpa will provide you the queries but that doesn't means you can use anything you want you have to follow a particular thing here so example it should start with find by you can't simply say anything else here so it can be get by or find by and then here after that this should be your variable name the first letter has to be Capital you can't say a name or you can't say anything else you can't even say name because we don't have a variable as name the variable name is a name so you have to follow that protocol the first thing is you have to say find by or get by then you have to mention the variable name you have to also pass the name which you are searching for so that's how we can specify a our own stuff but what if you want to have something more let's say if you want to provide descending order you can also do that you can also say order bu if you got a list and you can if you want to reverse it you can do that you can say order by a name let's say control space okay but let's verify this once and go back to your Safari I'm fetching by name I will just submit and you can see at least we got one name but what if if I go back to database let me just insert some mode in fact why from database we can do it from here uh let's say it is 106 and let me add naven one more time oh we have how can I say order by naven because we only have one record right my bad let's try so I will say submit and you can see we got two nain here and first is one1 second is one6 so I will say order by a ID a name uh here as well I have to change it so I have to say order by Aid let's relaunch and let me say refresh here they are in sequence let's also say descending order so that's what we do in the SQL right we say order byy and then we also say DC if you want to see in Reverse okay this is what we wanted right so you can see we got a 106 as first and 101 as second so that's how we can play with uh with this in fact we can also apply or and and here so you can say find by a name and you can pass the first parameter you can also say or uh find by a name with some other name so you can try that so you can have multiple methods available so that's the beauty of query DSL here so that's great we have query DSL we can have multiple methods here but what if you want to have your own query can we have our own queries here that we'll see later till this point we have seen how to work with jpa repository now jpa repository provides you certain features right you can create resource read update delete but all this thing works with the help of of primary key but what if you want to fet some data with some other parameter which is not a primary key in this case so yes you can create those methods just by writing find by and mention the variable name you can also specify is it order by is it like or not like or something like that you can mention all those stuff but what if you don't want to use methods but you want to use the query okay I'm not talking about SQL query here we can do that but what if you want to use jpql what if you want to use hql uh so this JP repository or spring boot data provides you that feature so what you can do is uh if you want to use a query it's very simple first of all what I will do is I will just come in this section uh so I want to fetch the alien based on their name but not using this method so I want a different method name here so I will say list of alien and I will have the method name as find just to you know just to show that it is not matching with any of the method which we have done before and then in this I want to accept a particular variable so I will say string a name the way we have done in the about code okay so once we have done that we have to do one more change if you go back to your home controller here as well you have to mention the method name as find uh so it's not anymore find by a name order buy something we just have to mention find go back to alien repo and now here this will not work right because we have not specified in the name what you want to fetch in fact what you want to pass and is it order by or something so so in that case you can specify the query here itself and if you want to specify the query we need to use a query annotation so I'll be saying adate query uh okay you have to make sure that you're coming you're taking it from JP repository now once you got the query annotation in bracket you can specify the query which you want now in this case I'm using jpql so I'll be saying from Alien so I'm not using SQL where you have to say select star from stuff you can simply say from Alien where a name is equal to okay now this uh the actual value is coming in the method itself right so whatever value you're receiving here from the controller will be passed in the query so in that case you have to use a placeholder now if you are coming from jdbc you know we have used uh what do you say prepared statement where you pass question marks here we don't have to pass question mark we have to use placeholders we can say colon a name right but how do you specify that whatever is coming in the a name will be replaced here or maybe I will say simply say name just to show the difference so I want to specify this name is a placeholder for this a name and the way you can do that is by using a param annotation you have to make sure that is coming from uh the repository data repository again and in the bracket you have to mention the name which using which you're fetching it so it's name so whatever is coming in this a name will be assigned to this name at the r time okay that's what uh we have specified here let's verify if it is working let's relaunch the application go back to our safari and now I'm fetching a particular alien click on submit and it worked can you see that it worked so yes we can use the methods or we can use Query in fact the advantage with query you can pass complex queries okay not just this is simple but try different complex queries here so that's about how can we use Query in jpa so let we say client and server we have two different entities here and normally we imagine that client will be a human normally that's what we do right so as a human we go to different websites and we access the different resources available online so basically there's a server there now server is responsible to send you data and as a client you consume data now as a human we want data in a proper presented format we don't want our data in XML or Json or a plain text we want our data with a good list out like HTML we have images we have Mark we have headers we have all these different amazing designs there so basically server is responsible to serve you HTML but then now things are changing initially when you say client on server we would having machines and humans right so human will be client machine will be server but now we have two applications let's say you are building two different websites or web resources or web services now one web service want to access data from from the second web service now mind you here the first machine is a client the second machine is a server so in this case even the client is a machine now as a client we don't want HTML right I mean as a machine we don't want HTML we only want data so if one service want to access the second service data it's only about data it's not about the design so let's take an example here so let's say you have a Android app which will be showing you the current weather okay okay so now the thing is your application will not know the exact weather for this particular area or maybe in the weather in us or maybe in specific area in India so you're not sure exactly the values right so now here you will request the servers so we have different services available so you can send a request to them by saying hey I want to know the current temperature I want to know the current humidity and This Server will send you the data not design only data because this app has a design inbu So when you say you are sending data so how it will request so we only want data but then you will simply say okay we can do that so we can use Ser JSP we can send request to return data in fact we can do that in normal spring MC as well but we have second issue here the second thing is when we talk about Sate or JSP we normally perform actions if you remember when we create forms in forms we say action and we specify the name now that is your URL and you also specify the action which you want to take so one action will be having one URL but then what if you don't want to depend upon action you want to depend upon noun okay something like this so let's say you're going to a website you have a website which provides you a lot of resources it provides you all the employee names it provides you all the account details and different stuff now if you want employee details instead of saying ww. abc.com query is equal to employ you can simply say ww. abc.com now which makes more sense having Q equal to employee or slash employe the employees right so you're specifically mentioning that you are going for a noun here you want employees so that's what we are going for so we are going for a good URI with with the help of nouns and we are going for only data now this data will be basically the object State okay so let's say on the server we have lot of objects and you want to send data in fact the Java we do that so every object will be having data and this is represented by some pojo class so if you just want to send a data which is the current object state so you're sending the state of the object and that's why this can be done with the help of representational state and we are transferring so it will be transfer and in short we call it as rest okay so that's why we have this very famous API or the service named as restful web service or API so this is a API which will give you data that will be representational State okay that will be we sending to the client in fact client can also send data to the server the amazing thing is when you talk about MVC when you talk about Ser JSP we maintain session so server is responsible to maintain some data but that's not the case with rest in rest scenario what we do is uh every request is stateless okay so even Ser don't know that this client has sent the request before so that means every request from a client will have all the data you can't simply Say Hey I was there last week you can't do that because server don't know you every request will be stateless so in that case you know if you want to let's say if you want to maintain login if you want to maintain some data that you want to inform server hey I was there last week you have to send a token okay so that means when you have sent the first request to the server server will be giving you some token so next time when you go to the server you have to carry that token so these are the some things which are important when you talk about Rest full web service so the first point is we are not working with actions we are working with nouns okay so you simply saying ww. abc.com slash employees slash aliens SL laptops or even if you want to pick up a specific laptop let's say the laptop of Lenovo so you can simply say SL laptops SL Lenovo it will only give you Lenovo laptops uh second everything will be stateless so you can't simply maintain session on the server and we are talking about the state of the object here but how will you implement that so we are not going to use Q equal to something so we are not going to pass parameters in the address bar and the client will not be a normal browser oh there's one more important thing when you say rest it uses the inbuild methods of the sttp protocol so we have certain methods right so we have get post put and delete so we'll be using these methods so if you want to create a new data on the server so normally we call them resources data will be resources so if you want to create a new resource on the server you will say post if you want to update the existing resource you'll be using put if you want to fetch the resource you will say get so we can use all this normal HTTP methods in rest API but how exactly we can do that that we'll see in the next video once we know the theory of rest let's try to implement it but hold on we have a issue here when you say you have a normal web application so we have a server and then we have a client so normally when we imagine client we are talking about humans but on the other side when you talk about rest it's more about a client server but then here the server will provide you some content but not a design or not in a format of HTML So when you say a client it will be a machine or it will be an application now this application we have to build it maybe Android maybe a website but then since we don't want to do all those stuff we don't want to do something extra let's use a client which is provided by someone else the client which we are going to use is especially used for rest testing or rest API use the client name is Postman in fact we have so many rest clients available but Postman is quite famous I will say Postman initially I was using it with as a extension for a chrome you can see we still have it I guess it is deprecated uh so they recommend you to use the application so it doesn't matter is it Windows or Mac it still works on all the OS available so if you click on this so you can see you will get an option of download so depend upon which OS you're working with you can select it so click on download and it simple installation say next next and your job will be done now in this machine I already have Postman with me so I'll be using it so it will take some time to download so let's not use that let's use the existing version so I'm launching the postman which I have and it will ask me for login okay I will simply sign up with Google or sign in with Google so I'm starting my Postman here okay so it says the Chrome app is been dicated okay I know that close that and close this one now you can see this is how your Postman looks like I guess the I'm still using the Chrome extension but that's fine I will just install the new one for the next video but as of now this will work the UI is same for everything so you can see we have a history here for all your uis and this is where you will fire a particular request time being let me request google.com and let's see what happens so I will say google.com enter so I'm sending a get request to Google send okay so you can see we got the data here normally this is your HTML page but then when you say rest API you will be getting your data in XML or Json so this is where you will see your XML or Json we have an option here but then we are not returning the XML or Json right that's why we will not be seeing that so this is your get request if you want to work with post it's very easy you can click on post specify the URL or URI here and click on send if you want to use put for updating data you can also use delete for removing the content if you want to send data with some headers you can do that here uh if you want to send actual data you have to put that do that in body we have certain options here again we'll be seeing that everything one by one so I hope you will be installing Postman for after this and then you'll be doing a proper setup so that you can be ready for the next video now once we have our Postman ready let's change the code now till this point we have worked with normal spring MVC where where we were sending data we were fetching data but not just data we were fetching the entire page that's what we do in normal MVC right and all the request were actions so we were sending post request we were sending get request but here let's use noun so what I will do is I will open my Postman and here we'll send a get request now this time I want to fetch all the aliens now if you see the earlier one if you go back to your code and if you open index.jsp what we are doing is we are sending the we are saying submit and the action is ADD alien so in the address bar it was ad alien in fact to fetch all the aliens we were using get aliens but why you have to say get aliens which sounds like a verb here we want to use noun example if I go back here I will simply say Local Host colon 8080 that's the protocol we are using slash so instead of saying get aliens I will simply say aliens and here you can simply say send and this should give you data now here you can see we don't have any mapping there which will handle aliens and that's why we got an error so the error is it says 44 not found okay that's great we got the error and if you see the we got the error in Jon format that's weird I know uh okay let's let's go back to the application now here what I want to do is now first of all we don't need the index.jsp anymore in fact we don't even need any JSP because we are not using views here we are simply working with Json or XML so let's go back here and now in the controller I want something which will handle my aliens mapping now in fact I can create a new controller or we can change the existing controller now I'm quite famous when it comes to changing the existing code so let's do that so I will go back here this is the same thing we want right we want this particular method so I will just copy it uh and I will say paste here itself or maybe it will make much more sense if you create a new file right let's do that so here let me create one more controller I will say new and let's name this as a alien controller of course you can have a different name but since you're working with alien let's do that so I will say alien controller okay nothing much I want it to be in the same package that's fine click on finish now you can see we got our alien controller and in this I want that particular method which is the mapping so we got this method right here and then I want this to be a controller so let's do that so I will simply say controller nothing fancy right now we got our class so let me import the package first okay but don't you think it should not be get alien should be aliens so I will say aliens here okay and then we have one we don't need model anymore because when you say model that means we are working with session we are sending JSP here we are not sending JSP so we don't even need a model but we want to fetch data right and we have done that before we wanted repo as well uh if you remember we also need a alien repo which will fetch data from database let's copy that as well okay so once you got your alien repo let's fetch data so I will simply say repo dot we want to fetch all the aliens I will say find all as simple as that but this will return you the list of aliens now we have a choice you can save that in the list or you can simply write returns report. find all that's your choice I will go step by step I will say least aliens let fetch aliens here say control space and control space for this as well okay once you got all the aliens here we are not supposed to specify the page name because we are not using JSP so let's specify aliens but will this work think about this this alien this aliens is a list object we are sending data to string we don't want that right so what I will do here is I will convert this into two string let's try let's see what happens now once we got this let's run this application and let's see what happens so let's relaunch the application here and go back to postman and here you will send request here and we got an error can you see that it says again not found that's weird we have a mapping for aliens right but look at this output it is giving you the message which is all the aliens that's weird we got all the aliens but still it says error the thing is that's how your springes build right spring MVC says whatever you return I will search the JSP with this name because that's how we we have configured the application if you see our application do properties here also we have said that whatever name you will send I will simply add JSP and we'll search that name in views so whatever you're sending here is actually your JSP name we have to mention hey this is not a JSP name which I'm sending I'm sending actual data the way you can specify that you are sending actual data is by using another annotation which is response body so when you say response body here it will send that data as a data not GSP name let's relaunch the application go back to postman and here you will say send can you see that we got our data but with small error okay it's not actually the error but we wanted Json right we we wanted XML but we got a plain text this is not Json we want data in Jason format if you want to achieve that if you want to send data in Jason format don't say two string because when you say two string it will convert your data into string we don't want that so we'll send aliens and here as well we'll not send string let's return the list as it is now you might be wondering will it work that is yes it will work let's try let's relaunch the application and here if I say send you can see that we got about data in Jon format that's what we wanted right we got Jason so that's how we can have a get request now what about post request what if you want to fetch one particular alien that will say in the next video so we have done with the first step of rest where we were able to fetch all the aliens and that to with a URI so if you want to see this is a URI which we are using you so we are saying local colon 880 aliens so when I say all the aliens when I click on send it will send a get request to the server and from a server side we are returning all the aliens but that to a Jason format the amazing thing is we are not using Json anywhere I mean as a programmer we are not converting our Java list into adjacent and that's where your spring boot provides you this amazing feature so if you can see your M dependencies we have a converter so you can see we already have a converter here which is Jackson so Jackson is responsible ble to convert your Java data into Json now since it is inbu so we don't have to change the things by ourself it is that easy now what if you want to return XML can we do that with Jackson that will be one question but then at least the Json is working and that is happening with the help of Jackson here if you just go to Google and if you search for Jackson so we are searching for Jackson documentation and that's your official website of it so they have it on GitHub let's go there okay so Jackson basically convert your Java data into Json so you can see it has Json pass in that we have Jason generator so Json using Json generator you can generate Json files and vice versa as well so what if from a client side you receive a Json so it will convert that data into Java object that's how you can use Jackson and in fact you don't have to add in the project will be done by Spring boot now once we know how to fetch all the values in Jon format and that with the help of Jackson now I want to fetch one particular record normally what we do is in rest API so when we create our uis what makes much more sense is let's say I want to fetch one particular alien here which is mayure if you want to fetch mayure we will be saying 104 and the way you do that is by saying first of all this is your actual website in fact you we can replace this with the website name as alien now this alien will accept a particular Aid so I will say a ID here now this has to be mapped so have to say get mapping since we are using get here and then we'll be having mapping like alien but then we have to also mention 104 here right because that's how you are mapping so we have to say 104 and here we have to return the alien so the way you can do that is by saying alien I want to fetch first fetch the alien alien equal to using repo so you can say repo Dot get one so get one is a method which belongs to JP repository which will give you one particular alien and let's pass a ID here and I guess our job is done we can simply return alien but do you think it will work what is your first reaction now see it makes sense because whatever we have done is almost same with this method we are missing one thing we have to also say request response body before the method but we have one problem so this might work for 104 what if you have 103 what if you have 101 okay we have to make it variable right you can't simply say 104 here because it will only work for 104 in fact you know let's try this for 104 once and let's not accept or maybe I have to accept it so it will give you eror because we are uh expecting something here and we are not passing it let's see what data you get so let's go back to the postman and let's say hit and you can see we got an error it is your status 500 and it says optional a parameter a ID is present in the parameter but it cannot be translated to a null because we are not passing the values so path is there's no 44 this time but then we have to assign the Val to here right so this 104 need to be assigned to a ID so what we to do is since this 104 will change let's put that in a cudly bracket so you have to say a ID so that's how we work so if you think the URL will be different it will be alien for sure slash it will be some number we are not sure about the number so we can simply put that in cly backets a ID and here we have to use an annotation which is path variable now let's restart the application and let's see what happens let's go back here click on send and we got an error okay I guess we have missed something here so we have to also mention what is the name which you're trying to map here so you can see I'm mapping this Aid with this particular Aid so we have to use path variable Aid here and now let's relaunch the application I hope that was the issue so let's click on send and okay we are still getting same eror it says type definition eror so let's try find by ID because maybe that there's a conversion issue with get one okay so find by ID gives you a optional aliens that's what happens in Java 8 you know in Java 8 just to make sure that you don't get null values we get an optional value there so the only thing is you can do here is we can say to uh we can say or else and let's return the blank alien object so that we can say uh we have not received uh we don't have that alien in the database so we can say new alien and we can have a value which is let's say 0 comma double codes okay so this should work now I hope let's go back there let's send okay so it is working so that there was something wrong with get one okay so that's how you can do it so we can say find by ID what if this ID is not available in database and that's where you can pass blank value so what I've done is if I go back to my hit and let's say if I search for one Z we don't have this database so if you click on send you can see you will get empty data and you can say you know we have not found it so that's how you can work with one particular data okay so everything is working but now what if you want to send the data because till this point we have seen how to fetch data but now I want to send data so what we will be doing is if you see database we have four records here right we have four in fact we have five one6 is also there n so we have five records I want to insert a new alien and that to in from Postman so we don't have a form officially we don't have HTML page where you can send data we have to do that from this particular Postman instead of saying get we'll be using post now get is responsible to fetch data and post is responsible to send data to the server and the URL now since we cannot specify the URL by specifying the query string here so we'll not be doing that in post so we'll send request to alien itself okay but we have an issue don't you think we already have alien for get mapping the advantage with rest is you can have the same URI but with two different methods one is get one is post so get will be used for fetching data post will be used to send data and that's how you can differentiate between two methods okay we'll see that once we write the code will make much more sense so you can see we got we sending request with alien and when you click on send it will send a request but hold on we got an issue it says 404 why because we don't have a code on This Server right and then we are not even sending data how do we do that if you want to send data you have to click on body and here you can type the raw string you can have your own Json format here or we can actually use this form data in this form data you can mention key what you're sending so I'm sending Aid and the value for me is let's say one7 and I'm also sending a name and the value for me is let's say KL so I'm sending this two data 107 and KL here and that's it just click on send and it will send your data but yes we still don't have the mapping here let's do that quickly so here in the code we'll have one more method which returns same object alien and will'll have this method name as ADD alien now will it accept anything and the answer is yes we will be accepting an alien let's have this as alien itself let's return the same alien again but before doing that we have to add that data into the repositor so we will say repo dot the method which we use is save so we can say save and we can pass the particular alien our job will be done that's it oh okay not that's it we still have to do the mapping right uh so that will be done with the help of youit right so the way we have get mapping we also have post mapping so let's use it post mapping and here we'll say alien we don't have to specify anything after that no slash nothing because data will be coming in post so we can directly assign that to alien so you can see we have both as the same URI what is different is the method name so this is get and this is post so there will not be any confusion let's restart the application let's go back to postman and here click on send and you can see it worked at least we got our data here and there's no error let me just go back to database to verify let's hit refresh and you can see we got it we got 107 and KL see it's that easy Once you have everything set up it's very easy to add more methods and then doing stuff so that's how you can fetch all you can fetch one particular data you can also delete data we can also do that you can also update data with the help of put mapping but before that I also want to talk about the content negotiation example if you look at the code now this is your Json format what if you want XML you can see we are still getting Json format even if I XML in fact the idea is you can say I want all the aliens and you can hit a get query you can see you'll be getting all the aliens and even if you say XML you will still get Json so how will you get actual XML here the way you can do that is by going to headers and inside headers you have to say I will accept so you have to mention I will accept only XML so we'll say application SL XML hey I'm not expecting Json here I'm expecting a XML format by doing that you have to send a request now and it will return you XML but you can see this is not working and we also got a code here which is 406 not acceptable what's the issue it should return XML right but unfortunately what if I say okay it says cannot find acceptable representation so what is happening is by default in your code we only have a support for Json we don't have a support for XML and to get that support we have to add one more jar file what is that we'll see that in next video so let's do content negotiation here so when you say content negotiation it's quite simple from your client client can mention do we need XML or do you need Json in fact you can accept both that's your choice but here we mention something in fact in the last video we tried to fetch XML but then it's not working you can see we got an error the thing is by default your spring boot supports Jason and that's why you got Jackson Library here but if you want to get XML support you also need to add one more jar file now which is that so let's open our M repository and here let's search for Jackson data format XML so since we are using Jackson let's continue with that and then we are using XML converter now okay now make sure that you pick up the version which is suitable for your application so whatever version you have for uh normal Jackson so you can see it is 2.9 .8 let's pick up the same one or which is the latest one here uh let's copy that and go back to application we want to do that in a pom file let's do it here let's add it here paste and now you can see we'll have one more jar file for Jackson XML format so yes it will take some time to download and I guess it's done so let's restart the application go to postman so when we did last time we got an error which is 406 but this time let's see what happens and this time you can see we got all the values in XML format that's how you can do content negotiation I mean not just for fetching data but also for sending data you can also mention on the server side if you want to restrict what type of data you can return example you can just go to server side and here you can mention hey I don't want to return Json even if a client ask you for Json will not return that will only an XML you can do that here so here we have talked about get request we have talked about post request and the amazing thing is from a client side now you can actually mention what you need example if you go back to postman so let's say you're sending a request for alien so if you click on send you can see we got all the aliens here okay but we got it in the format of XML because we have mentioned here so if I remove this part and if you send a request you can see you got Json and if you say XML uh in fact you can request for XML as well so you can just go here and you can mention you want XML it will fetch XML but can you restrict it so let's say from a server side you can specify hey I don't want to return the Json I want to return only XML you can do that so here you can say path and then you can give a comma here and you can specify what exactly your server is going to return is it XML or is it Json so you can request whatever you want from a client side if you request for Json you will return you Json by default it will return you Json but if you specify hey I want to accept only XML then it will go for XML so if you click on send you can see it will return XML but what if I don't want client to request for Json so you can mention that here so you can go back here and say producers so we have a attribute as producers and you can in the curly brackets because you can specify multiple types here uh so you can specify that in this cly bracket so you can give a comma and you can you can specify that but here I want to accept only one so I will say application SL XML so this is the only thing I want to return let's see if this works so let me restart the application go back to postman and you can say send so you can see there's no error when you request for XML but what if you request for Json now the request for Json uh you can see we got the same eror which we were getting for XML so it says 46 not acceptable right so you can spe spe ify from a server side what you need or what you want to return you know it makes sense to return both XML and Json but sometime server has a choice you can specify hey let's not work with XML let's work with Jason hey let's not work with Jason let's work with XML so you can mention that with the help of prodes in the server code likewise you can also accept data from a client and in the post mapping you can specify what you want to accept we'll see that in next video so with the help of producers we can specify what a server is going to respond is it XML or Json or some other format but what if when you send data from a client to server with the help of force request let's go for post here and let's say I'm sending a new alien now and this time let's specify the body and let's specify the values as 108 and let's have a name as brain now if you specify the data here let's go back to to header now in header as well you can specify the data in different format in fact in body we are sending a form data okay but then we have not specified will it be Json or XML so you can go to header and you can specify here content type and if you say application let's say Json and instead of going for form let's go for raw so you can see in raw you can mention are you sending a Json or XML so let's send Jason first and the way you can send that first of all we don't have Jon format let's create one so we'll have cly brackets open the first attribute is Aid let's give a colon there and in double codes we can mention the value so the value let's say it is 108 we'll give a comma and here we have the name as a name and the name is Prine so now we want to send this data and we are sending ad Json data so in headers we have specified that we are sending Json the body type is also Json let's click on send and it work worked is it no we got an error what's wrong there's nothing wrong on the server side we got 204 let's verify from the database I guess I'm not sending the data properly let's verify let's go back here click on refresh oh it says null that's bad okay is there something wrong with the format let's use an anation here which is request body I guess that's the issue let's try once more and it worked okay now what was the issue so normally when you send data from the client to the server of course we have two ways we can use a form or we can send data in Json or XML now when you send data in Json or XML we need something who will convert the data into the Java object format and that's where you can use request body so if remember we have also worked with response body before so response body is responsible to convert your object into Json if you want to do reverse we have to use request body so that was the issue that's our desent format and we can send it so the this is working you can just go back to database we have added some two null values Let's ignore that and we can see if I say refresh we got the answer now this is working but what if on the server side I have to mention hey This Server will only accept Json because now this will accept both Json or XML or maybe I was I want to specify only XML so in that case uh you will specify so we'll say path equal to aliens and alien and then here we'll specify consumes in cly brackets in fact we have two options here we can spef ify in double codes the way we have done for producers otherwise we have an option of media type so we can say media type dot we can specify the type here we can say application so it depends what type of data you want to work with is it you want to work with this type where you have double codes or you can use this type I prefer go for the double codes part because it makes much more sense to me but that's your choice so you can say application.xml and then let's work with Json here so let's say it only consumes Json because we are doing XML let's consume Jon and let's restart the application and you can see if I give a new data let's say 109 and let's stick to PR now and let's click on send so it works there's no issue but what if you want to send XML will it accept that now first of all to get the XML I need to know the XML format okay so that's our thing so let's say copy this I just wanted the format so that's why I'm doing all this stuff so I'm sending post request now I want to specify the content type what I'm sending so I'm sending XML uh in the body I will mention the XML now so we'll say paste uh this is XML that's right but then the data which I want to send is 110 and the name will be let's say Asha and click on send so you can see we got the error it says not supported so content type application XML not supported because we are accepting Json here so that's how you can negotiate with the content that's how you can specify y uh what you want to accept and there's one important thing which we have done in this video is request body so if you want to convert a data from the adjacent to the Java object you use request body so that's from the content negotiation where we talked about producers and consumes so basically we are going to build a project in Java now see it's not about how uh big your project is or how complex your project is it's all about understanding how do you think when you make a project okay it's all about thinking it's all about uh working on something okay so we are going to build a project and this will be a Project based on a product so how do you manage a product now basically uh you know in my office there are so many things in fact when I was to when I used to work alone uh at my home or at my office I I had a small team okay so I was only one and then someone used to be there there to manage everything especially H was there and now since we got a good office we got uh lot of machines in the office now for different purpose for some for development some for videos uh some for editing and some used by uh the admin team right uh so there are so many things in fact you know I wanted to show you the entire walkth through of my office and so many things what happens you know at this point now uh there are certain things in my office which we use regularly example the laptops uh the sound system uh the AC remote these are frequently used things right but let's say there are certain things which we don't use often example cables the C Toc cable HDMI cables uh there there are one or two laptops which are lying in my uh in the drawer which I use sometime depend if I want to travel somewhere I want to carry a lightweight laptop or if I if we want to do some testing of some uh some software some service we use a different machine there right and those are there in the drawer now what happens is the moment I need something I will ask hush hey hush I need that particular thing and then it took it take normally 10 15 minutes for H to search in the end all the draws where that thing went and I thought let's solve the problem okay so hsh are we solving your problem today Yeah so basically let's create an application a console based application because we have not learned a GUI or web development till this point so let's create a console base application where we'll try to track the product so where that product exactly is is it in the drawer is it on the table uh and we can basically search a product where it is we can basically search for a particular type of product let's say I want to search the laptops in the office or in that particular drawer what other things are there so if you want to know and let me know if it happens with you as well at your home you know uh before before my marriage I used to keep things here there and then I used to find it very easily but after I got married now everything at my home is that exact place nail cutter should be here uh the the clothes should be here right and then since they are properly organized now I find things difficult to find yeah so let me know if it happens with you as well so that's what we are going to do in this session so what exactly we are trying to build here so let me share my screen okay I just need to log to my okay so basically we are going to build a product management application now in this application what we want to manage is products right now let's say if if a client comes to you by saying okay I want an application to manage the products now what you will do uh so basically you will have something like this right okay let me just hold on a minute okay so uh if if a user comes to you by saying okay I want a product based application right now what you do is especially in language like Java and when you are working with the database Concepts you need to basically create entities now what are entities if you heard about Java thing in Java is object right so if you want to imagine something always imagine them in terms of object now if you talk about a product even product is object right so a product will have certain things so example if I talk about a product here now this is one of my entity uh product yeah this is my entity basically okay let me just write in bracket this is my entity right and for this entity of course every object or the entity will have some properties of course it will also have Behavior here example if you talk about your computer that's that behaves some way right uh in the same way if you talk about uh let's say something like a camera camera also behaves but it also has a property right so if we talk about a product it will have certain properties now what properties I want of course uh we can have different properties to a product but just to classify for this particular example uh let's say we have a name for a product so a product will have a name a product will have a a type of product let's say if we talk about a my machines those are computers right the Mac Studio which I'm using here is a computer if you talk about cables the type type c cable that's a cable right uh we can also have something like uh a mouse example if I'm using this particular Mouse here which is Apple Mouse now this is a mouse yeah so that's a type next we can have is uh the place where it is stored okay so example if I want to find something I want to find where is the Asus uh laptop Asus we book basically that's one of my old machine which I was using but it is now used for the development purpose so if I want to find where that machine is I should be able to find it right it can be on different tables it can be in different draws uh so I need a place and also uh I also want a warranty you know this is the problem which you face when you have multiple device when you have only one or two device with you you actually remember okay for this particular mobile phone I have this warranty for this particular laptop I have this warranty but let's say if I have so many machines in my Mach in my office I want to know if I go for a product I I want to know the warranty of it so that I can claim it what if uh what if the laptop is not working at least I will know if the if it's under warranty or not okay uh yeah we can also have color we can also have the owner who owns that particular product right yeah makes sense right we can have all this properties but just to keep it simple let's work with this for and then you can also mention uh when you do this project you can also add some extra things to it so that's my entity right okay now if you want to implement this in programming how will do it so let's get back to our code uh this is our coding screen right and in this particular coding screen what we're going to do is uh we will write the code so that you can achieve uh the thing you can manage the products right okay so what I will do is I will go back here and this is the IDE which I'm using and one of the question which I got yesterday was about uh which IDE to use and the thing is you can use any particular ID which you want uh you want to use uh intell that's your choice inj idea uh you can use Eclipse you can use net beans you can use vs code your choice okay ad is not something which should you just stick to I use different IDs on different machines uh if I want to make a YouTube video I normally go for vs code because that's very lightweight and most of you are already using it so if I want to build a project normally I use intellig idea to build to build a project because it gives you little different features yeah and so again you can use any IDE so what I will do now is I got a project which is product uh product demo and in this I have this SRC file which is at empty at this point uh let's create our first file and this will be a file of main now why it is main is because the execution will start from this particular class and of course when I say this is a main class I also need a main method because I want to start the execution right that's something we will do here so let's say if I want to manage a product so how will you do it of course you can create variables right we have mentioned all the variables here if you can see we have a name we have a type we have a place we have warranty now this can be your variables as well so I can uh simply come back here and I can say string uh product or maybe I can say string name one since we have different products we can have name one and I can have my first product as Asus uh Vivo book which is the laptop and then I can say string uh type one is equal to what is this type this is a this is particular laptop and then I can say string uh Place one and this is is sitting on a brown table so basically in our office we have colorful tables I'm using a white table uh the the table which I use for the development purpose that's a black table uh the development team uh developer sets on the brown table right and editing team I'm not sure which it's also Brown yeah okay so is it white white and brown white and brown Okay cool so I can say a brown table right and then I can say string uh what is warranty warranty one and I can say this is uh okay warranty should be integer right so let's say this is 2024 or 2022 I guess the warant is already over so we can have this values here okay this is not string this is int yeah and then for a different product now this is one product right what is a different product now we can have what do you think if I want to have a different let's say a mouse how do you specify that of course I can just simply copy this and paste it here and this will be product two or name two uh type two place two and warranty to of course you can change the value to uh Apple Mouse and then M type is Mouse uh where it is sitting it is sitting on the white table and then uh the warranty you can mention that so do you think this is a good idea to have different variables like this or we can create a separate class where you can have uh the types and instead of having four types here we can have one type which is product of course right in object un programming it's all about uh creating a object or a class right so if you want to create an object you create a class first so in Java basically if you want an object you create a class so I will right click here and I will say new class and I will I will name this class as a product which also represents an entity okay um so and I forgot to do one more thing so today we are focusing on uh building this product as a console based application so because whatever output you will get is on the console but of course in this current ERA we don't do that right what we want is a web application a guy or some some way to interact with the application so what we'll do is today we are going to build this on a console tomorrow we'll build this on the uh tomorrow we just try to connect with database because today we are not going to use database we are going to Simply have the data somewhere in the application itself tomorrow let's convert that into a database and then uh if we have enough time we can also create a server to it okay so yeah so we can have a website development so we will shift the entire application to a normal application to a web application okay so here what we can do is we can create those variables so what are the variables we have we have name type uh place and warranty so I can simply come back here I can say private string uh name and then we can have private why I'm using private is because in Java we have concept of encapsulation uh so all your variables or properties which you say should be private and the only way to access them so what is the only way to access them so the only way to access them is with the help of gets and sets so I can say string uh name then we have string type then private string place and private paint uh warranty see for warranty we can actually go for exact date as well but then just to keep it simple let's go for the year only okay so you can see we got a class here which is public and the name is product and we have four variables and if you want to if you want to follow the concept of object oriented programming we also need a Constructor so maybe I want to create an object by passing all these values and maybe I want to create an object by not passing any value so I need a Constructor so I can you know you don't have to type the Constructor you can simply uh right click here and you can say generate and you can click on I want a Constructor so if you want a default Constructor you can say select none and you can you you got a Constructor but I also want a Constructor which can take parameters so I can say generate uh Constructor with all the variables and click on okay and you can see we got a Constructor with all the variables uh next we can we also need a Getters and Setters so basically these are the methods using which you can assign the values and fetch the value uh because all these things are private right so the only way to access them outside this class because see this is a different class Min is a different class so if I want to access them I can simply create gets and Setter so I will say generate get a Setter for all the variables and okay right our job is done okay if I go back to main now I don't want to assign values like this right so what I can simply do is I can uh come back here and I can create an object of product I can say product p is equal to new product so we got uh the object and then I can simply print this object so tell me uh yeah of course AKA says we can use here Lok as well so let me try to do Lok tomorrow so once uh this is over because this will take a lot of time uh tomorrow we'll try to do long walk as well so if I try to print this what what will be the output of course let me just run this code to see what happens we can just click here say run main so you can see it is printing something weird uh it's printing some hash values and uh the name of the class we don't want it right we want to print the values so whatever value this P has the product has I want to print it and to achieve that we can use something called uh a two string method so in Java if you want to print an object and if you want to print the values of it you can just right click here and say generate two string for all these values and now what we doing is every time you try to print the object or reference it will call the two string method and it will print something something like this so let me try I will just run this code once again and you can see uh we got the object and the output is something like this you know what I will do okay let's keep it there itself so you can see it is printing the product name is null type is null place is null warranty is zero is because we're taking the default values so basically when I create this product I should add some values here right so we can do that okay but let's do that differently let's say I have some products with me and I want to add them now we can do that in main as well but then there's a in software development there's a concept of Delegation what you do is you don't do everything by yourself in fact let's say if you're working for a big company and you know we always have these questions if you let me know if you're working on a company and you feel that you do all the work and your managers manager is doing nothing do you have the same uh thoughts so if you feel your manager is doing nothing then you're wrong is because okay there are some managers who does who does nothing but then managing people is very difficult task in fact in my office if even if I have less number of employees you know it it it consumes a lot of my energy just to manage the team just to manage the work uh but then we have this idea of Delegation right so from the upper management if there's a decision made it should be assign it will be assigned to the lower people who are lower on that uh tree and then the this people who are managers assign the work to the people who are actually working example developers testers uh admin people right so uh that's how you do it right so we we have thearchy so that's delegation so we delegate work in the same way if you want to do all those things we want to add the products we want to search a product I don't want to write all the logic on Main so main should be simply saying Okay add this product main says give me the product the actual logic will be somewhere else and that's where where we'll write the actual Logic the actual logic of all this thing will be going in a separate in a separate file so I will right click here uh on source and it will say create a new class class and I will call this class as a product service okay now this class is responsible to do all the things so logic should be here of course you can write the logic in main but then let's follow the concept of Delegation here so let's delegate the work how do you do it how do you delegate a work so of course I want this to do everything right uh so what I what I will do is I will say create the object of product service here so product service Service uh let's name this as a service itself equal to new product service yeah we can also say this is a service layer so when you move to the web development or Enterprise development thing this is called a service right a service layer okay now to this service I will say hey service I will say hey service add the products okay uh how will you do it so I can simply say service dot add product so I want service to give me this option of adding a product but service say unfortunately I don't have uh any method in this class which which says add product so what we can do is we can go back to service and let's create a method so public void add product and in this it will add the product but then where is the product and which to where to add now basically since we have multiple products right we don't have one product in my office we have I don't know let's we'll calculate one day how many items we have there are so many items which are which I think is of no use there is a air purify air freshener sitting there okay we have used it from a long last time we have used air air fresh now maybe last month so uh so let's say we have so many items in the office right so many products how do you store them where do you store them of course it should be stored in a database but then in session we're not talking about it so one place where where you can store all the products is a list so we can create a list of products now people who are finding this difficult to understand see we are basically building project right I'm not teaching you basic Java here if you're still confused there's an entire playlist on Java so I can say list I want a list here control thing yeah okay uh so we have to also import the package the list package or List class list interface from this package and let me name this as products equal to and I can say new okay now list is a interface so the class is at a list so we got a list of products here okay so I will add the product in this uh thing so how do I add it's very simple get the products and in this now since product products is a list here right so we can have we have so many methods there so we got products. add and we can add a product but which product we can't simply say add product and it will add we have to basically pass the object of a product so here in this add product I have to pass the object so what I can do is I can say product p is equal to new product and this product will have some values now what values I want uh the first one I want is uh let's save uh Asus let's go for that Asus Vivo book and this goes in a type this is of type laptop the next thing we have to mention is okay we got yeah place where it is where it is set so it's on the brown table and what's the warranty is 2022 so if you can see this is the product which you have created now once you got this product object I can simply pass it here so what I'm passing is I'm I'm passing this P so if you can see I'm passing a product refence reference to this add product now the same product will go to product service and we have to accept it here so I will say product P again and just add a p our job is done does it make sense now okay so we have added one product but then we have so many products right uh so we have to create those many number of objects here and add it right so we can do that uh just to uh simplify the work what I've did is I've created this Sublime file okay it's not opening Yeah so basically I have okay before doing that one I can do one more thing so don't you think of instead of adding P here we can simply take the entire thing from here and paste here so we don't have to create a reference we can simply create the object with the help of new keyword and add it to the add product does it make sense and since we have so many products here what we can do is we can I can simply copy the entire stuff and replace with this okay it will save our time right so I've added all the products and you can see we got all the products here now if you try to get this list this list will have this number of products I'm not sure how many products we have 15 maybe yeah these are the anonymous object so what are the things we have we have a type c which is a cable uh which is there in the black drawer we have a Mac Studio which is a computer sitting on the white table we got this mixer the audio mixer which is there it's audio system white table then we got VI book which is laptop brown brown drawer then we got Asus Rog machine laptop it's on black table then we got MacBook Pro laptop brown drawer the vacum pad which where we can write it's a writing pad black drawer and then Le goes on you can see we have uh keyboard Logitech keyboard HDMI cable Java blackbook uh Logitech mouse Apple Mouse Lenova Mouse and blackb now this blackb is basically a machine which is used for the editors editing machine uh we we have named name it is a black based it's a computer on a white table okay so cool does does it make sense we got all the products here now let's say once I got all the products I want to uh let's say I want to print all the products how will you do that how will you print all the products it's very simple you can say hey service hey service get all products is as simple as that okay if you want something just ask for it we are saying service get me all the products but Services I have no idea what I'm talking about okay so if I if you want to do that go back to product and here create a method which will give you all the products right but my question is when you are returning all the products what should be return return type when you return all the products what should be the return type so the return return type is list of product right we are not returning one product we're rning all the products so we have to say public uh list product and then we can say method name is get all products and then we have to return here now what we want to return so where do we have all the products in this products here right so simply return products our job is done right so I can just go back to main and here now you can see there's no error it is just a warning now since get all products will give you the list of products what you can do is you can accept it here so I can say I'm getting a list of product and we'll name it as products so basically get all products will give you the list of products does it make sense now once we got this list how do you print it there are multiple ways of printing a list you can simply say s out and you can say products and it will print all the products let's try let's try so I will just run this and let's see what it prints it prints the list so you can see this is the first product and if I have to scroll this is my second product and if I scroll this is my third product and scroll and this goes on but instead of printing that in one line I want to print it one by one so what we can do is we can use a loop here and specifically can we can use enhance for just write all these terms okay so if you're not sure what I'm talking about if you want to watch the video later uh the concept video so example let's say if you don't know what is enhanced for Loop just write it down after this session I want to learn about enhanced for Loop then after this session just go to YouTube and search for enhanced for Loop and uh you will get my video there watch it and hit that like button okay here also and in that video okay so I will say product this is how you use enhan for Loop so I will say product P from products so what it Bas basically does is this product will have lot of different values right a lot of different products it will pick up one product at a time and that particular one product will be there in P just print it so take one product and print it and let's run this code and let's see if it is printing one by one yeah you can see that it is printing one by one now this are all the items we have so basically we were able to uh save the product we were able to add the products here right this number of products there should be a way to you know minimize it uh yes in this 10 days we are also going to see examples of hybernate okay cool uh so once we have printed everything uh what next what I want here is let me just print uh not so to let me print the line here so that we can divide the output right so this is the first output which we got all the products next what I want is I want to get let's say a specific product not all the products but a specific product okay so let's say out of this I want to see where is the Logitech mouse okay I want this particular product it's very simple what you can do is you can just go back here and say hey service let me also print what we are printing so we are printing a particular product okay so you can go back to your service by saying hey service get me a product get me a product whose name is so I'll say whose name is uh logi Mouse will this work what do you think will this work um so if I go back to service we don't have that method right so what we can do is we can create a method so I can say uh public now this time it will return only one product so I can say public product get product which will return one product right but then it will also take a name which product I want to fetch so I can simply say string name right but the question is how will I get that how will I get that one particular product what do you think okay so basically from this particular Leist of products I want to find one product which name matches with this so if you want to search and that's why the searching algorithm come to picture right but we don't will not do that we'll do a linear search okay so what I will do is I will just go for a for Loop and I want to iterate between all the products so I can say product P from products so iterate between all the elements and check if a particular product which we got got get the name of it so whatever name you get from this list of products compare with the name which you got here how do you how do you compare two strings in Java you do that with the help of equals so equals and you will say okay not a name but a name right and if you find the product simply return it the job is done right or you can simply return not inside for Lo if you don't find anything return null of course not a good way but then yeah just to get the just get getting rid of the error we can say return null otherwise you can return the empty object if you want okay um and then you can see get product there's no error and of course it is returning a product so I will simply say product p is equal to this and then here we can print the product let's see if this works let's run and you can see we are searching for a Logitech mouse this is the output which we got here because of this in fact you know what we can do we can commment this section which we don't want now let's once run once again and you can see this is what we wanted we want a Logitech mouse okay and yeah now you'll be thinking in the code we have to mention Logitech mouse what if you want to ask the user yes you can can do that you can take the input from the user and if you don't want console you can build this as a web application that's your choice okay so this makes sense right okay so what we wanted now is uh searching with a particular place let's say okay in fact that will be your assignment okay that's your first assignment thing which you do so there are two things which you have to do as assignment for this particular project which is what if someone wants to search based on the uh Place let's say I want to I want to find all the things at this particular place let's say on a white table I want to know all the things I want to know all the things which are there in the draw uh so that's your assignment first one the second assignment question you have is I want to see what are the products which are out of warranty example all the products which are which has a older date if you can see we got 2022 here 22 done 2020 all this warranty is completed right right so I can't I can't uh get it solved now or repaired from the company for free right so I want to know all the things which are out of warranty so these are the two assignment question at this point uh I will mention that at the end of the session as well but two assignment questions one find all the products in a particular place and find all the products in U uh which are out of warranty uh now what next we want to do is we want to search for a particular name example the moment I type black so it should say black based it should show black book it should show Vivo book oh sorry I'm searching for black right so it's black B black book black table anything which is there on the black table so it should print all the black stuff so how do you do that uh so basically I'm searching with the text right so I just want this line copy and paste and we are searching for for we searching for a particular product no we are searching for a particular text and next I will say okay so when I say give me all the products which has black in it so will I get one product or I will I get list of products of course right list of products so I will say list of product that's what I will get and I will say this is prods I will get all the prods and I will say hey service get uh products with text and I will mention the text here so text is what I want to search based on a word like black uh okay but unfortunately we don't have this uh this word right so what do you think what can be done here okay I can get some yeah good good job so there there's option of subring we can do that uh yeah we can convert any into upper case and lower case okay we'll do that so what I will do is I don't have that method so let me click here in fact you can also click here you can say create method get product with text and you can see we got the method here but at this point the method is not doing anything so basically I want a list of products here because that's what I want to return so say products equal to new at the list okay and at the end I want to return this return products okay so let's let's run this and let's see what happens of course I want to print all the values I will just use this forlo which is already mentioned here it's not copied copy paste okay this is not copy pasting right this is code reuse what we say from prods prods yeah prods okay so basically let me just name this as a product because we're already using p somewhere we are using p here in fact you know what we can do we can also actually comment the entire section here so that we can focus only on with a product with a text okay so will this work let's try if I run this code of course you will not get any data here you know why we got empty is because the list is empty we want to update this list from the all the products we have so so we got all the products here from that product I want to fetch few products in fact you know I will just name this as prods here so that we can have different names plots okay so let's add here but how will you add of course we want to use a for Loop here because we want to iterate right uh so what I can do is I can use a for Loop and I want to iterate between the products I can say product p is equal to products so I want to iterate between all the products and find and add only those products in this list which has a black in it it can be with a black in our name black in a type black in the uh place right so how do we do that so it's very simple you can simply use a if condition here and you can say p do get name do we have uh how do you do it can I use substring that's one option or if you talk about a string it also has one more method called contains you can see that we got contains because we don't want a string we we just want to check if the text is there if it contains the word okay why is black here it should be uh text whatever text you're searching if that text is here or check in the place so I will say get or maybe type check in a type does type contains this text or does my place contains this text contains the text so basically I'm checking everywhere I'm checking in the name I'm checking in the type I'm checking in the place as well right if it is there I will add this particular product in the prods so prods do add p and of course you might have to rewatch this session right by yourself because when you're just watching on the go it's difficult to remember everything right so yeah before we go ahead hit that like button everyone come on sometime you have to keep YouTube Happy you know YouTube how YouTube promotes the video they basically see the number of likes they see they see the engagement they see the comments uh so do that let make YouTube Happy YouTube will make me happy and that's how it works that's the EOS that's how the ecosystem works why okay i' okay there's a equal to here there should be a colon my bad okay cool uh I think this should work now let's see if this works I will just go back to my main or I can just stay stay here run okay still not working I think the problem is when you say black exactly this text black and if you see the products we don't have any black with small B you can see all are capital capital B B Block uh B here okay that will not work so what we can do is we can actually convert this into a lower case so whatever text we are getting I will simply say string s Str and so if you want to compare two strings it's always better to have in a lowercase so I will say text Dot to lowercase so and then don't compare with text compare with string because that's a lower case and not just this you have to also convert this G get name I whatever get whatever name you get here whatever type you get here also into lower text so what you can do is you can create some local variables here for the name in fact not here inside the loop because the value will change every time so I can say string name and this name will be P.G name and then we want a lowercase of it so dot to lower case that's how you compare basically then we got string type is equal to p. get uh type to lowercase and again string uh place is equal to p. get place. to lowercase and now instead of comparing with the data which you get from the product just compare with the variables which are already converted into a lower case you got the point right what is happening here and how do you solve that yeah so basically you have to converted into a lower casee or upper case it should be common case let's see if this works now yep it worked can see that we got all the products which has something black you can see the place is black black drawer uh here it's also black table uh wcom is in Black drawer Logitech keyboard is in black table uh HDMI cable is in Black drawer Java black book Black Book then we got black table black drawer and black Bas it worked so we are going to enhance this product more uh in terms of connecting to a database in terms of connecting to making this as a web application and then the way we have buil this product we don't have to change much okay and the class this class will be used as it is you know you will see that once we start uh doing this okay what else we can do here so assignments for you let me just write down the assignments on my iPad so that you will remember the assignments uh okay not sharing my iPad Okay so assignment the first one you have to search by place okay that's one uh next is okay what what are the next thing you have to search by warranty search products out of warranty so the first assignment is search by play second is search product out of warranty next next you need to implement a stream API here okay so if you if you see the uh the thing which we have done here let me show you that once again the thing which we have done here searching it with this this is basically uh a normal iteration right I want you to convert this into a stream API convert this code into stream API how will you do that so on my channel uh there is a video for stream API watch it you will understand how to use stream API right and Implement that so that's your third assignment so this is the work which you have to complete before the next session yeah and I I I love to see your post by by saying this is all about learning it's not about MacBook and that's what that's what we want okay because see in your life if you know if you know something if you master something and if you're consistent you will get thousands of MacBooks it's not just about one okay it's of no use if you get a if you get a Macbook without knowledge and I have learned that in a hard way so basically in my entire career I'm never focused on how much money I earn how many gadgets I have which car I drive it's more about can I what what I'm learning every day if I don't learn something every day I feel I'm not done enough okay so do that okay focus on learning not on the things cool so what's the agenda for today the agenda is very simple we are working on a project which is a product uh so basically we are managing the products which I have in my office and for that we were able to add the products in the list and then we were able to fetch it we were able to search it and I have seen your assignments uh you know I don't know I never thought I can make such a bigger impact in just it all started with a small thought of giving giving away then we got a thought hey can we just do a challenge of 10 days and now look look look at your response on different platforms it's amazing uh every time I open LinkedIn now I get tons of uh notifications and then in the entire day I'm actually spending now two to three hours just checking your messages and uh notifications in uh in LinkedIn and that's quite fun so it was it was an amazing experience uh looking at your uh messages and notifications and and one thing you can do if you want to improve your rich on LinkedIn because it's a very good platform to kind create networks create connections uh getting a job so what you can do is uh when you are posting something try to make it more interesting uh maybe by putting an image of what you're doing example I saw one of some of the post uh let's say if you talk about Pascal triangle so having a pascal triangle in your LinkedIn post as a photo will give you a better reach because when people scroll on LinkedIn they get excited looking at the images not the just the post and when you start with your post or the article or not the article but the post on LinkedIn uh the first line is very important that's how you can hook users there or your connections okay so let's talk about what we are going to do today it's very simple basically we are going to focus on a project which we are doing and then we'll convert that project into spring project now we will introduce a framework you know let me give you my experience when I started my programming journey and then when I looked at uh different uh uh Frameworks in fact I started my career with Java then I worked on Spring swing uh tool which is the GUI Tool uh then I moved towards uh web development Sate and jsps and I was very happy but then someone suggested hey you know instead of working on sub JSP should work on STS framework and when the when I when first time when I heard about a framework or a tool I was like I I'm happy you know it's it's all working here why you have to learn a different framework to do the same thing of course the project which you build in without using a framework is same as when you you when you build a project using a framework it's almost same right the output will be almost same then why to learn a new framework and do it it's because framework will reduce your work of course you have to learn something but with less efforts you will get more and that's what you get from the Frameworks and today after this session I'm sure you're not going to learn spring framework because it's very difficult to teach spring framework in one hour are but you will realize the power of spring you know lot of magical things are happening behind the scene and you will love it okay so yeah you're going to learn about the POG as well which is a database I wanted to go for some uh database like my skill but then you might have worked on my skill I wanted to give you a new flavor and that's why we have po okay so let me share my screen and uh let's try to understand what is a problem here so this is the this is the this is what we are building here right so if you can see uh we have our code which is the main file uh in which you have basically what we have is we want to add products what now what is a product here so if you talk about a product class it has uh four private variables we have name type place and warranty uh example let's say a product can be anything uh example here if you see we have a product which is typec cable which is a type of cable uh and place his black drawer this is the warranty I'm not sure if you do okay we don't get the warranty for uh cables but it's it's just that uh then we got Mac Studio which is a computer on white table and that's a warranty so basically what we did is we created different products and we have added all the products in the list and for that we have used a product service class and then uh in the product service class we have a list of uh elements here list of products and then we can add the product we can uh get a single product by a name we can get get the product by a text we can okay we I have also given you some assignment on warranty and stuff I hope you have done that okay this this is what we have done right and we were able to add the products we were able to fetch the products okay but then what is a problem we we are trying to solve here so before that let's understand the architecture of the project which we already have uh so we have a main file right so this is this is your main file and then uh if you want to add a product basically we have have a product service class which is doing all the logical part so from the main basically we are saying add product right but then how exactly product will be added the logic is maintained here so logic is there inside the product service now how to get the product so again we are just calling it right from the main we are just calling it and the logic is mentioned in the uh product service and then how do you represent a single product we don't want to use variables and that's why we are uh basically using uh a class called Product now this product class represents an entity for you right this will have a name and uh name Place type and warranty and basically in the product service what you have is now since we have multiple products we have to store it somewhere and where we are storing them is in the list so basically you have a list and inside this list you are storing all the products the problem with this is it's a very common problem where uh every time you run this application so whatever application you have here every time you run this basically you are creating a new list and then you are initializing the list with this values and then you are fetching value right I don't want to do this I don't want to uh every time I run the application it should add the list like this ultimately we are working with a code right example if you have a new product basically we have to change the code here we don't want to do that so where do where do you store data because at the the moment I close this application I will lose all the data right and to store this data we have to store it somewhere and that's why uh in this particular project what we going to do is we will be using a database here okay now this is your database and currently we are using postgress uh SQL which is one one of the most amazing open source project open source database okay I think you can you cannot see that that's weird yeah basically we are continuing with the extra Cod so we will be expanding it yeah and this is what I was talking about uh Wades has mentioned an important thing here uh which is code looks difficult uh yesterday but after doing it by myself it seems easy now that's that's important you have to try The Code by yourself because when I type I have a clear picture in my mind because I'm typing it I know what how the flow going but if when you watch a video or when you watch it live uh sometime you're not clear what is happening and that's why we have to write The Code by your yourself so basically what we are doing is we are creating a database now and we want to store this data in the database so basically whatever list you had here uh this list this list instead of storing it in a list we have to store that in the database directly now we can directly write the code so question arise how do you write a code for database some we have to use something called J DBC now jdbc stands for Java database connectivity right so we need to use jdbc here now the problem is if you want to use jdbc you have to write some steps to work with jdbc and the question is where you will write the steps and that's where we have to use something called a layer so don't don't write your DC code inside service service is meant for logic processing logic if you want to work with database what you can do is you can create another class here called Product uh product DB now this class is responsible to communicate with the database cool so that's what we are going to implement here I just need to check if everything is proper in my machine just a second everyone yeah basically we have to get a different class right so what I'm going to do is in this code basically uh we are going to write database connectivity database connections and to achieve that uh we have to make some changes now what kind of changes we want here uh the changes are very simple if you want to connect to a database of course first of all we need a database here right now if you want to work with database what you can do is uh first of all we start with jdbc part uh if you go back to okay so in my machine I have installed something called a postgis so you have to install postgress first now how do you install po very simple uh so go to browser and search for POG this is where you can download it and Pog and myql is almost similar the difference is post is being open source now and my is a part of Oracle it's also open source I I guess there's one version of Masco which is open source I'm not sure but uh if you totally want to use the the um the open source version I would prefer to go for post you can see you will also get the source code of it not needed but anyway so you can download the post in your machine and uh once you install it it's very simple to install it you just have to set the password you will get something called uh PG admin so once you install post you will get PG admin which is postgis admin and you can use myc if you want in your assignment uh so you have to set the password I have set the password which is 0 so 4 * 0 and click on okay now this is how your post gist database will look like or post gist admin will look like and in this you will have an option of server so if you expand server okay just asking me for the password enter yeah so this is how your post will look like I know this looks some fancy tool you know you can see all the dashboard uh the benchmark what is happening with your system U and if you can see we have databases here by default you will get this post G database test is what I have created uh but you can use po and if you want to create a database which we are going to use what we can do is we can right click here and we can say create database and what database name I want to have here is uh Tesco so that's my database name if you want to see the SQL query for this you can click on SQL and that's the query so create database teliscope uh we don't need all these things but then owner is postgress doesn't matter click on Save and you can see you got your database here so this is your post GD the database which you created here and in this if you look at schemas now this is where your table you'll be having your tables uh so I'm assuming that you know the concept of uh SQL database tables okay otherwise we have to take next six months to talk about SQL it's a huge topic but yeah we got we got the database with teliscope and here you can see we have tables and by default it is empty okay I want to create one table here so what you can do is you can right click here and you can click on create table and what table I want so my table should be a product table okay so we want a product table and in this we want to okay so we want a product table and how many columns you want so basically you can just click on the column here and you can create the columns so the First Column which I want is ID so I know we don't have the ID in our remember when we started the when we started talking about our project of product management I've mentioned that your product is actually an entity right so every entity will have one table so let's say in your project you have five entities every entity will have a table right uh so in the table basically you need to have your uh primary key so every table will have a primary key so in this case I'm getting a primary key here see name cannot be a primary key because I can have two laptops with the same name yeah possible right okay so I have ID uh what is a type of it I want the type to be uh integer do we have integer here yeah we want integer of type lens scale doesn't matter this should be a primary key I also want this to be uh to have a generated values I don't want to generate value by myself so every time I enter a new product it should give it a new ID 1 2 3 and the way you can do that is I'm using pus after a long time so there should be an option of uh identity generated no identity identity always increment by one start by one and that's it I think it should work let's see the SQL query for this uh generated as always as entity uh identity okay increment one start one cool if I click on Save done so you can see we got table here but I want to check if this table works what I can do is I can just go to the query tool where I can write the SQL queries so this is a place where you can write SQL query I know I'm going bit fast but you have to rewatch this session this this is not about teaching you uh SQL this is more about how do use how do we build a project right so what I will do is I will first of all say select star from product let's see if you have any product there uh it's empty uh relation product does not exist is it I created this table is it case sensitive from when no something is not working so I'll say use theis uh select Stu from product is okay something is not working what's wrong I've clicked on this I'm using this database let me just refresh it once and run okay something is weird how about this if I say this belongs to Tesco this is not working columns is there okay we got only one column oh I forgot to enter all the different columns here you know what I will do I will just drop this and let's create this table by query so I will say create table product uh the product should have an ID and if you want to have a SQL query you can also mention uh serial okay you have to mention this of type integer serial if you want the auto generated and it should be a primary key and as dbms changes this also this also changes yeah now table is not teliscope database is Tesco and yeah I'm in the right thing doesn't matter and then we can say name is name is text I think uh po supports text not string or vcad so we got name uh then we got type which is also a text then we got place which is also a text text text and then we got what we have one more thing which is warranty yeah so warranty which is of type integer let's run this and let's see what happens okay did I mention the wrong query do don't have to mention integer is it so it says near integer okay yeah it worked okay so we should not mention integer if you mentioning Cal because it is uh see is also a type okay cool uh we have created this table I will just say now let's say let's let's say if this works select star from uh product I I want this to be empty yeah you can see it is empty now so we got a table and this is auto increment now this will increment automatically I will not even insert this but then I want to do that from the Java code now so if you can see I'm adding a product here but if you see we have we are adding all the products and then in the service we are adding in the list but what we are going to do is we'll not be doing the changes in this particular file the problem is when you are working with a database connections we need to create a different uh we have to add some libraries we need to connect with jdbc right for jdbc we have to add a jar file okay and then you can you can actually do that in the external files you can just go back to external files and you can add or external libraries you can add a jar file but to better manage the product we can use something called Maven now Maven is a build tool which also helps you to handle these jar files so what I can do is I can right click here I hope there should be option of converting to mavin okay do we have it here refactor no I can't see any option let me know in the chat if you know where to because I have used this on Eclipse because in Eclipse you get this option here no problem what we can do is uh we can simply create a new project and let's say new project okay and then instead of selecting a normal project what we're going to do is we're going to select a MAV project and we'll name this as product demo DB because we working with DB database now and then uh everything is as usual so okay there's a question mavin versus G which is uh which is best or which is better both are good it's just that I'm more comfortable with Maven because it is easier to teach in grle there are lot of new things and then indentation is one thing so for projects gr is good for teaching m is good yeah and for beginners me is good Okay click on Create and it will create a m project I will say new window because I want both these windows okay and then in this I'm going to Simply copy the files so if I go back here I want all these files so I can just simply copy and P paste over it all and I also going to change so package com do sorry o. example let's stick to that now what you get in the mavin project is this if you expand this or if if you go to pom.xml so whatever dependencies you need you just need to mention those dependencies here so what you can do is you can put a thing called dependencies and whatever dependency you want you can just add it here now what dependency we want we want a dependency for for this what is uh the connector right now how do you get the connector how do you get the dependency do you have do we have to buy heart it uh not exactly so what you can do is go back to your browser there is a website called mvn repository and in this yeah so in this you just have to search for postgis SQL jdbc and if you can see we have it here this is the library which we have to use and make sure that you get the recent ones okay don't go for the versions which are way old uh and which one to choose here so I I always go for second last version because all the latest version will always have some issues okay and it also tells you which of the library has VAB abilities and you can see there are certain vulnerabilities here this one is safe but we'll go for this one 42.5 point4 select this one and copy this go back to your project and just paste so what you got there is dependency okay and every time you make the changes depending upon different IDs you will get different options yeah okay here add framework support Maven okay we got the option cool hey done oh it was so fast we don't even need to have a create a project so what you do is you can just click on this right click on the project thank you so much Prine add framework support and select Maven here and you will get this option oh that was cool so now let's go back here specify the dependencies and paste it here okay so good thing is you don't have to create a new project thank you and now the moment you add this what you have to do is you have to refresh your M so if you can see there's a option of Maven changes click on this and it will download the jar files for you if you want to see where it is click on the external libraries and you will find a jar file for poqu cool so this is what we wanted now once you have added that support it's very simple now what you do is instead of adding to our list we are not going to do that now so what I will do is I will just come in this section and in everywhere we have to make the changes okay let's not comment because our code is dependent on it we are not going to use it so let's say if I want to add a product so what do you do is instead of adding to a list because not we are not going to do that we're not going to add to a list we will add this to a database now how do you connect to a database it's very simple what you do is you simply say I want a database connection so I can say I can say DB Dot uh DB do add a product or maybe I can say save db. save and pass P just by writing this line the data should be stored in the database right will this work of course not right it's not rajnik to know everything we are not sure which database you're using uh what is your database name I mean which dbms we are using what is your database name what is a password to connect with it so this will not work so what we want is we want a extra layer remember in the iPad when talking about it we have mentioned about the extra layer this is the layer which we want to create so how do we create that layer it's very simple you can just right cck on your project here and create a new class and name this class as product DB okay do it later and in this class basically you have to create a save method so I want the object of product DB so I will say product DB and I will say this is DB equal to new product DB once I get this object I can simply say db. save but unfortunately we don't have a database here so what I can do is or we don't have a save method so I will click here and say create method save and done we got this method here and this method is responsible to store data in the database the entire service class should not have any idea about database it will simply say save it it I don't care how you do it it is your job prodct DB to connect to database and save it now to connect with database you have to learn how do you use jdbc so you will find the descri in the description you will find some videos one is for spring framework one is for lombok which we're going to use and also for jdbc so in this we have to connect to database right and I will not be teaching you database connections jdb connections here I'm just going to write it so basically for databas connections you need something called a connection object so you have to say connection which is coming from java.sql now this is coming because you have added the jar file of pogress if you don't add this jar file you will not get this so we'll say connection con equal to okay we'll just say equal to null by default and let's create this object in the Constructor so I will say public product DB Constructor and in the Constructor of this I will simply create the object and the way you do that is by using a driver manager do get connection and in this you have to mention the URL okay now it will take three parameters from you not one it will take three parameters the first parameter which you have to mention is about uh the URL of the database the second one is a username and I know the username so this is po that's the username the password for my machine PO is 00 you when you install make sure that you change it and then we have a URL here now this this is something you have to remember or by heart that's your choice but when you work from a long time you remember that so you say jdbc colon uh postgress SQL colon SL slash not this slash sl/ Local Host because it is installed on your machine colon 5432 which is the port number for postgress okay for different database we have different port number uh I think for MySQL I forgot the port of a Mas that's weird so this is the port of a post so you you got this details right we got the URL we got the username and password so you might get an exception what you can do is you can click on more actions and you can say surround with try catch because it might th an exception so once you got this database connection created let's save the product now to save it what you need to do is you have to write SQL query right so what we need SQL query here so I can say query now what will be my query my query is very simple I want to say insert into product uh with the fields because I don't want to mention the id id should be autogenerated so I want to insert name I want to insert uh type of the product I want to insert the place I want to insert the warranty cool uh and then values we have to insert the values as well now question arise from where you will get the values tell me if I want to insert all this data from where you will get the values so basically the values you will get is from the product this is where you have all the values right because one product will have details about your product it will have a name type place and warranty so what you can do is you can add the values here okay but since we are going to use prepared statement in jdbc now if you want to know more about jdbc you will find the link in description and you can watch the later video later about jdbc but here I will say question mark question mark question mark four question marks for four different values and we can replace that ler but how do you execute the query so we have to create something called a prepared statement St equal to con do prepare statement and we just have to pass the query here so whatever query you have created just pass it here now this might also throw an exception so what I can do is I can say surround with try catch we are just creating a query now let's execute it so how do we execute it so we can simply say st. execute now what it will do is it will execute the query so let me just go go with the flow again in the main what we doing is we are saying add product now this product is actually going to product service inside product service we have this add product method which is taking a product it is asking database to save it so the same object goes to product DB and then you are accepting it here and we are using SQL query to send it to dat to the database right uh but the problem is we can't simply say execute because we have not mentioned the values yet it is still question mark we have to we have to change these values now how do we change it quite simple just before the execution you can say St dot each question mark is one parameter right so you can say Set uh set string because the first one is string so the first question mark since we have four question marks we have to replace it the first question mark will be replaced by the name so p. get name the second question mark will be replaced by uh p. get type no yeah type the third question mark set string again the third question mark will be replaced by P do uh get place and last question mark which is set integer because last one is a integer here so it's say set int question number four I mean the question mark 4 this is p. get warranty so we are able to set all the values and we are saying execute our job is done the question is will this work I'm not sure let's try so what we can do is we can simply comment all other things only focus on adding a product what do you think will it work or did I completed all the steps I'm not sure sometime you get to know things when it gives you errors right uh so let's go back here say run oh we got an error it says po uh phrase unable to pass the phrase what's wrong what's wrong what's wrong what's what wrong just check oh someone also mentioned that you can wrap it where do you have the option of wrapping it in the oh yeah okay this looks good so we can wrap the entire output cool so uh we we forgot to do one more thing we have we forgot to mention the database name so the database name is telis here okay we have not started with the data uh the data jpa yet I wanted to show you uh the part of data jpa right and that's why I'm doing I'm going step by step here let's try to run this once more time and oh something happened you can see there's no error it says process finished with exit okay let's try let's see how do you verify this let's open our PG admin and let's run this particular query oh can you see that we got ID which is autogenerated and we got all this data here in the database this looks cool okay now uh once we have entered the data let me Comm in the entire section because now we have data in the database so let's so let's let's not insert every time now this time I want to fetch all the products this is something I want to execute okay so get all the products uh so here when you say get all products let's not get it from the list question from where you will get it again simple DB dot you can say uh get all this is the method let me see if I get the empty here let's run this and you can see we are not getting any data because it's empty the list is empty so how do we fill this it's very simple again use the same thing let's copy this because we are going to query the database paste what are the changes the first change you're going to make is the query we're not going to insert this time we're going to select so we'll say select star uh okay we are not getting star we are going to mention the column name because we don't want ID there so we'll say name type place and warranty from table name is product and we don't have to apply well Clauses because we want all the data so basically we are fing everything from database right and let's execute the query now since we don't have any question mark here you can skip the entire section from here and simply say execute the difference is execute will not return the data we want data right so execute query is something you can use to to get your data and what it returns is the result set so it will return you a result set and what you can do is you can get data from this result set and store it in a list but the problem is it will give you uh data in the result set format you have to convert that okay cool so basically we want to F data from result set and set it in the products here because that's what we are rning here right so how do you add so basically we can run a loop because if you say RS dot there's no method using which you can get all the data and save it in a list we have to do it manually one by one uh there's no option so what I will do is I will use a loop here and fesh data one by one so you can say fesh data till the end and every time you FES data you can simply okay what we have to do is we have to create a product object because okay I know we don't have time to explain this now but just go with the flow everyone so what we trying to do is we want to create a list of products right so we will be having multiple products so every time you run this Loop create a product object and it is highly recommended make sure that you watch the video of jdbc and Maven trust me this will be very very easy Okay cool so uh let's get the product object and save each value in the product values okay uh so how do we update the value so we have to say p do set name and this is coming from rs. getet string from the First Column so since we are getting multiple columns right this is the First Column second column third column fourth column then we can say p do Set uh type this is coming from second column so I will say get string column two P do set place you know this is like a beautiful poem from a language which you don't understand right it sounds beautiful but you're not sure what is happening three then P do set warranty and rs. get string or get integer this time it is integer and we are saying four okay so we were able to set the value for the product now once you get all the values just add the product in the product so we say products do add P now let's say you have 15 values right so in the database now this while loop will run 15 times and every time you will get the new product object you are setting the values which is coming from database and adding to a list the same list go goes back to the product service the same list goes back to the main and in the main you are printing all the values let's see if this works run and voila can you see that this time you're getting data from the database is this making sense at least you are getting the gist of it right how things works out so yeah so basically we were able to fetch all the data right now same thing can be done for other things as well uh for other methods which we have here so you can see we have it for uh get all we we can also do it for the get product name for a particular name we just have to put say we have to change the query here okay cool so let's get back here and what we have to do is so yeah you can also do it for the uh get get a product with a name but that is your assignment okay so write SQL query and convert this into uh uh database I mean convert this so that you will get data from database make sense now I want to show you a real magic okay and that magic is how do we change this project into a spring Boot and a data jpa we'll compare this code from the code which will'll write in the spring boot okay now I don't want to teach you entire spring boot now uh so yeah I don't want to teach you entire uh spring boot but I want to show you the power of spring Boot and what it can do for you okay uh so how do you work with a spring boot project the first first thing is if you want to create a project of spring boot you can go to inj and you can say file new when you create a project you have to make sure that you select spring boot but there should be an option in your IDE I think my particular IDE is the ultimate version yeah so I have ultimate version here uh you will not get this options the spring initializer option I guess in the community version so if you want to do if you want to get this project even if you have a community version that's completely fine what you need to do is you need to go to the website and uh he where the other file went yeah so you have to go to the website called start. spring.io okay and here you have to select so you have to select the project type first which is Maven in this case the language it supports is Java cotlin groovy we'll select Java here here version whichever is default you can go for 3.1 and then you have to mention your group ID so that represents your company ID basically a unique one so this is com. Tesco and then the project name so project name is product demo we'll say product spring you can change this as well uh make the packaging as jar and the version so make sure that whatever version you have in your machine so like that and then from here you have to add dependencies now what are the things we need so basically the first thing you need here is um what you need you need a postgis driver so post SQL driver added it next you need something called a data jpa because I wanted to show you the part of it yeah this is called a spring insu visor so add these two dependencies post and spring jpa and click on generate that's it your job will be done you just have to unzip this okay so click here unzip it I will just hide this so that I don't share everything uh it's in downloads okay nothing is nothing is risky there okay cool so you can see I have this file here what I will do now is I will go back to my intell J I guess I already have the intellig DB here let's use this window I will open this project which I've downloaded which is there in downloads it's there in product spring and yeah open this project this window Okay cool so you can see we got our spring project here and trust me once you see spring project you will say this is easy very very easy okay okay so if you go back to your pom.xml uh this is what you have you can see got two dependencies here in fact we have three dependencies uh you got data jpa if you can see you got postgress and you got the testing part testing framework okay cool now in SRC if you go to main you have Java and this is your basically main application whatever code we have in main you will be writing that here expand this I will explain this in some time okay this part so what I'm going to do is here I'm going to uh copy the code which we had here so let's you go back to main and let's not add everything because we already have that in database let's execute this part only this part we are fetching all the products how do I fetch all the products just paste it here the question is will this work uh of course not because we don't have a service class yet so how do we get the service class it's very simple you can just copy this service class and paste it here so you can see we got a service class which is here we also need a product class paste here okay so we got the product we got the service uh service says it is searching for product uh product DB okay we'll we'll get that and you know what I will do I will remove product DB and in get products let's say okay let's solve this problem later let's solve this errors later and just to keep it more simple I'll remove this part just to make it easy and yeah cool okay uh so the problem is we don't have a product service class the object we don't have the object of product service so let's go back here and say product service let's create the object for it as service equal to new product service okay and then in this we have the object of product we don't need a Leist as at this point so we can remove the least as well and okay now one thing to remember about spring framework now spring framework provides you one of the most important feature in programming which is the spring the dependency injection now what is dependency injection if you go back to the main method you can see we are creating an object of product service when you say new keyword and when you mention the Constructor basically you as a programmer you are creating an object by yourself is it a bad thing yes is because when you create the object it is your responsibility to destroy it basically you have to manage the life cycle of an object spring framework says you don't have to create the object I will I will do it for you trust me Spring framework says you don't have to create the object I will do it for you let me show you what I'm saying what I will do is I will just comment all the methods here comment and let's create a very simple method so I will say public void show and in this show basically I'm saying in show okay and we all know that if you want to create if you want to call this show you need to First create the object let me come in this section as well yeah so first you need to create object of product service then only you can call it right example I can just go back back here and I can say service do show this will work let me try run and you can see oh it is giving you errors because we have added this dependency of database okay okay okay let me comment this the post and data jpa for some time only data jpa commenting should work when I comment it and if I go back to my application and okay what did I just comment it oh yeah if I comment that then we have to get this spring boot dependency okay you know what I will do just trust me just trust me because I have to make a lot of changes if you if I want to achieve that because I have to get this spring boot dependency then if I comment it uh let just reload the M dependencies okay so the thing is you have to say service do show then it will work right but then spring boot says you don't have to create the object you can just remove this and it will still work how step one you have to tell spring framework to create the object for you spring framework will give you the object and for that just on top of your class say add component so what you're doing is you're telling your spring framework to create the object so spring will handle it for you you don't have to handle it uh ignore the XML file at this point if you're finding it confusing and make sure that you do it by yourself then it will make much more sense okay cool uh and then go back to your see the problem is we are doing so many things at the same time in less amount of time it it does create some confusion okay so what I will do here is I will just try to uh okay so how do I get this object from spring so what you do is uh when you talk about the first line spring provides you something called Spring container so every time you run this line I mean the moment you run this line it will create a container for you imagine a container a box where the objects are already created so you don't have to create the object objects are created and if you want to learn everything in detail uh you will find the link in description about this spring video just watch the video you will get the complete idea now this basically gives you a application context object so let's use it so this run returns you the object of application context let's just use it and using that context now since the object is created right so the object of product service is already created by Spring framework and it is available for to use how do you get the object it's very simple you simply say context Dot and you say spring framework give me the object okay so spring framework get bean and mention which class object you want I want the object of product service service do class so you mention which class object you want and spring will give you the object yeah this is called dependency injection and now this should work but problem is I can't run it because you know there there are certain properties you have to mention so I will not run it now so get bin will give you the object but this will work trust me okay so what we saying what we are saying is we want to get the data right so from the service uh let me just uncommon this part so from the service we are saying get all products which is here okay we are not going to go do add product right which is already there data is already there let's use get all product s uh okay so you can see we are saying get all products and this will work only when your application is connected to a database and that to with the help of queries okay and you can see we don't have any file here which is which has a database connection but let me create a file here because we want it DB right so what I will do is I will just right click here and say create a class and we'll name this as product maybe you can say uh d or whatever name you you use doesn't matter and in this class you have to mention all the queries but then if you really want to use queries here then why you are using data JP and all these features let me show you something let's not even create a class let's create the interface okay and the only thing you will do here is you will extend something called jpa repository now jpa repository is a interface which will help you with the concept of omm now on this channel there's a there's a video on om please watch it hush is not here so you can't add the add that in description but you can search on what is om and watch the video you will get much more idea but ultimately when you see the output for this you will get the gist of it now repository says tell me which class you're working with so I'm working with product and what is the type of your primary key the primary key is integer so we simply create this particular uh interface here and no configuration okay we have not done any code here but see the magic and okay now this is the exciting Point people who are still watching just see the magic if I go back to product service and let's create the reference of product DB so say product DB DB just a reference and DB dot can you see that we got methods just say find all no need for any SQL query no need to do everything which we have done here if I show you the code what we have done in the earlier project is this if you want to get all you have to line you have to write this number of lines with this no line this is what data jpa is doing for you it's not like behind the scene there's no SQL query because the only way you can interact with your database is with the help of SQL but this is happening automatically is it making sense okay but the thing is we got the method but unfortunately your your your applic has no idea about the configuration nothing because nowhere we are mentioned which dbms you working with is it postgress is it Mas is it Oracle we have not even mentioned database name we have not even mentioned username password we have to configure those things somewhere now where is that somewhere if you can see there's a resource folder here in the application. properties this is where you mention all the properties now what's the property I'm talking about I'm talking about the URL the URL is for this spring data source. URL and the URL is same which we have written before which is DBC colon postgress let me just copy it because this is the URL which we want copy and paste it here next thing you have to mention is the username so spring data source username which is posis single s and we also need a password so spring data source password which is 0000 and that's it once you mention this let's see if this works run oh we got errors I thought it will work okay it says there are certain things which are missing what is that missing thing it says error creating a beIN with product DB oh okay the thing is in the uh product service we are creating a reference for product DB right where is the object where is the object that's the problem so the thing is as we mentioned for product service remember we have mentioned component here in the same way in the product DB as well on top of it we have to say this is a repository because we are working with repository here and in product service if if I want see the the thing is just by mentioning this spring will not know that you want this object you have to say hey Spring I want an object of product DB and for that you you have to say Auto wire search it and give me the object okay if you want to know more about Auto Wire Watch the video which is there in the description about spring frame work okay if you want to know how I got this full thing when I just mention user just type user it will give you prompt and it happens with some of the IDS okay and it should work now let's rerun the code okay now we have different error and I love errors see the thing is it says and sometime you learn by erors right it says everything is good now I got the object but the problem is this product class is not managed it is not it's still not a be and also there's one more thing which you have to remember when you talk about the product it is not just a simple class right this is trying to map so basically your table and your class is getting mapped and to achieve that you have to say add entity here next you also need to specify the primary key which is the primary key here which is private int ID that's your primary key and the the do to mention that it's a primary key you have to say at ID is it ID yeah so at ID and then we have to also mention that this value is actually autogenerated okay so how do you get this autogenerated uh so ignore this part because this is just something which you can see later you have to say autogenerated uh generated value equal to uh you have to set a strategy if I'm not wrong you have you have to set the strategy and the strategy is generated type uh identity sequence should also work I guess but let's say identity and also we need to get the geta sets for this for ID right click generate geta set for ID you know we are doing so much and so much of thing in one video and you need to rewatch this things are looking good now let's see if this works run and voila can you see that you got all the data from the database without writing any SQL query yeah we can also add ID if you want to get the ID but this this should work now what other the things you can do you can also add you can also add the product because if you see if you go back to your product service and let's try to find what are the objects what are the methods we have so DB DB dot you can see we have find all we also have save we also got uh count how many column how many rows you have you can also delete if you want you can also say find one we have so many options there is it making sense and just imagine just by using this your productivity will go on the next level and that's why in the industry spring framework is so famous so let me explain the entire flow once again if you want to create this project the first thing you do is you go to Spring uh start. spring.io mention all these properties here which is uh Maven Java whatever springboard version you want to use make sure that you don't use Snapshot because this creates some issues uh and then mention the group ID mention the project name and add these two dependencies the post SQL driver and spring data jpa next you need to uh copy the files which you have already created Service uh let's start with the XML file first in the XML file make sure that you have these two dependencies or three dependencies at least these two is these two are very important for us oh I got tired in one hour only next in the uh produ service you have to make sure that this is component because I want spring to uh give the project yeah sorry uh I want spring framework to give me the object of product service and uh I can use something called Product DB which is an interface not a class and it is extending with jpa repository and with this you can perform the operations the cut operations with the uh with this particular interface you can fetch data you can save data you can uh you can update data you can delete data and we are able to do that but then you have to also set some properties which is application. properties which we have done here so go with the video once again uh I will try to share this code today itself hey yeah you can also use service here for different layers basically we use different annotations doesn't matter it works so all the uh entity class or normal classes you can have component entity class will have entity and uh service class can have service so got the idea what is spring data jpa so all these operations where you don't have to add SQL query is happening with the help of spring data jpa so before spring we used to do that with the help of hibernate and if you want to learn about hibernate there's a video on hnet as well and know if you check out the Channel there are so many videos in last 5 to 6 years sometime even I forgot what videos I have your code will be available so after after sometime check out the description for this particular live session uh you will find the code repo as a title and then there will be a GitHub link and now today we are going to build the same project which we have done before so that we can it it will make much more sense okay what we are doing but this time we are going to use uh MVC okay so basically we are converting our project into a web project okay that will be cool right so let's get started and let me show you the code what we have done till now and let's talk about the code and then we'll also see what we're going to do today in this particular uh in the image okay so if you if I want to go through one by one uh basically we have this number of classes here let me just move it here yeah uh and also move it this side so basically we we have a product class it's all about this class which is product class and in this we have you used four variables basically and for those four variables of course we need to get gets and Setters in fact um before this session around 6:00 p.m. or 5:00 p.m. ISD I have uploaded a video a shot on YouTube which talks about uh the longbo how do you reduce this code okay so if you have not checked it out do check it out later so we'll we'll also convert this into a longb but we'll see later how do we do that uh so this is the class which you want to work with so every product in my office basically basically it's a that's a product right even if you talk about this remote that is a product now what I want to achieve with this is with this product basically I want to store I want to have a place where I can check where a product is actually is example there are some products on this table in fact lot of products because I'm going live I have a stream deck for the switching between uh the screens I have a Mac Studio for the streaming I have my personal laptop here uh to check everything is going well I have iPad but I write something and I don't know why this light is sitting here I also have a light here that's huge okay so it gives you a good light on the face uh so basically uh we have so many items here right so sometime we keep this item somewhere and we we lose it right so we have to search where that particular item is and for that we have this uh database or this application which STS where it is and that's why you can see we have a name typ place and warranty because I also want to know if if the current product is still in warranty or not now to manage it uh what if you want to add a product what if you want to remove a product what if you or not remove we have not done that what if you want to add a product what if you want to search something what the where the product particular product is and also I want to know uh in a particular place how many products I have what are the products we have and if you want to put that logic we can do that in here so we have this product service class and the reason I have created different classes here of course you can write everything in one class because that's what we do uh when we start learning coding right u in fact I remember when I used to start when I started uh my programming journey I used to write everything in uh in one particular file it's good it works the problem is if you want to change something and you're not sure what you're changing right and that's why creating different layers makes much more sense so what layers we have so basically this is my main. Java this is where I execute something so if you want to call something and if you want to do all the execution stuff with calling methods and stuff you will do that in the main class of course you can name this class anything you want but if since this is a console based application which we already built we have this main method here and in this basically we are getting all the products now how we are getting the products that's not the concern of meain it's just it just says I want the product give me the products uh same goes for other me as well if you want to search a particular product uh if you want to search for a place if you want to add the products so basically we have the we have the calling here the actual logic goes here so you can see we have a product service here this is where the actual logic goes okay but now since we want to store this data in the database right see not every application will have a database and if you're using a database of course you can write your database connectivity code so doesn't matter which Lang language you work with which database you work with we have to connect to a database right we have to follow certain certain steps in the same way here if you want to write your database connections don't do it in service class service class simply says this is where your logic will be if you want to process something uh example let's say if you want to add two numbers right so from a main method you will say hey add method take this two values which is two and five just add it the actual calculation will be happening in the service so all the processing will be done in the service but what if you want to get something from database and that's why we have one more layer here now this particular layer actually communicates with your database right uh in fact in the earlier session we were able to implement uh okay I think I have done different thing yeah this was the project right because we have also implemented spring let's talk about this one uh but things remain same right if you if you go back to your uh previous score this is day two which I'm talking about um so you see we are writing all the SQL queries now what we have done in the previous session so basically we are doing a quick revision of what we have done uh but if you if you go back to our previous session we tried to reduce the code right we tried to say let's use certain tools and some Frameworks so that we don't have to do everything by ourself now my question to you is is it a good idea to use Frameworks to reduce the code what do you think you know some of the people I know they say Hey you know uh we as a programmer we should write everything by ourselves right what do you think that's a good idea whatever the application does don't takes help don't take help from anything else just build it by yourself okay let me talk about the first problem which is if you try to do everything by yourself there are a lot of things you have to do by yourself building your own OS because ultimately your appliation runs on the OS uh building your own runtime that means your jvm and JRE uh building uh I mean not using any Library example here for database connection as well we have imported some packages don't do that do it by everything by yourself right and we can't do that right we we want to complete the project in a month or two or maybe in a year if you just complex project if you try to do everything by yourself it will take months even to write a simple database connection the second reason is uh before I talk about the second reason I have a question for you and just complete the sentence I have used this multiple times so even if you know it you can answer that or if you don't know about it think about it it's a funny thing not a uh ser ious issue or serious thing so my question is if debugging is removing bugs okay that's what we say right debugging is removing bugs then what is coding if debugging is removing bugs then coding is adding bugs so what I'm saying is every time you write one line of code that is a potential bug let me show you this line can be a potential bug this line can be a potential bug it will not it may not come up when you run the application but may based on the different uh input from the user it might behave differently and that's why we do testing right so every time you write your application it's important for you to test your application that's very important so one way to reduce the testing and uh one way to reduce the potential bugs is don't write the code write minimum lines of code what if the frame the framework which you are working with says hey you know you don't have to write all these lines I will give it to you behind the scene the good thing is all this particular uh Frameworks they are well tested and they are getting used in the industry from a long time so at least you can trust them that they are not creating issues okay I'm not saying it is 100% safe there there have been some is some uh cases where you have used certain libraries and they found some issues with the library and because of which the Hat attack happened uh you might have heard about log 4J in fact in the spring framework also there was some I forgot the library but but if anyone knows let me know in the chat uh it was having some issues and again it's not like the issue stays it gets rectified but it happens I'm not saying it is 100% safe but try to write less amount of code okay cool uh so this is what we have done in in the database but then in the yesterday session we have seen something called a spring framework Now using spring framework spring provides you something called a dependency injection which means you don't have to create the object you simply have to use the object uh example if I want the object of product service in the previous example uh on day two basically we were able to create the object with the help of new keyword but the moment you use a new keyword that means you are handling the object you are creating object you are doing everything with it so in in uh spring what we doing is we are not creating object by ourself we are simply saying there's a spring container so spring Frameworks actually create a container for you imagine a container as a box which has lot of different objects and you simply Say Hey I want that object example let's say if you want a phone of course you will not manufacture the phone by yourself you will simply go to a shop and buy the phone in the same way spring container says I have all the objects which you need just ask for it okay and that's what we're doing we're saying hey Spring get me a bean get me the object uh basically the objects in Spring containers are called Bean of course this has more logic to it but let's not talk about the logic at this point imagine being as an object okay and then we are getting the object of this particular class which is uh product service and then you got the object and you can use the object for your purpose right and we able to call the methods now the beauty is there's nothing change in the service right you can see we have all the same methods of we have commented it but it should it should work and uh product service needs the object of product DB right of course if you have a class which is calling another class methods you need object for it and you don't have to say new keyword what you can simply do is all the classes or all the interfaces which is repository basically uh what you can do is you can write a annotation now let's not discuss the difference between repository service component again it will take another two hours just to understand it but you know just a gist basically spring will say okay the moment you mention repository the moment you mention uh service the moment you mentioned component where we have used it uh entity and you can also use component what spring will do is every class which has this annotation on top of it it will create object for you and you you don't have to get object by yourself spring will give it to you and you simply say give me the object and one way to do that is by using autowired now how exactly it is searches let's not talk about those things okay but then it will it will give it to you now what we are going to do today is we will be building we will be converting this project into a web project now today's session might be Bas shorter because we don't have much to do uh we just have to convert this project into web so let's do that okay so let's get back to our drawing board so basically what we're trying to do is first of all why do we build a web project is because when it comes to Standalone project what we have done it only works on my machine if I want to use the same thing on someone else's machine basically have to build have to take the entire project give it to them and they will run it example in our office we have four working machines and if only one machine has the software they will not be able to access it and also where will be database database is is in this machine then we have to connect and all the stuff so what we can do is instead of building a standard on Project we can build uh a web project where anyone can use it not just us we can can use it uh the entire world can use it you can also use it uh maybe we can create a account repository you can have your own account and you can manage your own products uh we can do that and uh okay that's one so if you talk about client you know we are poor people we have a client we have a laptop here so basically from this laptop I want to access this database see ultimately it's all about database right as a user you're not concerned about uh I mean you you are concerned about the data you want data right so how a user will be Ed to a database so basically a user uh I mean of course if a user wants to send a request database only works with one language which is SQL if you talk about the langu uh database like dbms so when you talk about D dbms we have uh our dbms specifically we use SQL language example we are using post giz here right now when we use poiz so that is rdbms you need to use SQL now from a user of course as a imagine a normal user a user will not be searching something online or not be searching something by using SQL query I mean ask your parents or ask your friends who are non from non it and say hey if you want to check your profile on Instagram just say select uh star from Instagram database where the username is this of course as a normal user don't do that right U so what you get is a UI so basically a client will get a UI here so this is where the UI will be and this UI will send the request to the server of course a server can be on the same database can be on the same machine or database can be on some other machine doesn't matter but let's say at this point we have on the same machine now in this machine we have to first create something here so a user will send a request right a user will send a request and what a user will get is response now specifically if we talk about a protocol which we use to send and accept the request we use something called HTTP right so we have to use HTTP protocol to send and receive the request now this particular box here this is responsible to accept the request from the client so whatever client's request it will say okay I got this request let me see if I can fulfill that or not okay and then based on that it will send the response as well yeah so based on that based on that it will send the response as well now this type of layer in between we call them as controllers now what is a job of a controller just to accept the request from the client and send the response let me repeat the only job of a controller here is to accept the request from a client and send the response now whatever extra you want to do example let's say if you want to process something client says Hey controller I want to add two numbers now question arise where you will add those two numbers will you add that in a controller or somewhere else so never remember never do any calculations in the controller controller job is to just to accept the request and send the response now after controller you will be having one more layer here which is your service layer now this layer is responsible to perform the operations example let's say client says hey uh server I want to add two numbers okay server says give me the request uh I mean of course I will work only when you send the request the controller this particular controller here will accept the request and then it will say oh you are basically trying to add two numbers let me take help from the service now it is service responsibility to add those two numbers and then of course service can send the response to the controller and controller will send the response to the client if that makes sense but what if client says Hey I want to know some data which which is there in the database example let's say uh you want to fetch a product which is in the database now question arise who will do it is it your service who will do it or someone else so basically we have one more layer here which is your data layer in fact uh yeah we can say data layer it has multiple names to it but just to keep it simple we have a database layer or we can say database layer uh data layer doesn't matter and then this request goes to service will request to a datab database layer and database will send the response now this is how you basically have multiple layers in the web application a client sends a request request go to the controller controller sends a request to the service service s send the request to the data data will have the SQL queries which will send request to the database fresh the data send it to service send it to service service will send it to controller controller will send it to the client okay uh now the beauty is uh a client or or a controller can send data to the client right but how the data should look like of course the data should be fancy right it should not be a simple uh data you want to see dat data on a proper layout and all the stuff so what you can do is you can send a HTML page so basically what a clients receive HTML but nowadays when you talk about clients it's not just a web page or it's not just a web browser right in the earlier days we used to only use web browsers but now we have multiple devices example you can have a phone uh you can have uh what else we can have any anyway let's say we have phone as well now a client here wants to access data from a phone now now if you open any application a application we already have a layout right because see in a browser when you see a page you don't have a layout here you simply have a blank page whatever it has to show should be coming from the server but on the mobile phone when you talk about any application doesn't matter is it Android iOS or any type of mobile phone you have a layout specified what changes is data example let's say if you go to Instagram and when you scroll basically you have those boxes fixed the data changes right so we already have a layout here now from the server side do you need a HTML page or do you just want a data of course right you just want a data now question arise what should be the format of this data normally a data can come into different formats in the earlier days we used to be we used to use XML but now the famous one is you can also write in the chat the famous One is Json which is Javascript object notation so whatever data you get from the controller will be coming in the Json format now the same Json format will be uh will be showed on the Android of course the layout is fixed data is coming it will show the data and even if you're using a a browser what you can do is instead of sending an HTML file what you can do is you can send a react page a single page application and then the react will already have a layout every time you do something it will send the request to the server and get the data and that's why if you see on the front end part react is very famous right and mobile you can also use Android or iOS applications I hope this makes sense everyone okay uh so now what we want to do is we basically want to create this controller now how do we do it and since we are creting a web project we also need a server so we need a server where you can run this now different l angues have different servers example if you want to work with PHP uh I think PHP uses a Zam server right uh we also have vam server Zam server there are entire shop of servers for PHP uh then if when it comes to okay what other language JavaScript so if you if you are using a JavaScript in the back end uh basically you need a node there nodejs and that's why if you want to build a back end application you use node there and uh if you are using Java basically if you are using uh if you want to use a web server one of the most famous one is your uh Tomcat okay so basically if you want to use the entire thing you need to install Tomcat in your machine okay so that's what we we are trying to build here this is this this is the entire thing we're going to do now in this particular session we will not be talking about the front end okay so this section here this section here is not something we are concerned about we just want to focus on the backend part does it make sense we are focusing only on this but now question arise even if you build this how will you use it if you don't have a front end how will you use it so what we can do is see see the thing is every time you have a server who says you can send a request and you can get the response even if a server provides 10 features so let's say a server says I will provide you 10 features or a server has a 10 feature but server only wants to provide five features to the public how do we do it how do we provide the service to the outside world let me repeat my question if a server has so many features or so many services how it will expose those services to the outside world and that's why we have this concept called API so basically from the Ser side you have to expose some apis does it make sense everyone so we basically have to uh build this API and uh expose it now question if you don't have a front end how will you expose this so that's why what we can do is instead of using this front end here we can use a API tool a tool where you can send request to the API and one of the most famous API tool is Postman there are multiple options but then Postman is very famous so let's use it cool let's now it's time to get started with the project project okay let's rewatch the session then if this complex yeah you have to watch it if you if this is the first time you're seeing this it's very complex so do rewatch it okay let's get back to the code and what we're going to do here is we will build a web application right and of course we can reuse the code here and what I want to do is I just want to close the earlier project so that we'll have less windows open we already have so many things going on and uh stuff I hope you practicing what we are doing in the session because just watching a session will not make any sense uh you will forgot this concept in a day or two so do practice this yeah behind the scene uh behind the scene even if you're using a spring framework behind the scene these are your suets controllers are your suets uh if you want to go detail of spring mvy I will I will I will make sure that there's a link in description you will get entire idea how this controllers are created uh because see what we are going to see today is spring boot okay and spring boot actually makes your work very very easy but the thing is if you really want to understand what is happening behind the scene that's what you have to understand how spring framework works so spring boot is actually a layer on top of spring framework to make your work easy so what I will do is I will create a spring web project now how do we do that just go to the browser and the same website which we have went yesterday which is spring initialiser select Maven select Java all the same settings which we have done uh project name here product spring web because we are building a web project now and jar file v file okay now the difference between jar and war is in V basically you uh you deploy the project on a tomcat server so basically the place where you deploying it should have a tomcat server jar what it does is it embeds the Tomcat server inside the project that means if you have a project ready you can just run it because Tom CAD is there already uh okay now what are dependencies we need Post gisp in this particular uh thing as well we also need data jpa because that's how we're going to connect with database apart from this we need one more thing which is web okay so we need a spring web what else we need let me just check am I missing something here I think spring web is what we need uh we can also add lombok here because I want to reduce this code let me see if I have lombok I've never tried Lok in this yeah we have lombok here cool now once you have all these things click on generate your project will be ready to use let's unzip it done and let's open this project so go back to intell idea file uh open uh downloads spring product spring web click on open uh new window because we want want to use the earlier window as well and voila you got your spring web project ready okay now as I mentioned the Tomcat is already present in this particular project if I go back to if I go to external external libraries and if you search here Tomcat will be already present where are you Tomcat oh don't tell me they are giving you glass fish now oh it's here so you can see we got TomKat I don't know why I was not able to see that properly oh he hes here okay H says can I also win this MacBook I watch all your live stream for sure hes uh but I think you already got one from Apple yeah H H says so you already have a Mac Mac Studio so you can see tomat is already already here so what I will do is I will just minimize this and let's create a project now or we already have a project let's expand it now if you check out the pom.xml now this is where you have all the dependencies right uh so we got dat jpa we got spring web we got postgress we got the lombok as well to reduce the code and apart from this if you go back to SRC and if you see the files we already have one file here which is the main file and if you expand this no fancy code and you can see we got okay we have one more file here which is application properties which we'll see later but we got the project right now if you are coming from the old world of java let's say if you are talking about cetes or spring frame framework normal spring MVC we need to do lot of configuration even before you run the project let me run this project what I will do is even before running the project let me go back to my browser and here let me search for local host colon 880 enter and you can see it says this site cannot be reached because the server is not running so if you want to access your Tomcat with a default port number now this is a default port number for Tomcat and you can see this is not running what I will do is I will run this application without installing any tomcat and stuff because Tomcat is their embedded if I run this code okay oh the same problem I don't know why I add the data jpa because the the the thing is spring boot is very smart now since we have added data jpa it is searching for the database connections you know I just to keep it happy what I will do is I will just get those connections which we have done yesterday all these connections copy and paste in the application. properties which is empty in this case at least let's keep our uh data jpa happy right we have done that and now let's run this once again let's see what happens run and you can see it is running the Tomcat started on port 8080 so just by downloading the project you are able to run it no external dependencies is that's fun right uh let's go back here refresh can you see that yeah we got the eror because that's our life but at least you got the error from the Tomcat it says the homepage which you're searching for is not there because we are accepting the request but then the request which which is coming from the client we not able to process it because there's no mapping there right okay so what we'll do is uh let's do the mapping now question where to do the do the mapping so I will just just go back here and create a controller remember in this we have talked about a controller so we need to get this controller here so I will say new and let's create a class and let's name this as a product oh I'm not sharing my screen let me do that again what I've done so right click on the package say new and say just Java class and click on product controller so we got a product controller here now how do you make this a controller this is a normal class right how do you make this as a controller we have to give it some extra power how do you give that to extra power how do you make this a controller just by having a controller in your name will not make you a controller so how do you make this a controller it's very simple you simply add something called a rest controller now question arise controller makes sense why it is called rest see there's a concept of rest API now since we are uh yeah now since we are using uh HTTP request if you want to do anything on the server side if you want to uh get the data if you want to save data I mean send data if you want to update data if you want to uh delete data what you can do is you can use something called rest which stands for represent representational State transfer whatever communication happening happening between client and a server it's all about transferring the states right sending the values and that's why we say a rest controller to know more about rest controller I will put a a video in description if this will make much more sense then so you can see we got a rest controller and now this is a controller it is ready to accept the request but the thing is if you talk about a web application you have multiple pages right uh so you have multiple pages and every page will have a different address if you see if You observe the address bar your uh address bar changes for different request in the same way for this particular application as well you have to specify what you want to do uh do do you want to uh get the products do you want to add a product do you want to update a product do you want all the products do you want a product based on a particular uh name for that you have to provide multiple mappings example if I go back to my browser and if I say okay from this page not the courses I want all the products I can simply say Local Host colon 880 SL products the moment I enter it's not working because there's no mapping happening so what I will do is for this particular request for this particular request what I can do is I can create a method so if someone is calling products I have to call this method now this method returns a list of products right because you're asking for all the products so list of products uh we have to okay we don't have that class yet so I will say get products or I can say get all products because you're are fetching all the products that's it the thing is we don't have this class product here can you see that we don't have this class so what to do okay so basically uh we have a class here which is a product and we don't have a class so what we'll do is we'll go back to earlier project and copy this class product okay not here here let's space this class so we got product now there's no issue to we have to import the package for list okay and then here we can simply return okay what do you return oh I don't know how to fet this value it's because controller will not do all those things right controller needs helps from another layer with what is that layer yeah that's right we want these two classes service and dat database copy this and paste because we need these two layers as well the service layer and the DB layer so what you can do is you can create an object of a product service so you can say product service I will say service and should I say new keyword no right I can simply inject it so the way you can inject that is with the help of Auto wired and we got it we got this service class and now with this service you will say service dot get all products job done you simply converted your normal project to a web project voila this is your web project now but will this work that's the real question let's try let's restart the server and there is no error you can see go back to the browser refresh oh it's not working it is not working what's wrong what's wrong there my friend what's wrong the thing is we created this method but nowhere there's a mapping right of course a controller can have multiple methods different methods should execute for different requests so we have to mention in English hey this is for products request simple right no but that's a comment right how Jaa will understand that uh so what we can do is we can say add rate to do that we have to say get mapping and in this bracket you have to mention what is your mapping so this will be for products SL products so whenever you get request for products this is what you want to execute save go back okay we have to restart the server restart no issue refresh wo we got it is this making sense okay how to copy files is very simple just go back to the earlier project and just copy the files right click copy and paste in your recent project now whatever project you're building in fact I'm not following a proper standards here you know the controller should go to controllers package uh the DB goes to the repo package uh service goes to service package product goes to model package and that's why we say MVC right which is model view controller so the data which is passing between all these layers are called model Model S right product is a model here yeah springboard is actually interesting cool cool cool you know why I'm not creating those packages because the project is already so complex that's a good practice you should always do that but yeah I'm teaching you the concept so it works for me okay now what about uh I want to send the data okay I want to create new entry in the database how do we do that how do we send the new recorder that's a question now let's say if you have a form on your mobile application or a web page you can enter data and you can say send but I don't have a UI here right how will I send data from here and what should be the request now the request can be I want to send a product not products because when you are fetching it you are fetching the products right but what if you want to send a product one how do we do that in fact you know before that I just want to show you one more thing what if you want to fetch one particular product not multiple uh so let's say if I want to fetch okay all let's say I want to fetch Black based okay so what I can do is in the request I can say hey I don't want all the products I want one product that's why product slash I want Black based enter this is not working how to make it work it's very simple you can add one more uh mapping copy and paste and here instead of saying get all products I can say get product and it is not returning a list it should return a product simple product and the mapping will be product slash Black based right and then you cannot return all the products you have to return one product so you can say service. getet okay but the problem is I think we have commented it in the service class so if I go back to Service uh you can see we have commented this and the thing is this should be coming from the database not from uh list because we have not coded this in the last session let's me let me code it fast so I can simply say return DB dot uh find by okay we have to find by n uh name right because you can use find but then it will work with uh the prime key you can see we got find by ID uh if you want to do find by we do have find by but will this work oh we have to pass the query function as well so what I can do is uh spring J data jpa in fact data jpa provides you one more thing you can go to database and if you want to fetch from particular column not from the primary key uh what you can do is you can say return a product and find by name so whatever column name you have so say find by this is fixed what will change is the column name so find my name and whatever name you get here string name and the beauty is you don't even have to Define this okay you don't even have to Define this okay where do you find the the last session uh go to the channel and there's a question where do you find the last question let me just do that so go to the channel and uh search for a playlist tab so in the playlist there's a 10day chall challenge you can follow the the videos there okay just add this method go back to your service and say DB do find by name and whatever your name is no need to Define no need to write any uh SQL query or something now go back to the controller and say get product by name which is uh okay where is a name let me just try with uh black beast I'm hardcoding it at this point let's see if this works black beast restart the server of course you can also install Dev tools which will help you to directly restart the server every time you make a changes okay let's refresh this oh it works you can see we got Black based but the thing is it will only work for Black based if you if you try to use some other name it will not work you know why is because we have hard coed the values we are saying black beast black beast at both the places so what you can do is instead of saying black black beast you can put that in curly brackets and say name so whatever data you passing from there is saying uh this is so much easier than day three content okay see this is easy because we have done this in day three if could have skipped the day three this will this is like next level complex because we have not even talked about this layers here so we have done the difficult part in the last session okay so you can see so whatever data you're getting you will save that in this particular name in The Curly brackets and then you can accept it in here so you can say name so whatever name is coming here you can accept it here okay let's try and the same variable can be used here now instead of saying black Bas because see it doesn't matter what you search it will always give you Black based we don't want it we don't we want to so whatever user says here whatever user says here that's what you want to accept so restart and refresh oh it's not working you can see it's not working what went wrong I was expecting error but there's no error give me something okay so let me tell you what the what the problem is the thing is you have to use one annotation here which is path variable if you want to use this so by path variable what you're saying is in the path whatever variable you have just try to map it so you can see name name try to map it that was missing there and now refresh it works you can use it for any other name uh in fact remember we have Asus Rog right that's one machine but the thing is since we have a space between two words uh on the internet if you want to use web address or web URL you have to use percentage 20 which is a code for space just say enter and you can see we got Asus Rog is it making sense and the format which you can see here is dis format okay Patrick sounds more like uh giloy from Silicon Valley okay so let me go back to my code again and now let's do it for the adding a product because we able to fetch products what if you want to add a product okay so if you want to do that what you can do is uh you can create public and since we are adding it we can use void because we don't want anything in return normally you know a good practice is to return the status code uh HTTP has different status code example 4004 is for error related to resources 500 is the other related to the servers uh 200 is okay okay everything is going well so every time you perform some operation you have to return the status code as well at in this particular session we are not doing it because again it would be very complex okay so public void uh add product and now question arise what should be a part of these parenthesis or sorry the round brackets if you are passing a single value you will accept that in a string now you are passing an object from the client side how will you do it so since we are sending data from a client we have to send the entire object of product because that's what we are adding so we have to accept that here so we have to say product P so we are accepting it the entire product we are accepting it now once you accept it uh we can simply say service dot add okay we don't have ADD method okay we have to implement so many things now uh in this service this is the add method okay product and t. save okay this should work let's go back to the controller say add product pass p and your job is done but how do you map it now question arise what type of request you are sending so basically whenever we talk about HTTP we have different protocols there or different methods there in HTTP so if you want to fetch data we use get and when you want to save data or when you want to send data we use post okay so we have to use post mapping here not get mapping so let me repeat when you want to get data we use when you want to fresh data we use get when you want to send data we use post and what should be the URL I want to be consistent with the product we'll say SL product now you will be saying don't you think the this one and this one matches this product and this product then are matching but then look at the mapping as well we got get mapping we got post mapping here okay of course this is also different but even if they are same we got two different types of mapping there one is get and second is post mapping now people who are coming from this seret background they know what I'm talking about right do get do Post okay so we got this right but the question is will this work let's go back to our browser and let's send the data oh but how do I send data from here okay because see browser supports fetch in of course you can use some tools internally to do that but if you want to send data let's use a real tool which test which will use to test the apis and that's why we have Postman now Postman is specialized in uh request sending and response what I will do is I will start with the products so let's try this first this address in the postman this is how Postman looks like if you want to download Postman you can go to post man.com and you can download depending upon your OS so go back to postman and send set a get request get request for this particular URL which is Local Host colon 880 products and say send and you can see you will get all your data here and this is a status code you can you got there but if you you can customize it from the server side uh let's try it for product/ blackbeast send and you can see we only got blackb but now I want to send request I want to send a post request so you will go to post the URL is this which is Local Host col 8/ product but now this time we have to send data as well how do you send data so what you can do is you can just copy this now if you want to send data you will do that in a body so in the HTTP body you do that and replace this with this and let's change values so we already have till 15 let's add 16 one of the item on my table is uh a pen pen is of type pen it's on on white table expiry P expires let's say 9999 okay so yeah so basically you have to use yeah we can also use headers actually yeah but then let's let's use body uh you can set the raw and you can set the data like this you can set the format what you're sending you're sending Json and say okay the pro the problem is uh yeah it should work we can't we don't have to set the value right for ID um let's try let's try says send okay it says product not found that's weird we forgot to restart the server is it yeah it is actually autogenerated so we don't have to pass the ID but let's say even if you're passing it no harm run something happened you can see there's no error you got 200 let's send the get request again for the products send you can see we got all the products if you go down oh oh it's not working it's not working you can see the data is not assigned it's I mean we were able to send the request data is also saved in the database but not the data which we have sent okay it was not able to accept data that's the problem so we have to use one more anation here which is request body so you have to mention hey I'm sending something from the body accept it okay uh so we have to explain everything to Spring framework okay let's try now let's send this data again because this time it will use another ID run oh sorry we have to send a post request in fact where is my history here I'm using Postman for a long time yeah here so let me use a post request with the same body send nothing went wrong here let me use the get request send go back yeah can you see that we got it so we got pen a white table and everything is this making sense oh that's great okay now uh I want to show you one more thing about Lok right how do you how do we integrate Lok here okay and now it's time for lombok let's go to Lok Lok how do we integrate that we have already added the longbo part in the uh in the XML and where do we want to use it I want to use it for the product if you look at the product class there are so many things here right what you can do is for this particular variables I don't want to create gets and Setters you can skip the entire part from here even two string you can say I don't want to write it it it makes your code so lengthy right uh so what you can do is you can just go on top of your class and say data and that's it it will autogenerate the GTO status for you so basically lombok is used to reduce the number of lines of code you write in a class welcome back aliens my name is ready and finally the Java project is here that's right I know you are waiting for this from a long time uh so basically after watching Cod Java after watching spring framework there was one question right what next how do we build a project and that's where I have a small project for you so basically we'll do this project together using spring Boot and we are going to use a database as well for data storage right and for this project we are going to use mongod Deb now why spring boot why mongod DB we'll discuss that bit later and also we need a front end right and for front end we are going to use react now basically in this project we are not going to build theact application let's say we already have the UI ready now we have to work on the backend part so the back end part will be done with the help of spring Boot and the database will be mongod DB even before looking at the project there is one particular question in your mind and that is is it a good time to watch this video maybe you don't know spring boot maybe you don't know mongod DB how will you understand this project so I am making this video considering two situation the first one is what if you know spring Boot and mongodb the basics you are at the right place is because you understand how will you use your knowledge of spring Boot and build an application and also if you know mongodb Basics you will know how to use the mongodb cloud which is atlas now what if if you don't know this Technologies and that's the beauty now I'm making sure that when I'm explaining you the concepts at least you will understand the flow of a project you'll understand why I'm doing certain things and why exactly that line of code but yeah if you are if you want to do it by yourself after watching the videos that's where you will get stuck but of course there's different way of learning stuff sometime you have to learn from the basics and sometime you have to come from top to bottom okay this is what you can do in the project and now let me learn the basics so doesn't matter at what stage you are you can enjoy these sessions now let me talk about the project which we're going to build so the project is actually very simple this is a project basically we are building a platform for someone to get a job and what if someone want to hire someone so basically this portal or this website is good for both the employer and for the job Seeker let's say you are a job seeker and you are looking for a job I know this is a very basic UI but again as I mentioned right the idea of this video is not to understand the UI part the main thing is the back end so I wanted some UI so we have it here and you will find this entire project the UI the back end in the description so you will get a GitHub link and you you can access the project and you can run on your machine okay so you don't have to type everything by yourself don't worry okay so let me click on this get job now so basically I'm looking for a job and these are all the job description which I have so these people are looking for candidates who knows so let's say if I talk about the first one they're looking for developer and basically they're looking for software engine enger okay I've just typed the description the way I want it doesn't matter you know the grammar is not may not be perfect the technology might not match we're just here to learn right so let's go with that so we got some description looking for software engineer who can work with spring Boot and mongod and react okay you are watching the right project then uh the experience required is one year it should be year by then that's fine uh these are the skill set required in terms of Technology uh then we have the same profile let's say if you go scroll down uh we also have Junior data scientist uh spelling mistake here but you can ignore uh so data scientist who has end to endend project experience that's great 12 years I'm not even sure if data science is that old but anyway it's my data I can do whatever I want with that okay so skill set requir Python numai and those stuff okay so this looks cool right and also we have an requirement for editor who knows Final Cut Pro and who can use my Mac Studio uh the actual edor is not using it so I'm looking for someone else okay doesn't matter so let's say these are the profiles we have here and now let's say if I have lot of job requirements let's say 1,000 or 10,000 how do I search for my specific requirement of course you can use a search box here so in the search box of course you can search for a particular profile let's say I want to search for developer so I can type developer here okay and it should give me the results so it is giving me all the results of developers you can see developer developer developer developer here but what if I'm not sure what profile I need but I'm sure that I know something example let's say I know I know thre threads so I can simply type I know threads and you can see it will find a profile where the skill set is threads okay so basically we are also able to search and that's exciting right because you're implementing a search feature and trust me if you do it by yourself using a Java code or any programming language it is very time consuming and we have to write a lot of lines of code so we'll understand how do we do this using the mongodb address so here let's say if I want to search for maybe I'm looking for a job only in Mumbai so I can type Mumbai you can say it will give you the jobs which in the description where Mumbai is mentioned so basically for sure when you search for something you're not just searching in a profile so let's say this is a profile here this is a description this is the number of years of experience required and these are the Technologies you should know so basically we are not just searching in the profile we are also searching for the description and the technology okay maybe it's like a more of a Google search which we are trying to build it will be quite awesome right and also what if you want to hire someone maybe if we want to post a new job so let's say in my office I need a requirement of a blockchain developer so I can click on hire talent and let's say job profile I will say blockchain developer and number of experience required is two let's say a description person should be familiar with ethereum developer who knows how to make dabs that's it and then the the requirement is let's say the person should know JavaScript anyway uh we don't have a option of solidity here but again uh I wanted to keep it light so we got JavaScript there you can change the UI the way you want it's all about the submission which you do for the server so when I click on submit and if I search for let's say blockchain developer now is it there it should be okay let me refresh so you can see if I go back to this page if I go back to get job now we have a requirement for blockchain developer cool right and if I search for dab okay you can see we got the develop here okay so this is what we want to build now for this of course the react app is ready so I will show you the code a bit but we not be writing the react code by ourself but we will be requiring a spring boot so we are going to build this with spring Boot and we are going to use mongodb in the back end so let's get started so we'll minimize this screen I I don't want this and in fact I wanted to show you the vs code so this is basically the the uh the react project which we have so you can see we have multiple files here this is for the form this is for the feeds what you see all the developers all the job posting and you can see the request is getting sent to the server again if you don't want to use react in the front you can you can use normal uh UI as well or even if you don't want to touch the UI part I'm not a big fan of UI by the way we can use the apis calling maybe you can use Postman to test these apis or we can use Swagger okay so we'll use all those things in this project I know it's quite exciting so let's get started okay so what we'll do is first of all let's create a project from scratch now what are the Technologies we going to use here so we are going to use intj for uh Java we are going to use the database which is mongodb now why I'm choosing mongodb here why not other database see the thing is there's a huge debate between SQL and nosql datab databases which are good I have made a video as well before this so you can find the link in description which talks about what is nosql so basically just to give you an idea there are different types of databases available we can categorize them as SQL and non-sql in SQL we create tables right so we have a database in database we have tables so in in no SQL basically we don't maintain this structure now why it is important is because when SQL got famous the at that point it was around 1970s at that point the memory was very costly so we have to make sure that we use minimum storage and we can store maximum data and that's why the SQL type of table makes sense now if you talk about the current world we have so much of data which is unstructured you can't actually create the columns based on that so what if your table is created and now you want to add a new column trust me it's a big headache in SQL that's one so basically we don't want to use SQL here the second one is you want to scale your application you want you want to have replicas of your of your database it is very difficult to do it in SQL of course you can have replicas but then it's very difficult to sync it so no no SQL database excel in that and we can Implement that with the help of mongodb so mongodb is a nosql database where you don't have tables what you have is documents so if you want to relate it's very easy database database is there on both the side in SQL we use tables right which is which has data here we have collections so we can have tables related to collections in SQL you store your data in the row format right so if you want to represent a particular entity let's say I want to store my information my name my age my phone number my email ID that becomes one row now that in nosql becomes a document now the way we can have multiple rows in SQL we can have multiple documents which is a part of a collection that makes sense right so basically whatever data you're storing in fact the data which you have here this is a profile this is a description this is the experience and this is a technology you know so this becomes one document and if you have multiple data that multiple documents okay and we don't have to install any software for mongodb because we are going to use the cloud service and trust me this skill sets which we are going to do in today's session is very important okay so we are I already have a intj on my machine which is intell community version we are going to use that but then we want to create a spring project right and since we have a community version I'm not paying for it so we have to make sure that we have that second we want to create a service we want to create a mongod de service right so basically what we need to do is first we need to get the account on mongodb Atlas okay it's very simple you can just click on it and the important thing is it will not ask you for credit card information hopefully let's see if that uh is asking for you uh so I will sign sign up with my Google account and this should be a quick process so click on submit you can choose your email ID and you can choose you can click on submit I normally prefer to have Google login everywhere so that I don't don't have to remember the passwords so it says welcome you use your account to deploy a cloud database okay that's what we are going to do and it's still building it okay so basically it is asking you some questions you can just enter okay we are here to learn uh mongodb whatever application we are building we are building content management and then preferred language so basically we are going to do that with Java now that's a beauty about mongodb okay which is you can use multiple languages here now since we are going to use Java so I can click on Java here and click on finish now if you're getting confused how exactly the document looks like so if you have ever worked with JavaScript JavaScript object you know how you create that's how the document is getting stored so basically that's a Json format for us but in the storage it is getting stored as Bon which is binary sun right which is Javascript or we can say binary Json to make it easy okay they still loading the page maybe the internet is low on my side and that's why it is we got our story the only thing is it will ask you what type of service you want okay so if you can go for serverless you can go for dedicated but you have to pay for it now since we are into learning stage we can select the free one so you can you can click on share and you can click on create it will create a cluster for you now of course this is not a point where we talk about cluster imagine you getting the services and the beauty is you can choose any of these cloud service so mongod DB behind the scene will allow you to choose AWS or Google cloud or Azure so we are going to use AWS here okay so region is Mumbai by default you selecting the good things uh don't need to change anything here the only thing I want to change here is the cluster name I will name this cluster as Tesco and click on create cluster the next thing is it will ask you for the username and password to protect your database right if you want to connect you have to enter the username password so you can see we have the root uh by default I've enter entered this before as well so it is taking the default values so you can enter whatever data you want to have I'm just typing root and root and I will click on create user so my user is created quite simple right uh the next thing I want to do is I will be using this from the local environment so I'm connecting my application which is running on my machine with the mongodb address so we have to enter the IP address I want to protect only I want to protect this by allowing that only my machine can use that database so I can click on ADD my current IP address click on that and click on finish and close so by doing this you got your database deployment so as you can see our database is created now what's the next step in fact we got our cluster not the database if you can see we don't have a database yet so it says database deployment but then we need to create a database so you can see we have an option of database here when I click on database we already have one now what I will do is I will click on browse collection so basically we don't have a database we'll create that in this so this is the dashboard and we can see we have an option of monitoring but we not do all those things let's click on browse collection and as I mentioned the tables in SQL you can map it with the you can map it with the collection here so let's click on the browse collection and it's trying to retrieve the database and you can see we don't have it yet so databases number of database is zero so we have a cluster ready which is teliscope and we need to create a database now so click on add my own data so basically you can also load some sample data set which they provide you uh so if you don't want to have your own database or stuff I mean if you don't want to have your own data so you can click on load sample but since we have our own data we can click on add my own data here and it will ask you for database name so I will say this is for job listing and then the collection is job post in fact you know what I will do I will also keep my database name asisco just just to make it easy and the collection name is job post so click on create okay so as you can see we got our database name as telescope and we got our a collection name as job post and as you can see we have an multiple options here we'll see this aggregation search index later so we'll have a separate video on it uh and I also try to explain some of the more features but as of now just to keep it simple let's say this is our data here and as you can see we don't have any data it's empty so we can click on insert document because I want to see some of the listing some of the jobs so I will click on insert document and this is how you can insert and the beauty is this data looks like a Jon format maybe a key value PIR just to simplify that now I don't want to enter the data by myself I already have some data set ready with me so as you can see this is the data I already have here so I will just copy paste this part into this so as you can see I have inserted data now how this data looks like so you can see we have an ID here we got description the same thing which you were able to see on the page we got exper I profile and the Technologies which you know and you can see this is an array basically in Java now how can you relate this with Java pojo so in the Poo we create a class right and we have different fields example if you talk about a person a person will have a name a age now that thing comes as a key value pair here so you can say this is age uh this is name and stuff so I will click on insert it will insert some 11 10 11 records and you can see how many records we have so in total we have 10 records that's great so even this this enough in fact sometime in real application you'll be having lot of data to play around but let's say we have 10 and I want to fetch this data from my Java application okay so remember this this is the thing we are building so if I remove the entire stuff this is what I want to fetch so what I will do now is so this thing is working because I already have this project ready in my intj so if you can see I was just doing some testing here so the project is ready but we are not going to use this project so I will stop this the moment I stop this project from the spring board so you can imagine we have two different application running the front end and back end so what I will do now is if I go back to my browser if I refresh you can see data is not coming you know why because the server is not running of course you can do you can print some message here the server is down or something of course that comes at a later part uh but as of now I'm not printing any error you can check in the console log it will show you the error that Ser is not running or something okay so this is what I want to make it work and as I mentioned before you can also use a Swagger API so let me show you how what Swagger API looks like of course we are going to build this the entire stuff one by one so let's say if I run this project and if I go back to my browser and here I will run 880 port and you can see the Swagger API is running with that so if you don't want to work on our UI part you can also use Swagger and you can see we have a post controller here and these are the options you have so even if you don't want to use frontend you can use Swagger you can get all the post so you can see if I uh click on try now execute it will show me all the documents now this is coming because the server is running okay now if I stop the server even this will not work so if I click on execute now so you can see we got an error in fact this Swagger itself will not work so you can see that this is not working so if I open the Swagger homepage it's not running so we have to make it work okay now for this what we going to do we have we have to build the project from scratch so basically we have to go to one of the page which is start. spring.io now basically you you can use int Enterprise version you can use Eclipse you can use SS your choice but I'm going to use intj Community version where you don't have an option of creating a spring project by default so I'm going to use start. spring.io which is spring initializer now in this we are creating a mavin project so basically imagine you're creating a project and you need certain modules you need certain project structure so you can click on mavin Project here and we are going to use Java language the next thing is you have to specify the version which is 2.7.4 the only thing is if you want to use swag this version does not work properly so I will change the version once the project is created but if you don't want to use Swagger this perfectly works the next thing you have to mention is the group uh so I will say this is com. Tesco and the artifact is job listing okay that's the project and we can keep it as it is this is the package name that works for me and then we got the jar file so we have to create a jar file here so once you deploy the application when you export when you create a package the packaging will be happening as a jar not one and you can choose a Java version 11 works so we can go for 11 now that also depends upon what setup you have in your machine so you need to have a code Java setup basically jdk setup uh 11 17 works you can use that 19 just launched so you can use that as well but I'm a I'm a person who prefer stability so I will go for 11 the next thing is you have to add dependencies now what are the dependencies dependencies we need here so basically we are building a web project so I will use a web spring web okay uh the next thing I will need here is mongod DB so I will search for for mongodb so basically we want to go for spring data mongodb and the beauty is you know all these things which will happen happen with less amount of lines and trust me it's beautiful so if I click on this uh you don't have to write a lot of lines and sometime It's Tricky when you have lot of lines so I will click on generate and you can you can see we got our project here so what I will do is I will just unzip this project so you can see this is the project which is unzipped okay so I'm making sure that that project belongs to idea project so in my uh user account I have this folder which is called idea projects I'm I'm keeping that project there so that I can open that in from intj so I will open intj of course when you open this you will not see the project so this is what you will see and this is my demo project which I've already done and now I will click on open project from here so basically when you open intell idea of course this is the community version free one so I will click on open and navigate to the idea projects and in that I have a job listing click on open okay we have to trust we don't have a choice and we got the empty project as you can see we don't have a code here but I just want to verify if my pal file has it so pal file you can see the version is this this will may not work with you so I'm going back to 2.5.7 there's not much change between these two versions and it perfectly work so this version normally works with swagger and so you also do it when you are trying this also try with the older version and once it is working then try to change the version to see if that if that is working okay now these are the dependencies which I have right so if I it you can see it is is downloading dependencies whatever jar files you need in this project will be downloaded but till that point we also need two more dependencies which is for Swagger now how will you get this dependency it's very simple uh you can go to one of the website called mvn repository and you have to get this two the Swagger UI and Swagger 2 okay so you can click on that search and you can click on the first one the version which normally works is you can go for 2.9.2 you can see we have lot of use here so I will use this particular version copy this go back to your code and paste the same thing you can do for the second one with the Swagger 2 so you can see I'm searching for Swagger 2 here the first one and the same version make sure that you have the same version so that you will not get any conflicts and paste so you can see we got these two libraries and if I open my external libraries you will find all this uh files here so you can see we got mongodb we got spring framework files and this should be for Swagger as well it will be there don't worry you it will be there so let's say if I minimize this now so we got Swagger in in case it's always good to refresh your Maven dependencies you can also click on this Maven and click refresh it will make sure that you have all the dependencies required anyway otherwise it will give you error here if it is not working you can see we have all the jav files and now it's time to write our code the first thing you will do is if you go to your main see ultimately we have to connect our application with mongod DB right so we have to specify ify where you do you have your mongodb do you have your mongodb in your system or you're trying to connect the cloud that's first second what is your database name what is username password of course right for connection you have to pass the cenes as well so what I will do is I will do all the configuration if you can see we have a resource folder in which you have application. properties this is where you do the configuration now what's the configuration I need here so basically I need spring. dat. mongodb do URI so this is the the uh First Property have to assign spring. data. mongod b.u and we have to assign a URL now from where you will get the URL or URI in that case we will go back to our mongodb the cloud and you can see we have we should be having an option of if you go to overview you can see this's option of connect okay so if you click on this connect here it will ask you for what type of from where you where do you want to use this URI so I want to say I want to connect to an application so click on this now it will ask ask you which language you're using so we are doing it with Java and the version is this and this my friend is your URI so I will copy this and paste done okay the things we need to change you can see it's a long string the things you need to change here is you can see this is your cluster name and this is the URI but password is not mentioned here so you can enter the password manually so the password for my machine is root so username root password is root and that should work the next thing I want to do is this is where I want to specify the database name as well so basically this is the property so string. dat do I mean spring. dat. mongod db. database and this is where you have to mention your database name and I guess database name was delisco itself so we'll skip it delisco I can just verify once again I can go back to the collections part here and you can see we have a database which is Tesco so use the same name so database name is telis even my cluster name is TCO you can have a different uh so make sure that you enter your database name here okay that's it this is the property have to mention and now I can simply close this I don't even need the form file close this okay now what next now we have to write a code to make it work so what I can do is I can go back to my Java and you can see we already have an application here now the thing is I want to enable the Swagger API and as of now we don't have a Swagger API enabled if you don't want to use Swagger you can skip the entire step and since we are not here to learn about Swagger and stuff so just to give you an idea it helps you to request to your web service okay now if you don't want to use Swagger you can skip this step so if you can see I have just copy past it and this is the only thing we are copy pasting okay because we are not here to learn about Swagger so the only thing you have to do is for the application you have to enable the Swagger 2 and we have to import the package for this uh we have to we have to make a bean because this is how this Swagger will loaded as I mentioned before this is a optional step for the project so I'm just I'm not explaining this stuff because I just wanted to show you how do you build an application uh let me import the packages okay and the good thing is I will make sure that this project is available in your uh in the link in the description as a GitHub link okay so this is done we have done with our mean okay now this is only for Swagger and we are not going to use this file anymore so we are just enabled this Swagger okay but then now I want to send a request to the server right now how do how do we do that so in Spring framework basically we create we use something called Spring MVC which is model view controller and here we have to first create a controller and you can see we don't have a controller here so what I will do is I will create a new class and I will this is a class basically the name would be let's say post controller because now we are going to work with the post right job post so instead of saying job post every time I will say post controller and that's our controller here now how do you make this a controller it's very simple you you make you make this controller with the help of rest controller okay and in this you will write your methods so for every request you want to send let's say I want to fetch all the post so I can use the URL which is Local Host colon 8080 that's the that's the port number where the server will be running and then I can say slash all post it will give me all the post what if I want to submit a new request so I can say SL poost with the post method uh so the first first one was get method the second will be post method and what if I want to search based on a term and for that we can use again a get to fetch but in this particular video we are going to focus only on how do you get all the records okay that's it or maybe I can also do it for the submit part but the first thing I want to do is I want to enable the Swagger right so in that case what I can do is I can create a method and this is only for swaggers I can say redirect so every time someone send a request for the homepage it should redirect to the Swagger page and to do that we have to import HTTP Ser response response okay so basically what I want to do is every time someone request for the homepage okay we have to do the do the mapping for that as well which is request mapping so when someone request for the homepage we have to mention that hey call the Swagger API okay and to do that what I will do is I will say response. redirect and in bracket I can mention Swagger do Swagger ui. HTML and it is giving me some error okay it's asking me to add the exception so I will say throws iio exception and that's it the Swagger UI will be ready let me just prove you that now to prove that I will run this application first I will right click the main uh application and say run okay it will take some time I hope it will work without any error okay you can see it is working and how do I verify that so I can just go back to the browser and okay Swagger API is not working refresh enter and you can see we got our Swagger API it's simple and this is our controller name if I click on this controller you get all these options by default but I don't want this options so in that case I can I can what I can do is I can just write API ignore API ignore okay that's it so when I say API ignore and the server go back here and now when you refresh so you can see it has removed all the pre predefined requests we want to Define our request by ourself so I can say no operation defined in the specs okay that's good so what I can do is now I can Define the request now what request I want to send so basically here I want to have a option of fetching all the post now for that I can create a mapping so basically I want to what what mapping we want to do we want to map it for all the posts so I can say public which will return the least of post now what is this post hold on so for a document I also want a pojo right which will map the document and pojo okay we don't have a pojo here and the first and one more thing the post controller should be a part of a controller right so let me just move that so I will say new package and I will make this as a controller and let me just move this I want to create one more class here in fact one more package first package and this is where I will keep my models and in this model I want to create a pojo now what pojo a mapping class which will map my Java class with the uh document so I want to name this as post itself the job posting okay not the post method of HTTP the job posting and here I want to have some variables now what are the variables I need so if you compare with the database if I go back here so you can see we have a description here let me just increase the font size a bit it's not visible so you can see we have a description we have experience we have profile and we have technology which is an array the same thing you'll be doing here okay so what are things we need in the post so first of all we need a profile right so if you remember database we had profile description Technologies and experience so let's put that so we got first thing we got we are going to get is the profile then we are going to get the description then we'll need uh the experience and the technology so we can say text that makes sense okay but then this text will be an added and that's done of course you can keep this bracket here as well that works and for this I need Getters and Setters so I will right click I will say generate uh I want a geta set up for all the fields and click on okay and with this I also need a Constructor generate a Constructor with in fact I I want a blank Constructor without any field and we also need a two string in case if I want to print that's it so I will say right click generate two string okay so we got our post right it's so simple okay so going back to the controller we got our post now so we can import the package that's imported and we have to also import the list that's done so if I want to fetch what method should I have so I can say get all post even that works and name doesn't matter right so we can say get all post and now I want to do a mapping for it so when do you call this particular method see of course the homepage is done now hom page Returns the swager page but let's say in this Swagger if I go back and I want to have let's say uh a URL which request for all the post it should fetch the post so in that case I can just go back here and for get request I can say get mapping and the URL which I want to have is let's say all post so if we are requesting for all the post or maybe I can simply say posts that works right so we can say posts and it should return the post but then how do I return the list here byy to return empty that works now if I want to fetch this data what I will do now is I want to return the list how will I get the data so of course I want to fet this data from database right now if you know code Java jdbc and database we have to write lot of steps right but not in spring so we can use spring data remember we have added one of the library for spring data this one spring starter data so basically we can use that to F data and for that what I can do is I can create a new uh interface basically not the class interface and we'll name this as let's say post repo or we can say repository okay now this is this is how you will fretch data okay you don't have to write any methods any implementation I know that sounds new for some people they're like hey without writing any queries without writing anything how can I FES data let me show you the magic now so what I not the object but not of not the object of the interface but behind the scene the spring framework will create a class for it it will create an object for it you don't have to do that and I will simply say this is a repo and I will just map this as autowire So when you say autowire it simply means spring will create an object for you and it will map it that's it the that that auto wiring part and you don't have to do anything else just come back here and say repo do find all can you see that we have so many methods we have not return those methods it's in build in repository that's it I know you will not trust me at this point the only way I can prove my point here is by restarting the application and I know you might be saying there's a difference between the time here is just that my camera was not working properly so I have to take a pause and do it again and if I refresh oh can you see that we got all post and if I click on this hopefully this will work it's already been a lendy video I will click on try it out execute Ute oh okay we got data but it's not printing data here oh something went wrong here so we're requesting for posts and it is requesting for the Post find all okay now what is missing remember when we go for the Post repository we have mentioned that there's a post right there should be mapping but post is not aware of that so what we have to do is we have to say document here and we have to mention which collections this will map to so this will map to okay what's my collection name if I go back to my thing it is drob post so we will just come back here and say job post now this should work so if I restart this server uh there's no error and if I go back to my Swagger API hopefully this time it will work execute oh can you see that we got the data this is what we wanted right oh it's working and if you look at the code it's so simple we just got a controller here where we are doing the mapping we got the post repository and nothing much here we're not declaring any methods or stuff and if I look at the post this is just a pojo so fetching is working that's awesome how about posting the data what if I want to submit data Maybe I want so apart from this all post I also want to have post which will submit data a post method let's try this should be simple so I can create I can simply create another method here for posting data what if I want to get my own records so I can create a method which will return the post itself so whatever post you are submitting it will return that and I will say add post of course method name can be anything and okay the mapping what type of mapping I want to do any guess we are submitting data so for submitting we have to use post mapping and here we have to specify the URL I can I say URL post see when I say post for get it is it will fetch okay so in the method we are specifying it's get method so it will fetch in post we are submitting data okay this post and post might be confusing but this this post is basically the job post this post is sttp Method but then when you submit data we have to also accept it right here so I can use a request body here to accept now see if you don't understand all this concept see flow is something very easy right easy right but what if you want to do do it by yourself don't worry I have a spring boot tutorial you'll find the link in description which explains each and everything properly this is a project video right so I'm just focusing more on the project part okay so request body and here I can mention the object where it will receive the data so I want it to receive data in a post format so whatever data you will submit from this from the client side will be accepted here in the post object I know that's something which spring is amazing at okay once you got the data I can simply say repo dot uh okay how do I which method I have to use for saving I I think there's a save method yeah there's a save method which will accept the post object that's it that's it no need to mention any query no need to mention any code for it we're done with this but I guess save returns something okay what I will do is save Returns the object of post so we can simply say save it will return the post object and we're returning it that's it and here I think it should work if I go back we have to rest the server so you can see we are fetching data we are also submitting data and if I go back refresh okay post can so that we got post here that's that's awesome and you can see it will give you some demo data as well so that it is already mapped right so I want to submit data I can say description okay description first of all I will set the profile profile I'm looking for is blockchain developer and here description can be ethereum developer with daps experience in Mumbai or maybe I can say this time I can say pun okay experience I need only two years BL developer technology what you should know let's say you should know Java I don't know why you should know Java but let's say Java then we can say solidity and you need to know XML I don't know why but let's XML and let's click on execute it should work oh it worked can you see that we got the data but then we are getting the data on the client side right let's verify from database so if I go to database now and let's see if that is there let Let me refresh the database hopefully should be there it should be there it should be there if I scroll down down down can you see that we got the blockchain developer ethereum developer whatever we have written there and the array of your skills awesome it was so simple right so basically we were able to request data we were able to save data but of course at this point we have only worked with the swaga part we have not connected with the UI uh we'll do that and also there is no searching option okay we have not created something for searching we'll do that in the next video so in the next video we'll also try to understand how will you use mongodb what is the feature it provides and then we'll explore one more application which is called mongodb Compass okay which is a UI basically I'm just everything which I want to do I'm just going to the browser right and I'm accessing the cloud service what if I want to access everything on a desktop application some sometimes is very easy to access a desktop application than going to the side so we'll try to explore that as well in this video we'll focus on two important features of mongodb the first one is the mongodb compass and the second one is mongodb atlas search that's right search because remember in the first video if you have seen it so basically we were able to search the data from the document and irrespective where that data is is it a key or in the values okay somewhere in the description there was something mentioned it was able to search search for every text not just the title okay so let's try to implement that so how will you get the compass so there should be an option here otherwise what I can do is I can just go to Google and search for search for mongodb Compass and this is the website in fact you can directly click on download but let me just go through the website first so this is how the mongod DP Compass website looks like so this is a GUI for mongodb that's right so basically you can access the mongodb address from the browser in fact you know we have multiple options you canuse use mongodb on your local machine when you do that basically you have to use the console and you have to type a lot of commands and if you of course as a programmer it's a good thing but if you want to use a GUI tool you can use compass for that as well which connects with the local system or you can use Compass to connect with the cloud as well you have a choice so this act as a GUI for both and for this example we're going to use compass for the cloud so you can click on download now it will download and it's a straightforward setup you can just click click next next next uh nothing difficult it's just that you have to specify which version of compass you want to download uh which platform you're using and what format you want to download so click on download it will give you the setup now once you click on the setup once you click on next and install it's very easy uh nothing fancy there the only thing is when you open mongodb compass you need to do some configuration that's it when you open it and I'm just I have just clicked on it it may it might take some time so this is how your mongodb compass will look like and you can see it will ask you for the connection now mongodb Compass is not sure do you want to connect with the local machine or from the cloud now based on that if it is a local address this is support number you can click on connect but I think in my machine I don't have a local setup you can see connection refuse I don't have the local setup but what I have is I have a cloud and to connect the cloud you have to enter the URI oh from where you will get the URI remember in the project we have done that but if you have not following the project that's completely fine when you open your uh let me just start from the Cloud itself so when you go to the mongodb atlas and when you have your database here you can click on connect and when you click on that you can see we have multiple option but this time we are going for the mongodb compass and the beauty is they both have the same URL so we click on this and I will say yes I have mongodb compass and the version is this and later this is the URI so just copy this and for my database I have a user name which is root and the password is also root so I will replace that with with this so here I just have to replace this part with root that's it and click on connect I hope it will be connected oh it is you have to also mention the database right why just giving test database I want my database it should be theis let's try so click on connect and it says connecting oh it connected Can you see that you got your host and that's the beauty of mongodb so every time you create a database it will create multiple replicas of it of course we have to be fail safe right what if you have only one server and that goes down you need a backup servers so mongodb for every database it creates two extra copies for you we call them as replica set so in total you have three working databases at the same time and you can see that is what you can see here on the left hand side if I click on databases here it will show all the database which we have created and we have created only one telesco it was by default and if I click on telesco database you can see it is giving you option of the collection which is drop post if I click on this we got all our records so it's better to use the GUI then using Cloud uh the only drawback which I can see is if your machine is slow Compass will give you issue that time you can use cloud but it's not that heavy normal machine can take it up so again it's depend upon the developers what they want to use I normally prefer to use the cloud service Some people prefer to use the GUI tool to do everything or the desktop application but again that's that is preferred depend upon the people okay now this is the first thing I wanted to show the next thing is I want to implement the search feature and this is the beauty about Atlas search so in the mongodb the atlas we use something called a search option remember when whenever you work with data and we have created database right and normally we only had around 12 13 records or 11 records in normal databases which is production grade we have thousands of Records oh that's less we have millions of Records now if you want to search for a single word from all the records trust me it's very timec consuming so basically as a user of that application when you type you have to wait for minutes to get the output and we don't have that much of time right so how will you speed it up and that's where in the database concept we have something called indexing so every field every text will be indexed so that it will be easier for search okay but then how will you do that do you have as a developer do you have to do everything uh not exactly if you can see the the same option is there in the on the cloud as well okay so you can click on this index and you can create index from here but since as I mentioned before I'm a big fan of cloud so I will be doing that from the cloud itself so let me go back to the cloud service and if I go back here okay so I will click on my collections there is an option of creating a search index so if I click on this index okay this is where you can create your index so it says make your data more discardable with Atlas search and I can create the index here the only thing is when you're creating the index it will give you two options Visual and Json I'm a newb to this so I will click on visual that's what you do right when you learn something new you go with the basic steps keep it simple learn the first layer and I always believe in the learning where we have onion uh peel like learning right we have different layers so you learn the basics and then you learn the advanced part then you learn the word Advanced part so we click on next it will ask me for the index Name by default the name will be default keep it that as it is you can change it but let's keep it default and you can mention what database and collection you want to index I want to I want to index the job post if you have multiple databases and collection it will show you the options you can choose one and click on next and that's done oh we have to click on this create search index now before I do that in fact let me just do that and I will show you something there's interesting thing called index Fields there's option of dynamic so what happens is by default it will IND it will try to index everything from your database or everything from the from the collections it's good but then if you have a huge database and if you have huge number of documents it is very timec consuming because it will it will take a lot of time second it will consume lot of memory and sometime you don't want to do that why you will not do that because what if you have multiple Fields let's say we have eight or 10 fields in the document and not everything need to be indexed maybe you can specify these are the three three fields which I want to index example in our case maybe I want to index description I want to index the profile that's it I don't want to index other things I can specify that with the static mapping by default is dynamic it will map everything I can also make it static now even for 11 records it is taking so much of time maybe it is done I'm not sure let let me just refresh this so you can see we got only 11 records and it is working now so the mapping is dynamic mapping here and if I yeah that's done so indexing is done now let me just go back to my collections so that's the first thing if you want to apply the search we have to first create the index and now using that index we can actually test it uh so okay how do I test let me just go back to search there should be an option of testing as well click on visual editor okay so if I click on visual you can see we have an option of search tester it is indexed right it should be able to search now example let's say I'm searching based on Java so if I say Java and if I click on search it will show all the documents where Java is mentioned in the profile in the description or in the Technologies example in the blockchain developer we don't have Java mentioned in the profile and in the experience in the description but it is mentioned in the Technologies if you can expand this you can see we have a Java here so basically everything is indexed it is easier to search and you can use the search from the from the client side but you will say hey that works right I mean this is working from the cloud what if I want to do that from the Java code that's tricky right how will you do that let me show you how to do that so if I go back to the collections and if I click on aggregation can you see that we have an option of aggregation now this is where you can create a pipeline what you want to search and you it's not like you you can search only one thing maybe I want to search based on Java and even for Java let's say we have 50 records so let's say in total we have 200 records and if I specify Java it is giving me only it will it is going giving me let's say uh 50 records now out of 50 records I only want five and that two in the sorted order of experience can we do that so basically you can see I have applied three filters the keyword Java second I want to sort it and I also want to limit the number of post which I can see so we can do that from the aggregation uh so if I click on aggregation here it's taking some time for the aggregation Builder so if you don't apply any filter it will show all the post so in total how many post we have uh so we can see we have 11 documents here but then if I want to apply a filter I can do that but from here so you can see we have an option of stage by default it will give it will show you one stage I will click on select now you can you can basically apply multiple operators here the first op which I want to use is search so if I scroll down you can see we have an option of search and this is what it will give you so I will just remove this extra things from here now index what index we have remember when we created the index we have to also mention the index okay so if index has not specified or not created this will not work so you can specify the default but even if you don't mention that's fine since it is we have a default index we don't have a different name so you can skip this part next you can specify the query what are searching so basically I'm searching for Java in description or maybe I can specify in text now you can see it will only it is only giving you eight documents so out of 11 documents it is giving you eight documents here which has Java in it okay that that makes sense now you will say what if I don't want to just search Java in text let's say I want to do I want to search for Java in description profile everywhere so what you can do is you can make this as an list so we can say list of text comma maybe I want to search in description maybe I want to search in profile so basically we are searching for Java in everywhere and also we can we only got eight documents which is there so you can see we got Java in profile uh we got Java in profile okay we got Java in Technologies this time right and we got Java in um we have Java here so basically this is how you get it this is how you apply search what if you want to apply a sorting on this how I want to sort this I want to sort this based on the experience you can see we got experience 10 we got experience five I want to sort in ascending order so what you can do is you can apply a stage here that's what aggregation is pipeline right the first search then sort it then limit it so we can expand this and I want to sort so we can just scroll down here and you can see we have an option of sort now it will ask based on what field you want to sort I want to sort based on experience ience and sorting order so if you say one it is ascending minus one is descending if I'm not wrong yeah so one is ascending so you can see the experience we got experience here the first profile has experience one second profile experience two it is working you can see that we got experience five experience 10 and then least goes on it it keeps increasing right so the Sorting is also done and that's eight documents but I don't want eight documents I only want five documents again I can apply a stage and this time I want to do it for I want to limit right so limit should be there yeah limit is here and it says how many documents you need mention the number I will say five and that's it you can see here it is loading sample five documents and we only got five documents it's that beautiful right and yeah you got your pipeline now you will say okay that the pipeline is done here itself what if I want do that from the code we still AR we have we have still not done that right okay that's one thing we'll do that okay in the next video we'll try to implement this feature in the Java code but before that I want to mention one more feature of mongodb now mongodb Atlas has one more feature which is client side field encryption what it means example let's say you have a client application where user is submitting data now it can be your name it can be your age it can be your email ID that that works if if anyone can see that even I don't think email ID should be shown email ID SSN maybe there are s sensitive information so you have a bunch of data with you and some of the data is public is okay for you but I want to encrypt some of the fields maybe email ID SSN number or whatever fields we have how will I do that I what I want is from the client side the moment your data goes on the public database of course right in mongodb behind the scene it is using AWS Azure gcp which is a public Cloud I don't want to compromise my data based on their security how about encrypting the data and sending it there so basically it provides you something called field encryption where you can specify which field you want to encrypt from the server side okay let's say this is your server that's your database the moment you send your data from server to database it will get encrypted okay so the what you save when the database is encrypted but what if you want to get data back when you fetch data from database it will be coming in encrypted format itself but the moment it it is received on the server where you have your driver it will get decrypted okay so basically you don't have to do that encryption decryption uh Atlas will take care of it of course we're not going to do that in this course because that's a way out of the curriculum but yeah we can do that again try that there's some amazing documentation available I will try to put that link in description as well otherwise you can search for client side field encryption using mongodb and you will get the documentation now basically till this point we were able to create a spring boot project which has basically some controllers using which you can request to the database which is in mongodb and we have some uh documents available there we have a collection using which you have fetching data and then we were able to fetch data we were able to submit a new post right we are basically working with a job post application here and then in the previous video we have also seen how do we create a search index how do we create a pipeline and then we were able to filter data right so basically we were able to search we were able to sort data we were able to limit if you have not seen those videos first I would recommend you to watch the first two videos to get proper understanding what we are doing if you have already seen that let's continue so basically at this point we know how to do it from the mongodb aggregation pipeline console and we were able to get the data right but now I want to achieve that with the help of java so how do I fetch data from Java application whatever we have done here and I think that will be tricky right it will we have to write multiple lines of code multiple files to filter out and all those stuff let's see how complex that would be apart from that we'll also connect our application with the react project remember we have the react project we which was not working because we we stopped the earlier project which was working and then we are building the Java project from scratch so we'll also connect those two applications let's see how so basically uh the pipeline is ready here right so this is what you got but now I what I want is I want to do the same thing from java so maybe as a Java developer when you maybe we can create an API where you can search for particular term and it should do those processing here now to do that it's not very complex okay so in the Java code which we have here we can add one more controller let's say Let Let me just add one more controller here which is public uh it will return the list of post of course the same thing but this time it will be filtered right so the difference between in fact let me just write it next to or just below the get mapping this one uh so this should be similar right maybe I can say search to make it simple so we are searching ing for something right so it should also return some data so I can say return repo doind all as of now let's let me just go with findall and the request for this would be get mapping and I want the request type should be uh it should be for posts the only difference is in the post I will also specify the text which with which I want to search example let's say I'm searching for Java so it should be Java but then I want to make it Dynamic so I can set text whatever is user is entering so if a user says Java this will replaced by Java if a user says blockchain or maybe Mumbai so whatever text a user want to search this will get replaced but how will I accept that in a variable so in that case we have one more anation which is called path variable and I can say string text so whatever data is coming here example the URL is let's say posts SL Java now this text will be replaced by Java and that's what you will get here okay as of now we are just doing find all just to see if this works and whatever text you are passing here and how do I verify that let me just run this code I me restart the server and okay everything is working there's no error let me just go back to my browser and Swagger the UI let's refresh controller uh you can see we have three now so initially we had only two this time we also have a text search if I click on that it will ask you for a text oh we have not um okay try it out it will ask for the text let's say if I say Java and if I let me very specific which is Mumbai because we only have one or two with Mumbai post but if I execute you can see we have received multiple not just two if I'm not wrong you can see we have received multiples filtering is not happening okay we have to do something for that right now why it is not working is because it's very simple we are saying find all we don't we should not be saying find all we have to be very specific can I say search by text I mean whatever text I'm getting here it should search by text or maybe I can say find by text so I can say find by text uh but you can see it is giving you error because there's no method which says find by text uh that's tricky so what I can do is instead of touching our post repository which is very specific so post repository is extending repository here which provides you basic operation fetching data posting and all those basic features right c c operations create read update and delete if you want to do something else you want to search based on your own condition that's where we have to implement it that's where we have to do some customization so what I can do is I can just use I can just create one more uh okay this should be a part of something else this should be a part of a repository package so I can say this is repository and I want to move this post repository here and in the same repository I want to create one more class now or interface actually which is let's say for search search post or maybe I can say search repository I'm just finding a good word for it so I can say search repository that's it now in this what I want is I don't want to use any mongodb uh already present methods I want to create my own so can I create a method in fact it's an interface I can declare it here so I can say find by text which will accept a string I can say string text right and it should return the list list of posts so basically I'm creating a new interface but then the only thing is this thing is not present in the spring data or mongodb features we have to implement it by ourself okay for that what I will do is I will create a class which basically implements this I can I can say search uh repository implementation and this particular class should Implement our search repository as I mentioned we have to implement this code by ourself right uh okay it says we have to implement a method which is this find by text so basically we have to return a list right so what I will do is as of now let's keep it empty so I can say list of post and I will also make this as a final variable because it's good to have a final variable when you when you know that it will not be changed okay so I will name this as posts itself s equal to it's an array list so I can say array list okay that's it so we have created this array list here and we have to import the package is it done am I missing something here oh I'm missing a new keyword my bad okay so you can see we got the list here and the same list I will return of course at this point it will return a empty list right and I just want to verify if it is doing that so I will rest the server at least should return the empty list it should not be giving all the data okay it's not working oh I forgot to one more thing this is a new class which we have created right and I want my thing to read this so I can say it should be component yeah one more thing in fact in the post controller we still have an error we don't want to say repo right repos don't have find by text what what where do we have find by text it is there in the search repository so apart from the post repository I need to create one more which is search repository and I can call this as sreo which is for search I'm very bad with names so let's go for S and here we'll say this is auto wir so basically now we have two different repositories one which is getting the features from Spring data and second is from the what we have created and instead of creating this as repo do findind text we have search repo by find text it is asking for the text as well let's enter that so at this point things should work and we should get a empty string let's let's verify if that works let me just run this application uh no error let me just go back to my Swagger and if I say execute now it should return the empty at least that's the idea yeah so we are we getting to empty right at least we are not getting everything now we have to make sure that we fill this up how let's go back to our code and we just need to change this particular method we have to fill this list how will we do that it's quite simple actually uh if I go back in fact I don't know the code right so what I can do is I can just go back to my cloud and see the beuty here uh if I you can see in the aggregation when we do all this pipeline here when we created this pipeline here there's an option of export to language let's see what happens if I click on this uh this is for this is the pipeline which we have okay for mongodb but if you want a language specific code this is for python what if you want to do that for Java this is a code that's it just copy this code and it will work uh not exactly uh we have to do something extra as well so we need to import you can see we have two check box here let's click on this click on this you will get the entire stuff so the first thing we need is this import libraries and copy and paste and if I go back here paste I I I need to import all this the only thing is uh I think mongod DB client is not a part of in fact you know what I will do I will not just use import because there's some version issues and uh I was able to resolve that by not copying it okay uh the only thing you will need here is uh do we need mongodb client okay how will I get mongodb client here okay it says we have to create an object okay I will not do that what I will do is I will just go back here and since we using spring framework we have we can do something extra here I can say client and I can say this is client I will just declare it I will not create the object I want spring framework to do it for me so that will reduce that line so we don't have to use this line now the next thing it will ask you for database connection now we already have a database connection uh but just to keep it simple I will use the same line I can use the entire code from here and paste it inside my method here that's simple okay it's just that now uh it's not more no more mongodb client we just saying client and we have to import all the packages for mongodb we have to import for control space anyway we have already done that before right so this should be coming from the B is it Bon uh yeah and then we have to import for mongodb collection as well from a client and then we have to in fact we'll change this we'll not be using find it trable uh because when I was trying it it was giving some issues and I found a solution inste of going for find it trable we are going for aggregate iterable you know why I'm why I'm doing that because this aggregate method returns a aggregate trable so let's go with that let's import it for arrays as well and done this is your code I know this doesn't look good because it's not formatted properly so I can just do the formatting here so that you can see everything in one line so basically let me explain uh in brief what we have done so basically what it does is whatever we have done in the pipeline here right so this pipeline is converted into Java code so basically it is using a document search you can see we are doing a search here search is happening based on a text so we can also replace that with a wild card uh and then it is asked for the query what is your query we have specified the query is Java of course this should be changed this should not be uh Java here we'll replace that later and then it also ask you for where exactly you want to search you have to specify the path so I want to search this in uh I want to search this in text uh Technologies description profile I also want to specify I want to sort but based on what I want to sort based on the experience and one is ascending order and then we have to limit by five I I only want to see the five post but then I don't want Java here what I want is I want text so whatever text I'm receiving from here that will be the search term okay this works but then if you say we only got data in result I want this data not in result I want data in a post how would you do that so maybe we have to go we have to iterate we have to search one by one post and put that into post how do you do that okay so how do you get data from result oh there's one more thing missing here the database name is theis you can also get this from the uh application. properties you can just see uh we already have a database name here so you can fetch data from there you can fetch this value but just to keep it simple since we got code from the uh website itself we'll use that and here we need to make sure that we have okay we are fetching one by one so what I can do is I can use a result here and result is iterable right so it says iterable so we can iterate between the elements and if I say dot there is a method which is for each so basically we will iterate between each document so I got a document here and I'll will be using a Lambda expression here which is much better so whatever document I receive for each iteration I will add that in a post as simple as that so I can simply say post dot or posts do add now the only thing is I can simply add doc here but that will not work because Doc is a different format post is a different format this Doc is basically a document format the post here is basically a Java format uh that will not work so what we can do is we can basically use a converter here so there's something called a converter so if I go back here and if I say converter and I can say converter we just need to use this and since I want the object of this I can say Auto wir Now using this converter so I can say add but I have to convert this before so I have to say converter dot there's a method called read okay and then in this you have to pass two parameters first is the Class Type you want to convert so I want to convert this doc into post format so post. class and the second is a bent Source Bon is basically document the document itself is Doc so whatever doc you're receiving we are passing that here and this converter will convert that dock into post and it will add into the collection or the list and wait we are returning it I think we are done with this but the main question is will this work let's try so I will save this and I will go back to my Swagger it's here oh we have to restart the application as well restart okay Sur is running go back here and hopefully this will work click oh there's an error we missed something so basically we are passing Mumbai and and Mumbai is not working in the post so basically there's something wrong with the mapping which we have done it's sending text here that text goes here so what is the exact eror let me check this file so we got a client and then we have specified database name as well oh this is empty we have to also specify the collection The Collection is job post I missed that okay no errors let's go back here and and say execute oh can you see that we got it so we got only one entry which is for Mumbai because we don't have more entries for Mumbai okay let's try something else let's try with threads there should be only one post or one job with which has threads involved and you can see we got thread so we are basically searching for text now and if I search for let's say Java execute and you can see you will get multiple records now it's searching for Java in the uh Technologies it is searching for Java in the text now what is important is if you can search the exper you can see the list it starts with the one Experience One experience two because we have sorted and in total you will get only five you know why five because we have mentioned the limit here if you remove the limit it will be coming with the all the records and if you don't mention sort it will just print as it is added in the collection is that simple okay at this point the SOA is working SOA is very happy but remember when we have a react application refresh if I go home and if I say get job now it's not working the data is not coming here okay what is wrong what why is not coming if I see the console let's see the console here let's see what it says console says there is issue with uh okay there's a block there's a problem with the access control allow origin basically we have to allow the origins because they're coming from different server right the request is coming from the Local Host 3,000 so what you can do is for that it's very simple you can you can just come here and on top of your rest controller you can mention cross origin and you can also mention from where you're getting the request so I can say Origins there's only one as of at this point which is HTTP colon Local Host colon 3000 that's the react application we have and that's it we are done with this let's I hope this will work go back here refresh oh it's still not working there's something missing it still says blocked or do I have to apply okay let me the solution the proper solution for this what I will do time bin is oh first of all it is post it should be all post that's what we have mentioned in the react application I can change that maybe you can change that in re react application or you can change it here itself just to make it easy I'm changing it here itself all post and then I will use cross origin everywhere in every method just to be safe and now if I go back and refresh oh can you see that we got our data yay things are working out I don't need you anymore close and you can see we got our entries and let's see if that search happens and if I search for Java or let's search for Threads it should print only one yeah it's working can you see that so what I'm what we have done in the react is every time you type something if the length is more than two characters it will hit the request so for every character it is hitting the request I guess it is uh no it it has to match with the words so it is sending the request and the moment it matches with the words you can also implement wild card and let me know how you do that in in the comment section if you are implemented wild card and if it is working record a video upload that on YouTube and paste that link in the comments and I will approve it okay I want to I want to see your implementations how you are doing it so this is the threads and uh yeah this working we can also try it for High the Talent let me just enter the job profile here which is for let's say editor editor is already there right let's say maybe I want to search for I want to hire a mongod d developer and experience should be let's say 3 years I'll type something here and the developer should also be familiar with Java submit let's refresh and you can see that we got mongodb developer working out right so this is how you build a project so don't worry I will just push this project in the GitHub you will find that in the description for all three videos and yeah we have talked about everything everything is working fine and let me know your experience in the comment section and let me know if you want me to create more of such videos on [Music] projects so our code is working here so we have seen how do you work with spring MVC we have seen how do you work with spring rest now it doesn't matter what type of application you're building it may be a standalone software it can be a web application or a web service there's one thing common for all the application which is maintaining the log file in in fact we have one more maintaining Security in fact we have one more handling the transaction so basically it doesn't matter whatever application You're Building you have certain task which you'll be doing everywhere now look at the methods here in the controller itself so this is our alien controller and you can see we have get mapping for get aliens we have get mapping for get alien for one particular alien and then we have it for add alien what happens is whenever you call a particular method we always Main aain a log in fact in this code just to make it simple I have not written any log statement but that's what we do right so here we have to write those beautiful logging statements maybe there will be one line here by saying hey we are the execution started in fact just to keep it simple uh let's use sis out here we can use log as well logger class or something but let's use system.out.print and we can say get aliens method called so this is what we can do here and maybe before returning the statement or before return statement we can say uh get aliens method executed so we can do the log there right so before calling and after calling not just here we may have to write it everywhere in all the methods now that's one second we have to maybe sometime you have to also maintain security not all methods needed but let's say if you have certain methods and if you want to maintain security there so that means uh first two or three lines they will be used by secur security statements so we have to maintain log I mean we have to write log statement we have to write security statements and then maybe you have to also handle transactions now when we talk about a method in this method you can see if you talk about this particular line this is the actual business logic this is what we want to do we want to find all the aliens but apart from this we have certain other lines as well which is only for extra things okay there is not something a user expecting user expecting all the AL this is something which we do to maintain the application uh because we want log file to maintain the application uh we want security to handle the application now don't you think we should be doing all these extra things not inside the actual code but somewhere outside so what is recommended that don't write any extra code inside the business logic keep it outside so what we can do here is we can create a separate class let's say this is only for logging purpose so I will say logging and then we'll name this as aspect again we'll talk about why aspect we'll talk about that later but we got an aspect here so we got a separate class aspect and in this class I want to create a method let's say public void and I will say get log or maybe a log method that's it instead of having yeah so let's have a log method now in this method I will print get aliens method called okay so what I'm doing is I want to maintain log but not in the business logic so you can see we have our business logic here I don't want to do that here whenever I call get aliens I want this to be printed but how how we can connect them there's one way you can create object of this class here and then you can call the particular method from here but again we are disturbing or we are you know making our business logic dirty we don't want to do that we want to keep it clean business logic will be different from the other logic now this thing here is actually called as cross cutting concerns because these are concerns which are actual logic but then these are applicable for all different methods so when you can take it common you know if you instead of doing that in this method in this method in this method you can take it common so we can call them as cross cutting concerns okay but still the question is how this will get called and that's where we have to use a special concept which is a o which is aspect oriented programming but how can we do that that we'll see in next [Music] video so let's Implement aop here now first of all to understand aop let's search for aop in fact we'll search for spring aop documentation so it will take you to the official website of it and let's go here spring aop and you can see we have beautiful Concepts here so it stands for as aspect oriented programming with spring now there are certain terms okay now first of all spring aop is not that difficult it's just that the terms are bit weird now first of all we have a concept of aspect now what is aspect so you can imagine one class which will have all the methods all the cross cutting concerns example the class which we are working with here so this logging aspect will be aspect for us then we have a join point now see our code our business logic let's say we have certain methods and then out of those methods I want to maintain log for let's say two or three methods so we have to connect them so we have to connect a particular method with the cross cutting concern so I want to call this log Whenever I Call get aliens so get aliens becomes my joint point this log becomes advise so you can see we have one more which is advise here the expression the what do you say the Wild Card expression which we are going to use example I want to execute this log whenever I call get alien so if we have to specify some wild card pattern right that will be a point cut and then the most important concept here is weaving now aop is not new okay so in Spring also we are doing it but in general when you talk about aop we have to connect the actual method with the advice now this connection can be done at compile time or it can be done at one time now when it comes to Spring aop we do it at runtime so you can see that's what it says it says spring aop like other pure Java aop Frameworks perform viewing at one time okay we have some more uh just be with me so we have types of advice here you can see we have five types of advice here in fact three are after itself the first one is before advice this simply means when you want to call a particular method for example I want to call this log Whenever I Call get alien so this will be called before calling get aliens that is before advice after advice means I want to call log after the execution of get alien and then we have around which is during the execution of get alien again we'll understand each and everything step by step in these subsequent videos so that's about this aop words right and then we have certain types of advice so just to recap aspect will be a class where you will have all the methods join point will be the point of execution where or the point of the execution where you want to call a particular advice advice is the actual method you want to call which is log point cut is the expression which we use and then we have waving which specifies when you want to connect it in fact in advice as well so if we talk about this log advice we have three types there in fact we have five but in general we can talk about three so we have before so before execution of the method after execution of the method and then we have around which is during the execution of a [Music] method so let's Implement now so let me minimize this documentation which we have here so let's Implement so let's go back to our M dependency now since we want to use aop so of course we have to add some dependencies for aop as well now that's what we do when you work with normal spring application but in Spring boot if you can see we already have spring aop here and that's the benefits of using spring boot right you get all the dependencies required so we got spring aop dependency otherwise if you are working with normal spring MVC you can simply go to my repository and search for that particular Library there now the first step would be we need to make this class as aspect because this is aspect right and the way you do that is by using an annotation here which is at aspect so say control space and done we have made this as an aspect the second thing is I want spring to know this I want spring to know this there's a class exist like this and the way you can do that is by making this as a component so that's done we have specified that this is an aspect and this is also a component I want to call this log method whenever I execute get aliens that's what we are trying to do so that's your joint point right so this is your joint point and to achieve that we have to write a point cut oh but we have not seen the Syntax for it so what could be the syntax now to mention the syntax we have to also mention the type of advice you are working with so the type of advice is we have five options right out of which three are very famous before after and around or you can say in general I want to execute before so I will say before advice simply say add before your job will be done and in bracket you have to mention the expression so we have to go with the expression which is very simple you have to say execution because we are executing some methods but which method so in the execution bracket you have to mention the exact method name syntax would be you have to mention p in fact you have to mention public then you have to mention the return type now in this case is the get aliens return type is what uh in fact I will just simply copy the method name as it is instead of getting confused so I will say copy go back here and paste so that's my method name public list aliens get aliens but you have to mention the fully qualified name for it so okay when I say fully qualified name it means you have to also mention class name so the class name is alien controller and then you have to also mention the package name now I'm not sure what which package it is it is com dot delisco do spring MVC that was the big name so I have to mention the entire part so you have to mention the package name and then the class name and then your method name so once you do that your job will be done so you have mentioned two things here this is an aspect and second we have to use add before annotation so I guess this is done let's restart the application just to verify so as you can see we got an error okay it says there's no match matching type as alien okay that's bad maybe you have to mention the package name here as well uh let's do that quickly or maybe in fact you can also give Star there if you don't want to mention the entire this can be anything it can be any written type with name get aliens let's try now okay so there's no problem now let's go back to our Postman and here let's send a get request in fact we already have here let's send that okay so this is executed but we have not oh there was an error maybe because I have mentioned the okay send can you see that we got the output but look at this code here it says get methods called and that is happening with the help of this log okay just to confirm once more I will say from aspect just to verify if this is the meth which is getting executed uh let's say send once again and you can see we got from aspect so we are right it is coming from aspect now the amazing thing is your actual code don't even know that there is a log or there is a logging happening right that's the importance of separating the concerns from the actual concerns now not just for logging we can use it for other purpose as well for security purpose for transactions we can use it in fact spring framework internally uses this concept to achieve lot of stuff okay so we have seen before we have seen aspect but how can we use after and what if you want to maintain log really log file here so we'll see that in in the subsequent [Music] videos so let's use logger here now we are doing the log on the system.out.print and right that's not the right way now to achieve log you must have done this before as well with normal Java but the same thing we'll be using here so let's create a object for logger so we'll say private tartic final so the class name is loger itself let's import the package for that uh so let's take it from org. slf4j just to keep it simple and then we'll say logger is equal to now the way you can get the object of it is with the help of in class we'll say logger Factory dot get logger and you have to mention the class name here so the class which I'm working for is loging aspect so we'll say loging aspect. class now once you got loger object we can simply go back here and inste of using system.out.print we can say logger dot now we have to specify the logging level so it can be info it can be debug it can be error so in this case I will go for info because this is just an information and that's our message that's it okay but we have to also mention the file name if you want to go for and then what level of uh info you in fact let's run this application let's see if that if this works go back to postman and say send again and you can see you we have okay where is the log so you can see we got a log here which is get aliens method called from aspect now this is info level but what if you want to customize it so yes you can go to application. properties and you can mention what type of log level you need and the way you can do that is by specifying logging dot level dot so login. level. root and if you say equal to it should give you the popup so you can see we have all these options here so now if you say you only want to see warning then it will not show you info so I want info so let's you can specify info anyway it is coming there so you can specify in that way in fact you can also save the in an external file with the help of some with the property so let we have a property we can do that so we can say login. file and you can mention the external file uh maybe I can use app. log that works let's save this application and let's relaunch it now you can see if I say send we got the log that's great but it should also create one file which is app do log which I can't see here refresh the project it should be yeah so you can see that we got app. log and this is where you will get your log file so that's how you can use log in aop the way you generally use [Music] log in the last video we have seen how to use before advice now in this video we'll focus on after now when you say after it means it will execute after the execution of a particular method so in this case we are working with this method which is get aliens right so what we will do is let me create one more advice so we can copy this and we'll say copy and paste now this method name will be let's say log after okay and let's name this one as log before now the only thing you will change here is instead of having before we will making it is after and then we'll say aliens method executed so we have to import the package for this let's do that okay that's done uh let me just rearrange the annotations here once so let's relaunch the application after doing that so we got before and after now let's go back to postman and here so that's my send request and you can see we got data and look at the console it says get aliens method called from aspect that's what we are doing printing here and then after the execution we got get method executed in fact you know we are not actually confirming I mean we are not able to actually get is it really happening after the method so what I will do is before rning the statement here I will print a statement or maybe yeah we can just print a statement and let's say we'll say fetching aliens that's it just to understand if it is happening after the execution so let's relaunch the application and go back to postman let's say send again okay server got not started okay you can see uh we are saying get method call that means this is before aspect then we are printing this which is fetching aliens and then we are saying executed so that's how we can use after but if you remember when we have seen documentation we would having two more Afters right so one is this after so if you see three other parts so we have after returning advice after throwing advice and after finally by default it is after finally but we have this two as well okay now looking at finally you can guess if you are coming from java background where you have worked with exception handling finally simply means irrespective if you are getting the error or not this method will be executed so that means this method here will be executed irrespective of the exception to understand that what I will do is I will go back to my alen and here let me just generate an exception so before saying fetching aliens let's say if I have in I equal to divide by 0 so I'm trying to create an error here just to see what happens if you get the exception let's launch the application go back to postman and when you click on send look at the console we got the exception that is something I was expecting but are we getting the the message so you can see log it says get aliens method executed that means even after the error this is getting executed because this after is after finally so irrespective of the error if you got the exception still it will execute the advice even if you don't get the exception it will print the advice but what if you want to print this advice only when your method is successfully executing and that will see in the next video in the last video we have seen how can we use after which is actually after finally where doesn't matter if you got the exception or not this will get executed what we want is we want to execute this only when you get a successful execution in that case in writing after we will be writing after rning that's it that's the only thing you have to do nothing much I know in your mind you'll be having a lot of thoughts you know we will be doing something this or something that but it's damn simple you just have to say after rning your job will be done okay I know youone trust me let me just restart the application go back to post man and anyway we are generating the error so let's click on send you got the same error on this console but let's go back to our real console and here of course you got the exception if you see the message here nowhere it says method executed because it was not successful right and now if I make it successful of course this will work but before making it successful I want to do one more thing let me copy this method and paste it here I want to say get aliens or maybe I will say issue now I want to print issue whenever you got an exception which is error so I will say log exception So when you say log exception now this method will be called only when you to get an exception but to do that we have to change this so instead of saying after returning we'll be saying after throwing so as I mention we have three types of after we have after returning we have after throwing and after finally uh so after returning will work only when you get successful execution after throwing you will get when you get the exception and after finally you will get irrespective of the exception and let's run this code go back to postman and here if I click on send you can see we got okay let's go back here yeah can you see that we got an issue here so issue is because we are getting the exception and that is after throwing but now I will go back to my controller and here let me remove that particular exception now when you don't have the exception of course it will not be calling after throwing let's launch it and you can see we got normal output and here in the code you can see it says called and executed okay so executed is getting called because of log after so that's how we can use after returning and after throwing this video we'll talk about Spring Security Now Spring Security is a DEA facto standard for securing your spring based application now think about this now doesn't matter what type of framework or the language you are working with whenever you make a Enterprise level project or maybe a web- based project one thing which concerns everyone and that is how do you provide security for it okay I'm not talking about the jbm level security I'm talking about the the web level security example you know whatever application you have on the web now it can be a simple application it can be a very complex one where the data about your users are very important in fact overall the system is very critical so you want to secure those typ of applications in a good way when I was working on my first web project the problem is I was able to complete the entire project the only problem which I facing was about the login and log out and trust me as a developer my main focus was how can I complete all the features which a user needs I forgot about login and log out and then at the end of the project what I did I added some new features that's what we do right we complete the project and then we add some extra modules now when I was working with login and log out everything was working initially but after sometime you know I was getting access denied for some pages even after logging I was getting that I I have done that so we have two different type of pages right we have public Pages we have private pages and we have to make sure that public pages should be allowed to everyone even if without long in but what about private Pages that's where you need login right so that one concern which I was facing the second one was log out when I was logging out when you go back again you were logging in so that was some issues which I was facing the second issue we have when you work with the application is passwords how will you save your passwords normally when I was working on my first application the mistake which I did was on the client side when you see a page where you can have your username and password you can simply type a password there and then you can see the password as well well right and that's why we say okay make this password hidden so you will say hash there but what about the server side I was actually able to see all the passwords okay so in fact not the actual users but when we were testing the application when we were creating our own fake accounts we were seeing our own password because I was storing all the password as it is and then I realized I did some research I realized you should be encrypting your passwords before saving it but do you think encryption is safe people can hack it right so if you can hack the database if anyone can hack your database they can actually decrypt the passwords in fact they can do it with the help of Bruce Force attack or something they can decrypt your passwords then we can go with the stronger one right instead of going for a basic encryption maybe we can go for hashing uh we can go for sh 256 because md5 was something which was obsolete at that point but I was using S 256 and then I heard a news which is they were able to create two hash of the same message and anyone can hang it now right so even sha was compromised then we had a new Option and that is bcrypt now bcrypt is a algorithm which is specially made for saving passwords okay so that means when you create the application you have to provide login log out you have to do all the code in fact for every method which you call for every subet which you create okay you can translate with other applications as well other languages as well so for the subet for every subet we have to check for the username and password we have to ver for the sessions and everything has to be done by you what if you want to use l app what if you want to use the new Google one which is au to for everything you have to include libraries you have to do lot of configuration and that's when spring says Hey developer you don't have to worry about all these things you want login I will help you you want log out I will help you you want to use BP I will help you in fact in Spring boot security we already have a bcrypt library what if you want to use l app spring sec says I will help you there okay when I say spring will help you there that doesn't mean spring will give you everything by default you have to configure it okay spring will give you those features but then we have to do some configuration there okay when I say some configuration trust me it is some configuration because if you do that by yourself you need to create multiple files and you have to die doing that that was my experience when I was when I was working on my first few applications in fact I did well when I was working on a big project but everything has to be done by we developers okay we were five to six people and then everything was so difficult to manage I'm not talking about the features I was talking about the security okay uh so spring will help you there but the moment I say spring will help you that doesn't mean spring will give you everything there are certain things you have to do by yourself you have to configure it so when you do this for the first time it will be difficult because we'll be getting a lot of files we'll be getting a lot of functions but that's the thing right you will be doing this only once you will not be doing this every time because you will build the project only once and and while you're building the project you will do all this setup you know day by day you will keep adding new features you will not be doing the same thing again and again so that's why Spring Security is amazing it provides you login log out it provides you oh we have forgot about one thing it's not just provide you authentication it also provides you authorization which simply means authentication is who are you your username and password but what about let's say if I talk about web application where a user can read the articles a content creator can create the article and they can write the article the admin is responsible to create new users admin is responsible to create new content writers so here everyone have a different role right so as a user you can only read the articles you cannot modify it as a content creator you can edit your own content you cannot edit someone else content as an admin admin can add content admin can remove content everything how about those roles and that can be done with the help of authorization and Spring Security will help you in both authentication and authorization but how exactly to do that that we'll see in the Practical [Music] video this video we try to implement Spring Security as we have seen in the theory video that we can use Spring Security to secure our spring based application now it can be the Enterprise application it can be a desktop application or it can be a web application in this example we'll try to implement a web application and we'll try to sec secure that now first of all if you say you want to secure it of course you need a web application here okay so I had a I had a choice of making the application from scratch and then implementing security or we can use the existing application and securing them but I thought you'll be watching this for the first time so let's go with step by step so let's create a project in so I will doing I will be doing this project live here so let me create a simple project so I will say new and then we will be using here spring boot okay we'll not be working on normal spring because it takes a lot of configuration and nowadays most of the projects are built in Spring boot so let's go for spring startup project and here uh you can mention your name here so I will say this is my secure app of course you can name anything doesn't matter and then let's give a package name as com tore you can have any package name doesn't matter and here as well okay so this package name should be com dot the Disco do demo no not demo but secure app okay now once we got that let's click on next now here what are the things we need now first of all we have to specify this spring boot version now this version is working perfectly so let's not change this and then here we can search for certain things example we want to make a web project right so we'll select web that's one thing we required uh the next thing we require is jpa because we also need to work with database as of now we don't need that but later on we'll be needing the okay let just not do that now the next thing we need here is uh security button we'll do that later as well so we can select that here you can say security or you can add that later manually let's click on next and click on finish now once you got your web project you can see on the left hand side we got a project which is secure demo or secure app that's our project and you can see we have all the dependencies added here now once we got them let's create a simple application now first of all whenever you say you want an application you need a controller right because the query which I'll be finding now is okay this is the application which we got but when we want to call this methods example if I go to my browser here if I say the port number by Def with Local Host colon 8080 if I say local colon 880 you can see it is showing nothing right we got refused but even if you do that and if I say get aliens I want to get details about all the aliens because I feel programmers are aliens right because we live in different world if I click on enter you can see it will not return anything we want this we want it to return get aliens okay number of aliens let's say three to four aliens so then their ID their name and their let's say current technology they're working on so if you want to implement that let's go to our thing let's create a home controller of course why do you need a controller to work with this so let's create a home controller because we'll be having something to handle the home request so that's our controller and I'm not I'm expecting that you should be knowing spring MVC a bit so we'll have to say controller here that's one thing and then here you have to make mention certain methods the first method you need is public string which will return the homepage right I want to return the name of the homepage so I will say home and here we'll simply return we'll say request mapping and this will be for so whenever you call a homepage it should return the homepage oh but unfortunately we don't have a homepage we need to create one so we'll simply return we can also index.html or index.jsp or home.jsp the page name doesn't matter you can have any page name just make sure that you mention that page name here so we are saying home.jsp but you can see we don't have Pages here so let's go to our main and inside main so you can see in our SRC we have main inside this main we need a folder so let's create a oh not this one let's create a folder name as web app so we'll say folder web app this is where you have to put all your JC Pages we can also use theme Leaf here that's your choice again and here in this I want to create a JSP page which is named as home home home.jsp now in this page home.jsp I want to print welcome aliens that's it I just want to print welcome aliens so whenever I call home.js Page should print welcome aliens let's try this first right click here say run as I hope it will work let's try so I will as spring boot app you can see it says started let's go back to our browser and here if I say enter oh we got an error you can see it says download that's weird right we don't want to download page the thing is spring boot by default doesn't work with JSP right you have to add one more dependency to make it work with JSP don't worry you will get the source code in the description area where you will you can directly download the code but if you want to work with JSP with spring boot you need to add one dependency so for that we need to go to MAV repository and this is where you will search for Tomcat Jasper because this is responsible to convert your JSP into salet and we can pick up any one let me just go with this table one so let's go with 8.5 point this one this should work I guess let's paste it here and you can see in fact there's one way you can do that you can actually check the tomat version you're working with so you can see the tomat version which you are working here is 9.0.4 in fact that that should be the best option right let's go with the suitable one so that it will not create much issue later let's format it okay now once you got this Tomcat Jasper it takes some time to download okay so once we got our Jasper let's restart the application once again and now if I go back to the browser let's go back here and say okay I want to call the homepage and you can see we got welcome aliens at least this is working now we want to have one more page which will show all the aliens okay and I know we are going for a lengthy code but then this will make much more sense later in fact let's not do that now let's say I want to secure this homepage okay let's do that let's Implement Spring Security here what I want is whenever I use goes to the homepage like the moment I say enter I want to make sure that the user is logged in I know when you call homepage anyone can call the homepage right there's no username and password for the homepage but at least for this application I want to secure it I want to ask for the username and password for this homepage if you want to do that the first thing you have to do is in your controller instead of calling home.jsp you should be always calling login.jsp where a user will enter username password that's one thing you have to do the second thing you have to do is once a user is entering username password you have to verify it okay we'll do that the next thing you have to do it you have to also maintain the session there a lot of steps you have to do right inste of doing that what if I say you can use Spring Security here which will give you all those features what I'm talking about is let's go back to our pom XML file and this is where you just need to add one dependency trust me that's how it works uh let me just prove my point what I will do is if you want to get that login form okay let's go back here and search for Spring Security Boot and you can see this is the one we have to use again the version you can go for is 2.1.2 let's copy and paste in fact you don't even have to maintain the version because that's something you have defined in the global scope here let's format it once again and you can see we have added Spring Security as well now once you have added it here let's relaunch the application let's see the magic just by adding that Spring Security let's see the magic let's relaunch it let's let's go back and now here if I say refresh can you see that the moment I refreshed we got a login page we have not created any login page in our application and that's the beauty of spring boot or spring boot security Now one thing is amazing here we are securing the application everything will be maintained by Spring Security we don't have to maintain the session or something like that okay but then what is a username and password I don't know let's try so by default the username is user so when you talk about Spring Security the username is user oh but what is a BAL password is it ABCD or 1 2 3 4 so if you go back to your console let's scroll can you see that we have a security password which is generated here so of course this is not a recommended way of doing that this is just a static password which you got uh but just for the time bin we can try it so you can see if it says user and username and password let's click on sign in it worked can you see that we got welcome aliens so if you have 10 pages you know if you try to access any page now we don't have any other page but let's try with get aliens you can see it says oh we don't have that page right that's the issue but then if you want to access the application you have to provide login and password now I know there's one question in your mind of course as a user you will not remember this password what if you want to create your own account of course we can do that what if you want to fetch data from database of course you'll be having a table there which will have username and password so let's say if your application has 10 users every user need to have a different username and different password how can we do that even we can do that so we'll see that everything in the next video we'll see how do you change the usern password because we don't want to work with this user and password which is specified by the Spring Security what if you want to have username as naen and password as 1 2 3 4 can we do that so yes in the next video we'll see how do you change the usern password which is in memory maybe after that video we'll also see how do you fetch data from database okay so we'll do that everything but in the upcoming videos that's it from this video which we where we have seen how do you create a simple spring application and how do you add a spring security module there welcome back aliens my name isin ready and in the last video we have talked about how can we Implement Spring Security but then we have just added the jar file right and when we ran the application we were getting this login page the the username for this was by default user and the password was generated by the application itself so if you can see we got the password and when you copy this password let's go back to our thing and say enter you can see now we can access the application you can see we got welcome aliens but then we don't want to have the system defined username and password what if you want to have your own username and password and that's where you need to do some configuration now what conf I'm talking about now when you talk about spring application if you want to configure something we can use annotations or we can create a class where you can provide the configuration so let's create a simple class here and we'll name this class as application security config again you can have any name doesn't matter uh so I'm going for app security config let's click on enter and here if you want to do the configuration we need to extend a class here which is named as web security configure adapter you just need to extend this class so that you will get all the configuration features now since this is a configuration we have to write at configuration which is the anation which we normally use for the configurations that's one thing we need to add one more annotation by specifying enable web security which will enable the web security for the application so we'll simply say unable web security that's it we have to add this to annotation now once we have done with this we need to override two methods in fact let's go with the one method itself and that method is so let's say Source override and the method name is user details service so this is the method you have to override click on okay and this will override that method now we also need this object of user details service in our application and that's why you will simply write here at bean I want this to be a spring Bean okay but then we don't want this return return stuff let's create our own because we want to have a usern and password which I will Define of course you can fetch this username and password from database but time being let's go with in memory username and password to achieve that I will simply create a list of user details now user details is a inbu class in Spring okay first of all I have to import list and then I can import user details now you can see user details is a in build class in inbu type in Spring Security and now once we got let's say users equal to but then okay let's create a add list for this for all these users but then question arise how can I create users now if you want to create users it's very simple right we can simply since it's a list we can simply say users. add but you can see when you say add it is asking you for the object of user details I don't have any user details here so what we can do is we can use a inbu class name as user so we have our inbu class named as user so we say user Dot and you can see we have a method which is with default password encoder of course right when you work with the applications where you want to store the password your password should be encoded right it can be a plain text or it can be a encrypted text so time will go for the default password encoder which is actually not recommended to go for but for time being it will work with then we'll say with a username so we have to use a builder pattern here so we have to say username and the username I'm going for now is naven and let's say I want to go for the password as well again this is not a good way of doing it just for the experiment we are doing it just for to understand how it works behind the scene uh so we have user dot with default password do username do password dot I can simply say build now what it will do is it will create the object for you but I can see an issue here yeah it says this method which is with default password encoder is dicated but that's fine again we we should not be using this but we can we will see that later how do we remove that how can we use some other uh encoding type but as of now it will work this is how you define your username and your password but we still have issues now see spring is not just about authentication right it's also about authorization how can you specify your role in the application now you might be a admin you might be a user so here we can simply say dot I can also specify the rules you can see we have an option of rules where you can specify the list of rules I can go for time being let me say this is just a simple user so I'm saying username naven password 1 2 3 4 and the rule is user and then we can simply say build and then once you got this once you got the object of users we can add multiple users okay we can say users do add with the same syntax we can say some other name some other password and some other rule we can add multiple users of course right every application has multiple login and now I will simply return the new but then what should I return now since we wanted to return the object of user details service which is not using any database here so we are using inmemory database or not even database we'll say in memory there's a class which says in memory user details manager this is the class we have to use in memory user details manager and where you have to return the users that's it so this will take care of user details service now I hope this will work hopefully let me just relaunch the application so this time we'll not be using user and the password generated by the system to login and you can see we we are not even getting the default password now let's go back to our Chrome and here okay first of all we have to relaunch it so let me say Local Host I mean Local Host code 880 and now it is asking for the user and I will say password or not user because let's say if you try with user and some weird password you can say it says bad credentials we want to say naven and password is 1 2 3 4 let's click on sign in and it worked can you see that that's how you can have your own username and password but hold on we don't want to use this we want to use database right I want to fetch my username and password from database how will I do that that will see in the next video so I hope you are enjoying this series let me know in the comment section and do subscribe for further videos welcome back everyone my name is DAV ready and in this video we'll try to implement Spring Security where the username and password will be verified from database in the earlier videos what we have done is we created a spring application in fact spring boot web application where we tried to implement Security in fact initially we were getting the user and the password from the spring itself but then later on we thought why can't we have our own username password and we have done that here but now I want to fetch this details which is naven and 1 12 3 4 whatever username is there in the database I want to verify that from database in order to achieve that first of all I don't want to give my specific username and password I want to Fest this from database so what we'll do is we'll first of all we'll comment the entire part we don't want to use this then how can we achieve that secure so the steps are first of all you need to create one method which will return the object of authentication provider this is what it will return and the method in can anything we can say all provider okay that's the method we have to create now this method has to return the object of authentication provider so let me just import the package for that that's done and this will be a bean because we want this to be a spring bean and now once we I mean we want this to be spring Bean which returns now what I want is I want to create the object of it right now when you say you want to connect with database we have a special thing which we normally use for database which is Dow da basically stands for data access object so let's say da so there's a implementation for authentication provider which is Dow authentication provider let's use that uh we'll say provider we simply have to return return this object right so we'll say provider equal to new Dow authen ification provider that's it we just have to get this object and here let's return that object so let's say return provider now will this work of course not right we have not written any code which will connect with database of course this is responsible to you know talk to database and verify it but we have not done anything as of now now first of all we need certain things for example if you want to work with this type of application in your pal we need to have database properties which we have not done till now okay how do we achieve database properties here so it's quite simple we need to add boot right I mean we have to add jpa so what I will do is you can just download this libraries from mavin repository just go to Google search for MAV repository and you can search for the repositories which I have otherwise let me just copy paste which I have with me so I've have added two dependencies here the first one is for data jpa because we want to use jpa here and then of course you have to also mention the database connector which is in this case I'm using MySQL but hold on have we configured the database in MySQL so before recording the video I have created this database which is Tesco and you can see we have a database which name as Tesco of course you can use any dbms which you want not just myql uh you can use post you can use Ms access whatever database you prefer I prefer MyQ so you can see we have uh a database name theis and the table name is user now if you see this table user we have four records just ignore the first and second we will be using this later concentrate on this third and fourth so we have a username as Spider-Man and the password is web the username is Superman and the password is fly okay just random username and password here and okay so we are using mql the second thing which is important here the username to access myql is root and the password for this machine is 1 2 3 4 so based on the machine which you are working on of course you you will be doing some set up for MySQL remember the password for this MySQL the password is 1 2 3 4 let's get back to this and so we have to specify these two things and in order to connect your database we have to do some configuration for application. properties these are the properties which you have to add so first you have to mention URL which is uh the Isco in this case you have to mention the root username and password and then you have to also mention driver class name for MySQL as your dbms will change it will change the url of course whatever username you have whatever password you have and it will change the driver class name as well okay once we have done with that we need to have a model class which will be connecting with the database I mean of course a class which represent a table in this case since we're using jpa we have to do that so in this case let's create a class named as user and in this class user I want to have three properties the same properties which we have for database so if you go to my schol workbench you have ID username password let's create these three variables quickly okay so we got three variables and we need to have gets and sets for this right click here and we say Source generate get and sets I will say select all click on generate so you can see we got the fields and we got gets and sets here I want to make sure that this will connect with database I have to say this is entity let's import the package done and then this ID has to be a primary key so I will say at ID we don't have a choice right we have to mention that it's compulsory now once you got this class which will connect your database I mean it will represent the database table now what next let's go back to our application security config as I mentioned this is not complete yet so what are things you have to specify here so one more thing we have to specify here is every provider normally what happens you know when you work with the applications uh which will connect your database so your controller or your configuration will be inter interacting with a service layer and then this service layer will connect with the database layer that's how we normally work with so controller talking to service service talking to Da uh in the same way we have configuration talking to service service talking to Da okay so that means we also need a service class here and to achieve that in fact we'll say provider dot let's mention our service provider so we'll say set user details service that's my service name okay but then we don't have the object of user details service okay that means we need to create the object here so let's go back here we'll say private user details service let's import the package for this and then let's have object name as user details service itself and this has to be autowired I I will not be uh creating the object for this it it has to be Auto wir that's one thing we have to also mention one more thing which is the password encoder if you remember we have done that here as well uh we have to do the same thing for this so we have to say provider dot we have to set the password encoder so we will say password encoder now but which one uh time in I don't want to use any encryption technique or any hashing concept I will simply use no op password encoder do get instance uh because I don't want to encode now yes I know this is not something which you should be using never save your password in plain text but as of now we are just experimenting stuff so it should work now once you got this object everything should work but hold on we still have one issue we have got the database we have got user class but then this user details service how it will know to connect with a dow layer we don't have this class right let's create a class which will represent this so what I'm talking about is let's create a separate class and I will call this class as my user details service a very simple class and this will Implement an interface with the interface which we want here which is user Detail Service so that once you click on finish you can see we you will get the ready stuff everything you don't have to override this method by yourself this is just the feature of eclipse okay now once you got this load user by username you can see it something to do with database uh we have to it also throws the exception let's see how to use that but how will you get this username and of course you have to get this username from a repository if if you have worked on jpa before this is what you do right you create a separate repository class or interface for every table you have okay that means we have to also I know we are creating a lot of files but it will all make sense once we complete the entire code let's create one more interface which will represent the user repository so I will say user repository which will extends jpa repository and click on finish you can see we got user repository extend JP repository and in here instead of having T we should be saying Hey I want to work with user where the type of your ID is long that's it you have to mention user and long of course if the classes which you create in separate package need to be imported for sure okay but which method we have to use here see by default you will get all the methods for find all find one but we want a separate method which is if you go back to our application security config you can see I want this class so you can see we want want to find a user by the username so that means we need to have a special method here which is which would return return the user and it should be find by username it will accept string username that looks good we got this interface and it has a method which is finded by username let's get back here so here I want to call that method but then to have that method with us we need to create the object of user repository right so we'll say user repos repository and we'll say this is repo of course you can have any name on top of this you have to say at autowire because I don't want to instantiate it right and here you can simply say user user is equal to because this repo when you say repo dot find by username you have to just pass username so whatever username you got here will be searched in database and it will return return that particular user if what if you don't find the user of course right when you login it is possible that the user which you're searching for is not there in the database so in that case we'll check if user is equal to equal to null we can simply print a message user not found that's one way or the other way is as you can see this method throws the user not found exception can we simply use that let's say through new username not found exception we can do that right and we can print our own message we can say user 44 which is user not found of course we can do that right just for fun okay uh this is done what if there's a user what if there's a user in that case you need to create oh in that case what you will do because you can see we have to return return the object of user details oh that's tricky unfortunately this is an interface okay how can you create an the object of it that's bad right so that means first of all we need to create a class which will implement this method which is user details okay we still have one step to do so let's create that class okay I will repeat every step don't worry so let's create one more class here and that class we can have any name to it we can say user details implementation or we can simply say user principal okay why I'm using user Principle as a method name or the class name is because principal simply means the current user okay so let's have that this will Implement a interface which which is user details let's pick up that and click on finish and you can see we got a class which is user principle which implements user details and it has all this methods time bin since we are not implementing you can see we have method like is account not expired is account not logged is cenal is not expired so we have all these things which we have not configuring now so let's keep it through by default in case if you want to have database Fields there you know because let's say after every 30 days you want to change the password so you can do that in this method but as of now we are not doing anything else okay but still we have to say get password and get username because that's that cannot be true uh in this case you will say uh do we have the object access here we want the object of user right which we are working with so in that case what I will do is I will create a Constructor because I want that object anyway I want the user object here so I will create a Constructor with a field but which field I'm talking about so I will say private user user Which object I'm talking about so if you go to my user details you can see we have this object here right which is user I want to have this access to this object in this particular class and the way you can do that is by creating the Constructor I will say Source generate Constructor using fields for the user click on generate we got the Constructor it should work now so with that object user I can simply say user. getet password and or not here here we'll say user. getet username that's it we just have to write this methods but we have one important method here which is get authorities this is where you have to return the collection of authorities uh that's weird to achieve that we can simply say return I want to return a collection right so I will simply use a collections class I want to return all the unique collection so I will say Singleton collections. Singleton I may pass a list here but time then I just want to return the users so I will say new simple granted Authority and the rule which I want to focus now is only users I want to focus only on users now uh let's do that we don't want this return statement and that's it our job is done I guess here oh I know we have return so many methods so many classes okay once we got this user principal class let's get back to our code which we are writing we don't want to return null we want to return the user principal class which accepts a user as the object you can see this user which which I've created here I'm passing it here so that it will go to user principal and it get assigned here and we can get the username and password everything looks cool I think our job is done let's get back to app config everything is done yes it is implemented this is auto so this will be getting connected with my user details service object hopefully hope this will work let's restart the application and of course even if you get the error that's where the learning starts right we should be getting errors okay and we got the erors okay I was just joking and we got the erors uh the error is I'm not sure what this error means it says uh found being not able to inject the object for user details service but why oh I forgot to mention one important thing here we have to write add component all at service my bad let's relaunch this okay now there's no error as you can see everything is working let's get back to our browser and here I will search for local host colon 880 it will ask for the login and this time I will use VR password and something let's click on sign in you can see it says bad credentials and in the console I guess we have not got anything in the console that's fine let's get back here and let's enter the actual username so I will say Superman fly that's the password fly the password let's click on sign in and it worked great you can see that it worked we got welcome aliens so this time it is accessing the data from the database and that's the beauty of spring boot security let me do the recap what we have done see whenever you talk about security it will first check this file now this file is very important when it comes to Security in which you are creating a method which is or provider where you are fetching data from the database as you can see this is is for database code this user Detail Service is responsible to fetch from database why let's go back there and you can see here we are creating the object of user repository this is the interface which we have since we are using jpa so we don't have to implement this class or this interface jpa will take care for that and then in this we have a method which is load user by username where you got username we are fetching the data if the user is not available then you will simply throw the exception which is 44 this is fine but then we are rain the object of user principle so we need that class as well so you can see we have user principle which is which where we have the user object it is accepting the object which you are passing from the detail service and then we just want to return all the users and that's it nothing fancy we got a user class as well everything is simple and that's how you are able to run this application I hope you are enjoying this series let me know in the comment section and do subscribe for further videos and also let me know what else you need in this scod so that I can try to make a video on that that's it everyone bye-bye welcome back everyone till this point what we have seen is we implemented Spring Security of course using spring Boot and then we were able to log in with the help of username and password which is fetched from database to achieve that we have used an authentication provider in which we have created the object of Dow authentication provider and then we have assigned the user details service object and then we have used one thing here which is the password encoder now the amazing thing is we have specified it is no op password encoder it simply means I don't want to encode my password it will be in plain text and if you go back to your database which we have already used you can see we have a username as Spider-Man and we have a password as web now is it a good idea of having your password in a plain text of course not right if anyone can hack your website or anyone can hack your database they will be seeing all your user passwords we don't want that at least your user details should be secure so instead of using no op password we should be using some encryption way now of course you can use some encryption technique here and you can convert your normal plane text I mean the plane text password into a secure text something like that let's say cryptography or Cipher text now in this case the problem is even if you go for cyer for text it is not a good idea right because anyone can decode or anyone can decrypt so they can use certain techniques and there are something hacking ways so yes it is not safe to work with uh Cipher text what about hashes now one of the way you can do that is with the help of sha because in sh doesn't matter how big your password or how small your password is the output length will be constant that's not the case with cyer text right so we should be using hashing here but which one should we use message digit five of course not that's very old uh we can go for sh 256 or sha 512 but then you know few years back there was an attack on SJ as well so hackers were able to crack it or hacker were able to create some Collision codes so what to use now now specifically the problem with s is we can have multiple computation or multiple calculation happening at the same time you can find Sha but what if you can make it slow what if you can have multiple rounds of hash because if you have multiple rounds it will be easier for you to protect it and to achieve that especially for working with passwords we have a New Concept which is known as bcrypt so if you search for bcrypt online it says bcrypt is a password hashing function designed by these two great people and then it is based on blofish Cipher that's great the thing is it came because sha was not that secure if you read the article there are so many articles on this how to use it in fact we have some website where you can generate the hashing example if you go to this website which is browse Ling and if you type a password let's say if I want to encrypt 1 2 3 4 instead of saving 1 2 3 4 in the database we should be storing the hash of it so we can simply click on bigp here and you will get the output now you can see there's something interesting here which says 10 now this 10 simply means it is going for 10 Rounds by default just 10 rounds but you can also make it 12 rounds if you say 12 it will calculate the hash 12 times and if you say bigp now you can see the the code will also change so this dollar2 a means it is for bcrypt then we have doll 12 is the number of rounds you have and then if you go forward this is your actual password okay after dollar so this is your actual password but in the database you'll be Shoring everything uh if you try for ABCD here if you say bigp what will change is the next part right this thing will remain constant let's go for 10 Rounds here and click on bcrypt and that's your password for ABCD in fact if you go to database you can actually match can you see that at the end okay we got a different password this time yeah this is different but that's fine you can use this one as well so what I will be doing here is you can see we already have two passwords here but I'm not sure is it 1 2 3 4 4 which one I forgot the password is it 1 2 344 na let's try so what I will do is I will try to use bigri so this two is actually bigri password which I already have it in my machine so let me just go back to my workbench and here instead of mentioning no password encoder we should be using BPT now you might be thinking we have to add some libraries right don't worry by default in Spring boot we have a library for bcrypt let's try so we can simply create the object of BPT here so we can save new bcrypt and you can see we have BPT password encoder let's click on that and you can see it has also imported the package here which is BP password encoder that's great now we don't need this one so we can say control shift o which will remove all The Unwanted you know which will arrange all your UTS and that's it that's the only line you have to change it's that awesome right now let's relaunch the application now of course one thing will not work the password for Spider-Man and Superman will not work because they are in plain text so let's try for naven and uh K in here so we'll say Local Host colon 8080 and of course it will give you a signin page this time I will say naven and the password I guess it is ABCD for naven let's do that and when you say sign in can that it is working so the password which I have stored in the database is this one but I'm just trying ABCD it is working let me just do that once again so let's say Local Host call 880 login and we say naven if I use some different password you can see it says bad credentials and if I try for Spider-Man and if I say web this will not work now because the password is stored in plain so in database as well when you create the user account you have to make sure that you store the password in a bcrypt encryption format or BPT hash formats uh that's about bcrypt in the next video we'll also see how do you customize this form page because I don't want this login form of course this is good but this is given by Spring right what if you want to have your own the login form can we use that so that that will see in the next video I hope you are enjoying this s let me know in the comment section and if you and also click on the like button thank you so so much welcome back everyone till this point we have tried to secure our application with the help of Spring Security and then to achieve that we have created multiple configuration in fact we have done with this class where we are saying hey you have to verify from the database with the help of this class here and then we have also changed the password encoder from no op password encoder to bcrypt so that we want to store our password in a secure way that's done right but if you see the output which you used to get so if you go to that Local Host part so if you type Local Host colon 880 you can see we got this login page the amazing thing is we have not created this page this page is given by Spring framework now of course right if spring is giving you something it will give you something very simple you can see this page is damn simple but can we customize it unfortunately I don't know the way to customize this there might another way but what if you get the option of creating your own form of course right when you have your own form you can have extra features you can add anything you want right you can change the color scheme as well so what if we can create our own login and log out page so what I will do is I will just go back to my application and before the video itself I have added two pages here one is login and second log out now you might be thinking it will be a very complex page don't worry I just kept it as simple as possible you can see this is a very common page very simple page here which has a form tag which is calling login and then we got username and password two Fields just it both are input field and then there's a button that's it nothing complex here there's one line which I've given here again we'll talk about this later as of now there's some Spring Security message there that's simple right the log out page is a page let's say after logging out you want to show some page that you have successfully logged out and then you will also give an option of calling the homepage okay that's very simple page which I have created but then will this work work of course not right when you run this application by default spring will have its own login form so we have to inform spring framework hey I don't want you to give me a login page I want to have my own login page how do we customize that in order to achieve it let's go to app security config this is where you have to create a method where you can specify all this configuration let's do that quickly now instead of creating that method by yourself you can actually override it so there's already a method which is there inside web security configure adapter so let's override that method so I will go to source and I will say override a method and the method name here is configure which takes HTTP security as the parameter let's click on okay and you can see we got this method as of now this is only calling super we don't want to call Super config I want to have my own configuration in order to achieve that we can simply use this object which is HTTP and then we can set multiple properties here so the first thing I want to do is I want to disable csrf which is CR site reference and we can simply say CRF do disabled that's one thing now we have to mention that we want to go for a login page so we can simply say dot here and we'll say form login now anybody want to have your own form you simply have to say form login in which you have to specify the properties now of course right you have not mentioned what is your login page name that's very important so we can simply say login page and here you have to mention your login page in fact you have to mention the login page we have to mention the request type or request name in fact we are working with spring right so spring will have its own MVC so you have to mention whenever I want to login I can simply call SL login which will call a controller which will handle the login page okay so we have to say login. permit all because I want to call this page we have to also mention what happens when you say log out we have to also work for log out so we can simply say here log out because we want to achieve log out as well so I can say log out dot there's method which is invalidate HTTP session I know if you're coming from background of you know working with Ser JSP everything has to be done manually right so here we don't have to worry about that we simply have to say invited session everything will be taken care by Spring framework or sping security and then we have to also clear all the authentication yes we'll simply say true once you're logged out of course you have to clear everything and then what is a request type so let's say when do you say a person is want to log out maybe it is SL log out it is slash out or slash get out anything can be done right so we can simply say log out request matcher so on which request you want to do that so we can simply say and path matcher and in this you can specify the URL so I want to say I want to log out when I say log out that's it you can give any URL here it actually doesn't matter and you have to set one more important thing what after you say log out you have to call some pages after log out so I can say whenever I call a log out so we'll say log out successful okay something went wrong here oh okay so we have request matcher my bad let's call this class and here I will say log out success URL okay now which URL I want to call so let's say time and I will say log out success that's it so it will call log out success or you can also call hom page that's your choice and I will say permit all that's it this is the thing you have to enter here so we got a login page we got log out page and we are doing it here but will this work of course not right when you say you're calling a login and log out you have to also change something in the home controll right because we want to handle the login log out so here we have to add those two methods so I will say public string and I will say uh login page okay so we'll simply say login page and it will return in double quotes we'll simply return login that's the login page we have so we already have a login page here and then we just have to add a request matcher for this method this particular method here and here I will say login simple and then we also have to work with log out success because we have to call the log out page I will simply copy this since I'm lazy I will say this is for log out of course I will say log out hyphen success and here I will say uh log out page and then I want to call the log out page here so I will say log out that's it simple page I think it will work now everything looks cool let's relaunch the application the moment I say I can't say any eror we got eror it says permit only works with HTTP authorized request oh we have to do that as well so let's go back here and I guess we have to add one more so I have to say do authorize request. ant matcher I will say whenever I call login it should allow that let's try with this permit all and now since form login is different so we'll simply say and there cuz I want to specify more properties there okay it's working there's no error let's go back to our page I will say refresh and we got an error it says circular path we are not able to find login oh we have to mention the page right so you have to say login.jsp and here as well have to say log out. JSP so you have to mention the extension as well let's relaunch it and now let's go back here I will say HTP colon 880 enter and you can see it says welcome aliens that's bad I guess the maybe we have to also say any request has to be authenticated because we have not mentioned that so let me do that once again I will say Local Host col8 and now you can see that we got a login page this is our customized login page which we have and and now I will say naven and ab CD when I click on submit you can see we got welcome aliens it's that simple but we don't have the log out there so we wanted log out as well so when you say log out I just want to change one page in fact uh where the page we have it says home.jsp let's also have the log out button here so I will simply say button here we say AHF or a link we can say SL log out so whenever I say log out it should log out that's it normally we should be using some CSS of some format but as of now it should work everything done let's go back here refresh and now if you can see if I say naven and ab b c d if I click on submit you can see it calls the page and when I click on log out everything is logged out you can see we got log out home in fact even if you go back you will not see the same page in fact you will get login page that's awesome now if fortify enters some wrong username password when you click on submit you can see we got bad currenti you got this text here because in the login page this is what you have written right you you say Spring Security last exception this is responsible to print that message bad credentials that's the only importance of this line so that's about this video where we have tried to work with login our own login page and log out page the only thing you have to do is you have to add these two pages plus you have to go to app security you have to override this method which has all these properties nothing much everything will work in fact yeah we have one more thing we have added two more methods here so that's it from this video I hope you enjoyed let me know in the comment section and do subscribe for for the [Applause] video Welcome Back everyone till this point we have seen how can we Implement Spring Security that we have tried form login we have tried with database but now I want to use oo to so let's say if you are building a website where you want to provide security you want user to be logged in of course you can have your own database you can have your own way of verifying the user but we have an issue when you have your data I mean when you have user details in your database in your server you have to predict it right of course it becomes difficult to manage that database and if someone hacks it you are responsible that's one issue the second issue is even if you have your database even if you have your own server most of the users they are using some type of social networking login example if you have let's say your consumer has a Google account they have a Facebook account so can we have Google login in your website I mean you don't have to manage the data right it will be managed by Google so how can we do that to achieve that we have this amazing feature known as O2 so what I will do let me search for O2 so what exactly O2 is so you can see it says it's a authorization framework that enables application to obtain limited access to the user account okay so it's not just Google we can also use it Facebook GitHub dig lotion so we have so so many options here you can see you can get more details about what is or2 how exactly it works behind the scene but now we want to use it so we will be using Google login to access the website okay in fact we'll not be doing the entire code here the main focus would be how can we use Google login and let's access the token so once you log in through Google Google will send you a token I mean for the website and then based on that token your website will give you the access it's that simple okay so to achieve that what we'll do is first of all we need to say hey we want to use o or2 and for that we have to add a library so what we'll do will expand this and if you go to pom.xml we need to add one more dependency here for the O2 so let's search for MAV repository and here we have to search for Spring Security O2 autoconfigure now before spring 4 or spring five you could have actually go for spring boot security or two but now since after spring 1.5 or spring 5 we have to search for autoconfigure and here you can pick up any version let me pick up 2.1.2 that should work and here let me just copy this go back to our code and paste that dependency now once you say save it will get downloaded from the internet and it will be available in your M dependencies so that one step is done now once you got this you have to make some more changes so let's go back to our app security now don't you think we are using o or2 so we we have to specify it somewhere so here we'll say hey I'm using O2 so please enable it so you will say atate enable O2 SSO so that's you have to add so this is SSO here is a single sign on so you can see we got the package as well so once you have done that now here we don't need user dat because we are not using database so let me remove all this extra thing which we don't need as of now the only method we need here is configure now in that too we don't need to go for form login of course in the previous video we have worked on form login but this time I just want to use Google phage right so here what you can do is you don't need any of this stuff so we don't need form login let's remove that the only thing we need here is dot HTTP basic so that's the only thing you have here so we got the and matcher and then we got HTP basic so you have to say whatever request is getting that should be done with the help of O to so you don't need database anymore in fact just to clear some stuff we don't even need this method anymore so let me remove that just have a clean code okay in fact I think we don't even need this HTTP basic if required We'll add that later now think about this we are saying hey use O2 SSO and then it will search for the O2 token but we have not configured anything here right so don't you think we have to specify it somewhere we have to specify the configuration are we use using Google or we using Facebook even if you're using Google then how do we because see every developer needs a developer account on Google right if you want to have that access you need to create a client ID and a client secured key so what I'm talking about is you have to configure it in a file so this is our file which is application properties this is where you have to configure your or2 now what are the configuration we need so I already have a text with me let me just paste it here so these are the things we need so this is only for or2 so we are saying hey or2 client access UI here is Google so we are saying we want to use Google for my login this is your token name which is or token if you want to use in your application now this is important the scope whenever you use O2 of course it will also give you authentication but don't you think you have to also specify what are the features is accessible example whenever user access using Google they will be having the access to the profile of the user and the email ID in fact if you don't want profile just remove that you can now you're only accessing email ID let me just have it there okay this is something we have to specify which is for Google and then we are using o token but this is important the client ID and client security key oh we have not specified that yet because depending upon the application for different users we I mean for different developers we'll be having different client IDs here so how will we get this client ID and client secret so what you have to do is you have to create Google Cloud account where you'll be creating the application for this client ID and client secret so let's do that let's search for Google Cloud so this is their official website and and here you have to click on go to console because I'm already logged in and I have a Google Cloud account so if you don't have you can create one the only thing is I'm using this for a personal use and that's why you can see I'm out of trial but that's fine if you have a Google premium account you can use that otherwise you can go with the free one now here you have to click on this navigation bar and you have an option of API and services this is where you have to click on credentials now when you click on that you can see we have the second option second tab here as o Au consent screen this is where you have to specify the name of the application we already have one because I've used it for one of my batch but now let me just change it to theis scope nothing to specify here you can see we have specified the scope here email profile open ID you can add scope I guess you can also delete scope if you need uh no we don't have delete option but you can add scope nothing to specify much here click on Save now once you click on save you can see we have an option of create credentials click on that here we have to create a O2 client ID when you click on that it will give you an option you can use it for web application for Android for Chrome and iOS let's s web app here and let's give a name of course you can go with the same name I will say teliscope app and this is where you have to specify the URL of your application now since it is on Local Host I will simply say HTTP colon Local Host colon 880 and the redirect link which I want is on the login page I will say HTTP colon SL SL Local Host colon 880 SL login and then click on okay so enter click on create so that should work now once you click on create you can see it will give you two things it will give you uh the ID and it will give the secret key okay let me just copy that so this two this two is important so click on that copied let's go back to application I have copied the client ID it's time for secur key okay so once I have uploaded this video I'll be removing this client ID and security from my Google app so that no one can use it in fact there no one will use it for wrong use okay I guess that's done but hold on we still have one issue we have specified that we are using or2 we have specified the configuration but nowhere we have specified how we use the token so here we have to specify public in your home controller you can use any controller for that matter I will say public it will return the object of principal from java security and let's have the name as user so whenever you use a token we will be using a token as user and then it will return the principal object I mean the current user object so it should return principle okay but then when it will be called so the request mapping you have to specify that you have to say request mapping in here whenever your user request I mean whenever you request for user it should send that and I want it to be in Jason format or XML format so I will say response body I don't want a page I want XML or Json once you have specified that let's relaunch the application I hope this will work relaunching the application let's go back to our incognitive mode so let's say Local Host col 80 and you can see the moment you do that the moment you try to use an application I'm not searching for uh Google pag you can see I have searched for Local Host col8 the the moment you say enter it will take you to the home page or the Google page where you have to log in in case if you have already logged in it will not ask you for this page so let me use Tesco's ID here which is Tesco training gmail.com click on next okay you can see once I have logged in I'm calling this page but not this page I want to call Local Host col 80 nothing is coming on this page because nothing is there on this page Oh by okay I made some changes before recording the video let me call the homepage and you can see it says welcome alien and be got it right that's how you can use this but what if you want to know who is the user so you can say SL user and this is the current user so that's how you can use it you can use this token you can have you can save the data in database or in a file in a session it's your choice you can see the token it says who was the name of the user and then the picture you cannot use that as well what else we have here there should be also email ID here which I can't see but it should be there em should be somewhere yeah it's here and that's your email ID so that's how you can verify the user using Google or2 in this video we'll talk about microservices now to understand microservices let's go back to 1970s that's right when the computers were new so in 1970s we were trying to solve simple problems with the help of computers maybe scientific problems but then they were simple in today's term now after the successful use of computers for the scientific and research purpose we thought can we use it for commercial use as well and that's where in early '90s or late '90s lot of people lot of companies started using softwares and computers for their internal use also we also got uh social media websites in early 2000 but now if you try to understand the entire ecosystem or if you try to understand the world of softwares everywhere in fact everything in the world is happening with the help of softwares if you want to book a cab you will use software if you want to transfer money you will use software if you want to buy something online or if you want to buy something you will use a software right so if everything is done with the help of a software which we normally call them as applications what normally happens is we build a particular website and that's what we call them we build a website and in that particular website you'll be having all the services now when you talk about Services what exactly it means now imagine a shopping website example if you go to Amazon you get lot of features there right on Amazon you can basically search for a product you can buy a product you can basically add a product to a cart and then you can buy a product you can pay for that online and also as a seller I can sell my own devices or I can sell my own things on Amazon because it's also a Marketplace so basically Amazon itself is a huge software which has lot of different Services right now if you want to build this website by yourself what you will do is you have a traditional mindset of of course one of the best thing about building a software nowadays is we think about creating different modules so what you do is you break down the entire project into small parts in fact that's how we think right uh so if I want to build a website I will just break down the services into small modules I will work on one particular module and also that's what we also suggest right so if you have a big team you can divide your big team into small groups so let's say if you have 50 people working on a one big project you can have uh you can have five different teams of 10 or maybe 10 different teams of five members and then these people will work on one particular feature right and that's how you can build this amazing website like Amazon or amazon.com to buy something now the thing is the approach is actually coming from the software development field now when you build a normal software what is a tendency so we have a software which works on a desktop in fact when I started my career I started in that zone you can just write an application which works on a particular machine right so what you do is you structure your entire application for one device now that's what we do in the web world as well even if you have multiple teams they are working on different Services they are still one package right so in maybe in Java or in other languages what you do is you create a package right you create let's say in Java we create a w file or a jar file you will put all the services in this particular W file and then you will deploy this on a cloud or on on any server the problem is you have lot of service here on one one system so let's talk about some advantages of doing this the advantage of doing this type of architecture which we normally call them as monolithic application or monolithic architecture where you have everything in one place all the services right of course they are designed by other members you can simply group them you can form a particular package and you can deploy it so the good thing is you know what is happening with the application what are the drawbacks there are multiple drawbacks with this type of architecture the first one is team dependencies so let's say if all your team your 10 teams are working together to make all these features they have to actually dependent on each other they should know when you are going to go for a next release right so of course with every release you go for a version so you have to specify okay I got a new feature but let me know when I can push this feature in the product so that we can deploy the entire project the second one is the scalability let's say if you talk about Amazon itself in fact we can take this example or we can take this concept for multiple examples multiple domains but if you stick to Amazon here let's say if there's a sale going on now if you know that tomorrow there will be a huge sale maybe for some Festival some some other event what do you think which of the services will be needed scalability here in the monic application what happens is if you know that there's a sale going on or there will be sale happening next week what you will do is you will scale your application in multiple systems right maybe you will try to do vertical scaling by adding more servers or more RAM power or you can do the horizontal scaling so basically you will have the same application and having multiple instances of them see in terms of sale it's not like you will get new users or you will get a lot of new users you will get a lot of new sellers what you will get is more searches you will get more payments so instead of scaling the entire application don't you think we should be scaling only the small modules there so that's what we want right we want to use this particular service so instead of scaling the entire monic applic we can we should be able to scale that Individual Services next one what if you have a big application and if you think we will build this application using as a one one the application the thing is you have to stick to one technology maybe if you're using Java for the entire application you have to stick to Java even if you have a new team who says Okay I want to work on nodejs they can't because you are building one big application which will create one particular package which will go to one particular server what you can do is if you have have a different Services here you can actually create different services using different Technologies right so that's one of the advantage we want and that can be done with the help of microservices so each service which I'm talking about here they are called micro Services the advantage is if you have 10 teams they can divide the entire project into small Services one thing to remember here each service here should be self-contained they should be able to deploy it separately and also they should be scaled separately they should not be dependent on some other services so each service here will be called microservices now since they are microservices we can create them in any technology we want so one service can be done with Java second service can be done with nodejs and now since there are multiple Services you can scale them the way you want maybe if you have 10 Services I want to take this fifth and sixth service and I want let's say 10 instance of them I want the second service to have five instance so I can Scale based on the service requirement not for the enti application and also since they are separate Services even if some service goes down it will not affect the entire application now in terms of monic application if you make one change it is not working it will crash the entire application that's not the case with microservices so basically what is microservice they are Individual Services which are self-contained they can run as a individual service and maybe let's say if I want to go go to Amazon if I say I want to search for the products that's one service I want to create a new account account that's one service I want to sell a particular product that's one service so these are your microservices now everything looks good on paper right we are saying that microservices are great do we have any challenges there the thing is yes now since we have multiple Services how this services will communicate it's not like they can't but then it's difficult to configure them okay and you will be doing that only once you have to use service discoveries you have to use API Gateway which will stand in front of all the services and you will be also having some resilience system in between what if some service failed do we have a fail support there and creating a micros service is not just about writing code okay it's also about creating the architecture before you write a single line of code how do you design your system how do you create those Services that's also important and if you don't design your microservices properly ultimately you are creating a worst system of monic application now one of the question might be how this Services communicate so they basically use uses uh sttp request or response to communicate between each other of course they will be having endpoints to communicate and also we have to also think about security because now since you have M different Services if a customer sends a request which Services you have to go to and does that particular user has uh access to to do that so that's the challenges of microservices but it's an awesome thing big companies they are using microservices on big level so finally we are moving towards microservices of course we have talked about the theory of microservice then we move towards building a project now the project which we have built is called a quiz application and it was a monic application the reason we started with monic is it will make you understand how do you move a monic application to microservices and also it will also help you to understand that why we have to go for microservices the thing is whatever application which we have built now is one big application right of course it's a small application but imagine it's a big application you have multiple Services now what we have done is we have a application which we have done if you have not seen the video the entire project that's fine I will give you a complete walk through uh somewhere in between and then we can get started but just to get started here what we have is we have a quiz application right and in this quiz application if you can see we have two controllers one is your question controller and second is a quiz controller so let's say if you want to create a quiz now of course a quiz can have multiple questions and then you can also select which topic you want to uh build a quiz on so maybe you want to give a quiz on Java or maybe python or maybe any other subject doesn't matter and also you need a way to add questions remove questions update questions and also read questions right now that's what we can do in the question controller now since everything is in one place in one application imagine you know imagine this application has multiple uh Services now apart from questions and quiz let's say we also have user service where we are asking user to create an account or maybe we also have a payment Gateway so that if someone wants to give the quiz maybe they have to pay some amount or maybe we also want to give them a certificate so we we have to all those Services now when you do everything in one application that's your monic application right what we want to build is we want to build microservices so each service which I've talked about like question service quiz Service uh user service maybe we have certificate service these all are individual service and they are running independently they're not dependent on each other of course if you want to give a quiz you have to first generate questions right you'll you will get the quiz from the questions right so that's what we want to do so the quiz and question will communicate and of course quiz will also communicate with the certificate right because once you complete the quiz you have to also generate a certificate maybe quiz will also ask for the payment so everything is connected here right so or maybe they're connected with one big thing which is which is quiz here but the idea is we have to create small Services which will run independently now is it that easy to convert this project into two parts example let's say we want to make sure that the question and the quiz will be separated so there are two different microservices of course you can you can have some more microservices but just to keep it simple let's say we have two microservices and here when you talk about the quiz service basically it will it is going to use the question service now what we were we had before inqu is this so basically if you want to create a quiz you can use a create URL and then you can create a quiz you can use you can get a particular question number of questions from a quiz by specifying the ID let's say uh I take different sessions I take Java I teach python I teach let's say a blockchain so what I can do is for different batches I can create different quiz so quiz one quiz two quiz three now that's for this week next week again I have to create three different quiz so that will be four five six right so that's my quiz ID so if I specify a particular ID it will give me all the questions for that particular quiz and also we can also get submit the quiz basically so let's say if participants are attending this quiz and they have submitted some response we have to calculate a score and that's what we are doing in the quiz controller but then if you say generate or Create a quiz you need to get questions right and that will be done with the help of question controller so you can see here the question controller has the option of getting all the questions getting the category and add but if you see nowhere we are basically giving away the questions now that's something we have done in quiz itself because if I jump to the quiz implementation this will make sense more when you have seen uh the previous videos so if you if you click on the create quiz here so you can see this is where we are generating the questions and that is coming from the question that means the quiz and question are integrated tightly these are tightly dependent components we have to find a way to make them decoupled right so basically we have to make we have to make sure that they are decoupled right now to do that we have to make some changes now what are the changes we have now first of all when we talk about this create quiz which is here we don't want to use question doubt directly what we want is the question controller or the question service will be separated it will have its own database the weird thing is or not exactly weird but the thing is if you open your PG admin because we are using post database here okay it's asking for my pass password let me enter that and let me look at the database what we had so if you can see we had only one database which is your question DB right and in this question DB we had different tables if I see this schema uh here we had different tables here right so what we want to do is not just Separate Tables now we want to have separate databases so question service will have everything related to question and quiz will have everything related to quiz here and they will have a separate database that also means that when you go back to the code the quiz application or the quiz service will not be having question Dow so let's say there are two different servers quiz service and question service now when you say hey I want a quiz the quiz service which is there in other server will request to the question service hey I want few questions right so basically it is not in the same database they are communicating over the network so that's something we have to understand of course there will be a lot of changes in this and we'll understand those changes one by one so let's say we have separated the two different Services now the problem is it it is possible that if you want to scale up this application normally what happens is when you have one big application you scale the entire application right but what if you have different Services now in this case if you think hey you know uh the quiz service will need more instance than question service let's say for an example now in this case you can actually replicate or you can actually have multiple instance of a quiz service but not question service let's say question service we have two instance for the quiz we have 10 instance let's say I'm going to a big conference where we have 2,000 participants maybe I want more instance of it so that's something we have to understand that we can scale them individually also now since we have multiple instance they will have different IP address right how do you search so let's say now we have question service right have we have two instance and quiz now have five or 10 instance let's say when a particular quiz instance is requesting to the question which instance it will pick so we have to also do load balancing right between these two which one to select okay that's one what else about the first link so let's say the request comes from the client and then you have these two different microservices do you think a client knows which service to go to of course we don't want client to remember all the URLs right for the quiz and for the questions what we can do is we can basically send the request to one particular API Gateway now that API Gateway is like a door for all the services and that will decide where to go so that's one thing we need so we basically need load balancers we basically need API Gateway and also we only have two service right let's say we have 10 service each service will be able to call or any service will be able to call any other service how they know where they are how quiz service knows where the question service is do we have to remember the IP address or do we have to uh do something for that and that's where we also need to go for something called a registry where they can search each other right so that is called a service registry so we need API Gateway we need a load balancer we need a service Gateway and also what if something goes wrong what if the particular service is not working so a user is requesting for a quiz and quiz is dependent upon question what if question is down in that case quiz service is basically waiting for the response right so we have to also do something called fail fast right so you have to say hey if it is not responding let us know that it is not responding so that we can find Alternatives right so you have to find find a way to get the fall back you have to also respond to the client if something goes wrong and that all comes under this microservices you know building microservices is easy but connecting them and achieving all these things will take time and in the sub subsequent videos we we are going to implement each each of them so we'll try to implement the API Gateway we'll try to implement the service registry and the load balancer and also the circuit breaker which will what is circuit breaker we'll talk about that later so now let's create our first service from this particular monic application and the service which we're going to build now is your question service so first of all if you want to build this of course we have to create a new project and in the entire series we are not going to work with one project we'll be having multiple projects so every micros service is one project in intj or any ID which you use right so there might be some confusion where which service I'm using so the recommended way would be to practice along the video so that you will get confused if you're just watching a video and then when I'm switching between the projects you will get confused so when you are doing it by yourself you will know on which IDE I'm into right and also observe here this is where the application name is also there so we are into quiz application now once we create a new project this will also change so what I will do is I will create a new project so let me go to the same website which is start. spring.io and here the project which you're going to select is Maven type and the language is Java 3 3.1.1 this is the version I'm selecting I'm not going for any snapshot version and based on when you are watching this video you can select any stable version okay group ID I want to make sure this is com. Tesco and the artifact ID is now since we are building a question service so we'll say question service okay and then we can give the description here so that's it so we are we are building a question service basically here now as I mentioned before there are different uh Services which are going to build right we have a quiz service and a question service here we have to add some dependencies now what are dependencies we need so first of all we need web because that's a web project we also need postgis because we are building we are connecting with the database as well we need longbo because we don't want to write much of the code we want to uh get some code from the lombok for the data sets and since we are connecting with with database with the help of jpa so I will also need jpa now this is something which we have used before as well right nothing new here now what is new the thing is when you are building the entire project we are we need two more dependencies one is your open Fin and again I will not describe what this does at this point we'll talk about this later and next we need is urea client okay again why do we need all those things we'll discuss that later at this point just say we have those two things and once I download the project I basically going to comment it now I'm getting it is because in future we will need those dependen and instead of copying them we can simply uncomment it right so at this point just ignore it but when you are trying this on your machine also get this comment it as I'm doing and then we can get forward okay now after this we'll simply click on generate I'm just checking if everything is good looks good I will click on generate now it will give me a project in zip format I will click I will simply unzip it so unzipping done let me go back to my in J and here I will open the project so I will say open so basically basically we are into that particular file now and our project which is unzipped you can see question service and click on open now this time we'll do it in a new window because I want to use the existing window as well the existing project so you can click on new window now if you're using eclipse in Eclipse basically you just have to import the project in one Eclipse you can see multiple projects on on the left hand side so that's fine whichever ID we are using so we I will just click on the new window in fact I will say don't ask me again because all the project which I'm going to create will be in will be opening in the new window now when we do that this is the new window right now if you can see uh okay let it load first so basically you can see the project name here which is question service now if I switch back to other project this is quiz app okay just remember this is quiz app this is question service okay so this is monolithic and this is the microservice so it's downloading dependency which will take some time okay looks like dependencies has been downloaded if I click on maven there's no problem here everything looks clean and then if I expand the external libraries you can see I think we got all the libraries required but as I mentioned before we don't need those two dependencies at this point right so we'll go back to the Palm file which where you have all the dependency mentioned and I can simply comment these two things I don't need Ura server I don't need open fin at this point so comment it out and everything else will need that okay so we are just commenting it what next now of course I can build the entire project by typing but since we have already done that work right so what I will do is I will when you go back to your SRC and mean you can see Java is actually empty we only have one thing here which is the question service application now in this I will not I will need all those things of course we'll make some changes to make it more like a microservices an individual service or a independent service basically so what I will do is I will go back to my project and you can see in this particular project which we have done before we had all these things right we have a controller in which you had a question controller quiz controller at this point what I will do is I will just copy this entire stuff and paste in here so you can see we got all the things and actually we don't need all things so if you see controller we don't need quiz controller in this this is basically a microservice for question so delete in Dow also we don't need quiz da because the moment you say quiz da that means you have a different we have the same database no we want to use two different databases right and uh in fact let me also do that once I delete all these things so let me delete we will need question we will need question wrapper and we'll need response as well we don't need quiz so we'll delete that again we'll discuss why do we need question wrapper and response later and in the service again we don't need Quest service so we'll delete that as well so delete and done okay so you can see you have deleted of course this project will not work because we have deleted so many things and one more thing since we have made the changes in the P remember we have delet we have commented the things here so we have to reload this you have to remember whenever you make some changes in the pal file you need to reload your changes and once I do that you can see now it looks good we don't need pom file anymore now let's close it now these are the things now one more thing we need to also create a separate database for the quiz right now since we are not working on quiz we are only working with questions so let's create the quiz database later okay I thought we'll create but doesn't make sense now we are only working on question surveys so let's only focus on question service now here if you go back to your controller of course you have all these things and we have to import the package uh lendy things right so now since the thing is we have changed this name right the quiz app and that's the issue so no no issue I will CL quickly import everything the reason I'm not cutting this part of the video is because I think you should know how do we uh import something if something is not there import done question now I don't think there is a issue or there's a issue of of this import so let me import the questions question class as well and let me go back to question is it dependent on anything nothing looks good question wrapper dependent on nothing looks good response dependent on nothing looks good question service now it is depending on a lot of different things let me remove the Imports which is not making sense and let's do the import here question Dow question and then if you go down things are looking good here okay one more file which we forgot to import is this the application. properties it is empty at this point so what I will do is I will just go back to my previous project application properties we'll need anyway we'll need all these things so we'll copy this and paste it here right so all these things we needed this is basically for post uh database connectivity right we need to mention the driver we need to mention which database we are working with which is question DB uh the post username the password for the ddl update for for the database table creation and then Dil okay nothing to change here but we'll keep it open I don't need wrapper response at this point so I will close them question also I don't need it now let me Focus only on the controller now what are the changes we need to do here the thing is in the earlier scenario oh first maybe I think we should first run this to check if everything is working with this settings we just need to say get all questions let's see if this works run okay yes I need longbo okay no problem you can see service started on 8080 so let me go back to my browser and say Local Host 8080 slash all questions so it is working you can see we got all the questions but now we have also mentioned that we want to make this as a independent service at this point in the in the earlier project everything everything was mixed up right everything was there in one project but now we want to make it different so in the question controller we have to add some more requests so what are the things we need so what we will need is I will just write comments here first uh so first of all we need to find a way to generate the quiz the thing is in the earlier scenario the quiz was responsible to generate questions right but then it also needs access to the question database we don't want that now the quiz service will not be having access to the question database which is two different servers so if question service needs a quiz it is the job of your question service to generated it so basically quiz service will request to the question service hey give me questions the question service will say okay let me give you the questions and it will generate the questions so basically I need to have a method here called generate or create your choice so I can say generate this is one thing this one thing URL we have to work on next we need to also get the question for a particular quiz let's say we have a quiz database right a quiz service quiz database in the quiz database it will have its own data about the quiz but it will not have data about the questions right for the questions it has to go to the question service so what if your quiz service says hey you know I have the quiz number but I don't have a questions for it give me the questions so maybe we have to find a way to give them the questions based on the quiz ID or based on the question ID because we don't have the quiz ID right on the question database okay so it should be based on the get questions but it should be based on the quiz ID so I'll mention the bracket on the question ID not quiz ID question ID and also now we need to get this score as well you know why we have to calculate score here in the earlier project we basically calculated the score in the quiz service if you remember if you go down this is where we have calculat the results now we don't want to do that in the quiz service because in the question service we have all the questions even the right answers is there with the questions so in that case we need to do that here so we'll say get score so these three things we have to implement in the question service to make it independent now how to do that that we'll see in the next video okay so basically we need to do these three things right if you want to complete this we need to say generate uh get question and get score so let's start with generate first now if you remember in the earlier application which was our quiz app in this basically if you go back to your quiz controller this is where we were generating the quiz right now this time we are saying that it will request I mean of course this create will be there on the quiz Service as well but it will request the question service to create the quiz or to get the questions so what we can do is I can use something like this in my app so here the request will be coming for the generate so I'll say get mapping and this the request will be coming for Generate as we mentioned before and the method name let's say public is get questions for quiz and it will return something right so what do we return here now basically if you remember when we talk about quiz application or the before the earlier application it was getting questions but this time what if we don't return questions we just return the numbers so that see anyway all the calculations is going to happen on the question service right so this getting score uh getting the questions anyway is happening in the question service then why quiz has to know about the questions we can simply return the list of numbers the idas of the questions so we are going to return we're going to return response entity but that will be specific to the list of numbers so we can say list of integers okay and we are going to just return return it here but the question arise who will be getting us questions of course everything we have to every time you want to need some you need something you have a question service right so you will say hey question service get questions for get questions for Quest and then of course we have to pass some parameters right even from the quiz service when it is requesting to the question service we have to Bas basically specify some parameters here and when you say parameter of course if you want to generate a quiz or if you want to give a list of questions you just need two things tell me the category the topic name and tell me how many questions you need so I can simply say and this will be coming from the questions the quiz service right so quiz service will request to the question service and it will also pass these two values so we can simply specify request param we will need string uh category and we can say category name and here we can say request Pam and this will be your number of questions yeah know things are going on different line maybe I can just get this here this looks cool right so now we just need to pass these two values from the quiz service just remember that okay but then we have to pass this value here as well so I can pass category name and I can pass the number of questions and we don't have this method so let's create this method so I will just right click here or I will just click here and say create this method now in this method how do you get the questions okay now instead of typing the code what we can do is if you remember in the earlier project when we said Create a quiz and if I click on this this is the logic right this is what we wanted to do I can simply copy this because that's what we need nothing fancy create qu and we are getting this so This are the thing right and out of this we just need this line because we anyway in the Dow layer if you see in the Dow layer so I'm into the quiz application the monic application in the Dow layer not of this the question Dow we had this thing right we just need to get the questions for the category based on the number of questions we just need to get this anyway in our application we have that so what I will do is I will go to quiz service I just need this line of code I can just copy this go back to our new application which is the question service and paste that's it this is the thing thing we need now we have to make some changes the changes of course we have to pass the category name and the number of questions right now in the da of course it also has a DA here so if you see the DA for the question da we already have that particular method here the difference is this time we don't want to return the questions what we want to return is the question ID so that will be integer and here we don't need to return the star because star will give you everything we just need the ID so q. ID and with the same thing so we'll need the category we'll need the number and based on that I just need to F the questions that's it now this will return the list of integers right so in the question service it will return the list of integers and oh this should not string this should be integer I made a mistake here this should be integer and here also we have to say integer right and now it will it will work and the only thing is we have to convert this into a response entity so we'll say return new response entity and then we have to pass two values here the first one is questions and the second one is the status so we say status. okay and that's it so basically if you you say generate quiz this is what we need to do okay and now now that's question done generating questions done so next we need to do something for the get questions now get question is post mapping and here we'll say get questions now why do we need to Define this this is very simple this is basically if a quiz service is requesting for the questions for a particular ID we can do that with the help of get questions and here we'll say public now this will basically return the list of questions right so I will say response entity and here we have to pass the list of questions then the thing is we don't need to return the answer right because if you see the question it has answers as well we don't want to return answer what we want to return is the wrapper because in wrapper question wrapper we have ID question title and options we don't have answer so what we are going to return is question wrapper and we'll say get questions from ID now since we are saying that we are going to return questions for the ID right so of course we need to request for the ID as well so from the quiz service I'm expecting that we will get some IDs and since it was not one ID it is multiple ID so we'll say list of integers and we'll say question IDs so basically that's what I'm getting from the quiz service basically and we just need to return whatever question service returns to us so question service. get questions from ID and of course we don't have this method yet so we'll create that method so I will say questions ID so let me create this particular method here okay so method created let's go back to our service and you can see we have this particular method here now let's define it so the thing is for this particular IDs we have to communicate with our database to return the IDS to you right so how will you do that so it is bit complex so just be with me so we don't need to return questions right we need to return the question wrapper so of course we need to get the question wrapper first I will say question wrapper this is what we want to return so I will say wrappers is equal to new least now this is what we want to return so here we can simply return new response entity and then we need to pass two things we need to pass the wrapper and the status so I will say HTTP status. okay right now here we can't send empty right of course we need to return questions based on the question ID now how will you get that it's very simple get the IDS and get the questions out of it so the way you can do that is what if you can create a list of questions here and we'll we'll call them questions and we need we need to fill this at list from the database So based on this ID we we will fill this and from this question we'll get we'll create a question rapper okay now how will you connect your database to get this data we have Dow layer so we can use it so what we'll do is we'll iterate between these IDs and get one question at a time so I can say for integer ID colon question IDs so for each question we need to get the question question first and the way you can do that is by saying question Q is equal to or maybe we can directly add that in the list right so I can say questions so whatever data you're getting from the database you will simply add that to the list so we can say questions. add but what you will add here now this is something you need to get from the database so you will say question Dow dot there's a method called find by ID which will ask you for the ID and we got the ID the only thing is this will return optional so you will say get so that you will get the value okay now what we are doing is we are simply passing these values and getting data from database and we are adding that in the questions right but then we don't want questions we want rapper which will not have answers right so what we can do is we can iterate between the list again we can say questions question is equal to questions and from this questions we'll copy data into question wrapper and for that we'll say question wrapper is equal to new question wrapper and then one by one we'll convert the question into question wrapper not converting but copying data so we'll say wrapper. set ID that's the first thing we have to set and this will be coming from question now so question. get ID that's one you know I just hope there should be some library to do that I will say Set uh question title so we'll say question give me title and then rapper do set option one so we'll say question do set option one and I can you know I can we can just simply copy copy it now and we will set this as okay this that was one this should be two this should be three this should be four and this will be two this will be three and this will be four okay now once we have added the question so basically we have we are creating one question a time we just need to add that to the list so we have wrappers do add we are adding this wrapper here that's it and now now once we have added it we can simply return the value right so I think we have generated we have done with the get question IDs as well there's some error somewhere it is because of this okay I think we don't have the Constructor so I will go back to question wrapper and say I need a no argument Constructor now it looks good yeah okay things are looking good I don't see any problem here okay so that was for the get question ID right now we need to do one more thing which is for the uh get score this is where we need to do calculation so what I will do is I can do it for the get score now now when you say get score basically a quiz service will send up the responses which a quiz service got from the client so number of students are submitting the submitting the quiz to the quiz service now quiz service will send this data to the question service to get this score and each set of responses will get one score so single score basically you have to return only one score so so here we'll say get score and I forgot to put the method name it is public it should return response entity but then that will be just an integer now we just we want to return a score right a score will be a number and we can say get score now it will accept certain things right in fact it only needs uh list of responses so from the quiz service you will get a list of responses not response entity the list of responses which to import the package now what is response here so if you see response we only have two things here question ID and the response this is basically answer so let's say uh if you get a question and you get four options now that question will have an ID that we we need and also for that question what is the option selected by the user and once we know it we can get the score right for each question so I can say responses right now how do we calculate score we we will not do that we'll ask our service to do that so we say question service. get score by passing responses right but we don't have this method right so let me create this method in this service right okay now how do you calculate score now basically we have done this code before right so if you go back here in the quiz service if you can see we do have this entire logic here right now what I will do is I will just copy this logic and of course we don't need everything let's copy it copy it so copy and paste okay first of all all we don't need quiz now right this is we don't even have anything with this quiz here uh we don't need list of questions anyway we got responses what we can do is for every response in fact we don't need this int as well you can remove this we just need write okay see anyway the response has an ID right so what if whatever response I'm getting okay so for the response IDE I need to get the question right so what I can do is I can say question question I just need to import the package I think already done okay question because see why do we need question here is because question has the answer right now we can pick one question at a time and the way you can do that is by using question da and you can say hey question da give me the question for a particular ID so find by ID and how will I get the id id is then the response right so I can say response. get ID and for that particular ID I need the question first and okay it will give me optional so I will get for this particular let's say in responses we have two things right we have ID and we have answer now with each ID I can get the question first because the question has something called get right answer so here I can get this so what we are doing is we are comparing the response with the right answer if it is same then we are saying Plus+ otherwise we don't need to increment right simple stuff okay and that's how basically you calculate this code okay now if you look here uh for for the entire question controller we have done with this methods so this is basically what we have built our question service now this question service is going to be used by the quiz service now how we are going to do that that we'll see in the next video so basically in the last video we have talked about the question service right in this we were able to add questions delete question of course we have not tested it that's what we are going to do in this video but we have uh created all these endpoints we got all questions we got category add generate uh then we got get questions and this is basically if if someone is asking for a particular question based on some question IDs so let's say if you pass 5 8 12 and 13 so these are the question IDs you passing so what it will return is a list of questions right and just questions not answers and that's why we are using a question wrapper here and then of course we have also added the get code which will calculate this code but the thing is we have not tested it right so this that's what we want to do here we want to test this particular uh application and also since we're talking about micros service we will be creating multiple instance of it right see the thing is in the world of microservices if you want to scale a particular service you can do that so example in your application you have five microservices and if you want to scale let's say one or two you can scale them individually right and that's what we're going to do here maybe I want to run two instances of this uh question service right or maybe three instance so how do we do that how do we run multiple instance that's we check okay so first of all U let's run this code and let's see what happens so it's already running so what I will do is I will just restart so that if in case if it is not running on your machine you can restart it and one thing to observe if you see here in the command line it says it is 8080 so the port number where you are running this particular instance is 8080 okay and now I will go back to the post man so I was doing some testing before this uh before recording so I got this particular links here so I will show you one by one so let's say I want to get all the questions of course you can get the questions from the browser as well so if I go back to Chrome so it is 880 and then the URL is question SL all questions why this URL if you go back to your IDE if you can see we are saying question and then slash all questions it will return all the questions I just want to test that enter and you can see we got all the questions here okay and now I will go back to post man for testing other things of course I can do all questions from here as well so I can send a get request so this is the get request and if you have if you don't have this of course you will not have so you can just enter this you can send a get request and mention this URL which is HTTP colon loal host colon 8/ question do all questions and then when you click on send you can see you will get all the questions here that's what we wanted right we got all the questions same that the same thing which we got in the browser we got here of course we are also sending the status code which is 200 okay and that's what you can see here okay that's the first request right but if you see your uhod code we have done some more URLs let's not talk about category at this point or maybe we can do that right so we can say I want uh category Java right so this is the URL we want to enter and when you say send you can see we get all the Java questions now right so these are the get request how about the post request I don't want to add questions I let's say I want to generate a particular quiz uh so we can do that but again for that we need to mention these two things we'll do that later at least we can test get questions right this is important now so if you're sending a list of questions or question IDs I want to get the questions of it so what we can do is we can send the get questions and important thing is it's a post uh thing right it's a post method so if you see we are doing post mapping so we have to send a post method and then here we have to say question SLG get questions but then you have to also pass a body right the the list of IDs so if you see in the code we are accepting a list of integers here so you have to pass them so basically we have to pass adjacent in the ad format so I will go to body and let's say I'm I'm passing this questions here 2 4 7 9 5 you can send any numbers you have to just make sure that you are sending this questions you know two so I'm sending two four is here and uh seven and nine so we got seven n and five as well so basically remember this thing static exception this is what we are trying to fetch and also so at least once remember this these terms and two strings okay these are the questions we will be getting so what I will do is I will just go back there and send the request and you can see we got this so question number five was about static and we got it question number four is here four was about exception and we got it nine was about two string we got it uh five was about static we got it okay so you can see we got all the questions which we mentioned in these numbers of course if you change these numbers let's say I'm I'm fetching three as well so two three so whatever ID you mention it will simply give those questions to you so three talks about uh unised bullion variables let's see if three is that question uh yes so according to our test it is fetching only those questions which are mentioned so yeah this things are working we can also check for get scod but if you talk about get scode and if you go back to the IDE so what we doing in get scode is we are sending this URL which is get score and this is a post mapping thing to remember and then we are specifying the uh list of responses now what a response basically response has two things one is ID and second is the answer this is not the actual answer this is the answer given by a participant right or whoever is doing the whoever is playing the quiz so this will be ID and any option which which they are selecting we just need to confirm right but how will you send this data of course uh from the question controller we have to when we are requesting for gets code we have to pass this responses so basically we have to send the Json of all the responses list of Json right so I will go to get score here and this is the URL right so get score question SL get score remember we are working with the question service now and that's why everywhere you will see question SLG scod and here in the post so this has to be post because we have to submit data right we have to send the questions or sorry responses now how will you send data of course you have to go to body and this is response so you can type it manually by watching it it's very simple you just create a array because we are sending a list and in each element you have a Json response right so this is the ID of the question and this is the answer which a user is sending so this is the ID this is answer this is ID this is answer and then on the back end it will get calculated so randomly I'm assigning some values in fact we have done the same thing when we created the monic application so I'm using the same data and when you say send you will get a response in terms of score here if you can see we got three okay so it says visualize what is visualize option never tried it anyway so we can see we got pretty it's it says it is three and that's what we wanted right of course if you out of these five questions I guess there are five responses three are correct and you can also verify that let's say if I make one more mistake let's say it is I'm not sure if it is was four or five I don't know the answer even uh let's say for 19 what is the response for 19 we can check for 19 maintain the response is the correct answer is three okay and uh initially it was four so it was a wrong answer let's make it right so it should be score should be four now send and you can see we got four so this is working right and that's how you basically test it so all the URL I mean most of the URL are working we have not tested for Generate we'll likely do that when we are doing uh the quiz service not a good idea but uh I know it will work if not let's do debugging later okay so we were able to do that with one instance right and the instance is at 8080 which is 880 now it it may happen that you have multiple instance and every instance will have a different port number how will you do that how will you get a different port number so what you mention here in the application. properties and you can see by default we have not specified any port number so default port number is 8080 if you want a different port number you can specify here but then I want 880 as well and then the new instance will be having some different port number the way you can do that you can run multiple instance you can just go here on top in inj if you're using Eclipse you will see something like a configuration run configuration you can select that and you will see an option of edit config so just click on edit config here and this is the by default config you have so every time you build an application intell or any ID you will get a default config we want this but with some changes so what I will do is I will just copy this in fact you can see there's a button here copy configuration click this it will create a new configuration for you of course it has a different name what we can do is we can also customize this I think yeah you can just say instance 8081 so we are running a different port number now but then just by changing a port number here will not work this is just a name if you really want to change the port number you need to go to the modify options and you will also see these options in inj as VM options so when you click on modify option you will see add VM options you will see the same thing in intj but not in this oh sorry in Eclipse but not in this format it will have a tab the VM options just click on that and mention your option here so the option is you have to say d and server. Port so this is basically how you set it and you say equal to 80 81 so now what you're doing is you're specifying my new server port number server port number for this configuration is 8081 and when you say apply okay now you will see two options here one is already running right and you can run the other one as well so yeah so this is by default 880 this is 881 you can click on run now it will launch one more instance so in total you will get two instance of the same service you can see this particular service is running on 880 this one is running on 881 right so what you can do is you can go back to your browser so you can say this is running on 8080 and I can just copy this URL first try with something which is not there so let's say 882 is not there there so that's what you will get but if you try with 881 enter you can see we got a data so this data is coming from different instance this is coming from different instance right and if you refresh both are into running state so we got two instance of course you can run M multiple instance and you can shut down one of the instance let's say I'm shutting down the 8080 now and 80 8081 is working so if I refresh this you can see 80 80 is shut down now 8081 it is still running so that's how basically you can play with multiple instances now once we have ran this it is everything is working what if you have a quiz service now of course once we build quiz service it will run on one instance let's say some other port number uh let's say 809 90 that's a port number we will go for and when you're sending a request from the quiz service to the question service and if you have multiple instance if you have only one instance there's no issue right what if you have multiple instance which will which one it will select so that's what we want to do right that's the experiment we are going to do with this microservices tutorial so that's it from this video in the next video let's try to build the quiz service now once we have completed the question service now let's build our second micros service which is your quiz service so we are expecting that when a client sends a request client will basically interact with the quiz service not with the question service and then the quiz service will interact with the question service so basically we have two micros Services interacting so that they can share data okay that's the entire idea here so we have done with the question service of course we'll make some changes there to make it more effective but at this point for us question service ready it is working let's focus on the quiz service now what are we expecting inqu service of course we need three things the first one is see we are not doing any admin or user login here it's just that imagine when you expand this application if you want to make it big uh you also need the admin service or the normal user but at this point let's say we have three things create of course the admin will created but we're not checking that so it's create quiz next is what if participants wants to create uh play the quiz right or they want to submit the answers now in this case we have to create a method using which you can get the questions for a particular quiz so let's say tomorrow I have a session and after this session I want to uh start the quiz so that people can play but then I have to create the quiz today right so I will use create quiz and then tomorrow when I want to play the quiz I will simply call a meod called get with a quiz number number because of course we can have multiple quiz so I can specify the ID so we have to say get slash the numbers and then the third one is to get the score so of course as a participants they want to know their score so we just need this three right but to do that we have to create a new project this is a new micros service so we have to get a new project so what I will do here is I will go to our favorite website which is start. spring.io and this is where you can see we have uh again we have done this before so we'll do that quickly so we basically need a M project for Java and this is the version we are working with 3.11 or 3.1.1 and then we have to mention the group ID so till this point we are working with com. TCO so let's stick to it and then uh the artifact ID I will say this is a quiz service okay so before this we have worked with the question service now we are working with quiz service okay looks cool uh I want a packaging as jar the Java version I have is 17 and then now it's time to add the dependencies now basically we'll need the same dependencies which we have used earlier so of course we need web that's one then we need data jpa because we are interacting with the database next we need is postgis because the database is postgis of course you can use uh the inmemory database as well but at this point we are using poqu in fact in the entire project we are using poqu so let's stick to it and apart from this to reduce your code we need Longo so basically this is the thing we need right apart from this we need two more dependencies which we have done in the uh question Service as well one is your open F and next is your youa client so basically this we need do these two at this point again I will I will not explain what these two are once we our question service is ready or quiz service is ready then using this will make much more sense so I'm generating it but then I will comment it before we even start coding so looks good I will just download this so let's click on generate and you can see our quiz service is downloaded so we have to unzip it first that's a first step now once the unzipping is done uh let's open that in the intj so I'll go back to my intj and you can see this is your mon application this is your question service and we have talked about this before right uh whenever you get confused which window I am into so just focus on this particular qu thing which is quiz app or question service so I will open a project now so I will say open and we need to go to downloads quiz Service open now this is the new project which we got and it is indexing it will take some time okay I think the external J files have been downloaded and indexing has been done so if I open this of course you will get this particular files which is by default uh in the pal file we you can see we got all these dependencies at this point I don't need this two so I will just commend them so comment save and every time you make some changes in your pom file you have to make sure that you are reloading your Maven dependencies and done no problem here so that's done with the pal XML let's go back and if you can see SRC main we have this file which is quiz service application nothing fancy here just a simple spring application and in the resource we have application. properties which is empty at this point we'll fill this up and then uh nothing nothing much so we want to basically build a quiz service right now we we don't want to build this from scratch we have done that in the mon application so I can simply go back to my mon application which is quiz app and what are the things we need from here so we basically need this controller but we don't okay so what I will do is I will just try to copy everything and then we'll make the changes anyway we need these files right uh we don't need all but we need some so I will just go back here to my quiz service and paste inside this so I will say paste and you can see it will download it will get everything not download it will get everything here and then we have to also make sure that we are updating your appli properties so I will go back here we do need all this data so I will just copy this and paste it here and done so these are the things we need right uh let me just close this one anything specific yeah U there's one change here so basically we don't need to work with the question database now so we have two different microservices right so question service will interact with the question DB and the quiz service will interact with the quiz DB so here I want to work with the quiz DB we have not created this till now we'll do that okay so let's do that first so open your PG admin okay so PG admin is asking for the password let me just do that and okay so let's expand the servers and here we got post if you can see we have question debut now this time what we need is the uh qu quiz DB so what I will do here is I click here and say Hey I want to create a new database and this time the name is quiz DB so let's use that and click on save so you can see we got our quiz DB here okay okay and whatever tables you want to create of course we need a quiz table so that will be generated automatically because that's the setting we have done so at this point you can see we don't have any table but if you go back to your inj so if you can see we are saying update so it will create tables for us if we don't have it but based on what so we have to make some changes in the code first of all uh in the controller since we have copied everything we don't need question controller because that's a part of the question service so we can delete this next do we need d yes we need quiz da because we we need to interact with the quiz database we don't need question da what about the service in service also the same thing we need don't need question service let's delete that in the model basically we need all the reason being see the quiz makes sense is because we are generating generating a quiz and these are the things we have there we'll make some changes here I think there are some changes um we need question reason is see in the question service we do have question right and that perfectly makes sense there but in the quiz as well when you are interacting with a question service and if question service gives you a question where you will store it in fact I think this is optional so let's keep it now and actually we don't need question what we need is question wrapper why quiz need to know about the answers right so that's that is there in the question so we don't need this but let's keep it there for some time and then later on WE delete it we do need responses is because when a client is submitting the quiz the client will send the data in the response format so so you have to accept that as well and nothing much these are the files we need and we'll make some changes okay so basically in the quiz controller what are things we have the same thing nothing changes here right we need to create the quiz for sure we need to get the questions for sure we need to uh calculate right this are the things are there so what are the changes here nothing much of course we have to do a lot of changes in this service uh which is your quiz service because this time things are going to change here because not everything is coming from database of the question service right or the question DB basically we have our own database and a question or the quiz service basically cannot interact with the question database we have to do that with the help of question service so how will we interact with this tool so let's try to understand that in step by step so we'll do that in multiple sections so here uh let's try to do that in the next video so let's fix the application because we have copied the code from the existing service or existing application uh the the application I'm talking about so this is where we have copied the code right so what I will do is I go back to my quiz service and for me also I'm finding it difficult to understand which project I'm working on so I have to look here for the quiz service so that will be complex anyway so the first thing we have to do is if you look at the controller let's solve this first the problem we have here is the project name right so in the earlier one the project name was quiz app so that's the issue so let's delete this and let's quickly import the things we need so this is coming from the quiz service and here also question wrapper we need that uh nothing much here let's go back to the quiz service and Here Also let's get rid of all this unwanted Imports let's say quiz Dow question Dow okay we don't need question Dow right so this will not be used let's remove it in fact I will not remove it I will just commented because there are certain things which is dependent on this and you can see the entire stuff here is dependent on that so what I will do is I will just comment this entire section because we need to change this now the reason being uh in this particular create quiz when we were doing it it was using a question Dow we don't do that now I mean we can't do that now because we don't have a question database with the quiz service okay so we'll saw that and then let's import the question wrapper let's import the quiz let's import the question in fact this is also going to change we are not going to uh see even this is B basically connecting with the question da and this will create issue later so we'll solve we we'll resolve that later let's say responses this is need to be imported here quiz D is interacting okay so that's one and next we need to also change our Dow so if I go to quiz Dow any import issues here yes there is a issue let me fix that this is not needed now cool let's go back to service everything looks clean and service let's go to controller uh here we forgot to import the responses let's do that okay so we have got rid of all the errors but still it will not work first of all even before to make it work we'll make some minor changes see here when you talk about the quiz uh create quiz we are passing three values right so don't you think instead of accepting three values we should accept one object which will be easier to handle something like this let's say request uh body and let's create an object of quiz maybe dto for the data transfer object and we'll say this is quiz dto we don't have this class yet but if you have this class what we can do is let's create this class here so let's create a class with d this just to have those three values together so what are things we need so we need a category name then we need a questions a number of questions and also we need Title so we need this three and I don't want to create uh geta sets for this I will simply say d this will do our job okay this looks cool nothing fancy and what where do I want my quiz dto I want this in model because this is just a model right so let's keep it there and now we are accepting one value but while sending it I will send it individually so I will say quiz dto dot get category name uh quiz dto dot get number of questions and here we have to send title so I will say Quest dto Dot get title okay so that's the thing we need there nothing fancy okay so we have resolved about the quiz uh the create quiz but again this create quiz is calling create quiz of service and we need to make some changes here uh before that I will also make sure let me just check what are things we need here so when you say okay let's let's try to solve this create quiz problem only let's focus only one thing so here when you say create quiz with this uh Things Are looking good no problem with that and since we don't need question let me also remove that so we don't need these questions anymore so we can delete that it will get a table for us we don't need that table okay so let's get back to our service and I think this is where we were using question right so at this point we'll comment the entire section here in fact let's comment the entire method but if I comment the entire method it will create issue so we need to return this right so I will I will comment entire stuff accept this line because we just need to get rid of Errors right so this should work and here also I will comment the entire part accept this right so this will work see what we want to focus now is only on one thing right which is create quiz so when a client is saying so the client will be admin When an Admin says Create a quiz it will send a request to the or it will call the quiz service and this is where we have to code right so let's try to understand how do we create a quiz let's understand what are the changes here the this will be tricky now the changes is when you say create quiz the problem is when you say create Quest we are initially we were asking the question Dow which is interacting with the question database to basically create a question but this time we have we have mentioned right we have a separate service so this need to interact with the question service now that will be tricky so our quiz service need to interact with the question service to get the question and also this time we are not getting actual questions what we are going to get is the list of integers now why list of integers because we don't want to store uh the question data we just want the ID so when you say Create a quiz let's create let's take five questions from the from the Java concept and we don't need the questions we just need the numbers so ID 5 12 some something like that will be the questions right so here I will say the list of integers I want that but the problem is this line here will not work we need to change it now change to what basically we need to call the method of a question service so if you go back to question service and if you look at the controller this is what we need to call we need to call this method which is get questions for Quest how will you call this how will you call in fact you can't call a method what you call is a generate method this this is a request right so from the quiz service we have to send a request so let's go to quiz service so here this line will not work so let me just write in comment uh we basically need to call so call the generate URL So when you say generate URL what is a URL so if you go back to your question Service uh if you remember we were we were running this multiple instance right so let me just run the service once just to show you how this looks like so if I go back to the question service application just run this and by default it will run only on one port now which is 8080 and we have Al talked about this if you want to run multiple instances we can change the port number you can see here we have 8081 here but at this point we have only one right and if you want to say generate the URL is actually it's on the same machine so I have to say HTTP colon Local Host colon 880 SL generate okay so how do you send a request from one micros service to other micros service so basically from the quiz service we have to send a request to the question micros service and for this we have to use something called a rest template so there's a concept called rest template how rest template works is you can write some code here so basically rest template is an object inside this so if I can show you that it's a class basically so you can see rest template is there a part it's a part of spring framework so using rest template it it has certain methods which we can use uh to send the request right so it will send the request to the other other server so you can see that it has so many methods to do that so we have to send a request to the question service now the URL which I mentioned before is HTTP colon sl/ localhost colon 8080 and then we have to mention the URL which is uh question slash generate right this is a URL we have to specify and you will say okay this is good so using rest template I can hit this URL and everything is good not exactly the problem is here we are mentioning Local Host that means we are saying that it's in the same machine but what if you have in some other machine okay you will say okay we can use an IP address but then if you understand micros Services properly we are not sure where we are running the instance right so I don't want to depend on the IP address next I don't want to hardcode the port the port number here how do I know on which Port it is running right so we don't know this values at this point we are to development stage we can do that but when you are into production you don't know this value so how do you solve this problem and that's where we have to use we have to introduce some new Services now this is where there's a first time we are basically interacting between two different services and if they want to interact we need two things the first one we need is a f client now basically what is f f is same almost same as your HTTP web request the difference is it provides you something a declarative way of requesting the other service okay so you don't have to hardcode all the values you don't have to use something rest template where you specify everything it will help you to declare what you want and what are the apis you want to expose I mean what are the API you want to hit so we'll see that the next we need is a service Discovery okay now this will be interesting is because if you try to understand the quiz service is trying to search for the question service so that means the question service need to be discoverable so it has to we have to discover it how will you discover and that's something we have to solve right and let's understand how do you discover so to do that we have to use some server using which you can discover a particular API or particular microservice and one of the famous there are multiple options one of the famous one is Ura so it is from Netflix which is Netflix Ura so we have we need two things we need a Ura server and all the microservices they have to registers to the Ura server and then one micros service can search another microservice from the UDA server using UDA client so every micros service which want to search will also be having Ur a client so they will search basically and so by doing this we are solving a problem of IP address and uh port number we don't need to mention that and with the help of fing you can actually request directly to the service with the service name sometime we also call them as a naming server right that also makes sense because every service would have a name now we don't have to depend upon the IP address and the port number so how do you work with UDA client how do you work with UDA server now that's what so even before we solve this we need to to solve that okay so again I will just commment this so that will work and we'll remove this so even before we continue with this we have to get our urea server urea client and the open Fing so now let's understand how do you run a urea server so basically we want to have something where different microservices will register themselves and then of course they can also search about each other so let's say we have 10 microservices and then if one microservice wants to connect with another micro service how will they search so what we're going to do is we will create a server a service Discovery server and then every client here will register itself so they become a client and then we have a server so which server we are going to use there are multiple options but then one of the most famous and common one is Netflix Ura server so what we are going to do is we will create a Ura server now this is not that complex if you're thinking hey we have to write a lot of lines of code just follow me in few minutes I will tell you how to create a yura server so the first thing is you have to create a project so you have to go to start. spring.io and here you have to select mavin project and Java of course you can go for GR as well that's your choice so we can see we had Java and then the this is a version and the group ID which we are following here is. Tesco and the artifact ID and let's say I'm I'm saying this is my service registry okay uh so this is my server basically and then I want this to be jar and 17 version now what are dependencies we have to add only two depend pendencies basically the first one is we want to run this project so we'll say this is web and the second one is we need to create a Ura server so if you can see the we have an option of urea server here just select that so this is basically a spring Cloud disc Discovery and that's it we just need these two things and click on generate and you can see we got our project so let me just open this and unzip it so unziping done let me open this project using intell idea so I'm opening this these are the projects we are working with we got two microservices question service and uh the quiz service right now the idea was when a quiz service want to create a quiz it will use the question service there are two different Services right and in order to search for the question service we will need a Ura server so basically question service will register itself to Ura server and then a quiz service will be able to search it right so let me open that project now the Ura Ser project I will say open downloads service Reg open and now we are opening our Ura server project so it will take some time for the dependencies and okay still doing it so let's wait for that so by the time it is downloading the dependencies let me open the project and you can see the pal file we have mentioned two things here uh we need a web project and also we need a urea server so both things are there let me close this and if you go to SRC you can see we have a service registry application and then if you go to Resource we have application properties so basically now in order to make it work we need to change two things the application properties and the main application right now if you want to enable the Ura server once you got dependency that's your first steps you have to add the dependency the second step is you have to put an annotation here called enable urea server that's it so simple right so just by doing this we are enabling the urea server but we have to write some configuration one of the thing is what is the name of this Ura server in fact going forward will also name our services example if you go back to your uh let's say question service and in this if you open your application. properties you have to also mention the name of this particular service so when you have multiple microservices it's important to have a good name for it right so that you can search it so we don't have a name yet we'll do that later but at this point uh what I will do is I will just go back to my service registry and let's make some changes here the first thing you have to mention the name you have to mention the port number is going to run so let's do that first so it is spring. application dot name so this is a property and the value I'm going to set here is service registry okay that's the first thing the next thing we have to mention is a port number where this particular server will run so it say server. Port even if it don't mention by default yica server runs on This Server which is 8761 now apart from this we have to set three more properties the reason being when you talk about a client a urea client urea client going to ra itself to the Ura server right so let's say we have three things here we got a Ura server we got two clients here which is the quiz service and the question service of course these two Services the quiz and question they are going to register themselves to the Ura server but what about the Ura server do we want to register itself of course not right Ura server should not be registering itself and also we also don't want to fetch the registry so we'll make that false and we also set the host name for it so let's say to do that we have to say urea do instance host name and the host name is Local Host the next thing we have to mention is about the fetch registry and we'll make this false and also I don't want it to register itself so I will say register with urea false okay so you have to mention these three things as well so we don't want to register itself and then uh we don't want to Fresh registry as well and then we are specifying the host name which is Local Host so that's it uh we are registered now we have also have a port number and this this will make your urea server work so let me try this what I will do is I will just run this project I hope this will work so yeah so it says it started but which port number so you can say it is starting on port number 876 and anyway default was that even if it don't mention so how do I verify so I can just simply go to my browser and just mention Local Host colon 8761 and this is your Ura server running so you can see it says spring Ura but if you see here this is important for us instance currently registered with Ura at this point we don't have any micros service which has registered itself to Ura server they're not even running right so how do we make it connected how do we make it work so that's what we are going to see now so let's go back to our intellig and here if you see this is our this is running right we don't have to change make any changes here but the changes we are going to make is in the question survey so let's write run question service because the quiz service will not run because we have we have not done the changes yet so let's run the question service and see if this works now first of all even if you uh run this it will not register let me just show you that once so I will just run this code uh it will take some time to load and you can see it is running on 8080 port number but if I go back to Ura server you can see it is not registered even if I refresh it's not registered so how do we do that so the first thing you do is you go to your P file if you remember when we were creating this project we have added two more dependencies one is your urea server or urea client and second is your open Fame so we are not using open fan at this point we are using the UAB client so let's uncommanded so refresh so you can see loading done now this particular project which is our question service we have added a Ura client that's the first change you have to make let's restart and let's see if that works again sometime we learn from trial error right uh there's no problem you can see this is important this is the Discovery client right so we have done this so registration status is okay uh it's in good status let's refresh this and see okay can you see that we got it we got one application registered now which which is this application we don't have any idea and actually we know because we have run the question service but what's the name of it we want a good name for it right so what I will do is I will go back to my application. properties and here I will say spring. application. name I will say this is my question service and save this restart and go back here let's refresh and you can see we got it so we got question service registered and the beauty is if you run this with a multiple instance we have only one instance now what I will do is I will just go to the configuration remember we have also run this with a different port number let's run that one so now we are basically trying to run two instance of question service and if you go back here and if you refresh can you see that we got two instance and we also specify here so this is on port number 8081 the second the first service is on default port number which is 880 so yeah that's how thing works you know it's it's awesome so we got two two connections Here and Now basically your service basically your quiz service should be able to find it but again the problem is even if your quiz service let's go back to our quiz service and let's see what's wrong there I know we are working with multiple projects so at this point we are currently shuffling between three projects the quiz project the question service I mean the quiz service the question service and the service registry so if you go to your quiz service we want to say that this particular create quiz is going to search about question service right and then we mentioned if you can register yourself to urea server you can search it but how you going to do that how do I request and that's where there's a concept of fing so now we basically know how to run the urea server and if you can see here uh we have our urea server run running and then we have ran only one particular project which is your question service but then we have we are running two instance here one on port number 8081 and second is your default port number right and then we have also made sure that the service name is this which is question service right now what I will do is I will go back to my quiz service here so if you can see we are into question service let's go back to our quiz service now this is where we were running the project right so if you go back here in fact we are not running the project we are making the changes and the problem here when we say create quiz I want to uh get the questions from the question service right so we want the communication between these two so the first step is done now we got a UA server so basically the services can search about each other uh in fact the quiz service can search question service why vers is not possible is because I mean we are not doing it yet is because the quiz service we have not enabled the Ura client okay okay so it is not wasting itself uh but we'll do that in some time but if you go back to your quiz service this is what we were doing before and now we want to use the question service now to achieve that we need to use some rest template we have talked about this before right because how will you send requests to the question service so we can use rest template but then for that you have to mention uh the IP address port number and all the other stuff what if you can use some Shortcut way to search the service now one of the shortcut is to use Fang so there's a concept of open Fang or the Fang which is a client which you can use here now what fing says is you instead of you handling everything all the IP address and port number or all the configuration you tell me which service you want to connect to I will connect it for you okay that sounds good right so the way you can do that is first of all you need to create a f interface now I will do that in the general package of course you can make it some in other packages let me just create a pack package here and I will name this package as thinging okay and inside this I want to create a file now this is a file which helps you to connect your quiz service to the question service now if you try to understand what are the things we want to use from the question service only three things three particular methods uh so basically we want to generate the questions second we need to get the questions for a particular ID and the third one is get score we just need to call those three methods right all the other methods which are there in the question service will be used only for that service but then between the quiz and the question service I want these three methods so what I will do is I will create a simple interface and the point to remember is we need to create the interface so the interface name you can have uh anything let's say this is the quiz interface and now how do you make sure that you have Fang here so you have to use an annotation which is the Fang client but the problem is you can see it is not importing is because uh we don't have the dependency yet so what you can do is you can just go to your XML file and this is where you need to uncomment this part so this part need to be uncommit and also we'll also what we'll do is we'll set the urea client ready for this anyway we are changing making the changes right so let's unable urea client and the fing both and this is done so you can see now fing package is there so now if I go back here and if I say control space you can you can you can see we got the package let's import that is the first step in fact the Second Step the first step is to add the dependency the second step is to add this annotation here now in this annotation you have to mention which service you are connecting to so basically this Fame client here will uh we have to mention which client or what's the name of your service now the name of the service is mentioned in the Ura server so if you can see in the Ura server the name is question service right this is what we want to use here so in double codes I can mention question service in fact everything should be in caps so that's question service and once we done once we have done that the next thing you have to mention is those three methods of course right when you say a quiz particular quiz service need to access the question service we need to mention which Services we are going to access or which methods we are going to call now how will you get those three methods it's very simple what you can do is you can just go back to your question service and this are the three methods so again all the other methods we don't want we just need to focus on these three methods right so what I can do is I can get these things copied from here and go back to our quiz service and paste it here now of course this is a interface right and we are defining methods we don't want it the actual definition will be there in the question service here we just need to declare them not defining just declare same goes here we are not defining the methods we are just saying that that these are the methods which we have which we can use okay and then this part as well let me remove this part and put a uh semicolon and then we have to import the packages as well for the question wrapper and for responses so that's it these are the only change you need now right so now by doing this we are basically saying hey quiz service if you want to connect to the question service I will help you so F basically searches for this thing it will help you to connect to the question service but also if you want to use use this particular feature in your questions question service we need to create this object I mean we need to use this reference so where do I use this we let's go back to quiz service and here we don't want the question out right but we need the quiz interface so quiz interface and we'll say let's say quiz interface and we have to Auto wire this okay now whatever call you want to make so initially we were thinking we will be using rest template to make the calls to the question service we're not going to use rest template we are going to use this quiz interface to make the calls now how do you make the call so let me remove all these things here so the first thing we to do is we need to get the questions right and we don't want actual questions we just need IDs if you remember so what we can do is we can create a list of integers and we'll name this as questions and then we can use the quiz interface to call a method but which method so if you go back to your question service and who is responsible to give you the questions so you can see this method which is get questions for quiz is returning you a list of integers right now this is what we want so let's go back to our quiz service and we can say quiz interface dot get questions for quiz and we have to pass two things the category and the number of questions right that's what we we are getting from here and that's it what you will get is list of integers okay but the problem is it will give you response entity right we want only the list of integers so we can say get body so basically uh in the question service we have mentioned that we are going to return uh the response entity so what we'll do is we'll just say get body so that you will get list of questions and then based on these questions let's Create a quiz and for that I will create a quiz object I will say quiz quiz let me import the package I mean of course the same thing we have done before as well and if you remember I've deleted that code so we have to write it once again uh so let's set the title for this because that is something we got from the user and the title goes as title now it's time to set the questions so I say quiz do set questions now set questions will accept the list of integers right so I can simply say questions we just need to make one change which we forgot to do before is in the quiz now when we are saving the questions we are not saving the actual questions this is something we need to change we are basically saving the integers right so the change we have to make is not questions uh it's not questions but question IDs and then we are not submitting the questions we are submitting only integers now when you change this initially it was questions right but this time we are going for uh the IDS right now in this case when you only have numbers we cannot go for many to many what you do is in database we use something called element collection so when you have a numbers or a specific type which is only one so you can use element collection but yes if you have a entity type a different table then many to many make sense now going back to quiz service here I think it should work now so set questions there's still some problem I think in the quiz service no so set questions are not working oh okay the name is changed right so it's not set questions but set question IDs that's what IDs and then this will take questions now once that is done it's time to save the quiz in the database so what we can do is we can use our quiz Dow and we can say save and what we want to save is the quiz and done we are saving the quiz so what we have done here is we wanted to create a quiz but then if you want to create a quiz you need to request to the question service right now question service will give you the questions and that's what we have done here and the way you can interact between these two services and the way you can your quiz service will ask question or ask for something to the question service is with the help of fing client which we have done here we are specified The annotation there and we can access these three methods which we are trying to do we are using only one method here which is get questions and yeah once you got the question we created the quiz and we were able to save the quiz we have not tested it yet but I'm thinking it will work so let's only test this part which is Let's ignore all the other one let's only think about this create quiz and to achieve that what I can do is I will just run this code once okay there's one more thing if you want to enable the quiz uh the fame client we need to mention here enable Fame clients I think that's the only change we need uh let's run this I hope this will work okay there's a problem it says it is already in use okay so we forgot to do one thing in the application properties in fact we forgot to do two things so when we are using the Ura server uh we need to use a name so I will say this is my quiz Service uh the next thing we have to mention is a port number right so by default it is using ad8 which is used by something else so let's use 8090 that's the port number we are going to run this and things looks cool let's run this and here we go you can see that this is running on 8090 so that's good let's go back to our browser where we have our Ura server running you can see it so says only one server or one service running but after refreshing you can see we have quiz Service as well so now both the uh application or both the microservices have registered themselves to the Ura server with different names one is a question service which has two instance running and one is a quiz service but now we are saying that we want to run the generate quiz how do we do that so basically if you want to do that we need to pass the quiz dto data to make it work okay so now it's time to run the project right so in fact if the project is running we want to test it so let's create the first quiz and let's see if the communication between the quiz service and the question service is really happening so what I will do is I will open my Postman because that's what we are doing here so let's go to postman and okay so what is a request so the first thing you have to change is the port number so so we are running on port number 8090 the next thing you have to mention is the quiz and the URL is actually quiz SL create so if you go back here uh that's what we are doing right okay so we are into quiz uh controller yeah so it is quiz SL create so let's go back here and so that's our URL so we have to also send data right so what kind of data we want to send it's very simple uh if you can see we need to we need to accept three things that's your quiz d which has three things the category name the number of questions and title okay so what I will do is I will create a Json here the first thing we need to mention is the category name so that's category name and the category name we have is let's say Java we want to get questions for Java the next thing we want to mention is the number of questions so I will say num questions and okay how many questions we want let's say five questions and the next thing we want is the title so we'll say title and the current batch of uh Java which is going on the batch name is Springers let's say Springers quiz one that's the title for the quiz and okay so with this data I will click on send and let's see what happens okay we got an error that's weird okay there is a problem of 500 so let's check our server and let's see what is going wrong here something wrong with the fame client okay so it says not able to find something what is not able to find uh okay so path SL generate is not found okay that's weird but if you see the exact URL let's see so we are hitting the question service that's good and then directly we are jumping on generate oh okay so if you remember if you go back to your question service the generate is basically after question so we forgot to put the question there okay so it should be question SL generate so if you go back to your quiz service and quiz interface where is that yeah so you have to say question SL generate in fact for all not just for this it should be question SLG get questions questions SL get code say save it and restart the server I hope this one this time it will work okay there's no eror let's try once again click on send oh it says success that's good but is it really working let's try that so there's no problem here you can see it I think it it tried to fire to the question service now there's only one way to check if the data is there in the database so let's go back to our PG admin which is not opening I don't know why okay PG admin okay so if you can see we have a quiz table so basically the quiz DB is a database and the quiz is a table and if you try to view the data here yeah so you can see that we got the quiz and the title is Springers quiz one and if you see the questions and we got five questions and these questions are actually coming from the question service because quiz has no idea about the question database right so the quiz service is actually interacting with the question service oh finally this is a video where we are basically interacting with the two Services cool uh let me just check the urea server is it saying something okay things are looking good in urea okay in fact it is not showing the health uh we should have added the actor as well you can see actor is not added yeah but then okay let's try to do that later so yeah that's how it works that's how you are basically connecting with the help of Ura server so I hope you have enjoyed it till now where we are basically able to run the project and things are working out the two services are running out also I wanted to check we have two question service right so when a quiz service is calling the question service which one is getting called let's try to understand that in the next video so before we get into the load balancing we have to complete certain things in the quiz service now if you remember of course we have create we have created these three methods right uh so in the controller the quiz controller we have create we have get so basically for a particular quiz we can get the questions and then we have a submit quiz right so when you say submit it will give you this score now if you look at the implementation so if you see for the create we have done the coding right so the create quiz is done and it is working we have tested that before right so I will just remove this comment from here because the create quizz is ready but if you to go down we have two more methods to work with we have get quiz questions and then uh we have one more which is the calculate results of course uh we can change this method name if you have if you have some other method that works uh we can go with a consistent word which is get score so at this point just imagine get score and calculate result are same because both are getting the score right now this two things are not implemented okay so we want to implement this so let's start with the get quiz questions now what we trying to achieve here is for a particular quiz so let's say we have an ID right uh so what happens is every time I I can create multiple quiz for the same topic so let's say I'm running a batch in this every week I want to uh Create a quiz so every quiz will have a different name and different IDs now for that particular ID so let's say if I create a quiz fifth quiz so the ID will be five and in that particular quiz I have let's say 10 questions so for that quiz I want all the questions and then you can see when you say questions you don't need answers for it so you need a question wrapper so basically you need a list of question wrapper so how will you get that first of all we are not storing the questions in the quiz table so if you verify that once again uh let me open my PG admin and if you see uh we do have quiz table here so if I say uh view all data so if you can see here uh we have number of quiz created so I was doing some testing last time we have we have only created one quiz I was doing some testing for different features and then I've created this two but ignore that let's say I want to work only with the quiz number two okay the ID 2 and then we have one more table here and if You observe this we are not storing questions we what we are storing is question idas so for a quiz let's say we have quiz number two for this quiz which is which has five questions you can see we only have numbers but then if you want to conduct the quiz I need questions as well now question arise okay question arise who has this questions and the the question service knows the actual questions the quiz only has an idea about the numbers not the actual question so now we have to again interact between the quiz and the question service so how will you do that it's very simple so first of all for a particular quiz ID I need the quiz so we'll take that so I will just uncomment this part and also I will make some changes here so instead of optional let's say I don't want to use optional it's a good it's a good practice but just for example here I'm working with quiz directly right I'm using this get method so before we get the questions from the question service we need to get the IDS of it right so let's say if I specify I want to go for two I want all these IDs which is mentioned here now how will you get the IDS so we have to get that from the quiz ID itself so if I go back to quiz it has all the question IDs right so in order to do that let me go back to quiz service I will use a method so in fact I can remove everything from here and let's keep it clean so basically I need to get the IDs right now that ID will be list of integers right that's the type of it so I say list of integers and I will say question IDs equal to and then we need to get that from quiz so we'll say quiz. getet question IDs now once you get the question IDs I can request my question service to give me the questions for this IDs and if you remember if you go back to your quiz interface which is also a proxy between the quiz service and the question service we do have a method which is get questions if you can see get questions for ID it is actually accepting the list of integers okay so we are good we can just request to that particular interface so I can say quiz interface and the method name is get questions for or get questions from ID and we just have to pass the question IDs okay now what it will return so let me just go back here so it this method returns a list in fact it returns a response entity list question wrapper so I can simply get that and paste it here again this is not copy pasting this is code reusing why you type the same thing multiple times right but you don't do it so when you are practicing the code you type it by yourself so you can see we we got response entity list of question rapper and we'll say this is questions equal to in fact we need to put that here cut paste yeah so you can see we got the list of questions and this is the thing we need to return here so we have to return questions so that's what we wanted to return right okay wait we just we don't need to do all these things we can simply return the response object so I can say questions this should be good okay so this is done so this particular method is done which is the get questions and of course we need to test this as well so how will you test this it's very simple just restart the server and we are not making any changes in the question service right so I can simply restart the server here and let's see if this works so let me open my post man and now what request I have to send so basically we have to send a get request now and then it is in the quiz and what method we have so if I go back here the request we are trying to send is this so we have to say get and we have to pass an ID right so I can say slash getet slash and the ID is let's say two of course we have that two and I can click on send and you can see we got a response which is the questions the actual questions so we got for 177 and we can verify that 8 6 so it starts with 177 let's verify from database so for the Quiz 2 177 1886 so is it right 1886 that's right so we are getting the right questions so this is working uh now we need to work on one more thing which is the get score so what if you want to submit a qu now we are calling a calc calculate result but again this is not ready so again we have to interact with the question service because question service is responsible to get this code uh we have done that before so let me just go back to question service and in in question service if you see the controller so this is where we are saying get score we is calculating the score from the questions from the question service and all the operation we are doing in the in the question service not in the quiz the only job of quiz service is to request right so quiz is being very lazy here but that's how we build a system right now what are things we need so what I will do is I will remove everything from here the only thing we have to do is we have to call the get score method this particular method which is get scode so who will do that so it is quiz interface. getcode and whatever responses you got here these are the responses you have to send nothing much I will just simply say responses and what you will get is the result now in which format so basically if you go to quiz interface it will return the response entity integer so you can just copy this and paste it here this is what you're going to return and say let's say this is this code okay okay and that's it in fact you can also say write us code that also works and we are returning in fact we can return the object itself we can return this Cod because that is of type response entity integer and we got it right so this is so simple nothing to do in the quiz service everything is happening in the question service we're just requesting it I hope this will work let me try it out so restart the application because we have made some changes and now after making the changes let's let's go back to our post man and so we are getting questions there's no problem with that this time we have to submit the quiz okay responses right and instead of typing it you know somewhere we have done the response right somewhere it should be there get score yeah this is here so get score in get score basically we have to pass the response right so you can see these are the response we have the changes we have to make is we sending a post request the port number is not 8080 it is 8090 this time and uh the quiz and the get score is it the get score method let's verify that quiz controller oh no it's a submit I know we have used different terms here but let's do that so we'll say submit and then uh we can also pass the ID for the quiz so let's say we are working with Quiz 2 and these are the response we have I'm not even sure if you have the same questions but doesn't matter let's say get let's say send and let's see what happens we'll change it if it doesn't work and you can see we got three okay so this is working right and if you make any changes uh I think we have done this last time as well but let's say if I make one more change uh if I remove the space from here which is incorrect and if I click on send you can see the score now is two so this is working right so get score is working and uh the get questions is also working so by doing this basically we have made two microservices which is running there's no problem in that one microservice is able to call Second microservices and whatever features we wanted to implement it's working right but now remember in the previous section we have talked about we have two question service running so if I go back to the question service here we have two instance which is running which is getting called we are not sure right is it the 880 responding to the quest service or 881 responding to a qu service how will you get to know that and there's a concept of load balancing okay so we'll we'll discuss that thing in the next video now let's talk about load balancer the thing is when you have multiple instance of a same service right let's say uh even if you talk about more application where we have one application we can have multiple instance of it that's how you can scale it right we call them as horizontal scaling where you can create multiple instance so whenever a client sends a request now based on which instance is free or available the request goes to that particular instance so let's say we have three instance and then one and three are busy the request goes to Second instance right that is called load balancing so that one instance will not be busy with all the request so you can balance the load between instances if we can achieve the same thing in the microservices of course every service imagines of one application so let's say we don't have a lot of load on the quiz service so let's say we are not creating multiple instance of it we have only one but let's say the question service is very busy so what we can do is we can create multiple instance of it so let's say we have two instance now when a request goes from the client side to the quiz service when the quiz service wants to access a question service we have two options there right now now based on the load on each instance we can decide where to send the data where to send the request right that is called load balancing now in the earlier days or way back we used to do everything manually but because of some tools and some libraries it has become easy now and with the recent versions of spring we get that by default in the system so the moment you get your urea client and fing and if you expand your uh so let's expand this so let me expand this external libraries we do get those jar files by default so if I go down and if I expand this you will find the load balances somewhere so you can see we have by default load balances here so we don't have to add them manually but the question is how how we are going to use it so you don't have to do any configuration the moment you use Fame client it will automatically search for question service and whichever service has or whichever instance has a less request it will send the request there and even if you have two instances and if if you don't there's no load basically it will switch between the two instances and you don't have to worry about it right okay that that sounds good right uh but then how we are going to implement this or how do how we going to check this now even if you have not done any configuration and whatever we have done till now the quiz service was actually sending request to the first instance and the second instance of question service one by one alternatively and the way you can see that is let's go back to the question service and let's only Target on the get questions okay so we are in the controller which is question controller and every time you say get question let's say or let's see which particular instance it is using now how do you differentiate between these two instance with the port number right so one instance is running on 8080 the second instance is running on 8081 so what we can do is uh we can just print the port which is getting used and that will solve our problem right now to in order to do that we need to get a get hold on something called environment now in this spring framework we do have environment so we can just use that environment the the variable is environment as well and then let's Auto this so you don't have to instantiate this now once you get this environment I can go back to the method which is get questions and here let me print it so I want to print the environment now this has something called a property and which property want to Target I just want to get the port number right so the we can simply do that with the help of local. server. Port so it will tell us which Port it is running on and then it will print on the console now after making the changes I have to make sure that I restart both the instances of question service we don't have to restart the quiz one so let me restart so we are basically restarting both the services which will take some time and I think it's done both the service haven't restarted one is running on 8080 the second one is running on 081 so let me go back to my post pan and we request to the quiz okay now what we are getting is get questions so for a we're sending a get request for question id2 let just send and you can see we got the response right but let's verify which instance is getting used so if I go to the first instance you can see this this has been used and this has not used yet which is 8081 let me send the request once again and you can see it is again sending to the first one let's try once again and now again it is using the same one refresh okay let me try once again multiple times okay now you can see after multiple request the request is going to 801 so it is going in random so basically whichever service is free so if I can send multiple request and when you get lot of request to one particular instance of course it will deviate you can see response request is going to 8081 and also 8080 so alternately it will send not exactly alternative but uh it will check which is the free instance right so at least we are getting this by default which is your client side load balancing and at one point I thought it's not working but yeah it's working you can see the request is going here and and we don't have to worry about it right because both the instance are same so itres to which instance it is calling to weting the same output right so that's how basically you can load balance let me try once again it it looks cool so you can see going to 81 and also to Ed Z so that's how we can use this that's how we can use the load balancer so the way it works is with the help of f client it will help you with the load balancing and you don't have to do it anything manually and if you want to print it you just have to print environment do get property and the property name is local. server. port and environment variable is this which is coming from Spring framework so just make sure that the environment which you're using here is coming from the spring framework not from hybern because in the hybern as well we have the same environment type so that's it about load balancing and we're doing it with the help of f client and urea client now till this point we have done so many things in this project basically we created two different microservices and we have also gone through some Concepts example if one micros service wants to call the second micros service how will they discover so for that we have installed also we have worked with the urea server urea client uh if you want to make sure that one instance is able to call the other one or the one service is want to call the other one what is shortcut way so we have used fing as well now there's one important concept of course microservice is a huge concept there are a lot of things but then one thing which is very important is the API Gateway now why do we need API Gateway see the thing is if you talk about a user now user is looking at your microservices in fact all your microservices combined as one application right now user has no interest to know okay you have quiz microservice you have question micros service for user that's one application right so the way to interact with the application should be only one URL the problem which we are having now is we have two Services right we have a question service and we have a quiz service now both these Services have a different name different port number and when you want to access them so basically we need to we have to make mention the port number as well right example if you want to work with the quiz service you have to work with 8090 if you want to work with the question service you have to say 8080 or 8081 uh if you want to individually access them without quiz service right now the thing is apart from this there are few more problems let's say you want to also achieve authentication now you have multiple microservices and one if a user comes to access one microservice you will say Okay enter your username password and you're like okay we got the username password the user can access the resource but for that micros service what if a user says I want to go I want to access the next micros service now at this point again the next micros service will say okay tell me who you are prove that you are that person again a user will enter username password do you want to give this experience to the user where user is loging for every microservice to access of course not a user should access only once what if you want to maintain the log file what if you want to achieve some cross cutting concerns in at one place and that's where we can use API Gateway so this API Gateway becomes the entry point or the interface for the user how do you do that now in Spring Cloud basically you do get the API Gateway and we just have to use it and as we have done till now for every new thing we have to create a new project I know that's the only part of microservices which is tricky is because every new thing is a new project of course developer developers perspective when it comes to the Ops maintaining it huh I get a trouble but the thing is we are microservices right it has a lot of features and we can adjust one or two so the first thing you will do is uh you will go to this website which is start. spring.io and here what you will do is you will create a new project mavin uh the so from we're making the video now we got a new version between the video so earlier we were working with 3.1.1 now we got 3.1.2 it should be good let's try this we got com. telesco as my group ID and the artifact ID is API Gateway okay and jar version 17 because in my machine I have Java 17 here now the dependencies we only need two dependencies here the first one is Gateway because of course we need a API Gateway and also we need a Ura client because it has to register itself to the service disc to the Ura server and once you got the two dependencies of course you can add actuator you can add Dev tools which you can use in the other projects as well but just to keep it simple I'm going for two Gateway and ureas Discovery client with this let's download the project so you can see we got API Gateway in this machine I already had one zip file and that's why you can see in bracket it is one so let me unzip it so unzipping done uh let me open this in my intellig idea so open so navigated to the folder and now after unzipping it I'm opening the project and this is where you got your API Gateway and that's it this is your API Gateway is it okay so we have to make few changes the first thing thing is if you look at the SRC we got two files here in Java we got one file in Java which is API Gateway application and then if you look at the resources it is application properties which is empty and that's it this this is your project API Gateway now why this API Gateway with because of the name no because of the library which you have added or the J file which you have added which is the starter jpa right uh this is your API the thing is if you want this to work you have to make some changes because by default if you try to run this project it will run on 8080 and we already have that Port busy right so just to give you a number of Port we are using so we have two question service instance running on 8080 8081 we got a quiz service running on 8090 we got our Ura server running on 8761 and API Gateway we to run on some other server so we'll set two things here first is cloud. application. name so let's set this as API Gateway the next thing you have to mention is a server number let's say 8765 port number and uh we need one more thing in fact we'll need two more things we'll do that later so at this point we have done the configuration and now let's run this and let's see if this works I'll right click this and say run so there's running okay cool and the port number is 8765 I will go back to my urea server to check so I went to Ura and the number of application running is you got two question service running we got one quiz service service and we got API Gateway as well which is running on port number 8765 so we got API Gateway so now this is your front now what this also means is if you go back to your Postman remember we were sending request to the quest and the port number is this but then we don't want directly to access the quest service I want to use it through the API Gateway so this API Gateway port number is 9 8765 so every time you send a request for the question service or for the quiz service this is a way you have to go from here you will go so once we get into API Gateway Now API Gateway has no idea what this quiz is example if I run this code or run this particular URL it's not working because the API Gateway has no idea what this quiz is now this quiz is a URL which who knows about this URL the quiz service if you use question there who knows about qu question it is question service API Gateway has no idea but what API Gateway knows is the name of the service how we have used the UDA client so what it does is it checks okay so we have API gway knows about two things the question service and the quiz service so that means if you want to use a quiz service from the API Gateway here before the quiz you need to mention incaps because that's what we have mentioned quiz service so now we are sending request from the quiz service so API Gateway will connect to the quiz service because of the Ura client and then it is able to communicate see ultimately we are trying to connect with the quiz instance or the quiz service if you run this code or if you run this URL still not working okay we have to do one more thing in the code of application. properties basically here of API Gateway we need to set one more thing which is the locator so we have to mention uh spring. cloud. gateway. discovery. locator by defa is false and you have to enable this so what you're doing is you are you are asking the API Gateway hey I'm allowing you to search okay look the services in the earlier before this we were not allowing it so now we are allowing it to API Gateway you know enjoy your enjoy the search and know look look for the services with the help of youa client okay so it looks cool let's go back to our Postman I hope this time it will work send it worked so we are basically trying to access the quiz service but we're not talking directly to quiz service a client sending send the request to the API way and API Gateway says okay uh I need to search about the quiz service it will discover the quiz service from the UDA server and then sends a request that's what is working but then I have one little issue here which is this particular thing I don't want to mention quiz service here uh I mean in capitals of course you can also remove the entire part here we have to do a lot of configuration for that and we just want to make it simple at this point uh this is for beginners so we'll not be removing this there are some steps as well to remove this but at least what I can do is I can make it lower case instead of upper case so I can say service the quiz service and now when you send request it's not working because the capital Works small is not working at least this looks cool right compared to the Capital One so how do you make it work uh you can use one more property here which is the lower case so this is spring. cloud. gateway. discovery. locator. lowercase service ID because we don't want to go for Capital it will search for the lower one I mean it will convert that into lower and now make after making the changes let's restart okay restarting complete and now let's try send it worked right so this is basically how we use API gate with a lot of configuration that can be an advanced part of this particular series but yeah this is about the series on microservices I know we have done so many things the number of projects which you created is huge in fact I'm teaching you all those things and I'm working on different projects while I'm talking to you see as a trainer we have to be very attentive because when you're working somewhere and if you take a gap in between that works in videos it will not so sometime even I get confused which service I have to go I'm I'm talk I'm telling you that we going you have to go to ction service but in my brain it takes some time to register which project I have to open right maybe there should be a yura server in my brain as well that will be weird anyway so we were working with this three four projects the quiz service the question service the service registry API Gateway just to go give you a brief when a user wants to play the quiz first we need to create the quiz right but when you say create quiz where are the questions now question belongs to the question service right in the question service we can add questions we can remove questions we can search for a particular question we can uh generate questions for quiz we can get questions for the particular ID or list of IDs uh we can also calculate this score and everything we have done in the question service in the quiz service basically we were asking the user what you want to do you want to create a quiz okay I will do it for you but actually quiz service is doing nothing you know it just says hey question generate a quiz for me smart right so the thing is we have two microservices and then we were also able to do load balancing because of UDA client and fing but then we don't want to directly interact with the each individual services and that's why we have used API Gateway so as a user I will interact with the API Gateway API Gateway will interact with the other services and uh how do you search between different services and that's why we have UDA client and Ura server and we have seen that as well I know uh when you are doing this project there will be a lot of issues so let us know in the comment section and also you can join the Discord Channel you will find the link in description uh and you can ask your question there you know people will be happy to help you there and we will also be there to help you so that's it from this video and that's it from this series basically and I will also try to work on the advanced part of it and until then enjoy the other tutorials coming [Music] up