Recursive methods in java
A recursive method in the Java programming language is a programming concept or method that automatically calls itself to solve a specific problem in Java or other programming languages. Recursion is a powerful feature in the Java language. It is used to simplify complex numerical, statistical, or mathematical problems, or to solve large problems by breaking them into smaller module sub-problems. A recursive task in a program continues until a user-defined base recursive case or termination condition is executed, which ultimately terminates the recursive call process and displays the output of the desired recursive method.

How recursion works in Java.
- A recursive method in Java programming primarily consists of two key elements.
- Base case – This is a user-defined condition in recursion that terminates the recursion process. Without a base case in recursion, the recursion process would continue running forever, resulting in a stack overflow error in the program.
- Recursive Case – This is an important element of a method in recursion, where a user-defined function method automatically calls itself with modified variable argument arguments, gradually moving towards the base case.
Structure of a recursive method in Java.
- Base case – This is the condition in a user-created recursion process that terminates the recursion process.
- Recursive case – This is the element or portion in recursion where the recursive method calls itself. Generally, it displays the original large, complex problem in a smaller module or simpler format.
<return type> <method name>(<parameters>) {
if (<base case condition>) {
// Here it returns a value and applies user action to stop the recursion process
} else {
// Recursive case: Here it calls the recursive method again with new parameters or variables
}
}
Factorial example using recursion in Java.
Factorial multiplies a start number and an end number by itself from start to end, where a number is multiplied by its next number and displays all the multiplications of the number as a factored value, a factorial number n, displayed as n!. A factorial is the multiplication or product result of all start-to-end integer numeric values less than or equal to n!. For example, n!=n×(n−1)×(n−2)×⋯×1n!=n×(n−1)×(n−2)×⋯×1
Factorial example.
7! = 7×6×5×4×3×2×1 = 5,040
Example of the recursive factorial function method in Java.
public class main
{
// here Recursive method used to calculate the factorial of a given integer number
public static int fact(int n) {
if (n == 0) {
return 1; // Base case: here we define factorial of 0 is 1
} else {
return n * fact(n – 1); // Here we used the recursive call method to call the recursive function.
}
}
public static void main(String[] args) {
int fact_number = fact(7);
System.out.println(“\nThe factorial number of 7 is – ” + fact_number); // Output – 5040
}
}
Explaining the recursive factorial function.
- Here, a factorial recursive function is defined.
- Base Case – The factorial recursive function returns 1 if n is 0 (because 0!=10!=1).
- Recursive Case – The method calls itself from n−1n−1 until n is 0.
Example of the Fibonacci sequence using the recursion method in Java.
In Java programming, the Fibonacci number sequence is a continuous sequence of integer values or numbers, where each number is the continuous addition of the previous two numbers, usually starting from 0 and 1. The Fibonacci number sequence in Java looks like this:
F(0)=0F(0)=0
F(1)=1F(1)=1
F(n)=F(n−1)+F(n−2)F(n)=F(n−1)+F(n−2) for n≥2n≥2
Example of the recursive Fibonacci method in a Java program.
public class main
{
// here Recursive method used to calculate the nth Fibonacci number of sequence in continuous order
public static int fibonacci_num(int n) {
if (n == 0) {
return 0; // here base case define – F(0) = 0
} else if (n == 1) {
return 1; // here base case define – F(1) = 1
} else {
return fibonacci_num(n – 1) + fibonacci_num(n – 2); // here we define fibonacci_num Recursive case
}
}
public static void main(String[] args) {
int output = fibonacci_num(8);
System.out.println(“\n The Fibonacci_number sequence of 8 is – ” + output); // Output is – 21
}
}
Fibonacci sequence explanation.
- Here is the fibonacci_num function method defined in the recursive Fibonacci number sequence in Java.
- Base Case – Here in the Fibonacci function, F(0)=0F(0)=0 and F(1)=1F(1)=1 are the base cases defined.
- Recursive Case – Here in the recursive case, for n≥2n≥2, the output is F(n)=F(n−1)+F(n−2)F(n)=F(n−1)+F(n−2) Fibonacci value.
Example of adding array elements using recursion methods in Java.
Java users can use recursion function methods to add user-declared array elements in a Java program.
Recursive Sum of Array Method Example in Java.
public class Main
{
// Here we use a recursive function method to calculate the sum of all array elements defined in a given array.
public static int addArray(int[] array, int index) {
if (index == array.length) {
return 0; // Base case defined here – if index is at the end of the array, it returns 0.
} else {
return array[index] + addArray(array, index + 1); // Here we use the recursive case.
}
}
public static void main(String[] args) {
int[] array = {4, 7, 9, 11, 14, 17, 19};
int output = addArray(array, 0);
System.out.println(“\nThe addition of all array elements – ” + output); // Output is – 81
}
}
Explaining the Recursive Sum of Array Method.
- Here, in this program, a recursive function named addArray is defined.
- Base Case – Returns the end value of the array, 0, when the index reaches the length of the array.
- Recursive Case – Similarly, with the next index, the current element is added to the result of the recursive function call.
Example of reversing a string using the recursion method in Java.
Here, Java programmers can use the recursion method to reverse a text string.
Example of the recursive string reversal method in Java.
public class Main
{
// Here we use the recursive function method to reverse a text string.
public static String reverseString(String string) {
if (string.isEmpty()) {
return string; // Here we define the base case. If the given string is empty, it will return it.
} else {
return reverseString(string.substring(1)) + string.charAt(0); // Here we use the recursive case method.
}
}
public static void main(String[] args) {
String output = reverseString(“Vcanhelpsu”);
System.out.println(“\n the reverse string is – ” + output); // Output is – usplehnacV
}
}
Explanation of the recursive string reversal method.
- Here we have defined the reverseString function in this program.
- Base case – Here, if the text string is empty, it will return the string termination condition.
- Recursive case – It calls the reverseString method to string itself with the substring (excluding the first character), and displays the first character appended at the end.
