Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

Java Recursion

Java Recursion

Recursion involves a function invoking itself, offering a method to decompose intricate problems into more manageable ones for resolution.

.Understanding recursion can be challenging, but experimentation is the most effective approach to grasp its workings.

Recursion Example

While adding two numbers is straightforward, calculating the sum of a range of numbers is more intricate. In the given example, recursion is employed to add a range of numbers by simplifying the task into the addition of two numbers at a time.

Example

Use recursion to add all of the numbers up to 10.

public class Main {
  public static void main(String[] args) {
    int result = sum(10);
    System.out.println(result);
  }
  public static int sum(int k) {
    if (k > 0) {
      return k + sum(k - 1);
    } else {
      return 0;
    }
  }
}

Example Explained

Upon calling the sum( ) function, it adds the parameter k to the sum of all numbers less than k and then returns the computed result. When k reaches 0, the function simply returns 0. During execution, the program proceeds as follows:

10 + sum(9)
10 + ( 9 + sum(8) )
10 + ( 9 + ( 8 + sum(7) ) )

10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + sum(0)
10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0

 

As the function does not recursively call itself when k equals 0, the program halts at that point and returns the final result.

Halting Condition

Similar to loops encountering the issue of infinite looping, recursive functions can face the challenge of infinite recursion, where the function continuously calls itself without termination. It’s crucial for every recursive function to incorporate a halting condition, which denotes when the function ceases to call itself. In the earlier example, the halting condition occurs when the parameter k reaches 0.

Examining various examples aids in comprehending the concept more effectively. In this instance, the function sums a range of numbers from a starting point to an ending point. The halting condition for this recursive function is when the end value is not greater than the start value.

Example

Use recursion to add all of the numbers between 5 to 10

public class Main {
  public static void main(String[] args) {
    int result = sum(5, 10);
    System.out.println(result);
  }
  public static int sum(int start, int end) {
    if (end > start) {
      return end + sum(start, end - 1);
    } else {
      return end;
    }
  }
}

 

Recursion demands caution from developers due to the potential for inadvertently creating functions that either never terminate or consume excessive memory and processing power. Nonetheless, when implemented correctly, recursion can offer an efficient and mathematically elegant solution in programming.