Recursion Problems in Java
In a Java program, a recursive method is a method that calls itself. Recursive methods can help programmers to design a code excerpt to simplify a problem. Recursive methods also often appear to be concise, but creating an effective recursive function can be very challenging. As with loops and other control structures in Java, developers have to work through what will happen when a recursive function executes. Recursive methods cause a set of common problems when not correctly structured.
-
Problem Solving Failure
-
Programmers use recursive methods to solve particular problems. Naturally a recursive method will not provide useful functionality if it does not solve the problem it was designed for. This sometimes happens when the programmer has chosen to use a recursive function where it is not appropriate. Generally, a recursive method is ideal for situations in which you need to solve a problem using iterative steps. Each time the method executes, it should make the problem simpler and bring your program one step closer to solving it. The end result of the recursive method should be the solution to your problem, for example locating an element in a data collection object.
No End Case
-
In order for a recursive function to work without getting your program stuck in an infinite loop, it must have a well-defined end case. This means that there must be a final execution, rather than the method executing endlessly. Programmers often implement this using conditional statements inside the method, as in the following example:
public void doItRecursively(int num) {
if(num<1) return;
else {
System.out.println(num);
doItRecursively(num/2);
}}To call this function, a program can use the following code:
doItRecursively(10);In this case the function will execute five times, exiting on the fifth iteration. Each time the method executes it comes closer to the end scenario, which the conditional if statement checks for. If the method did not contain the conditional statement, it would iterate endlessly.
-
No Recursion
-
The counter issue to an endlessly looping iterative method is a method that does not iterate at all. This also happens when the conditional statement is not correctly structured. For example, given the "doItRecursively" method, the following altered if statement would cause a problem:
if(num>0) return;If the method is only called with positive integer values as the parameter, its content will never iterate as the end point will have been reached straight away.
Incorrect Method Calls
-
If the programmer calling a recursive method does not have a clear grasp of its function, they may use it in a way that causes endless iteration or no iteration. For example, the following altered conditional code within the method could easily be called incorrectly:
if(num<0) return;If the method is only called with positive integer parameter values, it will never reach this end point and will cause an infinite loop. Recursive methods can cause many of the same pitfalls as loops, so they do require practice for effective implementation.
-
References
Resources
- Photo Credit Zedcor Wholly Owned/PhotoObjects.net/Getty Images