Debugging
Last modified Sunday, 07-Jul-2024 16:06:39 UTC.
- Semantic bugs
- The compiler findes syntactic errors in your code, and might even point to
some semantic errors (e.g., variables used only once).
- Semantic errors are those that lead to the code "doing the wrong thing", e.g., computing
an incorrect answer, executing the wrong branch of a conditional, looping infinitely,
etc.
- There are many ways to find semantic bugs, including
- Using System.out.print statements to look at the values of variables
during an execution.
- Using debugging tools within an IDE like IntelliJ.
- Adding assert statements.
- Using unit testing frameworks.
- Formal methods for software verification.
- Here we use IntelliJ's debugging tools.
- In IntelliJ ...
- Make sure the Preferences->Build,Execution, Deployment->Compiler->Java Compiler
is set to Generate debugging info
- Add breakpoints at lines where the program needs to be stopped.
- Place the cursor on the line, click in the gutter - a red dot appears.
- Run in debug mode - the little "bug" in the task bar.
Execution will suspend before executing lines with breakpoints.
- Use the debug tool window to get information about the data in the program.
- Look at variable values in the "Variables" window.
- Step through the program.
- Step over the line - execute the entire line as one step, including any
methods called.
- Step into the line - execute the line step-by-step, including any methods
called.
- Step out of the current method to the point after the call to the method.
- F9 - resume (== run to next breakpoint)
- Example: PizzaCost.java
-
Here is another introduction to the topic.
- Read
all about it,
particularly ...
Exercises
Exam Style Questions