---
title: "What is recursion in Java and also write code for reverse string?"  
description: "What is recursion in Java and also write code for reverse string?"  
author: "Ravi Vishwakarma"  
published: 2023-01-27  
updated: 2023-04-18  
canonical: https://www.mindstick.com/forum/157349/what-is-recursion-in-java-and-also-write-code-for-reverse-string  
category: "java"  
tags: ["java", "programming language"]  
reading_time: 3 minutes  

---

# What is recursion in Java and also write code for reverse string?

What is [recursion](https://answers.mindstick.com/qa/111681/what-is-recursion-and-when-should-i-use-it) in [Java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming) also [explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) it, and write [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) for [reverse string](https://www.mindstick.com/forum/1554/java-regex-to-split-and-reverse-string) using recursion [technique](https://answers.mindstick.com/qa/93922/why-c-sharp-programmers-use-properties-technique-in-c-sharp-programming)?

## Replies

### Reply by Krishnapriya Rajeev

Recursion is the process in which a function calls itself directly or indirectly and the corresponding functions are called recursive functions.

Recursive function or method calls store new parameters and local variables on the stack, which are then removed as we return from the recursive calls. While recursive functions enable us to develop simple and clear solutions for certain problems easily, they can also lead to slow execution due to added overhead and additional function calls.

The concept of recursive function can be used to find the factorial of a number as shown below:

```plaintext
public class Main
{
	//recursive function for finding factorial
    static int factorial(int n)
    {
        if(n==1)
            return 1;
        else
            return(n*factorial(n-1));
    }
	public static void main(String[] args) {
		System.out.println("Factorial of 5 is: "+factorial(5));
	}
}

OUTPUT:
Factorial of 5 is: 120
```

Here, if factorial() is called with an argument of 1, then it returns 1 else it returns the product of n*factorial(n-1).

Code for **reversing string** using recursion in java

```plaintext
public class reverse
{
//recursive function to reverse a string
    public String reverseString(String str, String revstr)
    {
    //checks if the string is empty
        if(str==null||str.length()==0)
        {
            return revstr;
        }
        else
        {
            return reverseString(str.substring(1), str.charAt(0)+revstr);
        }
}

    public static void main(String[] args)
    {
        reverse rs = new reverse();
        String reversedString = new String();
        reversedString = rs.reverseString("MINDSTICK","");
        System.out.println(reversedString);
    }
}
```

### Reply by Sanjay Goenka

I have no idea about the theory parts.

I try to write code for a **reverse string** in recursion.

```javascript
function reverseString(s) {
   if (s.length == 0) {
       return s;
   }
   return reverseString(s.substring(1)) + s.charAt(0);
}

console.log(reverseString("kjhfdsfa-asdjhfkas-uw-1jas-5"));
```

## Output →

## 5-saj1-wu-sakfhjdsa-afsdfhjk

## I hope you enjoy this answer. Bye !!!

### Reply by Amrita Bhattacharjee

**Recusion** is a method where a function calls directly itself. In java, recursion is recognised as a process where any particular function calls itself. This process is used for avoiding difficult problems by making them comparatively easier. This method is used rarely and when any complicated type of code is needed to write then this process is used to make that simple. It is the process where the items are repeated in a same kind of manner.

The code for [reverse](https://www.mindstick.com/blog/63740/what-are-the-most-effective-and-safest-thanks-to-reverse-erectile-dysfunction) [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) using recursion will be as follows:

```java
public static String reverseString(String s) {
   if (s.length() == 0) {
       return s;
   }
   return reverseString(s.substring(1)) + s.charAt(0);
}
```


---

Original Source: https://www.mindstick.com/forum/157349/what-is-recursion-in-java-and-also-write-code-for-reverse-string

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
