Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of debugging skills of Intellij IDEA

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you the example analysis of Intellij IDEA debugging skills, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Preface

In addition to writing code, programmers spend a large part of their time looking up BUG and solving problems. To find BUG, you can't do without debugging code in IDE. Skillfully mastering debugging skills can help us reduce search time and quickly locate problems.

Debugging code in IDEA is very convenient, as long as you turn on debugging, use breakpoints at the appropriate code, and then debug accordingly. It is precisely because as long as you master these functions, you can debug the code, so for a long time, do not understand other debugging skills. In the follow-up in-depth use of IDEA process, continue to learn other IDEA debugging skills, to a great extent speed up debugging.

Step series button

In the process of daily debugging, in order to find the bug of the program, we usually need to debug step by step, follow the process of the program step by step, and find the final reason according to the value of the variable.

A series of Step function buttons are available in IDEA for step debugging, and all Step functions can be found in the Run menu bar.

It can also be found in the debugging window, but only some of the common buttons are displayed here.

During debugging, Setp Over is probably the most frequently used. Setp Over can step through the code, but when it encounters a method execution, it doesn't go inside the method, calls the method directly, and then goes to the next line.

If you need to get into the method, you need to use Step Into. Step Into can enter methods that are called, but by default it does not enter methods of classes such as java.*. If you need to enter, you can set it in Setting-Stepping and exclude it.

You can also use Force Step Into to force entry.

When you come across a line of code that calls multiple methods, it hurts to use Step Into. Step Into enters the called method in the order in which it is called, but often we only want to enter one of the methods.

In this scenario, we can use Smart Step Into and choose the specified method to enter.

In some cases, after entering a method, you can use Setp Out, execute the method quickly, and then jump back to the call.

In addition, after entering the for/while loop, if you do not break the point outside the loop code, you need to end with the bad code execution step by step, which is particularly troublesome. Here you can use Setp Out Of Code Block to quickly jump out of the loop block of code.

Techniques for using breakpoints

Temporary breakpoint

In general, when debugging, we use breakpoints on the code line, then run the test, and when the program encounters a breakpoint pause, we look at the relevant variable contents.

In IDEA, the default breakpoint will always exist. Sometimes we just want to pause for the first time and check temporarily, at which time we can use temporary breakpoints. Use the shortcut key Ctel + Alt + Shift + F8 to quickly create a temporary breakpoint or hold down ALt before creating a breakpoint.

Conditional breakpoint

Sometimes when we loop through the data, we may only care about the data of a certain condition, in which case we use conditional breakpoints.

Right-click the breakpoint, pop up the window to set the breakpoint properties, and we can set the condition at condition. IDEA will not pause the program until this condition is met.

In addition, the conditional breakpoint style set will also be special.

.

Multithreaded breakpoint

When debugging a multithreaded project, if you hit a breakpoint at multiple thread code, you will find that you can only debug serially when debugging.

For the above multithreaded program, after two threads are started, the program may be paused at thread1 or paused at thread2. As long as either breakpoint is paused, the thread at the other breakpoint is blocked. At this time, you can only finish debugging the breakpoint of the thread at the pause before you can jump to the second breakpoint, which is very inconvenient for debugging.

We can change this feature by setting the breakpoint thread property.

We can use the right mouse button to pop up the breakpoint setting box on the breakpoint and select the Thread option in Suspend. Restart the above program, and then you can see the thread1,thread2 two threads in the debug window Frames. Two of these threads can enter debugging, where program debugging is located at thread1. Select thread2 in the selection box at this time, and you can debug thread2.

In addition, the multithreaded property of the breakpoint can be set to the default property. Click the Make Default button to set the default Suspend property.

Roll back the debugger

When debugging, sometimes you get into the wrong method or miss a method call process, and you often have to start debugging again from scratch. From a new start debugging, may also need to set relevant parameters, modify the state of the database, etc., very troublesome. So if you can go back during debugging, you don't have to start over.

You can use Drop Frame and Force Return to roll back the debugger in IDEA.

Drop Frame

Drop Frame literally means to drop the stack. Stack frame is used for method call data structure in JVM. Each method call corresponds to the process of stack frame from stack to unstack in the virtual machine. After the method is called, the method stack frame will be at the top of the stack.

Execute Drop Frame, discard the top stack frame, and the program will return to the calling method.

Debugging the above program, at the breakpoint above, using Drop Frame, you can jump back to the method call, that is, the third line. Look at the output window and find that there is no output.

Drop Frame can be found in the debug window bar.

Note that unlike Step Out, execute Step Out, and the method will be returned to the call after execution.

Force Return

Force Return forces a return, which can be forced to end the program when the program is halfway through execution. You can find it at Frames.

Click Force Return to set the return value.

The above is all the contents of the article "sample Analysis of Intellij IDEA debugging skills". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report