CodeGym /Java Blog /Core Java /Java Recursion
Author
Aditi Nawghare
Software Engineer at Siemens

Java Recursion

Published in the Core Java group

What is Java Recursion?

The scenario in which a function or method calls itself is known as recursion and such a function or method is known as a recursive function or recursive method. This approach is always used to break complex problems into into a simple and easier to write one. A simple example is to place two mirrors in front of each other and they will start reflecting each other recursively which you can see easily.

Indirect Recursion

The above example represents the indirect recursion, that is, function one calling function two and function two calling function one.

Direct Recursion

When a method calls itself again and again, it is called direct recursion.

Halting Condition

Now you will be thinking if a method is calling itself again and again then sure it will face a Stack Overflow error. Yes, you are right, like infinite looping it can go into an infinite recursion. We must provide a base condition or a termination condition in order to stop this infinite recursion, which is known as a halting condition, and this condition is always provided inside the recurse method, which we will see in detail in the example below.

Recursion Example


class Recursion {

    static int factorial( int n ) {
        // base condition or termination condition
        if (n != 0)  
        // here we are calling the recursive method
            return n * factorial(n-1); 
        else
            return 1;
    }

    public static void main(String[] args) {
        // initializing the variables
        int number = 5, result;
        result = factorial(number);
        System.out.println(number + " factorial = " + result);
        
        // changing the value from 5 to 10
        number = 10;
        result = factorial(number); // recursive function call
        System.out.println(number + " factorial = " + result);
    }
}

Example Output

5 factorial = 120 10 factorial = 3628800
Java Recursion - 1In this recurse method example we have a function factorial called from the main method. We passed a value of 5 to see what its factorial is, which is 120. This factorial function is calling itself again by passing the value of 4, which again calls and passes the value of 3, and so on, until the halting condition is met, which is the value of 0. The same happens when we changed the value from 5 to 10.

Advantages and Disadvantages of Recursion

A recursive code is simple to write, debug, and maintain, which is time-saving. But on the other hand, with each call the memory is consumed for the variables, and because of this we can run out of memory.

Conclusion

We hope you understand Java recursion and how to implement it, as well as its advantages and disadvantages. Feel free to practice and get back whenever you need more assistance. Happy learning!
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION