Debugger basics in IntelliJ IDEA (Mala Gupta)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
A debugger not only finds and fixes bugs in your code, it also lets you analyze a piece of code and understand how it works. With a debugger, you can change the behavior of your code without modifying the source and do a lot of other interesting things. Starting a debugger for a console application is simple. Click on the Run icon in the gutter area and select the Debug option. You can also start it from the Run menu, or by using the short cut - Shift F9. To add Virtual Machine options or to pass arguments to the program, you can edit the configuration. When your application seems to be unresponsive, you can pause the program to analyze where your code is stuck. Start your application in debug mode and click on 'Pause Program' when it seems to become unresponsive. In this case, the code is blocked for user input. You can also view the call stack. By clicking on the call stack here, I can view the code in execution. You can resume the program execution, by clicking on 'Resume Program' or by using the shortcut F9. To restart the program in debug mode, select 'Rerun'. At anytime, you can stop debugging your program, by using the 'Stop' icon. Did you notice that I did not set any breakpoints in this case? A breakpoint stops execution of your program in the debug mode so that you can analyze your code. To set a breakpoint, click in the gutter area or use the shortcut Ctrl F8 or command F8. If you don't want to stop execution whenever this line of code executes, you can define a condition. For example, let me stop execution at this breakpoint when the field y of reference variable p is equal to 30. You can also drag-drop this icon to move the breakpoint. To delete a breakpoint, click on this icon again. But if you have defined conditions or other parameters, you might prefer to disable a breakpoint, rather than deleting it. A tick implies there is information for this line of code. Cross implies no information is available on this breakpoint. Let's see how the breakpoint condition works. Start your program in debug mode. In the variables section, you can see that that this program was paused when the value of field y for variable p is 30. There's more to breakpoints. Right-click on a breakpoint and click on more. You can modify a breakpoint so that it doesn't suspend the program execution and logs an expression when it is hit. Let us log the value of x and y fields of class Point and rerun our code. Now, the code execution doesn't stop at the breakpoint - instead, it logs the expression we defined to the console. From the debug window, you can also mute breakpoints and use this icon to view all the BreakPoints in your program, and further modify what they do. Let's see the various step actions you can use to move through your code to find the bugs. This is a simple code base, in which method createCoOrdinateList() creates two instances of class Point and adds them to an ArrayList. Class Point looks simple too with two fields x and y, and getter and setter methods. Method OutputValues() outputs the list items passed to the console. This line of code creates a Point instance and removeValue() method tries to remove it from the list lineCoordinates. Let's run the code to see if we get the expected result. The output shows that even though a Point with 'x' and 'y' values '13' and '30' was added to the list when an instance with identical value was created to remove it, it was not successful. Let's debug the code. Set a breakpoint and start the application in the debug mode. By using 'Step Over'92 or F8, you can go to the next line of code in a method. As you step through the code, you'll see the value of the variables - next to the code in the editor window. These values are also visible in the variables pane in the Debug window. You can view more details on the variables here. 'Step Into' or F7 will take you to the first line of code in method outputValues(), which is to this line. Let's step over the code in this method. 'Step Into' can also take you to the execution of a method in another class. Using 'Step Into' on this line of code will take you to the constructor of class Point. Notice how the inline variable value change as you step over the code. To debug the remove method that is defined in the Java API, use 'Force Step Into'. Lets 'Step Over' and check. The first array element and the Point we want to remove is a mismatch. But the second array value should not be a mismatch. Oh, I moved forward to this line, when I should have stepped into the equals method. No worries. I can drop a frame from the call stack and return to the calling method. Now, let's go back to the previous code and examine the equals method and see what does it does. Aah. found the bug. The equals method in the Object class returns true if the references match. If you are switching through classes and reading through code and miss where the code was executing, you can click on 'Show Execution Point'. To skip executing code line by line, you can move forward to a line and click on 'Run To cursor'. Let's fix this bug by overriding the equals() method for class Point. Now, let's rerun the code and check if the code is working as expected. Start the application and view the result. This looks good now. Se we managed to find a bug and fix it too. Though the inline debugger is very helpful, the variables pane shows a lot more details. In this example, since we didn't override the toString method for class Point, the editor window shows the class name and the debugger object id, which doesn't seem to be very helpful. The debugger pane shows all fields of a variable - including the private fields. Clicking on stacks will show the variables which are relevant to that stack. You can right click on a variable and select 'Jump to source' to view where they were declared - to understand your code better. By selecting the option 'Jump to type source', you can also view the definition of non-primitive variables. In a call statck, you might want to evaluate an expression to verify your assumptions. For example, I can evaluate the value of variable 'this', or other valid expressions like this is double equal to an instance of class Point, or this is .equals() to an instance of class Point. You can create a variable, whose value is accessible in all the call stacks by adding a new watch. Say, System.getProperty, the name of your OS. You can create watches to view the value of certain variables in all the call stacks. There are multiple ways to do that. You can right-click in the code in the editor and select 'Add to watches'. In the variable pane, you can also click on variable and drag and drop it to the watches pane. But the values of these variables might not be available in all the call stacks. It will really depend on the scope of the variable. Did you know you can change the behavior of your code without changing its source? In this code execution, the 'x' and 'y' field or Person instances being compared are equal, and this equals() method is about to return true. I can change the value of a variable by right clicking it in the variable pane and selecting 'Set Value...'. When I do that the behavior of the code changes. With the modified value, the equals() method returns false and this value will not be removed from the ArrayList. A debugger is a powerful and versatile tool that executes a program in a controlled environment. With a debugger, you can see the inner state of an application, find bugs, understand code, and do many other things. Stay tuned for the next screencast on Debugger in which I'll cover the advanced features like Remote Debugging, Breakpoint types, running tests until they fail and much more. Thanks for watching.
Info
Channel: IntelliJ IDEA, a JetBrains IDE
Views: 229,691
Rating: undefined out of 5
Keywords: debugging java, java, debugging in intellij, breakpoints, intellij java, intellij idea tutorial, intellij idea
Id: lAWnIP1S6UA
Channel Id: undefined
Length: 9min 33sec (573 seconds)
Published: Wed May 13 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.