Debuggers are very useful tools for analyzing the execution of your program. Debuggers allow you to pause your program while it is running, and slowly step through the code line by line to see the flow of execution. You can also print out the values of each variable at each step of your program.
The CodeHS IDE allows you to pause and step through your Java programs by entering "Debug Mode" in your editor settings:
Let's try out the debugger on a real program! Press "Run" to start the debugger, and "Step" to start stepping through the code
Debugger Commands
The debugger provides a few options for stepping through the code:
Note
When debugging Java programs, we always want to begin with a Step command. This will step us into the main method and pause on our first line of code.
Your Turn
Try using Step and Next in this program!
Printing Variables
While debugging your program, you can use the print and dump commands to print out the values of the variables in your code:
Your Turn
Step to line 10 of this program, then:
Now step to line 16 of this program, then:
Curious to Learn More?
CodeHS is using the industry standard built-in Java debugger, JDB, to pause and step through your Java programs.
In this tutorial, you learned about using the following JDB commands:
These are the main commands you'll be using to debug Java programs, but you can check out all the JDB commands at your disposal by reading the JDB Documentation
Stay tuned for breakpoint support!