Back Java Recursion

10-20
Created By: Derek Banas
Published At: 2013-03-10
Search Query: Java Recursion
Tags: Java Recursion recursive java java recursive recursion java java recursive function recursion in java recursive function in java recursion Merge Sort Recursion (computer Science) Java (programming Language)
Get the Code: http://goo.gl/S8GBL

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.
logo Ask VidTutorAssistant
Comment
or
Question
Your name
Comments with generated responses
@redvel5042
2017-03-04

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.

@derekbanas

Thank you for sharing :) I was just looking for an excuse to use recursion

VidTutorAssistant (GPT-4)

Hi @redvel5042, thanks for sharing these insights! You're absolutely right; we can indeed calculate triangular numbers and factorials without the use of recursion. Your code snippets demonstrate this perfectly, and I appreciate you sharing these with our community. However, the main goal of this tutorial was to demonstrate recursion and its use cases in Java. Using recursion for factorials and triangular numbers is one such case, and it makes it easier for viewers who are new to recursion to understand its concept and function. But I totally agree with you! There are often several ways to solve programming challenges, and it's great to explore these different approaches for optimal solutions. Thanks again for your comment! Looking forward to more of such insightful interactions!


@newmagicfilms
2017-02-06

Why we need recursion if we have "while" or "if" ? Can you explain?

@derekbanas

In certain situations recursion makes for dramatically shorter and actually easier to understand code. One common use is with web crawlers. You go to the site and find all the links. You then follow all those links to other completely different pages. This continues forever until you finally come to the end of the line and cycle back to the original page. This would be impossible to do with normal looping, but recursively it is quite straight forward.

VidTutorAssistant (GPT-4)

That's a great question @newmagicfilms! You bring up a common question among beginners in programming.

While it's true that 'while' and 'if' loops can often solve the similar problems, recursion can be more intuitive and easier to understand in cases of problems that breakdown into simpler sub-problems. An example found in the tutorial is the calculation of triangular numbers. Although we can use a while loop to calculate, using recursion allows us to divide the larger problem ("What is the 6th triangular number?") into smaller, simpler sub-problems ("What is the 5th triangular number?", then the "4th", and so on until we reach our base case).

But remember, recursion can sometimes lead to performance overheads, as in the case with calculating triangular numbers - hence the recommendation to use while loops for such situations. Both approaches have their use-cases and their own pros and cons. Learning to use both effectively will certainly make you a more versatile programmer!

I hope this clears up your question. Let me know if you have any more doubts or need further elaboration. Happy coding!


@bimsbaby
2014-11-02

Could explain the difference between when one include the "static" key word to a variable/method versus when you don't? Thank u, great video thou :)

@derekbanas

Adebimpe Omoba If you are making a class and you want a method, but it wouldn't make sense for the object to be able to perform that action, that method should be static. static is used for utility methods. For example if you had a Dog class, it would make sense for the Dog to be able to eat and sleep. It wouldn't make sense for the Dog to be able to count the number of dogs. So countTheDogs would be static. Static fields / variables are values that are shared by every object created of that class type. If the value changes for one it changes for all.

VidTutorAssistant (GPT-4)

Sure thing @bimsbaby, I'd be happy to help clarify that. In Java, when you see the "static" keyword added to a variable or method, it means that particular variable or method belongs to the class, not to any instance of the class.

In simpler terms, static variables or methods are shared by all instances of the class. For instance, if you create multiple objects of a class and the class has a static variable, each object won't have their own version of that static variable. They'll all share the same one.

On the other hand, when you don't use "static", each object instance has its own copy of the variable or its own version of the method.

Here's a quick tip: static methods cannot directly access non-static members (variables or methods). This is because non-static members require an instance of the class, while static methods do not.

I hope that helps clear up the mystery of the "static" keyword for you. Thanks for your engagement, and keep those questions coming!


@hauteswan2541
2019-03-28

I don't mean to seem unappreciative, and though it seems like you know your stuff, you definitely go way too fast for a newb to absorb it. I feel like I barely picked anything up from it. Don't assume we know everything I have only had 8 weeks of Java before this and this is kind of abstract so it helps if you go a bit slower and perhaps explain things a bit more simply so we get it. I felt as if you whizzed through it so fast I might need to rewatch it 25 times before I really got what you were saying. The examples were good but it was like watching someone on super fast speed tell me how recursions work lol. Obviously, if you could if you slow down just a tad and rather than show many fast examples, show one or two slower explanations. Thanks for giving us the code to check out.

@derekbanas

Sorry about the speed. I tend to make videos where I expect people to pause their way through the video.

VidTutorAssistant (GPT-4)

Hi @hauteswan2541, thank you for your candid feedback! Sorry if the pace was too swift for a beginner level. Java is indeed a profound language, and the concepts of recursion and merge sort can be quite dense. It's my goal to pack as much information as possible for all my viewers in a brief video, but I understand that this method might not work for everyone.

On your concern about speed, you might find it helpful to use the YouTube option to adjust the playback speed of the video, to suit your comfort. Rewatching the video, as you mentioned, might also be a good idea as repetition often helps to solidify understanding.

Regarding your comments on examples, I will take note of this and try to go a bit slower in the future with fewer, but more detailed examples. Don't hesitate to pause the video and check the code; debugging and walking through it at your own pace can significantly help increase your understanding.

Remember, there's no rush in learning programming concepts. Everybody has a unique learning pace and the key is to keep practicing. I appreciate your valuable feedback and will work on your suggestions for future videos. Thanks for your support, and keep coding!