 Welcome back once again, today in this session we will be mainly talking about how to debug Java applications using eclipse id following video. So while talking about I'll be in between I'll be using hands on, so I'll be switching from my PPTO over to eclipse in between. So what is an outcome of this session? So as an outcome of this session I'm expecting the student will be able to learn how to debug Java applications through eclipse IDE, fine let us move ahead. So let us basically see what we mean by debugging and as a famous saying always goes on that programmers are the one who always introduce bugs into that program and debuggers remove them through the various possible chains of exception tracing stack trace and all that. A good developer has a good debugging skills as well and knowing a proper way to debug application always helps you in writing a better programs and more optimized programs and knowing or having a good knowledge of debugging also lets us quickly structure a logic so that we can write error free programs and we won't end up debugging application. So what we do is we spend our productive time in writing more code than debugging the code. So what debugging is a general alignment term description of debugging is that removing defects from the applications. So here some of the very important things like here as we are using eclipse ID here some of the very important things we need to remember is while debugging we have to take care that some of the basic fundamentals of debugging fundamentals like say step into step over and step out. They have to be very well planned through breakpoints and eclipse. So in order to show you how exactly this works and how exactly I can use eclipse ID to debug an application through a live coding we will consider a small application and we will write a code and we will try to debug that application. So what Wikipedia says about definition because it's most popularly adopted definition what Wikipedia says about debugging is debugging is a process of finding and dissolving defects or problems within a computer program that prevent correct operation of computer software or a system. So the definition that it prevents correct operation of a computer software. So bugs are they are like they are not letting computer software function as they were expected to do. So it's very important we make sure this such type of bugs are removed from a program. So okay we have we did debug Java code using JDB a JDB is far more sophisticated debugger it can be even invoked through command prompt and but we won't be going into that depth we will be using eclipse. So through eclipse ID we will try to explore what we mean by step into what we mean by step over and step out. So while doing a live coding I'll explain you these terms and we'll also talk and we'll also see a demonstration of using variables and debug perspectives. So let us see how we debug Java applications in eclipse ID. So let's do a hands-on. So for this we need a prerequisites the prerequisites are that you need to have some basic fundamental knowledge of Java language and we need a Java 8 runtime installed on our systems and we also need eclipse installed on a system make sure you have eclipse version more than 4.5. So let's see how we do it in eclipse. So I let me switch to eclipse ID yeah and here I'm going to create a project and after creating a project we'll see how I can debug that application. So I'm creating a new project here Java project. Let us call it as demo application and I'll say next okay and I click on finish. Once I open the package explorer here I'll close the welcome window to this. I'll add a new program so I'll call this as main and I want a main method to be part of this okay now I have my main.java. Now let us visualize a small code. So let us suppose that I want to write a code to check whether a given number is odd number or an even number. So it's a plain simple logic. Now when we write our logic it's usually based on by mod2 operator wherein we divide a number by 2 and we see if it is divisible it's even if it has some reminder remaining it's odd. So we'll write the same program and we'll make sure that like we will try to test it and we will try to debug that. So let me take a integer variable x I'll initialize it to 0 and I'll take one more variable called as flag and I'll initialize it to 0 and I'll set flag as 1 if x is even and if x is odd the flag remains 0. So I'll write if to do that if x mod 2. If x mod 2 equals 0 it means it is completely divisible by 2. In that case I would like to set flag variable as 1 and here I'll do a statement like if flag equal to equal to 1 then I'll do a sysout saying that the number is okay so saying that a number is even else odd okay let me add in this okay now I'll execute this program and we will also explore what output it is showing us okay. So it's an even x is 0 0 okay I shouldn't have used okay let's make instead of 0 let's make x is equal to 2 so I'm checking the working of this program then we'll try to debug that. It's 2 for 3 it should print odd okay let me check it's odd fine okay now I want to see whether how the flag variable is changing is the value of flag variable changing to 0 or it's 1 in dynamic. So what I would like to do is I would like to set a break point at this moment. So how many break points okay this is one brand if you want to insert a break point break points are like a stop-over points in your code wherein when you debug your application what happens is your code executes and just stops at that level. So at that moment all your variables in your code they are all exposed and when they're exposed you can check their values. So checking their values will give you a good fair idea about okay is your code functioning as it was expected. So let's say so I have inserted a break point at line number 13 and I would like to insert a break point at line number 9 okay so and okay and I'll insert one more break point okay fine two are enough for this code. So I have two break points so the break the moment you double click on that it becomes a break point once the line is marked as a break point you'll start observing a blue dot here this blue dot indicates that at this point your application will stop its execution and and your eclipse will open your program into a debug perspective. So debug perspective is a special perspective provided in eclipse IDE the purpose of debug perspective is to let developer have a complete view of debug related parameters for example you would like to know like what methods have been called like he would like to step into step or a step out of debug sessions or if he wants to explore what's the value of variables at that moment like at that at at this top of point mark by break point. So at that time this debug perspective is quite more intuitive for a developer. So let us do that if you want to debug this application I will not run this application but what I'll do is I'm going to I'm going to run menu and here I'll click it as debug as Java application make sure usually we do it run as Java application but if you want to debug them you have set few break points in the code and now you want to debug that is Java application what we do is we click on Java applications here now it asks you okay you are asking me to debug this application do I open that in a debug perspective yes I want that so I'll say remember my decision and I'll click on switch now a debug perspective opens now you can say that the first break point where I marked here I marked so my execution stopped at that moment and all the set of variables up till that point they are exposed in this variable window this is what I was calling it as variable window for example this is one variable args x was one variable you can dynamically see that the value of x is three now the beauty of this is you can dynamically change this value as well okay so in a in a session two we'll see how we modify it dynamically so my execution stopped at this moment I want to execute this now here it is zero just observe this value when I would like to move now when I have a break points I want this control to move here so in this what I call it a I call it a step over you want to step over to next break point so in this case in this case what you need to do is you need to step over so here you find a mean is for example this okay this is step into this is step over and there's something called a step out as well so what happens in step out is now I'll tell you the difference between a step into and a step over step over moves to next break point step into what it does had this line being some method call it would go even within deep into that method so stepping into is debugging deep by going into method into method by exploring the call call chain but here we won't be going that detail we'll just see like how I move from one break point to other so now I want the execution to move to this in in in this case what I'll do is I'll call step over the shortcut is f6 so now I am here clicking on this window here through mouse you can even try like f6 when you start doing it and on your own so now I'm moving it to f6 just observe the values of flag variables when I move to next window so I'm going to step over now you can see that my control moved to this now if you care for you observe this line is not yet executed this line is not yet executed so if you want to execute you have to once again click on step over so two break points step over executed then my statement has stopped it here now you can see that the flag variable is zero it didn't because three mod zero is three now what you can do is you can see that the condition checking has work fine flag was not equal to one so it was wrong okay you can also explore it shows the flag value what what's where what the flag value is at that moment it so it directly jumps to else block and it prints the odd so now let me move again step over yes it just steps over just move around and you will find an output as odd okay so I would like to explore it once again so if you if you now it has stopped if I'll once again run this application so I'll click on debug okay one second it is executing stop x was three but suddenly you thought that okay you would like to test it for x is equal to four you can modify value here for example if I make it as four I have made it as four now if you step over you will see that the value of x is dynamically reflecting in a code debug okay now x mod four mod to zero though your declared is three you are modified dynamically here that's the beauty of debugging through eclipse okay so let me step over now you will see you will find that okay you can also see that the flag value is modified now this helps intuitively it helps a lot to the developer so by moving ahead you can see that now the if loop executed the condition turned out true and when you're going out okay now you can see that the number is even okay so don't get confused that why in spite of giving x three output is even no here dynamically I modified the value of variable so this gives a lot of flexibility to developers to try out their logic or alternate logic without writing or without compiling code once again again going through multiple cycles of logic design so entire burden is moved off to debuggers so these are these are the debuggers and there are some other points for example there are various views in debug perspective so here I have main these are the social the number of break points and these are expression window where you can add an expression and you can so this is the further reading which I wanted to share with you it's a good tutorial but from a Ogella and it contains a very in-detailed analysis of debugging how we do advanced debugging and all that so this is a debug bibliography which we have taken here okay fine thank you