Java Recursion


Video is ready, Click Here to View ×

Get the Code:

Welcome to my Java Recursion tutorial. In this video, I’m going to cover java recursion in 5 different ways. I figured if I show it using many different diagrams that it will make complete sense.

A recursive method is just a method that calls itself. As these calls are made the problem gets simpler until you reach a condition that leads to the method no longer making calls upon itself. This is known as the base case.

40 thoughts on “Java Recursion

  1. Hi Derek, how are you? Thank you for your tutorials, I just wanted to ask if you have any good links or places to do exercises on algorithms. I dont mean like textbook stuff, that really does not help, but mainly like "make a program" type of deal if you know what I mean. Do you by chance have any handy? Thanks again!

  2. Thanks sir. The explanation of the recursive methods was very good and it made it easier for me to understand that. Though, the later portion of the video was too complicated for me. Thank you for the video. I guess I will also use the same way as you to teach my viewers about recursion. Cheers 👏

  3. Awsome, but these examples all had a base case of one. Could you do an extra video for recursion problems with more than one base case? Also, could you cover recursive permutation as well? Where you can print out every possible combinations for letters in a word?

  4. Thanks for the great explanation and the effort you put into making the video!
    I'm quite a beginner, and it might be a very stupid question, but once number equals 1, and it reaches the 'return 1;', why does it go forward into the 'else' section again?

  5. Actually, for triangular numbers, you can calculate it like this:
    public int getTriNum(int num) {
    return (num + (num * (num – 1) / 2));

    Or a bit more understandably:
    public int getTriNum(int num) {
    for(int i = 1; i <= num; i++) num += i;
    return num;

    And for factorials, you can also do it without recursion:
    public int factorial(int num) {
    for(int i = 1; i < num; i++) num *= i;
    return num;

    But factorial still is a pretty good example, nevertheless.

  6. Wow thank you so much. My data structures text book didn't explain at all how this works. I ran an example in the debugger and was extremely confused why it went "back up" once it hit the base case. You cleared it up in the first 2 minutes. Thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *