Please enable JavaScript to use CodeHS

Using the Java Debugger

By Calvin Studebaker
Learn how to use the pause-step Java Debugger on CodeHS

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:

  1. Step - steps to the very next instruction, regardless of which method the instruction is located in
  2. Next - skips to the next instruction in the current method. Next will not step into any method calls, it will always go to the next command within the current method.
  3. Continue - continues to run the program without pausing


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!

  1. Start off with a Step command to enter the main method
  2. From here try using only Step to finish the program. It should take 13 Steps to finish every instruction.
  3. Now try again, this time using only Next to finish the program. It should only take 5 Nexts to finish, since Next does not step into any method calls!

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:

  • print x - prints out the value of the variable x. For Objects, this command will print out the result of calling x.toString()
  • dump x - For Objects, prints out the value of each field defined within the object. For primitives like int and double, dump is identical to print


Your Turn

Step to line 10 of this program, then:

  • Type print firstStudent into the debugger console to see the result of calling firstStudent.toString()
  • Type dump firstStudent into the debugger console to see all the fields defined inside the firstStudent object.

Now step to line 16 of this program, then:

  • Type print i to see the value of the for loop counter

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:

  • step - steps to the next instruction
  • next - skips to the next instruction in the current method
  • cont - continues execution without pausing
  • print - prints out the value of a variable
  • dump - dumps out the value of every field within an Object


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!