"Go" 10 Tips for debugging Java programs with Eclipse

Source: Internet
Author: User

You should have seen some popular posts like "N Things about debugging". Suppose I spend 1 hours a day debugging my application, and that's a lot of time to build up. For this reason, use these times to value and understand all the features that make our debugging more convenient. It will save you some time and make your life more comfortable and relaxed. It also shows that other posts on this topic are valuable.

1th: Do not debug too much

A crazy statement about debugging as the opening. But it has to be said! Try slicing your complex logic into multiple separate units and writing unit tests to detect the correctness of your code. I imagine a process like this should happen very often----some people click through a large Web application, fill in multiple forms, switch to different pages, examine the computational logic on the last page, and implement most of the logic in this debug view. Always ask yourself before starting your tomcat: Is there a way to detect these behaviors using a unit test? You may not have known or forgotten about these times in the past, but from now on, we will focus on some of the debugging techniques of Eclipse, and you will find a lot of good things outside of good code design.

Zhou Yi
Translated over 2 years ago

3 Person Top

Top translation of good Oh!

-Breakpoints View: Conditional breakpoints

If you're only interested in a part of the app Interesting, this feature is very useful. For example, if you want to check the program during the 13th cycle, or debug some functionality in an abstract parent class, you are only interested in one of the specific implementations. You can set the conditions in the breakpoints view or the right-click menu ("Breakpoint Properties") labeled by the Blue breakpoint next to the code. You can pause the program when the conditional code fragment is true, or suspend the program when the value of the code fragment changes.


Enixyu
Translated 2 years ago

1 Human Top

 

top   Good translation!

other translations (1)

-variable view: Show logical Structure

If you need to view a map object in the variable view or list pair The default settings for Eclipse are generally not as easy. Assuming that you are using HashMap, you need to click through the individual entity entries and face various HashMap implementation details. However, there is a button called "Show Logical Structure" above the variable view. It is very useful, especially if the information displayed by the ToString () method of your object is not friendly. My boss showed me this feature a couple of weeks ago. You know, he often deals with PowerPoint or Excel. How humiliating for a developer like me.  

Does not turn on show Logical Structure  
 
turns on show Logical Structure  
& nbsp

Enixyu
Translated 2 years ago

2 human top

 

top   translation is good Oh!

-Variable View: Change the value of a variable ...

When you need to change the input information slightly, you do not need to restart the debugging session, just enter the new information in a table, you can directly modify the value of your variable in the debugging phase. There are times when you can save some time, you can simulate some weird situations through this function more simply.

-Display view

Do you know the display view? You can activate it through "window", "Show View", "Display" during debugging. Your eclipse should now be a new, blank view. You can use this view to enter or calculate some new code. The code is executed in the context of the current debug location, which means that you can use all variables or even content assistants. To execute your code, simply mark it and use the right-click menu or Ctrl+u (execute) or  ctrl+shift+i (check).

Enixyu
Translated 2 years ago

1 Human top

& nbsp

top   Good translation!

-navigation: Drop to Frame

I think anyone knows "step into", "Step Over", and may also know "Ste P return ". These are the basic navigation features when debugging. I'd like to mention two advanced methods of navigating that I really like. The first one is "Drop to Frame". With this feature you can fall back in time   you can directly fall back to a frame in a running Java stack frame. When I debug, I accidentally miss a line of attention, I often use this fallback one frame. Using the Drop to frame feature, I can simply rerun a frame of code.

 

-navigation: Step into Selection

The second one is "Step into Selection". It's also very simple, but a lot of people use one. To use it, you just press Ctrl+alt and click on the name of the method you want to run to. Very convenient, very fast. Compared to "step into" this is very convenient, think you like tracking into a function with a lot of parameters, if you use "Step into" you must enter, exit each parameter calculation process, you can enter into the function you really want to go. "Run To Line" is also a great feature. Just put the cursor in front of which line you want to run and press "Ctrl+r".
 

 

Ljb_iss
Translated 2 years ago

3 people top

 

top   translation is good!

-Navigator: Using the keyboard

If you avoid using a mouse, you will be more likely to operate faster. You should at least have the following shortcut:

  • f5– Step into
  • f6– step and Skip
  • f7– stepping and returning
  • f8– "continue to execute"
  • ctrl+shift+b– "Add Breakpoint"
  • ctrl+shift+i– "Check"

-Breakpoint View: Watchpoints

What happens if I change this variable? Sometimes creating a watchpoint is useful for debugging. The debugger stops, regardless of whether the observed field is modified or read-You can configure the decision yourself. You just double-click a field, and you can see the watchpoint in the breakpoint view, and you can edit its properties. You can even have a number of visits, which means that the debugger stops when the number of times the variable is accessed reaches that amount. This is also true for normal breakpoints.


Enixyu
Translated 2 years ago

2< /em> person top

 

top   translation is good!

-friendly and readable objects

A variable view is a value that uses the ToString method of the object to realistically correspond. For this reason, it can be very useful for debugging if you provide a friendly ToString method implementation. The default ToString implementation of Java.lang.Object in Javadoc is also recommended:

Returns a String that represents the object. Usually the <code>toString</code> method returns such a string, "literally" the object. The return result must be concise but rich in information that can represent the object, and is friendly to read. We recommend that all subclasses override this method.

You can refer to the Tostringbuilder in Commons-lang. It provides functionality to write a "good and consistent" ToString method (referenced from Javadoc).

The default ToString


The default Tostringbuilder example

Tostringbuilder Example – Multi-line text style

If you cannot modify the ToString implementation, for example if you are using a framework or an external API, you can have another option to create a "Detail Formatter" in Eclipse. You need to right-click an object in the variables view and click on "New Detail Formatter ...". Then you can enter some code to show the object.

"Go" 10 Tips for debugging Java programs with 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.