Tan115's picture
Add files using upload-large-folder tool
33852e5 verified
Raw
History Blame Contribute Delete
4.12 kB
1
00:00:00,000 --> 00:00:00,000
Hello team!
2
00:00:00,000 --> 00:00:04,000
In this lesson we will learn what are recursive methods and how to use them.
3
00:00:04,000 --> 00:00:06,000
What is recursion?
4
00:00:06,000 --> 00:00:10,000
Recursion is a process in which a method calls itself continuously.
5
00:00:11,000 --> 00:00:15,000
A method in Java that calls itself is called recursive method.
6
00:00:15,000 --> 00:00:17,000
Let's write the easiest recursive method.
7
00:00:18,000 --> 00:00:24,000
I have the method which calls itself, and when it calls itself, it calls the same method again.
8
00:00:24,000 --> 00:00:26,000
This is recursive method.
9
00:00:26,000 --> 00:00:27,000
What is wrong here?
10
00:00:28,000 --> 00:00:29,000
Let's run our program.
11
00:00:31,000 --> 00:00:33,000
We have stack overflow error.
12
00:00:33,000 --> 00:00:35,000
Why this has happened?
13
00:00:35,000 --> 00:00:40,000
Because when a recursive call is made, new storage locations for variables are located in the stack
14
00:00:40,000 --> 00:00:41,000
memory of the JVM.
15
00:00:42,000 --> 00:00:46,000
So each recursive call will add a new frame to the stack.
16
00:00:46,000 --> 00:00:53,000
That means we can run out of memory, because information about initial and other method invocations
17
00:00:53,000 --> 00:00:54,000
won't be removed from the stack.
18
00:00:55,000 --> 00:00:58,000
We need to finish method to release memory in stack.
19
00:00:58,000 --> 00:01:03,000
But when we call the same method again and again, it just can't be completed.
20
00:01:04,000 --> 00:01:07,000
So here is an important rule which you have to always keep in mind.
21
00:01:07,000 --> 00:01:12,000
When you create recursive methods, you should clearly define stop condition.
22
00:01:12,000 --> 00:01:17,000
The condition when recursion must be stopped and value should be returned.
23
00:01:17,000 --> 00:01:20,000
Let's find factorial of a number.
24
00:01:20,000 --> 00:01:26,000
Factorial of a positive integer is a result of multiplication Application of all positive integers less
25
00:01:26,000 --> 00:01:28,000
than or equal to original number.
26
00:01:28,000 --> 00:01:33,000
We'll write recursive method in case I is not equal to zero.
27
00:01:33,000 --> 00:01:39,000
Then we can multiply I by the result of the same method, but with number decreased by one, and when
28
00:01:39,000 --> 00:01:45,000
I will be equal to zero, we return one and no recursion here anymore.
29
00:01:45,000 --> 00:01:47,000
We broke the chain of recursion calls.
30
00:01:48,000 --> 00:01:50,000
Let me comment this method first.
31
00:01:50,000 --> 00:01:54,000
Now I can run the program again and you see the result in console.
32
00:01:54,000 --> 00:01:58,000
Here we calculated factorial of three and four.
33
00:01:58,000 --> 00:02:02,000
Now when you know how recursive method works, let's recap.
34
00:02:02,000 --> 00:02:04,000
Pros and cons of recursive methods.
35
00:02:04,000 --> 00:02:08,000
Recursive methods usually look clean and easy to understand.
36
00:02:08,000 --> 00:02:11,000
Often we can achieve our goal without recursive methods.
37
00:02:12,000 --> 00:02:17,000
For example, here is the same method to calculate factorial but without recursion, and you see that
38
00:02:17,000 --> 00:02:20,000
it is a bit longer and less clear than recursive one.
39
00:02:21,000 --> 00:02:27,000
So clean design is an advantage of recursive methods, but recursive methods have one huge drawback.
40
00:02:28,000 --> 00:02:33,000
In case there will be a lot of methods, invocations or stop condition will be written incorrectly.
41
00:02:33,000 --> 00:02:36,000
Your program will be stopped because of stack overflow error.
42
00:02:37,000 --> 00:02:39,000
Now you know pros and cons of recursive methods.
43
00:02:40,000 --> 00:02:43,000
I will share with you coding exercises to write recursive methods.
44
00:02:44,000 --> 00:02:45,000
Thanks a lot for your attention.
45
00:02:45,000 --> 00:02:47,000
See you in the next lesson.
46
00:02:48,000 --> 00:02:48,000
Your.