288

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?

T.Todua
  • 53,146
  • 19
  • 236
  • 237
JavaUser
  • 25,542
  • 46
  • 113
  • 139

7 Answers7

379

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().

T.Todua
  • 53,146
  • 19
  • 236
  • 237
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 1
    "then back out to the calling function to end up at `g(3)`; in `main()`" **<--** Are you assuming that you got to your *current location* from `g(2)` and once you're all done you *return* to its next line `g(3)`? – mfaani Mar 13 '17 at 20:15
  • 1
    @Honey, yes, there was some ambiguity there (whether `g(2)` or `g(3)` call is currently active) so I've cleared that in the first paragraph. Thanks for the heads up. – paxdiablo Mar 14 '17 at 00:11
216

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

isherwood
  • 58,414
  • 16
  • 114
  • 157
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
30

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

Jean-Bernard Pellerin
  • 12,556
  • 10
  • 57
  • 79
13

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

Dean P
  • 1,841
  • 23
  • 23
5

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.

mtb
  • 1,350
  • 16
  • 32
wanana
  • 361
  • 1
  • 9
4

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.

Yogesh Yadav
  • 4,557
  • 6
  • 34
  • 40
0

Image you have the following functions:

func aa() {
   a1()
}
func bb() {
   b1()
}

And execute them in the following order. The annotates the lines that you have your breakpoints on.

1: aa() 
2:   bb()
3: cc()

Continue: Continues with the app flow. Will stop at the next breakpoint. In this case it will stop at Line 3.

Step Over: Will move onto the next line. In this case it will stop at Line 2

Step Into: Will go into the aa() function. Will Stop at line a1()

Step Out: If you did Step Into, then step out, will take you back to Line 1

If you want to visualize this more:

Continue: Just jumps
Step Over: Moves Down
Step Into: Moves Right
Step Over: Moves Left

The directions above correlate more or less with the direction that you read your code. Or how you read a book (or watch a movie). Continue just jumps to a specific page of the book. Step Over, just moves to the next chapter of the book. Step Into moves within the chapter. Step out, gets back at the previous hierarchical level you were at.

What are there so much differences?

Essentially any time you hit a breakpoint you could be like:

  • Jump to another breakpoint ==> "I don't care about this. Let's move on"
  • Step Over ==> "Ok I have a decent understanding of what's happening, but let's just see what happens next
  • Step Into ==> "Hmmm...let's just see what this function is doing things internally
  • Step Out ==> "Ahhh I see what this function is doing, "let's get out of this function and just go back where we were"
mfaani
  • 33,269
  • 19
  • 164
  • 293