Debugger basics in IntelliJ IDEA (Mala Gupta)
Aug 15, 2023
Debugger basics in IntelliJ IDEA (Mala Gupta)
IntelliJ IDEA provides a full range of capabilities for debugging your source code. 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. This screencast demonstrates some basic debugging features: - Start code execution in debug mode; - Pause, resume, restart or stop debugger; - Breakpoints; - Step actions; - Variable pane; - Modify code behavior without changing source For more information, go to: https://jb.gg/0geri9 Download IntelliJ IDEA: https://jb.gg/download-intellij-idea Top 15 IntelliJ IDEA shortcuts: • Top 15 IntelliJ IDEA shortcuts *Author: Mala Gupta Join us: Website https://jb.gg/website Blog https://jb.gg/blog Twitter https://twitter.com/intellijidea Facebook https://www.facebook.com/IntelliJIDEA/ #debugging #intelliJIDEA #getting_to_know_intellij #intelliJ #jetbrains #java #programming
Content
3.79 -> A debugger not only finds and fixes bugs in
your code, it also lets you analyze a piece
8.54 -> of code and understand how it works.
11.19 -> With a debugger, you can change the behavior
of your code without modifying the source
16.16 -> and do a lot of other interesting things.
18.66 -> Starting a debugger for a console application
is simple.
21.849 -> Click on the Run icon in the gutter area and
select the Debug option.
26.61 -> You can also start it from the Run menu, or
by using the short cut - Shift F9.
31.91 -> To add Virtual Machine options or to pass
arguments to the program, you can edit the
38.07 -> configuration.
39.89 -> When your application seems to be unresponsive,
you can pause the program to analyze where
47.86 -> your code is stuck.
49.3 -> Start your application in debug mode and click
on 'Pause Program' when it seems to become
54.17 -> unresponsive.
55.95 -> In this case, the code is blocked for user
input.
59.64 -> You can also view the call stack.
61.59 -> By clicking on the call stack here, I can
view the code in execution.
68.56 -> You can resume the program execution, by clicking
on 'Resume Program' or by using the shortcut
74.4 -> F9.
75.4 -> To restart the program in debug mode, select
'Rerun'.
79.4 -> At anytime, you can stop debugging your program,
by using the 'Stop' icon.
86.74 -> Did you notice that I did not set any breakpoints
in this case?
90.81 -> A breakpoint stops execution of your program
in the debug mode so that you can analyze
95.49 -> your code.
96.49 -> To set a breakpoint, click in the gutter area
or use the shortcut Ctrl F8 or command F8.
102.78 -> If you don't want to stop execution whenever
this line of code executes, you can define
107.1 -> a condition.
108.33 -> For example, let me stop execution at this
breakpoint when the field y of reference variable
114.5 -> p is equal to 30.
116.65 -> You can also drag-drop this icon to move the
breakpoint.
121.2 -> To delete a breakpoint, click on this icon
again.
124.8 -> But if you have defined conditions or other
parameters, you might prefer to disable a
129.509 -> breakpoint, rather than deleting it.
132.67 -> A tick implies there is information for this
line of code.
136.579 -> Cross implies no information is available
on this breakpoint.
140.969 -> Let's see how the breakpoint condition works.
144.98 -> Start your program in debug mode.
146.999 -> In the variables section, you can see that
that this program was paused when the value
151.2 -> of field y for variable p is 30.
154.45 -> There's more to breakpoints.
156.59 -> Right-click on a breakpoint and click on more.
159.579 -> You can modify a breakpoint so that it doesn't
suspend the program execution and logs an
164.65 -> expression when it is hit.
166.68 -> Let us log the value of x and y fields of
class Point and rerun our code.
172.499 -> Now, the code execution doesn't stop at the
breakpoint - instead, it logs the expression
178.129 -> we defined to the console.
179.79 -> From the debug window, you can also mute breakpoints
and use this icon to view all the BreakPoints
185.699 -> in your program, and further modify what they
do.
191.62 -> Let's see the various step actions you can
use to move through your code to find the
198.499 -> bugs.
199.499 -> This is a simple code base, in which method
createCoOrdinateList() creates two instances
203.969 -> of class Point and adds them to an ArrayList.
207.829 -> Class Point looks simple too with two fields
x and y, and getter and setter methods.
214.75 -> Method OutputValues() outputs the list items
passed to the console.
220.139 -> This line of code creates a Point instance
and removeValue() method tries to remove it
225.4 -> from the list lineCoordinates.
228.04 -> Let's run the code to see if we get the expected
result.
231.62 -> The output shows that even though a Point
with 'x' and 'y' values '13' and '30' was
236.229 -> added to the list when an instance with identical
value was created to remove it, it was not
242.519 -> successful.
244.159 -> Let's debug the code.
245.56 -> Set a breakpoint and start the application
in the debug mode.
250.049 -> By using 'Step Over'92 or F8, you can go to
the next line of code in a method.
255.359 -> As you step through the code, you'll see the
value of the variables - next to the code
260.03 -> in the editor window.
261.959 -> These values are also visible in the variables
pane in the Debug window.
266.85 -> You can view more details on the variables
here.
270.37 -> 'Step Into' or F7 will take you to the first
line of code in method outputValues(), which
276.91 -> is to this line.
278.99 -> Let's step over the code in this method.
281.46 -> 'Step Into' can also take you to the execution
of a method in another class.
286.66 -> Using 'Step Into' on this line of code will
take you to the constructor of class Point.
292.18 -> Notice how the inline variable value change
as you step over the code.
296.56 -> To debug the remove method that is defined
in the Java API, use 'Force Step Into'.
303.37 -> Lets 'Step Over' and check.
306.16 -> The first array element and the Point we want
to remove is a mismatch.
312.39 -> But the second array value should not be a
mismatch.
314.95 -> Oh, I moved forward to this line, when I should
have stepped into the equals method.
320.42 -> No worries.
321.42 -> I can drop a frame from the call stack and
return to the calling method.
325.89 -> Now, let's go back to the previous code and
examine the equals method and see what does
331.38 -> it does.
332.53 -> Aah. found the bug.
334.54 -> The equals method in the Object class returns
true if the references match.
340.68 -> If you are switching through classes and reading
through code and miss where the code was executing,
345.06 -> you can click on 'Show Execution Point'.
348.65 -> To skip executing code line by line, you can
move forward to a line and click on 'Run To
354.22 -> cursor'.
358.96 -> Let's fix this bug by overriding the equals()
method for class Point.
363.65 -> Now, let's rerun the code and check if the
code is working as expected.
371.58 -> Start the application and view the result.
376.95 -> This looks good now.
378.16 -> Se we managed to find a bug and fix it too.
383.16 -> Though the inline debugger is very helpful,
the variables pane shows a lot more details.
389.43 -> In this example, since we didn't override
the toString method for class Point, the editor
394.05 -> window shows the class name and the debugger
object id, which doesn't seem to be very helpful.
399.96 -> The debugger pane shows all fields of a variable
- including the private fields.
405.59 -> Clicking on stacks will show the variables
which are relevant to that stack.
411.84 -> You can right click on a variable and select
'Jump to source' to view where they were declared
417.139 -> - to understand your code better.
419.88 -> By selecting the option 'Jump to type source',
you can also view the definition of non-primitive
425.71 -> variables.
427.46 -> In a call statck, you might want to evaluate
an expression to verify your assumptions.
432.78 -> For example, I can evaluate the value of variable
'this', or other valid expressions like this
439.17 -> is double equal to an instance of class Point,
or this is .equals() to an instance of class
445.28 -> Point.
450.02 -> You can create a variable, whose value is
accessible in all the call stacks by adding
455.18 -> a new watch.
456.3 -> Say, System.getProperty, the name of your
OS.
462.17 -> You can create watches to view the value of
certain variables in all the call stacks.
467.09 -> There are multiple ways to do that.
470.3 -> You can right-click in the code in the editor
and select 'Add to watches'.
476.84 -> In the variable pane, you can also click on
variable and drag and drop it to the watches
482.98 -> pane.
484.02 -> But the values of these variables might not
be available in all the call stacks.
489.36 -> It will really depend on the scope of the
variable.
493.28 -> Did you know you can change the behavior of
your code without changing its source?
497.12 -> In this code execution, the 'x' and 'y' field
or Person instances being compared are equal,
502.83 -> and this equals() method is about to return
true.
505.95 -> I can change the value of a variable by right
clicking it in the variable pane and selecting
510.74 -> 'Set Value...'.
513.05 -> When I do that the behavior of the code changes.
515.87 -> With the modified value, the equals() method
returns false and this value will not be removed
522.769 -> from the ArrayList.
524.94 -> A debugger is a powerful and versatile tool
that executes a program in a controlled environment.
533.029 -> With a debugger, you can see the inner state
of an application, find bugs, understand code,
537.829 -> and do many other things.
541.17 -> Stay tuned for the next screencast on Debugger
in which I'll cover the advanced features
545.73 -> like Remote Debugging, Breakpoint types, running
tests until they fail and much more.
551.37 -> Thanks for watching.
Source: https://www.youtube.com/watch?v=lAWnIP1S6UA