Object-Oriented Programming with Java, part I + II

cc

This material is licensed under the Creative Commons BY-NC-SA license, which means that you can use it and distribute it freely so long as you do not erase the names of the original authors. If you make changes in the material and want to distribute this altered version of the material, you have to license it with a similar free license. The use of the material for commercial use is prohibited without a separate agreement.

Authors: Arto Hellas, Matti Luukkainen
Translators to English: Emilia Hjelm, Alex H. Virtanen, Matti Luukkainen, Virpi Sumu, Birunthan Mohanathas, Etiënne Goossens
Extra material added by: Etiënne Goossens, Maurice Snoeren, Johan Talboom
Adapted for Informatica by: Ruud Hermans

The course is maintained by Technische Informatica Breda


Debugging using a debugger

To see the contents of variables so far, we’ve been using the System.out.println() command. This works fine for showing the contents of simple variables, or the flow of execution in a simple program, but when programs get more complicated, the output will also get more complicated. This is why there are other tools available to debug your program, a debugger.

A debugger can pause the execution of your program, so you can view the values of variables, and then allow you to run your code line by line to see the flow of execution.

So far we’ve been using the TMC plugin’s run buttons tmc to start our program. In order to run the program with debugging, we have to use the debug tmc button, or use the debug option in the run menu. Running the program with a debugger won’t change anything about the execution of your program, so you won’t see a difference by just pressing debug

breakpointTo pause the execution to inspect, a breakpoint can be added to the code. This is done by clicking in the gutter of your code window (just right of the line number). A red dot will appear to show the position of the breakpoint. To remove the breakpoint, just click the red dot to remove it. When running the code with a debugger and a breakpoint, the execution will pause the moment java starts executing that line. The line with the breakpoint will not have been executed yet.

When pausing execution, a debugging window will appear in the bottom of IntelliJ. This is a powerful view into your application, with a lot of buttons and features.

debugger

On the left side are buttons for

On the top are buttons for

The main view consists of 2 parts, on the left is the call stack (we’ll go more in depth into the callstack in chapter 2-9), and the variables windows. The variables windows is used to view the contents of variables. By default, all the ‘used’ variables are shown, so if your program uses a variable called months, this variable will automatically be shown here. It is also possible to add variables or expressions in the watch, by double-clicking and typing the name of the variable, or the expression

The process of debugging is usually used to see if the program is doing what you have in mind, validate the code execution path and check the variable contents to see if they are valid. This means a lot of manual stepping through the code, step by step, and takes a lot of time. This is why debugging can take a lot of time, and is better avoided by thinking before you start typing