Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Recursion | Methods
Java Extended
course content

Conteúdo do Curso

Java Extended

Java Extended

1. Deep Java Structure
2. Methods
3. String Advanced
4. Classes
5. Classes Advanced

bookRecursion

How to easily break your code?

This can be done with recursion.
Recursion in Java is a process when a method calls itself. Why can this break the code? Because it can lead to infinite recursion, which will endlessly consume memory and decrease the device's performance. So why do we need recursion at all? In some cases, recursion can be useful, but it should be used with caution. Recursion, for example, can replace a while loop. Let's look at an example of using recursion to calculate the sum of all numbers up to a number passed as a parameter:

java

Main

copy
1234567891011121314151617
package com.example; public class Main { public static void main(String[] args) { // do not change code from below System.out.println(calculateSum(5)); } // do not change the code above static int calculateSum(int num) { if (num > 0) { num = num + calculateSum(num - 1); } else { return 0; } return num; } }

The method calculateSum() calls itself with reduced values. We also have an exit point from this recursion when the variable num becomes zero. This method calculates the sum of all numbers from 1 to the parameter, in our case 5. Let's look at the flowchart that shows how recursion works using this method as an example:

The example above demonstrates how the method calls itself with reduced values, and when it reaches zero, it goes back up by summing the values. We can also observe how the body of the if block is executed, where we add num to the result of the method call with a reduced value. The intermediate values after each method call are indicated near the arrows.

Can we avoid using recursion?

Recursion can also be replaced with a regular loop. Let's look at an example where we first use a recursive method and then use a for loop to perform the same operation:

java

Main

copy
123456789101112131415161718192021222324
package com.example; public class Main { public static void main(String[] args) { // do not change code from below int num = 5; System.out.println("result after using recursion: " + calculateSum(num)); int result = 0; for (int i = 1; i <= num; i++) { result = result + i; } System.out.println("result after using for-loop: " + result); } // do not change the code above static int calculateSum(int num) { if (num > 0) { num = num + calculateSum(num - 1); } else { return 0; } return num; } }

Here we can see how we replace our recursive method with a regular for loop, using the variable i to increment our result by i each time until it reaches the specified number num, which is the parameter in the method.

Conclusion

In conclusion, I can say that recursive methods are quite interesting but also dangerous. With recursion, you can both improve and completely break your code. Almost any recursion can be replaced with a loop using method calls. So, whether to use recursion or replace it with a method is your choice. But if you still choose to use recursion, even if you have mastered it well, be extremely cautious.

1. What is recursion in Java?
2. Which is better to use, recursion or a regular loop?
What is recursion in Java?

What is recursion in Java?

Selecione a resposta correta

Which is better to use, recursion or a regular loop?

Which is better to use, recursion or a regular loop?

Selecione a resposta correta

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 7
some-alt