Learn Java Tutorial for Beginners (Video), Part 45: Recursion

Recursion is a vital, if little-used, tool in every programmer's toolbox of tricks. While not specific to Java, due to several requests I decided to include it in this course, since after all it is one of those things that programmers are supposed to learn. Certain problem are very hard to solve without recursion and very easy to solve with it.

When the video is running, click the maximize button in the lower-right-hand corner to make it full screen. Don't forget to change to HD also if the video appears fuzzy.



Code for this tutorial:

App.java:
(Note: I didn't trouble to handle the value 0 here, so fix it before using this code in an exam :)

 


public class App {

    
    public static void main(String[] args) {
        
        // E.g. 4! = 4*3*2*1 (factorial 4)
        
        System.out.println(factorial(5));
    }
    
    private static int factorial(int value) {
        //System.out.println(value);
        
        if(value == 1) {
            return 1;
        }
        
        return factorial(value - 1) * value;
    }

}