Table of contents
An error was reported during the debugging process
Edit view method source code with debug
View the Arrays.sort() method layer by layer
Introduction: Breakpoint debugging refers to setting a power-off on a certain line of the program. When debugging, the program will stop when it runs to this line, and then you can debug step by step. During the debugging process, you can see the current value of each variable. If there is an error, the error will be displayed when debugging the error line of code, and the error will be displayed by analysis to find the bug.
Shortcut keys: F7 (jump into: jump into the method), F8 (skip: execute code line by line), shift+F8 (jump out: jump out of the method), F9 (resume, execute to the next breakpoint)
F8 case
public class Debug01 {
public static void main(String[] args) {
int sum = 0;
for (int i = 0; i <= 3; i++) {
sum += i;
System.out.println("i=" + i);
System.out.println("sum=" + sum);
}
System.out.println("结束循环");
}
}
Execute code line by line
When encountering output sentences, they will be output one by one
After the loop is executed, the following statement will be executed
It will automatically jump out until all the statements of the program are executed
An error is reported during the debug process
public class Debug02 {
public static void main(String[] args) {
int[] arr = {1, 2, 3};
for (int i = 0; i <= arr.length; i++) {
System.out.println(arr[i]);
}
System.out.println("循环结束");
}
}
Use debug to view method source code
import java.util.Arrays;
public class Debug03 {
public static void main(String[] args) {
int[] arr = {1, -1, 2, -2, 10, 5};
Arrays.sort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + "\t");
}
}
}
Two methods:
(1) Pressing F7 without doing any operation does not enter the source code of the method
In the settings, uncheck the following two statements to press F7
(2) alt + shift + F7 to force entry
View the Arrays.sort() method layer by layer
F9
Breakpoints can be dynamically set during the debug process
Press the F9 key, it will jump directly to the next breakpoint, and execute all the statements before the second breakpoint
You can also continue to dynamically increase breakpoints for testing
DebugExercise
Use breakpoint debugging to trace the process of the next object creation
Process of creating objects
(1) Load Person class information
(2) Initialize Default Initialization -> Display Initialization -> Constructor Initialization
(3) Return the object address
public class DebugExercise {
public static void main(String[] args) {
Person person = new Person("周小末", 18);
System.out.println(person);//默认调用toString
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
(1) debug view loaded Person class information
F8 from creating a person object will go directly to the constructor in the Person class
Keep going, it will return System.out.println(person) after the constructor is executed;
(2) Debug to check whether the person in System.out.println(person); has called the toString method
Then continue alt+shift+F7 String.valueOf(x)
If obj is not empty, call the toString method in obj, and because the operation type of obj is Person
So it will go directly to the toString method in the Person class