You are currently viewing Debugging Java Applications: A Guide with IntelliJ IDEA

Debugging Java Applications: A Guide with IntelliJ IDEA

1. Introduction

When you write your Java program, you want to solve a specific problem. However, there are situations where your program does not display the expected behavior when executed. You are then forced to debug the program to understand what is wrong. There are several debugging techniques including the use of log messages and execution of the program in step-by-step mode. In this tutorial, we will focus on this last technique.
By the end of this article, You will master the fundamentals of debugging Java applications in IntelliJ IDE. You will learn about breakpoints, how to create them, and how to run your program step by step.

2. Prerequisites

To complete the following tutorial, you will need :

  • The Java SE Development Kit 17 (JDK 17): If you don’t have it already refer to this tutorial for the installation procedure.
  • IntelliJ IDEA: If you don’t have it already refer to this tutorial for the installation procedure. You may also refer to the same tutorial to see how to create and run a Java project. We assume here that you already know that.

3. What is Debugging?

Debugging is the process of identifying, isolating, and fixing bugs and errors in a software program. IntelliJ IDEA provides robust debugging features to assist you in identifying and fixing issues in your code.

4. What is a Breakpoint?

A breakpoint is a designated point in the source code of your program where the execution should temporarily pause during the debugging process. When you are running your program in debug mode and it encounters a breakpoint, it will stop, allowing you to inspect the program’s state, variables, and control flow.

5. Running a Program in Normal Mode

Let’s consider the following program:

class Scratch {
    public static void main(String[] args) {
        int i = 0;
        int j = 1;
        int k = 2;
        i = j + 1;
        j = j + 1;
        k = i + j;
        System.out.printf("i=%d, j=%d, k=%d",i,j,k);
    }
}

if you run the following program in “normal” mode: Menu Run -> Run Scratch. You will see the following output in your console.

intellij_no_debug

Let’s see now how to run the same program in “debug” mode.

6. Running a Program in Debug Mode

6.1. Create Breakpoints

To run your program in “debug” mode, you need to create breakpoints first. Place breakpoints in your code by clicking in the left gutter next to the line numbers. A red dot will indicate the breakpoint. Create 3 breakpoints in lines 6, 7, and 8 as shown below:

intellij_breakpoint_create

6.2. Run in Debug Mode

Run your application in debug mode by clicking on the “Debug” button or using the menu Run -> Debug Scratch.

intellij_debug_button

The application will start and a debugger will be attached. The program execution will pause at line 6 as shown below :

intellij_debugger_stop

From this point, you can use any of the following debugging features. Here is a capture of the debugger options:

intellij_debugger_menu

6.3. Stepping Through Code

Stepping is the process of controlling the step-by-step execution of the program. IntelliJ IDEA provides a set of stepping actions, which are used depending on your strategy (for example, whether you need to go directly to the next line or enter the methods invoked on your way there).

Step Over

Steps over the current line of code and takes you to the next line even if the highlighted line has method calls in it.
Hitting this option from line 6 of the program above will take you to line 7.

Step Into

Steps into the method to show what happens inside it. Use this option when you are not sure the method is returning a correct result.

Step Out

Steps out of the current method and takes you to the caller method.

Inspect Variables

Hover over variables to see their current values. The variables panel allows you to inspect and modify variables.

Evaluate Expressions

You can use this feature to execute code snippets and inspect variables at runtime.

7. Conclusion

In this tutorial, you learned the basics of debugging Java applications. You learned about breakpoints, how to create them, and how to execute your program step by step. This article was just a quick introduction to debugging with IntelliJ. If you want to learn more about advanced debugging options, check out this article.

In the next article, you will dive into the object-oriented world.

Noel Kamphoa

Experienced software engineer with expertise in Telecom, Payroll, and Banking. Now Senior Software Engineer at Societe Generale Paris.

This Post Has One Comment

  1. Java Course

    Well written and insightful. Thanks!

Comments are closed.