CodeGym /Courses /JAVA 25 SELF /Debugging in IntelliJ IDEA

Debugging in IntelliJ IDEA

JAVA 25 SELF
Level 14 , Lesson 6
Available

1. Bug

Programmers have their own slang. And the first word you will get to know closely is bug. This word refers to an error in a program that causes it to do the wrong thing, crash, or produce a weird result.

If a program works strangely, but the programmer assures you it was intended, they usually say: "It's not a bug, it's a feature." The internet is full of memes on this topic.

Meme about bug and feature

Historical Background

Legend has it that in September 1947, Harvard scientists were testing the Mark II computer. It was malfunctioning. After checking all the contacts, they found an actual moth stuck between the relays. The insect was taped into the logbook with the caption "First actual case of bug being found".

Since then, the process of finding and fixing errors has been called debugging.

Photo of the first bug in the logbook


2. Debug Mode

To find a bug, programmers use a Debugger. This is a special tool inside IntelliJ IDEA that allows you to pause the execution of a program and look "under the hood".

IntelliJ IDEA can run your program in two modes:

Mode Icon Hotkeys Purpose
Run (Normal) Play Icon Shift+F10 (Win/Lin)
Ctrl+R (Mac)
The program just runs from start to finish.
Debug (Debugging) Bug Icon Shift+F9 (Win/Lin)
Ctrl+D (Mac)
The program can stop at specified places.

In debug mode, you can execute the program line by line, observing how the values of variables change.

3. Breakpoints

For the Debugger to know where to stop, you must set a Breakpoint. Without it, the program will just fly to the end as usual.

Let's practice with a live example. We have moved the multiplication logic into a separate method to show the full power of the debugger. Copy this code into IDEA:

public class Main {
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 1; i <= 5; i++) {
            int result = calculateMultiply(i);  // <--- We want to stop here
            sum += result;
            System.out.println("Step: " + i + ", Result: " + result + ", Sum: " + sum);
        }
        System.out.println("Final Sum: " + sum);
    }

    public static int calculateMultiply(int number) {
        return number * 2;
    }
}

Task. We want to see how variables change inside the loop and peek inside our method.

Action. Click in the narrow gray gutter to the left of the line int result = calculateMultiply(i);. A red circle will appear.

IDEA Editor showing Java code with a red Breakpoint

Now run the program via Debug (the bug icon). The program will start and instantly "freeze" before executing this line.

IDEA Debugger suspended at the breakpoint, showing highlighted execution line

4. Controls: F8, F7, F9

When the program is paused, you have a control panel at the top of the Debugger tool window.

Debugger toolbar with Step Over, Step Into, Resume buttons highlighted

F8 — Step Over

Press F8. The program will execute the current line (call the method, get the result) and move to the next one, sum += result;.

Execution moved to sum += result; inline values visible]

Press F8 again. The program will add the numbers. You will see how the variable values have changed.

Variables sum and result updated in the Variables pane

Press F8 again. You will see the output in the console.

Console output showing Step 1]

This is the main way to navigate: line by line, without diving into the internal details of methods.

F9 — Resume Program

We have a loop from 1 to 5. Pressing F8 five times to complete one loop iteration takes a long time. What if the loop has 1000 iterations?

Press F9. The program will "unfreeze", execute the rest of the code in the current iteration, go to the second round, and... stop at the Breakpoint again.

Pressing F9 jumps to the next iteration of the loop, i=2

Thus, using F9 you can quickly skip through loops: one press equals one iteration.

Variables showing values for the second iteration]

F7 — Step Into

If your code encounters a call to your method (in our case calculateMultiply(i)), and you press F8, the Debugger will simply execute it and move on. But if you want to see how the code works inside this method — press F7.

Try pressing F7 when the program stops at the line int result = calculateMultiply(i);. The Debugger will take you inside the calculateMultiply method, and you will see how the value is passed to the number parameter.

Stepping into calculateMultiply(int number) method

Shift + F8 — Step Out

What if you pressed F7 by accident on the System.out.println(...) line and got lost in the depths of Java's system code? Reading thousands of lines of someone else's code is boring and confusing.

Press Shift + F8. The program will quickly execute everything left in the current method and return you back to where you came from (to your main method).

Stepping out of PrintStream/System.out method back to Main

5. Where to look at values?

Let's go back to our loop example.

Debugger window overview showing Variables and Frames tabs

Method 1: Inline Debugging

Look directly at the code editor. To the right of the lines, IDEA writes the current values in gray.

Method 2: Variables Panel

At the bottom of the Debugger window, there is a Variables tab. It shows all the current variables.

Important note for Java: Primitive types (like int) show their value immediately. However, objects and arrays (like String[]) store a lot of data inside. To see their contents, you need to click on the small arrow to the left of the variable to "expand" the object.

Method 3: Frames Panel

To the left of the variables is a list called Frames (Call Stack). It shows what step the program is currently on.

For example, the entry main:6, Main means:

  • You are in the main method of the Main class.
  • You have stopped at line number 6.

This is your exact address in the code. If you dive into another method (press F7 on calculateMultiply), a new entry calculateMultiply:13, Main will appear at the top of this list, indicating that the new method was called from the previous one.

6. Evaluate Expression

Sometimes just looking at variables is not enough. You want to ask: "What happens if..."

Press Alt + F8 (Windows/Linux) or Option + F8 (macOS). The "Evaluate" window will open. You can also type expressions directly into the line below the Variables panel.

Here you can write any code using the current variables. For example, in our loop, write:

sum + 100

Press Enter and IDEA will calculate the result without changing the real program. This is your scratchpad for experiments.

Evaluate Expression dialog showing sum + 100 with the evaluated result

7. Conditional Breakpoints

Imagine you have a loop with 10,000 iterations, and an error occurs only on the 5000th step. Pressing F9 five thousand times is a bad idea.

Right-click on the red Breakpoint circle. A popup window will appear. In the Condition field, write a condition in Java, for example: i == 5. Click Done.

Right-click on a Breakpoint showing the Condition field with i == 5 entered

Now, if you run the Debugger, the program will ignore the first 4999 loops and stop only when the condition becomes true. This will save you hours of work!

8. Managing all Breakpoints

Sometimes beginners place so many Breakpoints in different project files that the program then constantly stops in unexpected places. Searching for red circles across all files manually takes a long time.

Press Ctrl + Shift + F8 (Windows/Linux) or Cmd + Shift + F8 (macOS). The Breakpoints window will open.

Breakpoints window (Ctrl+Shift+F8) showing a list of all active breakpoints with checkboxes

Here you see a list of all breakpoints in your project. You can uncheck them (to temporarily disable) or remove them with the Delete key.

9. Summary

We have covered the main arsenal of a Java developer for finding bugs. The Debugger is what turns writing code from "reading tea leaves" into precise engineering work.

The ability to quickly find the cause of an error is a skill highly valued in commercial development, even more than the ability to write code quickly. Practice with conditional breakpoints and Evaluation, and no bug will hide from you.

Comments (1)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Hoist Level 38, San Diego, United States
17 April 2026
observing how the values of variables change.