How To Debug Java Code The Right Way - Eclipse Debugger Full Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we'll walk through exactly how to use the debug tools in your ide to quickly and easily debug your java programs if you're anything like me when you started programming you just put print statements everywhere to try and figure out what was going on but you don't have to do that using the built-in debug tools in your ide makes debugging way faster and easier it's a lot easier to use than you may think and we'll walk through it all in detail my name is john i'm a lead java software engineer and i love sharing what i've learned with you in a clear and understandable way i also have a full job of course available and a link down in the description if you're interested first what exactly do the debug tools in your ide actually do for you well they actually allow you to walk through a live running program step by step line by line and see exactly what is going on every step of the way so literally at any individual line you want you can see exactly what all the variable values are and exactly what's going on your ide gives you really powerful tools for being able to do that and you just need to know the basics of how to use them in this example we're going to be using eclipse but it works in a very similar way with other ides like intellij the buttons and shortcuts might be a little bit different and they might be in a different place but all the basics are the same what a lot of people do when they first try to use the debug tools is they they see over here in the ide like you know right next to the run button is this debug button and they go great i've heard a lot about being able to debug your code in the ide so this must be the command to do that and then go ahead and click it and it doesn't do anything different it just runs their program like it did before you don't understand it and you kind of give up on it so what exactly is happening there why didn't we see anything different when we ran it in debug mode versus just running it normally well the issue is we didn't have any break points in our code so what is a break point well a break point is a spot that you mark in your code so that when you run your program and it gets to that spot in the code it'll pause the execution right there so you can see the state of everything at that spot in the code so let's do an example here so let's say you wanted to put a break point here at line six the easiest way and the way that i tend to use the most is to just double click on this spot right here between the line numbers and the left margin of eclipse if you double click right there you'll see this dot appear that means we've created a break point at this line in the code so let's go ahead and click this debug button to run our program in debug mode and see what happens the first thing you might notice is that the entire view changed when we ran the program in debug mode and hit a breakpoint eclipse automatically changes our perspective to the debug perspective the current perspective that you have selected is up here in the corner so right now we're in the debug perspective normally when you do a basic coding you're going to be in the java perspective you can also see right here in the editor it highlighted line 6 to let us know that that's where it's currently pausing the execution of the program one of the views that it makes available to you in the debug perspective is this variables view the variables view will show you all the variables that are available at the scope of this line in your program so right now there are just two variables available one is this first int variable so that corresponds to the first int int variable that was created here on line five and in the right column here we can see its current value we can see that it has a value of 7. the only other variable available in this scope is the args variable that's passed into every main method in every java program ever this variables view is probably the most basic part of debugging but also the most useful being able to see the value of any variable in your program at any time is super powerful another part of the debug perspective is this debug view over here on the left it'll list out every active thread in the java program that's running we have a pretty simple program right now that only runs on one thread so we only see one thread entry here and this part shows exactly which class and method name and line that it's currently paused on one thing to note is that when it hits a break point and is stopped at a certain line it means that it hasn't executed that line yet so it has executed everything up to that which means it has already created this first end variable and assigned it the value seven here on line five so that's why over here in the variables section we do see the first end having been created and has the value seven but we don't see this second int variable yet so how do we tell eclipse to okay go ahead and execute this line of code and move down to the next one well you do that by using the step over command which is this button right here you can also just use f6 as a shortcut so i'm going to go ahead and click step over now and what that did is execute the line of code that we were on and proceed to the next one so now over here in the variables section we can see that it has created the second int variable and gave it the value of eight so now the execution has paused here on line eight and if we wanted to execute this line of code and proceed to line ten we can just click step over again so what that did is called this add method passing in the first and second end variables and then assigned the result to this sum variable here and we can see that that's exactly what it did we see the new sum variable added with the value of 15. so now we're ready to execute the last line of code in our program that will print out the result so let's go ahead and click that and it stopped here on the closing curly braces of our main method and if we click step over one more time because we've hit the end of our main method that means it's the end of our program let's go ahead and run our program in debug mode again and talk about some other ways that we can navigate through the active running program as we mentioned we can hit the step over button to execute the current line of code that we're on and proceed to the next one but the line that we're currently on line 8 has a method call in it and we know that if we hit step over it'll just execute this entire line of code and then proceed to line 10. but what if we wanted to step inside the execution of this add method and walk through all of the code inside of it well to do that there's this other button here called step into with f5 as a shortcut so if the line of code that you're on has a method call in it you can click step into to step inside the first line of the method being called now that we've done that you might notice that a couple of things have changed over here in the variables view we now have different variables available and that's because we only see the variables that are available inside the scope of the line that we're currently stopped at so in here we only have two variables available to us and that is int 1 and into the first int and second into variables that are inside the main method aren't available inside the scope of this add method another thing to note is that over here in the debug section we now have kind of two entries here the reason there are multiple entries in here now is because we are now one method call deeper in the execution of our code so when you're debugging and you jump inside a method call that next level of method call is added to the stack here and so we can see that we're stopped here at the add method on line 15. but right underneath that we can see that the call to that add method happened right here in the main method on line eight and so if we actually click that entry we can kind of hop back to what is happening inside the scope of that particular spot in that method when we're inside a method like this we can use the step over and step into buttons just like we did before but there's also another command available to us called step return so you might use step return in a situation where you've hit a spot inside a method and you just want to have it execute the rest of the method and return to where it was called from and that's exactly what the step return command will do so if we go ahead and click step return it completes execution of that method that we're in the add method and returns back to the spot where that method was called from if at any point while you're debugging your program if you just want to stop you can just click this terminate button and it will just stop your program right then and there so of course you're not limited to just one break point you can add as many as you like so let's say we wanted to add another breakpoint here at line 10. you can go ahead and double-click the margin right there again to add another breakpoint there are other ways to add breakpoints too so you can right click align and click the toggle breakpoint command and that will add a break point you can also just hit ctrl shift b and that will toggle the break point on whichever current line you're on in addition to being able to step over and step into there's also this resume command so what this resume command will do is continue the execution of your program until it hits another breakpoint so because we have another breakpoint here at line 10 if we go ahead and hit resume it will execute everything right up until line 10 in our code so now if we hit resume again because we don't have any other break points in the rest of our code it just executes the whole rest of the program all the way to the end another thing to note let's go ahead and run our program in debug mode again let's add a break point right here in our add method here on line 15. because we have a breakpoint within this add method right now if we hit the resume button because the add method is called here on line 8 the execution of the program will hit our breakpoint at line 15 and stop there in addition to being able to just observe variable values at any given point in the code the debug tools even allow you to change those variable values arbitrarily at any time you want let's say for example we wanted to just test out what would happen if this first end had a value of 11 but we didn't want to change our code to do that well what you can do is if you're stopped in the debugger at some point in your code you can go into the variables view here and click on the value and edit it to whatever you want so we can change it to 11 right here hit enter and now this first int variable has a value of 11. so far we've just worked with some basic primitive int variables so let's look at how this works when we use some more complex objects now get rid of this method and replace all this with some code that deals with creating cats so let's put a break point right here on the very first line of our code and again run it in debug mode so this line here is going to create a new cat object and pass it the name cramer in the constructor call so the same way as before we can click the step over button to execute that line of code and we can see over here in the variables view we now have a new cat1 variable but what's cool is we can also click this little arrow here and expand that object and see the values of all of its individual fields so we can even see here that it has a name field and its name is kramer and as a quick note we can change the value of these variables here just as easily as we did for the other int variables so i can just click this name cramer here and change it to a jerry you can also change these variables by right clicking and going to change value and then enter whatever value you want and hit okay this next line will set the litter preference on that cat object to tidy cats so right now it is null and if we go over here and step over that line of code to execute it we can see it getting updated to tidycats live right there and stepping over again will set its age to three another kind of cool thing is that over here in the variables section if you execute a line of code and it changes a variable value that variable will be highlighted here in this section so because this last line set its age to 3 we now see this age variable being highlighted in yellow over here in the variables section and the same thing will happen for this cat 2 when we create it so right now it hasn't executed this line of code yet so cat 2 doesn't show up over here in the variables section and if we hit step over we see it being created and it has the name of george so let's go ahead and click the step over button until we get down here to where we are creating an array of these cats so line 16 here will create a new cat array variable called cats so we can step over that and we can see that cat array being created right here but what's neat about that is we can expand this cat's array and see the contents of each index of that array right here right now because we haven't set those indexes of our array to anything they're both null but right here at line 18 we're setting the zeroth position in our cat's array to be cat one so if we step over that the object at the zeroth position in our cats array is this cat variable and we can even expand that right there the same way we did up here and see its current age and litter preference and name of jerry one important thing to note here is you may have noticed these little ids here next to every variable in the variables view this id is auto-generated by eclipse the reason that's useful is because if two variables over here share the exact same id number you can know that those variables represent the exact same objects for example because we set the cat at index 0 to b cat 1 we can see that the ids of those two things match so if we look at the object at index 0 in our cats array we see that it has the id of 28 and that matches the id of cat 1 up here which is also 28. so we know that those two things represent exactly the same object in memory the same goes for cat 2 here so if we step over to execute this line of code the cat at index 1 on the array has an id of 40 which matches the id of the cat2 variable but there are even more really cool things that you can do with the debugger one of the other things you can do is over here in the expressions view the best way to explain this part is probably with an example so let's say i put a cat one dot get age and open and close parentheses so what this section actually does is keep track of what the value of this expression is that you put in at whatever spot in the code that you're currently paused on for example right now because cat 1 doesn't even exist yet before this line 5 gets executed right now we just get an error but if i go ahead and click the step over button to execute that line of code now it tells me that cat1.getage gives me a value of 0 which is right because 0 is the default value for ins but if we go ahead and click step over and then step over again to execute this line that sets that age to three we see that the value of that expression got updated to three of course the expressions that you put here don't need to be this simple so this might be silly but you can even do something like you know cat1.getage times 53 right now that has a value of 159. there could be a situation where you want to know what a certain expression or calculation evaluates to kind of throughout your code as you're proceeding through it and this expressions tool allows you to do that you can have as many expressions as you want and it'll show you the value of all of them at any point your code gets paused in addition to seeing the value of variables over here in the variables view you can also just hover over that variable here in the editor and you can see all the details about that variable and click through them right here you can also highlight any variable and then right click it and go to inspect but that inspect functionality isn't just limited to individual variables you can do that with entire expressions let's continue a few steps down in our code here so now we've created this cat2 variable and assigned it a litter preference so what we can do is highlight this entire expression cat2.get litter preference and you can right click that and hit inspect and that will show you what that whole expression evaluates to so right now of course since it's just a getter for litter preference it returns fresh step also as a shortcut if you don't want to right click and go to inspect you can always just hit ctrl shift i that's a shortcut that i probably use every day at my job when i'm debugging one really cool thing about this inspect functionality is you're not even limited to inspecting code that currently exists in your program for example if i wanted to i can come right up here to this spot in the code and write like cat1 dot get name and then i can highlight that expression and hit ctrl shift i to inspect it and it will still tell me the result of that expression to take it a step further you can even use this inspect functionality to change the current state of your program so instead of calling get name let's say this was set name and we set this cat's name to be elaine now let's go ahead and expand this cat1 variable so we can see that its name is currently kramer but now if i come back here and highlight this cat1.set name to elaine and hit ctrl shift i to inspect it it actually executes that line of code and we can see that that cat's name actually did get changed to elaine so this is really powerful functionality that allows you to change the state of your variables kind of any way that you want at any spot in your program another view we have available here in the debug perspective is this breakpoints view the breakpoints view just shows you every current breakpoint that you have in your code so right now we only have one breakpoint here but if we add another one say on line 10 that will show up here as well if we want to inside this view we can click this check box to temporarily disable any individual break point that we want so we can see when i uncheck this breakpoint at line 5 that break point turns from a blue into a white to indicate that it's currently disabled we can also enable or disable individual breakpoints here by right-clicking and going to enable breakpoint or disable breakpoint one thing to note here that i think may sound silly but i've seen trip up so many programmers in the past is this skip all breakpoints button so if you click this button it will temporarily disable all the break points in your entire program and the shortcut to do that is control alt b so what i've seen happen time and time again is people are trying to debug their code and they have break points in them but they see that when they run their program in debug mode it doesn't hit any of them and they're just ripping their hair out wondering what's going on and it's because they either hit this button or accidentally hit the shortcut to disable all the breakpoints now in eclipse it makes it a little bit obvious because it puts a line through the breakpoint but in other ides like intellij when you're skipping all the breakpoints it's not quite as obvious right there in the indicator here's another really cool thing that you can do with breakpoints so let's say in your code you had something like a simple for loop like this so this is just a simple for loop that starts with i equal to 0 and goes while i is less than 10 and increments i each time and then all it does is just print out the value of i each time through the loop so to debug a program like this you can go ahead and put a breakpoint right here inside the for loop and then when you run your program in debug mode when it enters that for loop for the first time it hits that debug point and shows you the value of every variable which for this one is basically just the i variable that starts with a value of zero but now each time you hit the resume button it of course goes into the next iteration of that loop and hits the exact same break point on line six just in the next iteration of the loop so now we can see that we've hit the break point again the same break point on the same line but now i has the value of one and that's fine right you can keep uh resuming your program and each time that you resume it goes back around to the beginning of the for loop so we can keep clicking through this and watch i get updated six seven eight but what if we had a situation where we want to debug our program but we only have a problem in our code when i is seven so right now you would have to run your program in debug mode and then keep clicking resume over and over again until i reaches the value seven and then you can inspect and see what's going on there that's not that big of a deal if your loop only goes through ten times but what if you did it hundreds or thousands of times you'd have to keep clicking resume for hundreds or thousands of times until you get to the exact iteration that's causing you problems so what you can do instead of doing that is use what is called a conditional break point so let's go ahead and stop our code and change this breakpoint into a conditional breakpoint and you do that by right-clicking your breakpoint and going down to breakpoint properties then down here in this section you can check the conditional checkbox then here in this section you have to enter some java code a java expression that evaluates to either true or false and then at each time your program hits the line with your conditional break point it will evaluate what that expression is at that time and if it's false it'll just skip hitting that break point but if it evaluates to true it'll stop at that break point like normal so in this situation we want it to hit our break point and stop when i has the value of seven so for our condition here we can just put i double equals seven and then we can hit apply and close now let's go ahead and run our program again in debug mode and see what happens so we can see that it didn't stop at every single iteration in our for loop it already printed out hello 0 hello one all the way up to hello 6 but when i got the value of seven it did hit our break point and stop execution right there when a breakpoint is a conditional breakpoint you can see that the icon changes a little bit to have this question mark right next to the breakpoint symbol i really hope you enjoyed this video and learned something because you don't have to spend the whole rest of your programming life just putting individual print statements everywhere when you're trying to figure out what's causing some kind of bug in your code as always don't stop now keep that learning momentum going by watching one of the other videos below thank you so much for watching i really do appreciate you being here with me see you next time
Info
Channel: Coding with John
Views: 171,682
Rating: undefined out of 5
Keywords: java, codingwithjohn, coding with john, java beginner lesson, eclipse debugger tutorial, eclipse debugging, java debugging, java debug eclipse, debug java, java debug, debug eclipse, eclipse debug, debug, eclipse tutorial, eclipse debug tutorial
Id: aqcJsKdjjvU
Channel Id: undefined
Length: 22min 18sec (1338 seconds)
Published: Tue Jan 04 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.