Debugging in Java with IntelliJ

Rohinth RathnaRohinth Rathna
3 min read

What is Debugging?

Debugging is the systematic process of identifying, analyzing, and fixing errors or “bugs” in the source code.

No programmer writes perfect code every time. Sometimes, we also want to understand what exactly is happening inside our program. Luckily, in Java, the JDK provides a built-in tool called jdb (Java Debugger) that makes debugging easier compared to debugging in C programming.

However, in most real-world projects, developers use IDEs (like IntelliJ IDEA, Eclipse, or NetBeans) for a more visual and beginner-friendly debugging experience.


Debugging with IDEs

Here, I’ll use IntelliJ IDEA Community Edition to demonstrate debugging.

public class Employee {  
    // Instance variables (non-static)  
    private String name;  
    private float salary;  

    // Constructor  
    public Employee(String name, float salary) {  
        this.name = name;  
        this.salary = salary;  
    }  

    // Getter methods  
    public String getName() { return name; }  
    public float getSalary() { return salary; }  

    // Setter methods  
    public void setName(String name) { this.name = name; }  
    public void setSalary(float salary) { this.salary = salary; }  

    // Instance method  
    public void displayDetails() {  
        System.out.println("Employee: " + name);  
        System.out.println("Salary: " + salary);  
    }  

    public static void main(String[] args) {  
        Employee emp = new Employee("Jimmy", 20000.0f);  
        emp.displayDetails();  
        emp.setName("Mpp");  
        emp.setSalary(456);  
        System.out.print(emp.getName());  
        System.out.print(emp.getSalary());  
    }  
}

In this program, we created a class named Employee. We then created an object (emp) in the main method, called methods to display details, and updated the employee's name and salary.


Setting Breakpoints

To investigate what’s going on, we can use breakpoints. In IntelliJ, click on the left margin next to the line of code where you want the program to pause.


Setting Conditional Breakpoints

By right-clicking a breakpoint, you can add a condition.
This means the program will only pause if the condition is true — very useful when looping through large datasets.


Running in Debug Mode

You can start debugging by right-clicking the file and selecting Debug.
Once paused, you can:

  • Step through code line-by-line.

  • Inspect variable values.

  • Change variable values during runtime.

  • Resume execution when ready.


Step Over, Step Into, and Step Out

When you are paused at a breakpoint, most IDEs give you three main navigation options:

Step Over

  • Executes the current line of code.

  • If the line contains a method call, it executes the entire method without going inside it, then moves to the next line.

  • Useful when you don’t need to see how a method works internally.


Step Into

  • Moves inside a method call to debug it line-by-line.

  • Helpful when you want to investigate what’s happening inside a method.

  • If there are multiple nested method calls, it steps into the first one.


Step Out

  • If you are inside a method, this runs the rest of the method without stopping and returns to the point where it was called.

  • Useful when you’ve seen enough of a method and want to go back to the caller quickly.


Advantages of Using a Debugger

  • Change variable values while the program is running.

  • Inspect the flow of the program and understand how data changes over time.

  • Quickly locate and fix logical or runtime errors.

  • Avoid printing unnecessary debug statements (System.out.println) everywhere.

  • Step directly into method calls to see what’s happening inside them.

  • Use conditional breakpoints to skip irrelevant execution steps.

📌 Next up in this series: Learn how to debug Java programs using the jdb command-line tool — perfect for working on servers or interview prep.

0
Subscribe to my newsletter

Read articles from Rohinth Rathna directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rohinth Rathna
Rohinth Rathna