|
|
Recursion
Recursion is a good tool to add to your
toolbox. You may not think of a reason to use it now, but years
in the future, you'll come upon a situation where recursion is a
perfect fit.
Recursive methods are based on two principles:
- Every recursive call must simplify the computation in some
way. In other words, as a method calls itself recursively, the
problem should be getting easier and smaller, not larger.
- There must be a terminal point of the recursion, which is
usually the point of the simplest computation. There must be a
special case to handle the simplest computation directly.
If you have a recursive call without simpler parameters, or if the
simple computation at the terminal point is missing, you will end up
with an infinite recursion. This will eventually cause your
computer, or the JVM, to run out of memory, and your program will crash!
More Recursion
|