Advanced Debugging Techniques in IntelliJ IDEA: Conditional Breakpoints and Evaluate Expression
This article explains how to use IntelliJ IDEA's advanced debugging features, including conditional breakpoints and the evaluate expression tool, with step‑by‑step instructions, practical code examples, and tips for efficiently locating and fixing Java runtime errors.
IntelliJ IDEA, developed by JetBrains, is a powerful Java IDE that also supports Kotlin, Scala, Groovy, and Android development, offering both a free community edition and a feature‑rich professional edition.
Debugging is an essential part of software development, allowing developers to step through code, inspect variables, memory usage, and thread states to understand program behavior and fix bugs efficiently.
This article introduces advanced debugging techniques in IDEA, focusing on two main features:
Conditional Breakpoint : Set breakpoints that pause execution only when a specified condition is true, useful for isolating problematic iterations in loops or complex branching logic.
Evaluate Expression (Calculator) : Evaluate arbitrary expressions, view or modify variable values, and even invoke methods during a debugging session without restarting the program.
Debug Basics
The following table lists common debugging shortcuts for Windows and macOS:
Operation
Windows
Mac(OS X)
Step Over (skip method body)
F8
F8(Fn)
Step Into (enter method)
F7
F7(Fn)
Smart Step Into (choose among multiple calls)
Shift + F7
⇧F7 (Fn)
Smart Step Out
Shift + F8
⇧F8 (Fn)
Resume Program
F9
F9(Fn)
Run to Cursor
Alt + F9
⌥F9(Fn)
Evaluate Expression
Alt + F8
⌥F8 (Fn)
Toggle Breakpoint
Ctrl + F8
⌘F8 (Fn)
View Breakpoint Info
Ctrl + Shift + F8
⌘⇧F8 (Fn)
Conditional Breakpoint
Example scenario: a List<User> is created where the third element has age = null . Adding 10 to age triggers a NullPointerException . By setting a conditional breakpoint with the condition Objects.isNull(user.getAge()) , the debugger stops only on the problematic record.
public class IDEATest {
public static void main(String[] args) {
List
userList = new ArrayList<>(6);
userList.add(new User(1, "Tom", 19));
userList.add(new User(2, "Giles", 25));
userList.add(new User(3, "Alex", null));
userList.add(new User(4, "Ryan", 21));
userList.add(new User(5, "DongGe", 19));
userList.add(new User(6, "RUI", 21));
userList.forEach(user -> {
String name = user.getName();
int futureAge = user.getAge() + 10;
System.out.println(name + " 10年后" + futureAge + "岁");
});
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
class User {
private Integer id;
private String name;
private Integer age;
}After adding the conditional breakpoint on the line that adds 10 to age , the debugger immediately highlights the record with age = null , allowing fast identification and correction of the issue.
Evaluate Expression (Calculator)
In IDEA 2023, the Evaluate Expression tool can be accessed via the shortcut Alt+F8 or through the UI. It enables developers to:
View current variable values without opening the watch window.
Modify variable values on the fly to test different scenarios.
Test arbitrary expressions or invoke methods during a paused debugging session.
These capabilities significantly speed up debugging and reduce the need for additional test code.
Tags: IntelliJ IDEA, Debugging, Conditional Breakpoint, Evaluate Expression, Java, IDE Tips
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.