What is Recursion in Java explain with example?

Here we will Learn What is Recursion in Java and explain it with examples? recursion is calling a function time and again and making it a loop with a given condition and updations and executing a set of code multiple times in a loop to get desired output.


Function Calling

A function call is a way to start the execution of a function by providing arguments as input and executing a set of code to get the output as the function returns the answer every time when it is being called.
And calling the same function multiple times for the purpose of executing the function multiple times is what we call Recursion where we keep on calling the function multiple times and execute the same code time and again.

It is somewhat the same as we used to do during loop statements where a set of lines of code are being executed multiple times. Now let’s understand more and get a clear idea about it and execute


As we know it is a way to call the same function multiple times by passing the updated arguments and keep on executing the set of code multiple times until the condition does not end.

Let’s understand it in a better manner with some examples.

public class RecursionExample2 {  
static int count=0;  
static void p(){  
System.out.println("hello "+count);  
public static void main(String[] args) {  

Here we can see how the function p() function is called time and again until the condition count is less than 5.

Point to remember:-

We need to keep on updating the variable each time must have some exit plan to terminate the program otherwise the program will keep on executing infinitely and which may break down the machine as it will keep on executing.

To learn more about recursion at: Recursion in Java

And also visit Java Tutorials to learn more about java problems and for Python follow How do I Reverse a List backwards? | Python Tutorial and keep learning.

Leave a Comment

%d bloggers like this: