课程: Learning Java 17
Debugging with an IDE
- [Instructor] Adding print statements for debugging and removing them later on can get a little annoying. There's another way we can debug our programs. At the beginning of the course, we installed an IDE to compile and run our Java programs, but there's another benefit to using an IDE. An IDE comes with some built-in debugging tools that we can use to find and solve errors in our programs. We saw one already when our variable name was misspelled. Now it's time to dig a little deeper. Another tool that an IDE gives us is a break point. A break point is an intentional stopping point put into a program for debugging purposes. This allows us to temporarily halt a program in its execution in order to inspect the program's internal state. A program's internal state includes the values of its variables, the result of certain lines of code and weather or not the program reaches a certain line in its execution. Instead of using print statements to reveal the program's internal state, we can pause the program during its execution and inspect its state as it executes. Let's see how this works. Here we have the multiple choice program code, but the value of correct answer is choice three. Let's add a break point then on the line with our if statement. We can add a break point on a specific line by clicking the space just after the line number. Now we have a break point on line 26. With this break point, our program will pause its execution just before this line. In order for these break points to work, we'll need to run our program in debug mode. Instead of clicking the play button, we'll click the bug in the right hand corner. Debug hello world. Another option is to left click the file and debug in this way. Let's enter our answer, Jupiter. Our program is paused and it paused just before the if statement was executed. At the bottom of our screen, we get a window and it has various details about our program's internal state. We have each variable, question, choice one, choice two, along with its value. We can also see the line we're paused on or in the main function on line 26. Looking at the value of correct answer, this is one way we could detect our program's bug. Correct answer should have the value Jupiter. Now the variables shown here are the variables in scope. Remember, a variable scope refers to where we can access and use the variable in our program. If the variable is created with a certain block, it can only be used within that block and not outside of it. Now that we've inspected the program state, how do we make the program continue its execution? In the debugger window, we have a few buttons that can help us. The play button will resume the program and it will we stop again if it runs into another break point. If we just want to run the next line of code, we can click the step over button. In fact, we can click this button continuously to run each line of code until the program has finished. If we find an error and want to fix it, we can always stop the program with the stop button. We can use the button in the debugger or the button and the top right. There are other buttons here that are useful for debugging but they're out of the scope of this introductory course. Ultimately, the debugger is useful because we can add a break point to inspect a program's internal state, instead of adding a bunch of print statements. Next, you'll get the chance to debug a program all on your own using the tools we've covered in this chapter.
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。