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.
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.
Right Debug run, be careful not to Run, Run, then break it invalid.
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.
The blue bar represents the variable Variable has just been modified:
End of run, and the results :( In fact if there Bug, find the words in the process discovered)
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:
The program jumps talked about above Debug interface:
Along execute, get stuck IO, how to do it? Click Console, cut to the Console Interface:
Enter the new value, Enter:
this is not automatically cut back, you need to click on the Debugger below:
The program continues:
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):
Next:
Next:
We found that did not enter the function (), how to do it?
Of course, the function () is also marked with a breakpoint:
Then go:
Next:
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:
Recursive to the termination condition:
Upon arrival recursive termination conditions, step by step, began to pop up the stack to form the answer:
Output:
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!
Right select Debug Run:
will be prompted to choose to enter Debug mode, be sure to go in, point Switch:
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 tips all breakpoints, do not do not forget to "pull" to prevent back trouble ah!
Recursive backtracking when the left call stack attention:
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 :
Mouse over variables can directly see the value and condition variables:
Small Bug pattern the upper right corner, right click and select Close to exit the Debug style, returns to normal mode:
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 |