1. What is Debug?

If you want to see a line of data without the debug function, add system.out. println to the next line of data and run the program to print out the data. If you want to see another line of data, add system.out. println to the next line. So the code would look something like this…

publicUser getMaleUser{ List<User> userList = userDao.getUserList; System.out.println(userList); // Print userList to see what the contents of userList look like. // Retrieve the boy from userList, and return User resultUser; For (User User: userList) {if(user.getgender == "male ") {resultUser = User; Println (resultUser);}} resultUser (system.out.println); returnresultUser; }Copy the code

As you can imagine, this is very inefficient. Every time you look at more data, you have to add a line of system.out.println, and after each change, you have to rerun the program to print the data again.

In fact, when we run the code, we can use Debug mode to reflect the value of the data in real time!

2. The Debug mode

Benefits of using Debug mode to run code

  • You can go through code step by step.
  • You can know the value of a variable in real time, or even change the value of a variable, so you don’t have to use system.out.println to print code variables.

In Debug mode, there are several important function panels as follows:

  1. Enable Debug mode: Nowadays, Debug mode is used to Run programs when writing code. Run mode is rarely used.
  2. Setting breakpoints: Click on the left to set breakpoints, and click again to cancel the breakpoints. Setting breakpoints is the biggest difference between Debug mode and Run mode, and setting breakpoints can help us to check the code step by step.
  3. Debug buttons: These buttons are used to Debug code.
  4. Service button: Related to the Debug breakpoint, mainly used with the 3. Adjustment button.
  5. Variables area: You can view variables before the current breakpoint.

3. Debug Basic usage

The basic usage of Debug mode mainly corresponds to the 3. Debug button and 4. Service button These two sets of buttons.

Debug button: eight buttons from left to right

  1. Show Execution Point: If your cursor is on another line or page, click this button to jump to where the current code is executing.
  2. Step Over: a line of code that goes down and does not enter a method if there is a method on that line.
  3. Step Into: If the current line has a method, can enter the method inside, usually used to enter a custom method, not Into the official class library method.
  4. Force Step Into a method, can enter any method, view the underlying source code can be used to enter the official library method.
  5. Step Out: To exit a method from the incoming method to the method call when the method has completed execution but has not completed assignment.
  6. Drop Frame: Rollback breakpoint, rarely used.
  7. Run to Cursor: Allows you to move the Cursor to the row you want to view. Using this function, the code will Run to the Cursor row without the need for a break point (if you have entered Debug mode, that is, you have stopped at a breakpoint).
  8. Evaluate Expression: Evaluate Expression.

Service button, 7 buttons from top to bottom

  1. Rerun: To restart the program after shutting down the service, but it is rarely used and usually just shut down and start again.
  2. Update ‘tech’ application: an Update program that executes the Update option that was originally defined. When the Debug mode is enabled, the Debug button will be displayed again.
  3. Resume Program: To continue the Program, for example, there are two breakpoints at line 20 and 25, but it is currently running at line 20. If you press one, it will run to the next breakpoint (line 25), and if you press another, it will run the entire process because there are no more breakpoints.
  4. -Leonard: Pause Program is not important.
  5. Stop: Press to close the program twice. Sometimes you will find that the port is occupied when the service is restarted because the service is not completely shut down.
  6. View Breakpoints: View all Breakpoints.
  7. Mute Breakpoints : Deactivate Breakpoints by turning all Breakpoints gray. Press Resume Program (the third key) to run the Program (because all Breakpoints are disabled). Press Mute again to turn all invalid Breakpoints red.

4. View variables in Debug mode

During debugging, it is necessary to keep track of changes to a variable. There are several ways to view the value of the current variable.

  1. The value of the current variable is displayed after the argument line.
  2. When the cursor stops on the parameter, the current variable information will be displayed. Click open to see the details of the variable.
  3. Look in the Variables area below, where all the Variables of the current method are displayed, and you can see the details of all the Variables at once.

5. Evaluate Expression

The Evaluation Expression button was mentioned earlier. You can use this button to evaluate an Expression during debugging or to change the value of a variable without having to redo the code to manually change the value and restart the program.

If in Debug mode you want to quickly compare the results of the current list.get(0).equals(“first”), you can use this expression to quickly calculate the value returned by the function without changing the code. The result is to run list.get(0).equals(“first”) using the evaluated expression.

Or if you want to change the value of a variable, you can change it by evaluating the expression, as in this example. Normal code would only have the first and second strings in the list. But because we called list.add(“third”) in the evaluated expression to insert a new string into the list, the Variables section below shows that the list contains three strings first, second, and third.

If you just want to change the value of a variable, there’s another way that doesn’t involve evaluating the expression. You can also change the value of a variable using setValue by right clicking on it in the Variables area.

6. Set breakpoint conditions

Sometimes in the traverse a collection or array, may just want to see on the surface of the for loop one more than how much I value variable situation, this time can be reached by a breakpoint condition, that is, only when the breakpoint conditions, will stop at the breakpoints, or it will ignore this breakpoint directly.

Right click on a breakpoint to set the breakpoint condition, as shown in the image below, until I =3. (If the breakpoint condition is not set, the breakpoint will be stopped once for each “for loop”.)

7. View all breakpoints

Click “View Breakpoints” in the lower left to View all Breakpoints that have been set. Sometimes you can easily forget where you set Breakpoints. You can use this function to know where you set Breakpoints.

8. Set an exception breakpoint

When an exception breakpoint is set, the system automatically locates the exception line when an exception to be intercepted occurs in the program

To set it up, click View BreakPoints and then click the + sign to add a NullPointerException.

When a null pointer exception occurs, it will automatically jump to the number of lines that throw a null pointer exception, saving us to locate the problem point, very convenient.

9. To summarize

Note: You should never debug in the official environment, only in the test environment, because debugging will stop the entire program at the breakpoint.