Docsity

Debugging: A Comprehensive Guide to Finding and Fixing Errors in Java Code, Exams of Computer Science

Toggle sidebar
bg1
1
1
Debugging
2
Debugging
• Despite the best efforts at design,
development, and testing, your code will
still likely contain errors.
• Programming is one of the most difficult
things people do!
• When you have errors, how do you find
them?
– Use System.out.println (slow, painful way)
– Use a debugger (much better way)
3
How to Debug
• Right-click on the java file with the main
function
• Select Debug as…→Java Application
• You may be asked to switch to the
Debug Perspective. Just click on yes.
4
Breakpoints
• If you follow the steps from the previous
slide, you probably will just run through
the entire program and not stop
• You need to put a breakpoint in your
code
• Breakpoint: A place in your code where
you would like to pause while
debugging
Breakpoints
To place a breakpoint,
right click in the margin
on the line you want to
pause at. Select “toggle
breakpoint”. You should
see a blue circle.
To remove a breakpoint,
click on the blue circle
and select “Toggle
breakpoint” again.
6
Debugging
Key commands:
1. Step over: don’t go into the function call on
the line you are on
2. Step into: go into the function call on the
line you are on
3. Step return: go to the next line of the
calling function
4. Run to line: run to the line that the cursor is
on
5. Resume: run until you hit the next
breakpoint or your program ends
6. Terminate
pf3

Partial preview of the text

Download Debugging: A Comprehensive Guide to Finding and Fixing Errors in Java Code and more Exams Computer Science in PDF only on Docsity!

1

Debugging

2

Debugging

  • Despite the best efforts at design, development, and testing, your code will still likely contain errors.
  • Programming is one of the most difficult things people do!
  • When you have errors, how do you find them? - Use System.out.println (slow, painful way) - Use a debugger (much better way)

3

How to Debug

  • Right-click on the java file with the main function
  • Select Debug as…→ Java Application
  • You may be asked to switch to the Debug Perspective. Just click on yes.

4

Breakpoints

  • If you follow the steps from the previous slide, you probably will just run through the entire program and not stop
  • You need to put a breakpoint in your code
  • Breakpoint: A place in your code where you would like to pause while debugging

Breakpoints

To place a breakpoint, right click in the margin on the line you want to pause at. Select “toggle breakpoint”. You should see a blue circle.

To remove a breakpoint, click on the blue circle and select “Toggle breakpoint” again.

6

Debugging

Key commands:

1. Step over : don’t go into the function call on the line you are on 2. Step into : go into the function call on the line you are on 3. Step return : go to the next line of the calling function 4. Run to line : run to the line that the cursor is on 5. Resume : run until you hit the next breakpoint or your program ends 6. Terminate

7

Inspecting variables

  • In debug mode, you will see the following window at the top right

8

Inspecting Variables

  • In the Variables tab, you can inspect the values of variables
  • In the Breakpoints tab, you can see all the breakpoints you have active/inactive
  • In the Expressions tab, you can evaluate the results of certain expressions

9

Inspecting Variables

  • If you right click on the Expressions window, you can add any expression you would like to evaluate as the code is running
  • An expression can be a variable, a bunch of operations on variables, a function call, etc.
  • This is a very useful tool! (^10)

What if those tabs aren’t

there?

  • Go to Window → Show View (you may have to then select Other)
  • Under Debug, select Variables / Breakpoints / Expressions

11

The Call Stack

  • At the top left of your debug perspective, you’ll see a window with “Debug” as the title.
  • This is called the call stack window.

12

The Call Stack

  • These are the current “stack” of functions.
  • This example shows that you were in the BankTester.main() function which then called the Bank.find(int) function.