How "Java" uses the debugger in eclipse

Source: Internet
Author: User

As long as there is a bit of programming based on the program error or can not get the results we want, we have to debug, but do not know how many people still like me, likes to use SYSTEM.OUT.PRINTLN () in some places, to print some variables to observe, C language like to use printf,c++ is cout, this method with more, never know how to use the debugger. In fact, the debugger in eclipse is simple and very easy to use. And, for the method of printing some variables, in some large programs, graphical programming interface, Web programming, you start to be ignorant. Why did the program go wrong and start thinking about how to print the variable? It's annoying.

In order to explain how to use the debugger in Eclipse to debug the program, deliberately designed such a very simple error program:

Class Debug {public void arrayindexoutofboundsexception () {int[] Testarr = new Int[10];for (int i = 0; i < Testarr.leng Th i++) {Testarr[i] = i;} for (int i = 0; i < testarr.length + 1; i++) {System.out.print (Testarr[i] + ",");}}} public class Javadebugtest {public static void main (String args[]) {new Debug (). ArrayIndexOutOfBoundsException ();}}

This code does not have any syntax errors, but it works as a result:


Almost everyone knows that this is an array out-of-bounds error, and when I print it, it would have been an array of only 10 units, but to read its 11th bit, it was obvious that something was wrong.

However, when we are programming, the program to be solved is not as simple as here, and if there is a mistake, it will also spit out a large number of unrelated anomalies, making it difficult to find the cause of the error.

Moreover, if you want to use the method of printing variables to detect errors, it is estimated that you have not come up with a half day.

At this point, we're going to use the debugger in eclipse.

The key is how to use it? Always see the Master in the PA caught the wrong reason, but only envy ...

First of all, in the running results to find the possible error, not the Java files you write, Java package do not tube, you introduce those jar package are written by others, how can be wrong, even if it is wrong, you write the program to get someone's jar error:


Then, click Debug to run the program:


After entering the following interface, which is the debugger in eclipse, we have to use, look, and use this debugger to change the program we want!

When you use debugging to run, your program will quickly execute to the first breakpoint you set, after, for example, please use good F6 and F7/f8, with the memory variable viewer in the upper right, and think about why you can not write a program well


Every little bit of your F6, variable variables in the memory variable viewer are highlighted:


In the process of debugging, please also note the output of the console below, for the loop body, please note that you are in the first several loops error, and for the condition body, please pay attention to the condition, that is, if the thing, and the current memory of the variables are consistent and so on all kinds of programming skills, debugging skills, it is needless to say I guess there are more big gods than me.

During debugging, you can change the code directly from the Code panel. Or wait for the entire program debugging finished, press the upper right corner of the Java cut back to the original code to write the interface to modify:



How "Java" uses the debugger in eclipse

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.