What is the difference between Step Into and Step Over in a debugger

Debugging

Debugging Problem Overview


I want to debug the whole flow of a (Java) program. I see there are several options for stepping through my program. What is the difference between step into and step over?

Debugging Solutions


Solution 1 - Debugging

Consider the following code with your current instruction pointer (the line that will be executed next, indicated by ->) at the f(x) line in g(), having been called by the g(2) line in main():

public class testprog {
    static void f (int x) {
        System.out.println ("num is " + (x+0)); // <- STEP INTO
    }

    static void g (int x) {
->      f(x); //
        f(1); // <----------------------------------- STEP OVER
    }

    public static void main (String args[]) {
        g(2);
        g(3); // <----------------------------------- STEP OUT OF
    }
}

If you were to step into at that point, you will move to the println() line in f(), stepping into the function call.

If you were to step over at that point, you will move to the f(1) line in g(), stepping over the function call.

Another useful feature of debuggers is the step out of or step return. In that case, a step return will basically run you through the current function until you go back up one level. In other words, it will step through f(x) and f(1), then back out to the calling function to end up at g(3) in main().

Solution 2 - Debugging

When debugging lines of code, here are the usual scenarios:

Step Into

A method is about to be invoked, and you want to debug into the code of that method, so the next step is to go into that method and continue debugging step-by-step.

Step Over

A method is about to be invoked, but you're not interested in debugging this particular invocation, so you want the debugger to execute that method completely as one entire step.

Step Return

You're done debugging this method step-by-step, and you just want the debugger to run the entire method until it returns as one entire step.

Resume

You want the debugger to resume "normal" execution instead of step-by-step

Line Breakpoint

You don't care how it got there, but if execution reaches a particular line of code, you want the debugger to temporarily pause execution there so you can decide what to do.

Eclipse has other advanced debugging features, but these are the basic fundamentals.

See also

Solution 3 - Debugging

step into will dig into method calls
step over will just execute the line and go to the next one

Solution 4 - Debugging

Method of Communicating with a Debugger

(Or, how I explain my road trip to my grandmother)

Step Into: "When the next statement to execute reaches a method call, dont execute the method as a whole, but rather, execute the first line of that method and stop"

Step Over: "When the next statement to execute reaches a method call, execute the method as a whole and stop"

Step Out: "Finish off executing the callee's code and stop when execution returns to the caller"

Continue: "Execute up until the next breakpoint"

Here is a great example to practically demonstrate the concepts above:

enter image description here

Solution 5 - Debugging

You can't go through the details of the method by using the step over. If you want to skip the current line, you can use step over, then you only need to press the F6 for only once to move to the next line. And if you think there's someting wrong within the method, use F5 to examine the details.

Solution 6 - Debugging

Step Into The next expression on the currently-selected line to be executed is invoked, and execution suspends at the next executable line in the method that is invoked.

Step Over The currently-selected line is executed and suspends on the next executable line.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJavaUserView Question on Stackoverflow
Solution 1 - DebuggingpaxdiabloView Answer on Stackoverflow
Solution 2 - DebuggingpolygenelubricantsView Answer on Stackoverflow
Solution 3 - DebuggingJean-Bernard PellerinView Answer on Stackoverflow
Solution 4 - DebuggingDean PView Answer on Stackoverflow
Solution 5 - DebuggingwananaView Answer on Stackoverflow
Solution 6 - DebuggingYogesh YadavView Answer on Stackoverflow