Whats the best way to recursively reverse a string in Java?

The best way is not to use recursion. These stuff are usually used to teach students the recursion concept, not actual best practices. So the way you’re doing it is just fine. Just don’t use recursion in Java for these kind of stuff in real world apps 😉

PS. Aside what I just said, I’d choose "" as the base case of my recursive function:

public String reverseString(String s){
    if (s.length() == 0) 
         return s;

    return reverseString(s.substring(1)) + s.charAt(0);
}

Leave a Comment