Master IDEA Debugging: From Basics to Advanced Techniques
This guide walks through IntelliJ IDEA's Debug feature, covering preparation, main interface, service and debug buttons, variable inspection, conditional breakpoints, expression evaluation, frame dropping, force return, multithread, Stream, and remote debugging, complete with code examples and screenshots.
简介
Debug is one of the most frequently used features in IntelliJ IDEA, allowing developers to trace code execution, monitor variable changes, locate errors, and explore third‑party frameworks; once enabled, the program becomes fully transparent, showing each step and its reason.
准备工作
Before using Debug you need IDEA installed and a piece of code to debug; the example uses the classic LeetCode “Two Sum” solution.
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class ToSum {
public static int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int sub = target - nums[i];
// 查找余数是否在表中,有则返回两个数的索引
if (map.containsKey(sub)) {
return new int[]{map.get(sub), i};
}
// 没有则将其信息添加进哈希表
map.put(nums[i], i);
}
throw new IllegalArgumentException("No two sum solution");
}
public static void main(String[] args) {
int[] nums = {2, 7, 11, 15};
int target = 9;
int[] ints = twoSum(nums, target);
System.out.println(Arrays.toString(ints));
}
}主界面介绍
The Debug mode UI includes the Debug button to start debugging, breakpoints set on the left gutter, service buttons for run/stop/configuration, debug buttons (Show Execution Point, Step Over, Step Into, Force Step Into, Step Out, Drop Frame, Run to Cursor, Evaluate Expression, Trace Current Stream Chain), Frames, Variables, Watches, and the Debug window placement controls.
变量查看与修改
Variables panel shows current values; you can hover over variables in the editor or view them in the panel. To change a value, select "Set Value…" and edit directly, then press Enter to apply.
条件断点
Set a breakpoint condition so the execution stops only when the condition is true, e.g., stop when a variable is null.
表达式求值
Use Evaluate Expression… to compute arbitrary expressions in the current context without restarting the program.
回退操作
Drop Frame acts as a "rewind" button, allowing you to return to a previous method call; note that side effects (e.g., database changes) are not undone.
强制返回
The Force Return button lets you exit a method early by providing a return value, useful when you have already identified a bug and want to stop further execution.
多线程调试
By default Debug blocks all threads; you can enable multithread debugging so only threads with breakpoints are paused, and switch between threads in the Frames view.
Stream 调试
The "Trace Current Stream Chain" button becomes active when a breakpoint is inside a Stream pipeline, allowing step‑by‑step inspection of each transformation.
远程 Debug
To debug code running on a remote server, start the JVM with
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005, configure a Remote JVM Debug configuration in IDEA, and attach to the remote process.
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -jar xxx.jar结语
All essential IDEA Debug features have been covered; stay tuned for more advanced topics.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
