Getting started with debugging
- Eclipse: Oxygen
- Java: 1.8
The debugging is used in java to rectify the code by setting breakpoints in the code. By setting breakpoints we can verify the variables and methods current state.
Here the concept of perspective is also stated.
Debug Perspective
The Debug perspective offers additional views that can be used to troubleshoot an application such as breakpoints, variables, debugging, console, etc. When a Java program is started in debug mode, users are prompted to switch to the debug perspective.
View of Debug: Displays the call stack and provides operations on that.
View of breakpoints: Displays all breakpoints.
View of Variables: Displays the declared variables and their values.
View of Console: Display the program output.
Debugging, runtime variable value update
The concept of debugging is further explained by an example here. The debugging can modify the values at runtime and used to avoid exceptions at runtime.
Breakpoints
A breakpoint is a signal that indicates the debugger temporarily suspends the execution of the program at a certain point.
To define a breakpoint in code follow these steps
- Right-click on the left margin in the Java editor
- Select Change Breakpoint
- On the other hand, we can double click on this position
Variables
Variables display the current value of the variable. The user can modify the value using these steps-
- Navigate Window
- Open Perspective
- Go to Debug.
- Click on Tab Variables.
- Right-click the variable for which you want to change the value and click on Change Value. Set the Value and click Ok
Debugging Continues
The menu bar tools for debugging are explained here. It includes various tools such as the cancel all the breakpoints, executes a single step, etc. these tools can help us to execute the program better.
Showing a menu item or toolbar button
To display a menu item or toolbar button, you can follow the same instructions as hiding one, except that you select the check box instead of disabling it.
If an item that you want to make visible is grayed out, this is because the group of contributing commands is not available. You must make it available before you can show or hide the items with which you contribute. You can do this by moving the cursor over the element in the tooltip that appears.
The following table demonstrates the description of the menu bar
Menu Bar Tool |
Description |
New | The new icon is used to create a new project or file. |
Open a Terminal | This icon is used to open a different type of terminal. |
Open a Terminal | This icon is used to open a different type of terminal. |
Open a Terminal | This icon is used to open a different type of terminal. |
Skip all Breakpoints | It helps us to skip all breakpoints. |
Resume | Resume icon helps us to resume our application. |
Suspend | This icon is equivalent to pausing the application. |
Terminate | This icon helps us to terminate the application straight forward. |
Disconnect | This helps us to disconnect the remote application of our program. |
Step Onto or Step Over | Step onto icon enter call method, step over will proceed the statement by one statement. |
Step Return | Step return is used to revert the execution of the statement. |
Drop to frame | It will use to re-execute the frame. Mostly the frame is a method. |
Use Step Filter | Step filters are commonly used to filter the types you do not want to see or step through the debugging.
For example, if you do not want to see or run anything from the java.lang.Object class, you must add this to the list of filtered types. |
Toggle Breadcrumb | The Toggle Breadcrumb allows you to invoke actions on any element shown in the navigation path. Provide access to our project explorer. |
Toggle mark occurrences | Stand on a variable, method, or type in the file. All the instances in which the element is referenced within the file will be highlighted. To configure the occurrence mark settings: To toggle mark occurrences, click the Toggle Mark Occurrences button on the toolbar. |
Toggle Word Wrap | It will wrap the words and remove the bottom scroll bar. |
Toggle Block Selection Mode | This icon helps us to write “block” in the popup. |
Show Whitespace Character | This will simply highlight the whitespace character. |
Inspect and watch are used for debugging purposes. In watch, we can constantly keep notice of the value of the variable at different points of execution. It is used for knowing the value of a specific variable at a certain point.
In the following program, we have created a pattern. In order to understand the current execution and what exact value is going inside this variable i and j per iteration we add a breakpoint on System.out.println() this statement and debug our application.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class Main { public static void main(String[] args) { for(int i=0; i<10;i++){ for(int j=0;j<i;j++){ System.out.print("*"); } System.out.println(); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
* ** *** **** ***** ****** ******* ******** ********* |
Contributed by: Poonam Tomar