断点处添加 log
很多程序员在调试代码时都喜欢 print
一些内容,这样看起来更直观,print 完之后又很容易忘记删除掉这些没用的内容,最终将代码提交到 remote
,code review 时又不得不删减这些内容重新提交,不但增加不必要的工作量,还让 log tree
的一些节点没有任何价值
IntelliJ IDEA 提供 Evaluate and Log at Breakpoints
功能恰巧可以帮助我们解决这个问题, 来看下面代码:
public static void main(String[] args) {
ThreadLocalRandom random = ThreadLocalRandom.current();
int count = 0;
for (int i = 0; i < 5; i++) {
if (isInterested(random.nextInt(10))) {
count++;
}
}
System.out.printf("Found %d interested values%n", count);
}
private static boolean isInterested(int i) {
return i % 2 == 0;
}
假如我们想在第 15 行查看每次调用,随即出来的 i 的值到底是多少,我们没必要在这个地方添加任何 log,在正常加断点的地方使用快捷键 Shift + 鼠标左键
,就会弹出下面的内容