Trace recursive methods 3

Java uses a stack to keep track of the sequence of method calls within a program. This approach can be used to trace recursive methods by hand. (The technique actually works for any sequence of method calls though it is usually only worthwhile for recursive methods.)

Stack based recursive method tracing technique

  1. Place the first method call in an empty stack.
  2. When a recursive call is made note the position at which the calling method stopped and add the called method to the top of the stack.
  3. When a method returns note the return value, cross out the method and return to the position at which the top most method in the stack stopped.

Simple stack based recursive method tracing example

Tracing recursive methods simple example (as PDF)

Additional recursive method tracing examples

3 thoughts on “Trace recursive methods

  1. Reply Edward Eldeiry Mar 9,2015 6:20 pm

    Thank you so much! I will reference your website more often!

  2. Reply ak May 2,2016 2:09 am

    This is the first time I have understood how to trace a recursive method properly! I finally get it! Thank you so much, I will definitely be recommending this site to my teachers.

  3. Reply Kathryn May 17,2017 12:04 pm

    Makes things so much easier. Thanks for your technique!

Leave a Reply