1.打斷點(diǎn),并啟動(dòng)
打斷點(diǎn):
鼠標(biāo)左鍵點(diǎn)擊這里就會(huì)出現(xiàn)一個(gè)紅點(diǎn)標(biāo)志 (有各種形狀,后續(xù)解釋),就打上了一個(gè)斷點(diǎn)。

啟動(dòng):
- 對(duì)于已經(jīng)運(yùn)行過(guò)的程序,直接點(diǎn)擊,小蟲子就能啟動(dòng)
-
對(duì)于沒有運(yùn)行過(guò)的程序,找到程序入口(main方法)前面的三角形
,鼠標(biāo)右鍵啟動(dòng)

啟動(dòng)后,當(dāng)我們的程序執(zhí)行到斷點(diǎn)程序處,就會(huì)出現(xiàn)調(diào)試界面,如下:
如果沒有出現(xiàn)調(diào)試界面,那就是說(shuō)明:根本沒有執(zhí)行斷點(diǎn)處的程序。
2.按鈕介紹
1.測(cè)試程序
先寫一個(gè)簡(jiǎn)單的類來(lái)做測(cè)試,如下:
public class TestDemo {
public static int add(int a, String b) {
int c = Integer.parseInt(b);
return a + c;
}
public static char sub(int d, int e) {
char res = (char) Math.abs(d - e);
return res;
}
public static void main(String[] args) {
String b = "45";
int a = 13;
int sum = add(a, b);
int d = 17;
int e = 89;
char f = sub(17, 89);
}
}
接著,找個(gè)地方打上斷點(diǎn)(確保程序會(huì)執(zhí)行的地方)打上斷點(diǎn):
然后開始debug。
2.Step Over
步過(guò):
作用:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-475079.html
- 一行代碼一行代碼地往下執(zhí)行
- 不會(huì)進(jìn)入到路經(jīng)的方法里面
3.Step Into
步入:
作用:
- 進(jìn)入我們自定義的方法
- 不會(huì)進(jìn)入其他類庫(kù)的方法
4.Force Step Into
強(qiáng)制步入:
作用:
- 可以進(jìn)入更深層次的方法
- 自定義類庫(kù)或者其他類庫(kù)的方法都能進(jìn)入
5.Step Out
步出:
作用:
- 從對(duì)應(yīng)的方法中出來(lái),返回上一層
6.Drop Frame
回退斷點(diǎn):
作用:
- 當(dāng)進(jìn)入到更深層的方法后才能使用
- 當(dāng)我們錯(cuò)過(guò)了debug的位置時(shí)使用
- 這個(gè)按鈕可以讓焦點(diǎn)回到調(diào)用方法之前,然后再次debug一次方法(給你犯錯(cuò)重來(lái)的機(jī)會(huì))
- 對(duì)于數(shù)據(jù)庫(kù)增刪改操作無(wú)法回退
注意: 此回退僅限于,那個(gè)方法還沒有調(diào)用完,否則是無(wú)法回去的再次debug那個(gè)方法的,只能重新啟動(dòng)debug。
7.Run to Cursor
跳到光標(biāo)處
作用:
- 將debug執(zhí)行位置跳轉(zhuǎn)到光標(biāo)位置,然后繼續(xù)debug
- 只能往后面跳,如果光標(biāo)在debug執(zhí)行位置前面,就會(huì)終止debug
8.Evaluate Expression
表達(dá)式計(jì)算:
作用:
- 獲取已經(jīng)debug過(guò)的參數(shù)的值
- 計(jì)算已經(jīng)debug過(guò)的參數(shù)的值(表達(dá)式、方法)
9.Resume Program
恢復(fù)程序:
作用:
- 當(dāng)有多個(gè)斷點(diǎn)時(shí)使用
- 直接跳轉(zhuǎn)到下一個(gè)斷點(diǎn)的位置
10.Stop
停止程序:
作用:
- 停止debug
- 會(huì)繼續(xù)跑完程序
11.View BreakPoints
查看斷點(diǎn):
作用:
- 查看所有斷點(diǎn),并進(jìn)行相關(guān)設(shè)置
12.Mute BreakPoints
禁用斷點(diǎn):
作用:
- 暫時(shí)禁用掉所有斷點(diǎn),放行程序
3.斷點(diǎn)分類
1.方法斷點(diǎn)

給方法打上斷點(diǎn)之后,當(dāng)我們的debug到調(diào)用該方法的位置的時(shí)候,會(huì)直接跳轉(zhuǎn)到方法處(直接Step Out就會(huì)跳轉(zhuǎn))。
2.屬性斷點(diǎn)

斷點(diǎn)右鍵可以進(jìn)行設(shè)置:
3.異常斷點(diǎn)
添加異常斷點(diǎn)流程:



作用:
- 在全局打上異常(上述添加的是堆棧溢出異常)
- 只要哪里出現(xiàn)了堆棧溢出異常,程序就會(huì)跳轉(zhuǎn)到那個(gè)地方,方便快速找到異常位置
- 可在run程序拋出異常后,根據(jù)異常去打斷點(diǎn),然后進(jìn)行調(diào)試
4.條件斷點(diǎn)
右鍵斷點(diǎn),設(shè)置條件,只有當(dāng)滿足條件時(shí),才會(huì)進(jìn)入該斷點(diǎn)程序停留,否則就直接往下面執(zhí)行程序。

5.流斷點(diǎn)
調(diào)試流程:
-
代碼:
public static void main(String[] args) { Stream.of(10, 30, 50, 90, 40) .mapToLong(e -> e*e ) .filter(e -> e > 2500) .forEach(System.out::println); }
-
斷點(diǎn)調(diào)試:
作用:
- 可以查看流的工作流程
6.多線程斷點(diǎn)
調(diào)試流程:
-
代碼:
public static void main(String[] args) { new Thread(() -> { System.out.println(); System.out.println("thread1 is running"); StringBuilder sb = new StringBuilder("."); for (int i = 0; i < 10; i++) { try { Thread.sleep(100); System.out.print(sb.append(".").toString()); } catch (InterruptedException e) { e.printStackTrace(); } } }, "thread1").start(); new Thread(() -> { System.out.println(); System.out.println("thread22 is running"); StringBuilder sb = new StringBuilder("."); for (int i = 0; i < 10; i++) { try { Thread.sleep(100); System.out.print(sb.append(".").toString()); } catch (InterruptedException e) { e.printStackTrace(); } } }, "thread22").start(); new Thread(() -> { System.out.println(); System.out.println("thread333 is running"); StringBuilder sb = new StringBuilder("."); for (int i = 0; i < 10; i++) { try { Thread.sleep(100); System.out.print(sb.append(".").toString()); } catch (InterruptedException e) { e.printStackTrace(); } } }, "thread333").start(); }
-
斷點(diǎn):
-
查看輸出:
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-475079.html
作用:
- 將 suspend 設(shè)置為 Thread
- 這樣就可以單個(gè)線程單個(gè)線程地調(diào)試,而不會(huì)出現(xiàn),線程之間爭(zhēng)搶資源的情況(線程不斷切換)
到了這里,關(guān)于Idea斷點(diǎn)調(diào)試(debug)詳解的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!