## Recursion In Java

We will learn **Java recursion** with examples in this post so we can fulfill your educational needs.

## Java Recursion

**Java recursion** is the technique of making a function call itself.

The purpose of this technique is to simplify complex problems into simpler ones that can be solved easily.

In some cases, you may find recursion difficult to understand.

Experimentation is the key to discovering how it works.

## Java Recursion Example

Two numbers are easy to multiply together, but multiplying a range of numbers is more challenging.

When it comes to Java recursion, multiplying a range of numbers together is accomplished by breaking it down into the simple task of multiplying two numbers.

The following example shows how to multiply all 10 natural numbers starting from 1 which is also known as Factorial of 10:

#### Example:

### Example Explanation:

By using the **factorial_of_ten_numbers()** method, the parameter ample is multiplied by the product of all numbers smaller than ample, and the result is returned as mrx. When ample becomes 1, the function returns 1. During execution, the program performs the following steps:

```
10 * factorial_of_ten_numbers(9)
10 * ( 9 * factorial_of_ten_numbers(8) )
10 * ( 9 * ( 8 * factorial_of_ten_numbers(7) ) )
…
10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1
```

When ample is 1, the function does not call itself, so the program stops there and returns the result.

The following example shows how to sum all 15 natural numbers starting from 1:

#### Example:

### Example Explanation:

By using the **sum_of_15_numbers()** method, the parameter ample is added to the sum of all numbers smaller than ample, and the result is returned as mrx. When ample becomes 1, the function returns 1. During execution, the program performs the following steps:

```
15 + sum_of_15_numbers(14)
15 + ( 14 + sum_of_15_numbers(13) )
15 + ( 14 + ( 13 + sum_of_15_numbers(12) ) )
…
15 + 14 + 13 + 12 + 11 + 10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1
```

When ample is 1, the function does not call itself, so the program stops there and returns the result.

## Halting Condition

Infinite recursion is a problem associated with recursive functions, just as infinite looping is a problem associated with loops.

When a function calls itself infinitely, it is considered infinite recursion.

Recursive functions must have halting conditions where they can stop calling themselves. A halting condition in the previous example occurs when k becomes 1.

The concept can be better understood by seeing a variety of different **examples**. As shown in the example, the function multiplies a range of numbers between a start and an end. End must not be greater than start to halt this recursive function.

Using the example below, you can use recursion to product numbers between 3 and 8:

#### Example:

Below is an example of how recursion can be used to sum numbers between 4 and 9:

#### Example:

## Recursion: Advantages and Disadvantages

- Recursive calls allocate new storage locations on the stack for variables.
- Each time a recursive call returns, the old variables and parameters are removed from the stack. As a result, recursion typically consumes more memory and is slower.
- In contrast, recursive solutions are much simpler to write, debug, and maintain.

The Java Recursion concept has now been explained to you, so you know how to take advantage of it.