IDEA breakpoint debugging – Basics

1 introduction

Debug Is used to trace the running flow of code. When exceptions occur during program running, the Debug mode is usually enabled to analyze and locate the abnormal location and parameter changes during the running. Usually we can also enable Debug mode to track the running flow of the code to learn the source of the tripartite framework. IDEA is the most commonly used tool for JAVA development, so we should know more about the Debug of IDEA.

2 Breakpoint Types

IDEA classifies JAVA breakpoints into the following four types:

  1. Java Line Breakpoints: line breakpoint
  2. Java Method Breakpoints: method breakpoint
  3. Java Field Watchpoints: Field monitoring (field breakpoints)
  4. Java Exception Breakpoints: exception breakpoint

2.1 line breakpoints

Line breakpoints are also one of the most commonly used breakpoints, creating a line breakpoint by left-clicking to the left of the code we want to view.

The breakpoint created by the left mouse button is the default configuration. When you want to customize a breakpoint, you can click Shift + left mouse button to display the details configuration page

If you need to go to the Debug page, select Suspend

Results demonstrate

Run to the breakpoint, then suspend the thread, enter the Debug interface, as shown in the figure:

2.2 Method breakpoint

There are two types of method breakpoints

  • One is to simply put a break point on the method

After the breakpoint is set, the thread is suspended when entering or leaving the method and the Debug interface is displayed

  • The other is to create a breakpoint directly on the interface

In this case, a method breakpoint is set for the implementation of the interface method. When entering or leaving the method, the thread is suspended to enter the Debug interface

Results demonstrate

Implementation class code

/ * * *@author zzn
 * @date2021/4/4 while he * /
public interface IService {

    /** * interface breakpoint */
    void execute(a);

}

public class ServiceImpl implements IService {
    
    @Override
    public void execute(a) {
        System.out.println("Method of execution...");
        System.out.println("Method of execution...");
        System.out.println("Method of execution..."); }}Copy the code

2.3 Field Breakpoints

A field breakpoint is a breakpoint on an attribute variable or field in a class

When the value of this attribute changes, the thread is suspended and the Debug page is displayed

Results demonstrate

In toString, the age attribute is used, but it does not change the attribute, so it does not suspend the program.

Entity class code:

/ * * *@author zzn
 * @date2021/4/4 however * /
public class Student {

    private String name;

    private Integer age;

    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName(a) {
        return this.name;
    }

    public Integer getAge(a) {
        return this.age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString(a) {
        return "Student(name=" + this.name + ", age=" + this.age + ")"; }}Copy the code

Debugging code

    /** * field breakpoint */
    public void field(a) {
        Student student = new Student("field".10);
        student.setAge(12);
        System.out.println(student);
    }
Copy the code

2.4 Abnormal Breakpoint

Exception breakpoints are created differently than other breakpoints (but other types of breakpoints can be created this way as well)

Then search for NullPointerException on the screen that pops up and click OK to create an exception breakpoint

Results demonstrate

As you can see, there is no breakpoint in the method, but when NullPointerException is thrown, the thread is suspended and the Debug screen is displayed.

3 Basic Usage

The basic operations in IDEA are as follows:

  1. Show Execution Point: Displays the execution point. The shortcut keys are Alt + F10
  2. Step Over: Step by, shortcut key: F8
  3. Step Into: Step in, shortcut key: F7
  4. Force Step Into: Forced entry, shortcut key: Alt + Shift + F7
  5. Step Out: Step out, shortcut key: Shift + F8
  6. Drop Frame: Frame loss, shortcut key: none
  7. Run to Cursor: Run to the cursor, and press Alt + F9

3.1 Displaying execution points

When we look at other methods and forget where the breakpoint was executed, Show Execution Point allows us to quickly locate the breakpoint.

Step 3.2 a

Execute the next step of the method

  • If this line of code were a method, it would not go inside the method

  • If it is the last line of code or a return statement, jump to the next line of code from the method at the previous level

3.3 to enter

  • If the current line is a method call, it goes inside the method

  • If the current line is not a method call, jump to the next line of code

System.out.println(); It is also a method, but instead of going into the method, it jumps straight to the next line. This is because for some system classes, idea is excluded by default, which will directly jump to the next line, and will not enter the method. Specific excluded classes can be configured in the Settings.

3.4 Forced Walk-in

Forced-in methods have the same effect as forced-in methods, but for some methods that have been configured to check, such as system.out.println (); You can use forced walk-in to enter the method.

3.5 step out

Jump to the next line of code in the previous method

3.6 Frame Loss (key)

This is because each thread creates a virtual Stack with Stack frames that correspond to each Java method call.

Therefore, to drop a frame is to drop the stack frame of the method where the current breakpoint is located and fall back to the previous layer

Dropping a frame is a method that falls back to the previous level. It is equivalent to calling the method again, but some parameters/data whose state has changed cannot fall back to the previous state, such as objects, collections, updated database data, etc.

As you can see, although we are back to the previous step, the objects in the List collection have changed

3.7 Run to the cursor

Run to the cursor position

  • If there is a breakpoint before the cursor, run to that breakpoint
  • If the cursor is not on the run path, the program runs to the next breakpoint or ends

4 summarizes

Before not learning, only know to use line breakpoints for debugging, encountered an exception error, but also need to locate the error place, hit the uplink breakpoint, debugging run again

By learning the types and basic usage of breakpoints, you can set breakpoints for possible exceptions. In this way, when an exception occurs, the Debug interface will be automatically entered, which can save a lot of time and facilitate us to Debug the program more freely