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?

Why can this break the code? Because it can lead to infinite recursion, which will endlessly consume memory and reduce the device's performance. So why do we need recursion at all? In some cases, recursion can be useful, but it should be used carefully. For example, recursion 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
1234567891011121314151617181920
package com.example; public class Main { // main method to run the application public static void main(String[] args) { // do not modify the code below System.out.println(calculateSum(5)); } // method to calculate the sum of numbers from 1 to num using recursion static int calculateSum(int num) { // if num is greater than 0, recursively calculate the sum 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
123456789101112131415161718192021222324252627282930313233
package com.example; public class Main { // main method to run the application public static void main(String[] args) { // do not modify the code below int num = 5; // printing the result of the recursive sum calculation System.out.println("Result using recursion: " + calculateSum(num)); int result = 0; // calculating the sum using a for-loop for (int i = 1; i <= num; i++) { result = result + i; } // printing the result of the sum calculation using the for-loop System.out.println("Result using for-loop: " + result); } // method to calculate the sum of numbers from 1 to num using recursion static int calculateSum(int num) { // if num is greater than 0, recursively calculate the sum 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, recursive methods can be useful but also risky. Recursion can often be replaced by loops, and while it can simplify certain tasks, it can also cause issues if not used carefully. If you choose to use recursion, even with experience, make sure to proceed with caution.

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