Java-note-debugging tips

Source: Internet
Author: User

10 tips for debugging Java programs using Eclipse! -10 Tips on Java Debugging with Eclipse

You should have read some popular posts such as "N things about debugging. If I spend one hour debugging my application every day, it will be a lot of time. For this reason, use these time points to focus on and understand all the features that make debugging easier. It will save you some time and make your life easier and easier. It also shows that other posts on this topic are also very valuable.

Article 6: Do not debug too much

A crazy statement about Debugging starts. But it must be said! Try to split your complicated logic into multiple independent units and write unit tests to check whether your code is correct. I think the following process should happen very frequently-some people click through a large web application, fill in multiple forms, switch to different pages, and are checking workers on the final page computing logic and most of the logic in this debugging view. You always ask yourself before starting your tomcat: is there a way to use a unit test to detect these behaviors? You may not know or forget this in the past, but from now on, we will focus on some eclipse debugging skills, you will find a lot of good things out of good code design.

Weekly
Translated five months ago

2Person-level

Top translation is good!

-Breakpoint view: Conditional breakpoint

This function is very useful if you are only interested in a part of the application. For example, if you want to check the program for 13th cycles or debug some functions in an abstract parent class, you only pay attention to one specific implementation. You can set conditions in the Breakpoint view, or by right-clicking the blue Breakpoint mark next to the Code ("Breakpoint Properties. You can pause the program when the condition code snippet is true or when the value of the code snippet changes.


Enixyu
Translated five months ago

1Person-level

Top translation is good!

Other translation versions (1)
-Variable view: displays the logical structure.

If you need to view the values of a Map object or List object in the variable view, the default setting for eclipse is generally not that easy. Suppose you are using HashMap. You need to click to traverse each object entry and face implementation details of various hashmaps. However, in the variable view, there is a button called "Show Logical Structure. It is very useful, especially when the information displayed by the toString () method of your object is not friendly. My boss showed me this feature a few weeks ago. You know, he often deals with PowerPoint or Excel. It's a shame for a developer like me.

"Show Logical Structure" is not enabled"

Enable "Show Logical Structure"

Enixyu
Translated five months ago

1Person-level

Top translation is good!

-Variable view: change the value...

When you need to slightly change the input information, you do not need to re-start the debugging session. You only need to enter new information in a table. You can directly modify the value of your variable in the debugging phase. When writing, you can save some time. You can use this function to simulate some weird situations more easily.

-Display view

Do you know "Display view? You can activate it through "Window"-> "Show View"-> "Display" during debugging. Now, your eclipse should be a blank new view. You can use this view to input or calculate some new code. The code is executed in the context of the current debugging location, which means that you can use all variables or even content assistants. To execute your code, just mark it and use the right-click menu or CTRL + U (execute) or CTRL + SHIFT + I (check ).

Enixyu
Translated five months ago

1Person-level

Top translation is good!

I think anyone knows "Step Into", "Step over", and "Step return". These are the basic navigation functions for debugging. I would like to provide two advanced navigation methods that I like very much. The first is "Drop to Frame". With this feature, you can roll back in time. You can roll back to a Frame in a running java stack Frame. When debugging, I accidentally missed a code line of interest, and often used this to roll back a frame. Using the "Drop to Frame" feature, I can simply re-run a Frame of code.

-Navigation: Step into Selection

The second is "Step into Selection". This is also very simple, but many people use it. To use it, you only need to press Ctrl + Alt and click a method name you want to run. Very convenient and fast. Compared with "Step Into", this is very convenient. You are tracking a function with many parameters. If you use "Step Into", you must enter and exit each parameter calculation process, to enter the function you actually want to enter. "Run to line" is also a great feature. You only need to place the cursor in front of the line you want to run, and then press "CTRL + R.


Ljb_iss
Translated five months ago

3Person-level

Top translation is good!

-Navigator: Use the keyboard

If you do not want to use the mouse, your operation will be faster. You should at least use the following shortcut keys:

  • F5-"single step in"
  • F6-"One-step execution and skip"
  • F7-"One-step execution and return"
  • F8-"continue execution"
  • Ctrl + Shift + B-"add breakpoint"
  • Ctrl + Shift + I-"check"

-Breakpoint view: Watchpoints

What will happen if I change this variable ?! Sometimes creating a watchpoint is very useful for debugging. The debugger stops, regardless of whether the field is modified or read. You can configure the field by yourself. You only need to double-click a field. Then you can view the watchpoint In the breakpoint view and edit its attributes. You can even access a variable. This means that when the number of times the variable is accessed reaches, the debugger stops. This applies to common breakpoints.


Enixyu
Translated five months ago

2Person-level

Top translation is good!

-Friendly readable object

The variable view uses the toString method of the object to calculate the actual value. For this reason, it is very useful for debugging to provide a friendly toString method. The default toString Implementation of java. lang. Object in javadoc is also recommended as follows:

Returns a string representing the object. Generally, the <code> toString </code> method returns such a string, which is "literally" the object. The returned result must be concise but rich in information that can represent the object and be readable. We recommend that all subclasses overwrite this method.

You can refer to ToStringBuilder in commons-lang. It provides some functions to compile (reference from javadoc) "good and consistent" toString method.

Default ToString


Default ToStringBuilder example

ToStringBuilder example-multiline text style

If you cannot modify toString implementation, for example, if you are using a framework or an external API, you can create a "Detail Formatter" in Eclipse ". You need to right-click an object in the variable view and click "New Detail Formatter ...". Then you can enter some code to display this object.

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.