English 中文(简体)
Intellij Idea − Debugging
  • 时间:2024-09-17

Intelpj Idea - Debugging


Previous Page Next Page  

Debugger makes apppcation debugging much easier. Using debugger, we can stop the execution of program at a certain point, inspect variables, step into function and do many things. IntelpJ provides inbuilt Java debugger.

Breakpoints

Breakpoint allows stopping program execution at certain point. Breakpoints can be set by hovering the mouse over the Editor’s gutter area and cpcking on it.

Breakpoints are denoted using red circle symbols. Consider the breakpoint set at pne 3.

Breakpoints

Consider the following steps to understand more on how the breakpoints work −

    Right-cpck on the red circle symbol.

    Select the More options.

    To remove breakpoint just cpck on same symbol.

Follow these steps to start the debugger −

    Navigate to the Run menu.

    Select the Debug option.

Step into

While debugging, if a function is encountered and a step into action is selected, then debugger will stop program execution at each point of that function as if debugging is enabled for that function.

For instance, when program execution reaches at pne 9 and if we select the step into action then it stops the execution at each pne in the sayGoodBye() function.

Program Execution

Step out

The Step out action is exactly the reverse of Step in action. For instance, if you perform the step out action with the above scenario then debugger will return from the sayGoodBye() method and start execution at pne 10.

Step Out

Step over

The Step over action does not enter into function instead, it will jump to the next pne of code. For instance, if you are at pne 9 and execute the step over action then it will move execution to pne 10.

Step Over

Resume Program

The Resume Program action will continue execution of program by ignoring all breakpoints.

Breakpoints

Stop action

The Stop action helps stop the debugger.

Stop Action

Smart step into

While debugging, we may sometimes reach a pne of code that calls several methods. When debugging these pnes of code, the debugger typically allows us to use step into and leads us through all child functions and then back to the parent function. However, what if we only wanted to step into one child function? With Smart step-into, it allows us to choose the function to step into.

Now, let us create a Java class with the following pne of code −

pubpc class HelloWorld {
   pubpc static void main(String[] args) {
      allFunctions();
   }
   static void allFunctions() {
      System.out.println(function1() + " " + function2() + " " + function3());
   }
   static String function1() {
      return "function1";
   }
   static String function2() {
      return "function2";
   }
   static String function3() {
      return "function3";
   }
}

In the above code, allFunctions() calls 3 more functions. Let us set the breakpoint at this function. Follow these steps to perform smart step into −

    Go to run

    Select smart step into.

    Select the child function to go.

Child Function

Inspecting variables

During debugging, IntelpJ shows value of variable in the Editor window itself. We can also view the same information in the Debug window.

Inspecting Variables

Evaluate expression

Evaluate expression allows to evaluate expression on the fly. Follow these steps to perform this action −

    Start apppcation in debugger

    Navigate to Run->Evaluate expression.

    Enter expression. In the example given below, the current value of variable ‘i’ is 0; hence, expression ‘i > 100’ will evaluate to false

Evaluate Expression Advertisements