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
- Place the first method call in an empty stack.
- 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.
- 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)
Thank you so much! I will reference your website more often!
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.
Makes things so much easier. Thanks for your technique!