Java Debugger (JDB) Interview Questions

This section covers frequently asked interview questions about the Java Debugger (JDB), a command-line tool for debugging Java applications.

What is a Debugger?

A debugger is a program used to find and fix errors (bugs) in other programs. Debuggers allow you to step through code, inspect variables, set breakpoints, and analyze program behavior.

What is the Java Debugger (JDB)?

JDB is a command-line debugger for Java. You use commands to control program execution, inspect variables, and handle exceptions without modifying the source code.

Advantages of JDB

  • Lightweight
  • Free and cross-platform
  • Fast execution
  • Supports multithreading and remote debugging

Common Types of Bugs

  • Syntax/Compilation errors: Errors in the code's structure that prevent compilation.
  • Runtime errors: Errors that occur during program execution (often exceptions).
  • Concurrency/Threading errors: Difficult-to-reproduce errors stemming from multiple threads interacting.

Types of Java Debuggers

  • Stand-alone debuggers (e.g., JLike, JProbe)
  • Command-line debuggers (e.g., JDB)
  • IDEs with integrated debuggers (e.g., Eclipse, IntelliJ IDEA)

Debugging Approaches

  • Code optimization
  • Using comments to trace execution
  • System.out.println() debugging
  • Remote debugging
  • On-demand debugging

Connecting JDB to the JVM

The simplest way is to launch JDB with the class name you want to debug:

Command

jdb MyClass

The Interpreter's Role in Debugging

The Java Virtual Machine's (JVM's) interpreter interacts with JDB to allow program execution to pause and variables to be inspected.

JDB Invocation Syntax

Syntax

jdb [options] [classname] [arguments]

Debugging Applets with JDB

Launch JDB within the AppletViewer:

Command

appletviewer -debug URL

Listing JDB Commands

Type help in the JDB console to get a list of available commands and their descriptions.

Starting Main Class Execution

Command

run [classname [arguments]]

Resuming Execution

Use the cont command to resume execution after pausing.

`print` vs. `dump` Commands

  • print: Displays the value of an expression.
  • dump: Displays the internal state of an object.

Breakpoints

Breakpoints pause execution at specific points in the code, allowing for detailed inspection.

Setting Breakpoints

Set breakpoints by method name or line number:

  • Method: stop in ClassName.methodName
  • Line Number: stop at ClassName:lineNumber

Setting Breakpoints in Applets

To set a breakpoint in an applet's method, use the stop in command, specifying the applet's class name and method name.

Stepping in JDB

Stepping executes code line by line:

  • next (step over): Executes the next line, stepping over method calls.
  • step (step into): Executes the next line, stepping into method calls.
  • return (step return): Executes to the end of the current method.

Exception Handling in JDB

JDB allows you to handle runtime exceptions using the catch command.

Further Reading: