Delving into Recursion
Recursion is useful for many mathematical problems, such as when working with cellular automata, Sierpinski triangles, and fractals. In computer graphics, recursion can be used to help generate realistic-looking mountains, plants, and other natural phenomena. Classic problems, such as the Tower of Hanoi, work well with recursion.
In Java applications, you will often use recursion when traversing tree data structures, including XML and HTML documents.
Note
You can refer to https://packt.live/2JaIre8 for more information on the Tower of Hanoi problem.
A simple example of recursion looks like the following:
public int add(int num) { return add(num + 1); }
In this example, each call to the add()
method will call itself with a number that is one greater than the one used for the current call.
Note
You always need a termination condition to stop the recursion. This example does not have one.