Debug take you into the world (based on Eclipse, IntelliJ IDEA)

Debug and on paper

Debug, meaning "debug", is one of the essential skills of a programmer.
Debug to write the text has been for a long time, because I just will not learn Java Debug, the school does not say (we call that class of object-oriented programming foundation, not a Java development technologies Han), there is no good talking about Debug, He said they could learn ah own or use System.out.println () ah, but I have never found able to understand things. Until then, do a small project, I met a lot of NullPointerException, had to use her own Debug how, by the way consulted about the teachers and friends, it basically learned to use Debug.
When Debug is still not used to start with, but slowly with a long, very incense.
I do, not quite want to write this article is very complicated, but I hope to give beginners a guide bar.

For some "big brother", your technology is certainly acceptable, but life need not be too harsh, thanks!

Debug of three means

  • Breakpoint (breakpoint)
    • After the marked breakpoint, the program runs to the breakpoint will pause, you can observe the operation step by step.
  • Trace (trace)
    • Follow the process step by step, take a look at the flow of execution of the program code.
    • Follow the process step by step, take a look at the dynamic changes in the variables.
  • Monitoring (watch)
    • Instant Watch: mouse pointer variable.
    • Quick Watch: right click, Inspector.
    • Add Watch: right click, Watch.

IntelliJ IDEA

The simplest procedure

With the first look at a simple program Debug process of IDEA.

We chose a program 1 + 2 + ... + 10, we pass this basic program to learn how to Debug.
IDEA Personally, I prefer progressive observation F8 to run, and then found the problem to track.
Here Insert Picture Description
In the left column of the space marked number of rows, tap, there will be a red dot, to bring out a red line, called breakpoints (IDEA breakpoint really nice and easy additions than Eclipse).

The purpose is to make the program break point Debug run time will stop here, we can observe the gradual change of variables, program flow statement is executed, and so on.

According to my observations, in fact, playing for beginners, need to test a portion of a method breakpoint is enough, because it will continue to keep pressing F8 line by line, I want to say that it will be easier to accept some.
Here Insert Picture Description

Right Debug run, be careful not to Run, Run, then break it invalid.
Here Insert Picture Description

When you start running, you can see that the program stopped at the line at break point, and there are a lot of gray kv Dui, Variable column below the value of the variables will appear, you can track the current value of each variable and value Variety.

Press F8 can continue.
Here Insert Picture Description

The blue bar represents the variable Variable has just been modified:
Here Insert Picture Description
Here Insert Picture Description

End of run, and the results :( In fact if there Bug, find the words in the process discovered)
Here Insert Picture Description

Continuous input

We may need to enter the command line a lot of data, sometimes we choose to copy the fall of the input Debug Console, but many times we enter a few values ​​and press Enter to automatically enter the Debug. Debug and run to where it is needed IO will be blocked, how to add new input it?

10 to enter, and then press Enter:
Here Insert Picture Description

The program jumps talked about above Debug interface:
Here Insert Picture Description

Along execute, get stuck IO, how to do it? Click Console, cut to the Console Interface:
Here Insert Picture Description

Enter the new value, Enter:
Here Insert Picture Description
this is not automatically cut back, you need to click on the Debugger below:
Here Insert Picture Description

The program continues:
Here Insert Picture Description

Console IO after identifying a cut Debugger, it is relatively easy approach is to put all of the first input into data.

Debug interrupt

Debug want to interrupt, and interrupt Run as a red box in the left column, you can tap the end.

Multi-method break point

The following code in addition to the method main () but also have more than one line is called, if we marked a breakpoint in for there can not enter the function () do?

public class PlusTest {

    private static int function(int num) {
        int sum = 0;
        for (int i = 1; i <= num; i++) {
            sum += i;
        }
        return sum;
    }

    public static void main(String[] args) {
        int sum = 0;
        int num = 10;
        for (int i = 1; i <= num; i++) {
            sum += function(i);
        }
        System.out.println(sum);
    }
    
}

From the beginning this time observing (the only one breakpoint):
Here Insert Picture Description
Next:
Here Insert Picture Description
Next:
Here Insert Picture Description

We found that did not enter the function (), how to do it?
Of course, the function () is also marked with a breakpoint:
Here Insert Picture Description

Then go:
Here Insert Picture Description

Next:
Here Insert Picture Description

Wow even, go! We can continue to observe the change of variables in the function () method in.

Debug recursive function

Recursive if there is a clear sense of backtracking, especially for binary tree recursive algorithm, if the break point will feel while advancing a while back.

To grasp the clear Where sub recursive process, saw the change of variables.

Here not take very complex recursive function that matter, the recursive function factorial to talk about it:

public class PlusTest {

    private static int function(int num) {
        if (num == 0 || num == 1) {
            return 1;
        }
        return num * function(num-1);
    }

    public static void main(String[] args) {
        int num = 10;
        System.out.println(function(num));
    }

}

Down decomposition problem, recursion:
Here Insert Picture Description

Recursive to the termination condition:
Here Insert Picture Description

Upon arrival recursive termination conditions, step by step, began to pop up the stack to form the answer:
Here Insert Picture Description

Output:
Here Insert Picture Description

Or the way, such as a binary tree, in a recursive when not careful may not know where to recursion, or to be more careful heart wow!

Eclipse

Eclipse ugly, I do not want to say, ha ha.

Or the recursive example above it, tell us about the Eclipse Debug function.

Note the small dot on the left, you see why I criticize Eclipse IDEA breakpoint compare it, really too low a ......
I am using Eclipse break points are generally marked with double-click the guarantee ......

Anyway, this is the left break it!

Here Insert Picture Description

Right select Debug Run:
Here Insert Picture Description
will be prompted to choose to enter Debug mode, be sure to go in, point Switch:
Here Insert Picture Description

Has left the call stack information, the right information table variables, breakpoints tables, etc., in the middle is the code, here is the Console:
Here Insert Picture Description

Here tips all breakpoints, do not do not forget to "pull" to prevent back trouble ah!
Here Insert Picture Description

Recursive backtracking when the left call stack attention:
Here Insert Picture Description

Press F8 F6 like IDEA, like you, line by line execution; and F5, then traced the call stack can find the source code, and even can be used to read the source code :

Here Insert Picture Description

Mouse over variables can directly see the value and condition variables:
Here Insert Picture Description

Small Bug pattern the upper right corner, right click and select Close to exit the Debug style, returns to normal mode:
Here Insert Picture Description

Debug of Eclipse and NetBeans compare

Functional Description Eclipse NetBeans
Statement by the Executive F5 F7
By execution F6 F8
Out of a function F7 Ctrl+F7
Run to Cursor Ctrl+R F4
Published 645 original articles · won praise 1334 · Views 570,000 +

Guess you like

Origin blog.csdn.net/weixin_43896318/article/details/104673363