项目代码
https://github.com/yinhai1114/Java_Learning_Code/tree/main/IDEA_Chapter08/src/com/yinhai/debug_
目录
一、断点调试基本介绍
在开发过程中,在查找错误时可以使用断点调试一步步的看源码执行的过程,从而发现错误所在,在调试过程中,是运行状态,是以对象的运行类型来执行的
1.断点调试是指在程序的某一行设置一个断点,调试时,程序运行到这一行就会停住,然后可以一步步往下调试,调试过程中可以看各个变量的当前值,出错的话调试到出错的代码行即显示错误,停下,进而分析从而找到这个bug
2.断点调试是必须掌握的技能
3.断点调试也能帮助我们查看java底层源码的执行过程。
二、 断点调试的快捷键
F7跳入方法内
F8 逐行执行代码
shift + F8跳出方法
F9(resume,执行到下一个断点)
如果遇到debug按F8或者F9没反应可以关闭其他程序,有可能热键被占用了,我是有道翻译和kook占用了关掉就好了
三、断点调试的案例
案例1.for循环
package com.yinhai.debug_.debug01;
public class Debug01 {
public static void main(String[] args) {
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += i;
System.out.println("i=" + i);
System.out.println("sum=" + sum);
}
System.out.println("exit for");
}
}
案例2.数组越界的异常
package com.yinhai.debug_.debug02;
public class Debug02 {
public static void main(String[] args) {
int[] arr = {1 ,10,-1};
for (int i = 0; i <= arr.length; i++) {
System.out.println(arr[i]);
}
System.out.println("exit for loop array");
}
}
案例3.追溯源码
演示如何追溯源码 小技巧:将光标放在某个变量上,可以看到最新的数据
对于object内的方法,默认无法使用F7跳转入方法体 有两个解决方案
解决方法1
使用force step into : 快捷键 alt + shift + F7
解决方法2
这个配置一下就好了:
点击Setting --> Build,Execution,Deployment --> Debugger --> Stepping
把Do not step into the classes中的java.*,javax.*取消勾选,其他的随意然后,就可以进入源码了
package com.yinhai.debug_.debug03;
import java.util.Arrays;
public class Debug03 {
public static void main(String[] args) {
int[] arr = {-1 ,20,10,0};
//want to watch how a method is implemented,can use Debug
Arrays.sort(arr);//use F7 cant jump into jdk original code,so,how to jump into the jdk original code?
//can use force step into Alt + shift + F7
//and refer to above other one solution method
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + "\t");
}
}
}
案例4.如何执行到下一个断点F9
可以在自己写的里面动态写断点,也可以在系统的原码里也可以写断点
四、断点调试的课后练习
1.追踪对象创建的过程和toString方法调用的过程
1)加载类信息
2.初始化开空间
3)toString 的调用
4)
2.查看动态绑定机制是如何运行的
1.当调用对象方法的时候,该方法会和对象的内春地址/运行类型绑定,对象的运行类型是哪个就用哪个类型的方法
package com.yinhai.poly_.dynamic_;
public class DynamicBinding {
public static void main(String[] args) {
A a = new B();
System.out.println(a.sum());//40 -->30
System.out.println(a.sum1());//30 --> 20
}
}
class A {
public int i = 10;
public int sum(){//run this statement
return getI() + 10;//and run getI , so here is a question ,where is run to? is A or B?
//so elicitation dynamic binding
//mechanism of dynamic binding
//1.when running object method,this object method will bind to run type
//so getI will use method of class B
//output 30
//2.when running parameter of object,have not dynamic binding! in other way , where declaration where used
}
public int sum1(){
return i + 10;//so run this line,here is will return i,this parameter is class A,have not dynamic binding!
}
public int getI(){
return i;
}
}
class B extends A{
public int i = 20;
// public int sum(){
// return getI() + 10; //to find class A of sum method ,if be annotated with this statement
// }
// public int sum1(){
// return i + 10; //to find sum method of class A ,if be annotated with this statement
// }
public int getI(){
return i; //not dynamic binding
}
}