How to Reverse a String in Java – 4 Easy Methods with Examples

In this tutorial, you will learn how to reverse a string in Java using 4 different methods. Reversing a string means printing its characters in reverse order. For example, if the input is "Codeamy", the output will be "ymaedoC".

This is one of the most common Java interview questions for beginners. You are given a string and the task of the program is to Reverse a String in Java.

Method 1: Reverse a String in Java Using For Loop

In this method, we use a for loop that starts from the last character of the string and reads each character backwards to build the reversed string.


import java.util.*;
public class ReverseString
{
    public static void main(String args[])
    {
        String original, reverse = "";
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a string to reverse");
        original = in.nextLine();
        int length = original.length();
        for ( int i = length - 1 ; i >= 0 ; i-- )
        reverse = reverse + original.charAt(i);
        System.out.println("Reverse of entered string is: " + reverse);
    }
}
Output

Enter a string to reverse : Codeamy
Reverse of entered string is: ymaedoC
Explanation: original.length() gives the total characters. The loop starts from the last index and uses charAt(i) to pick each character backwards and append it to the reverse string.

Method 2: Reverse a String in Java Using StringBuilder

Java's StringBuilder class has a built-in reverse() method. This is the simplest and most efficient way to reverse a string in Java.


public class ReverseStringBuilder
{
    public static void main(String[] args)
    {
        String original = "Codeamy";
        StringBuilder sb = new StringBuilder(original);
        String reversed = sb.reverse().toString();
        System.out.println("Original: " + original);
        System.out.println("Reversed: " + reversed);
    }
}
Output

Original: Codeamy
Reversed: ymaedoC
Explanation: new StringBuilder(original) creates a StringBuilder object. The reverse() method reverses the characters in place, and toString() converts it back to a String.

Method 3: Reverse a String in Java Using Character Array

In this method, we convert the string to a char array, swap characters from both ends toward the center, and convert it back to a string.


public class ReverseCharArray
{
    public static void main(String[] args)
    {
        String original = "Codeamy";
        char[] charArray = original.toCharArray();
        int left = 0;
        int right = charArray.length - 1;
        while (left < right)
        {
            char temp = charArray[left];
            charArray[left] = charArray[right];
            charArray[right] = temp;
            left++;
            right--;
        }
        String reversed = new String(charArray);
        System.out.println("Reversed: " + reversed);
    }
}
Output

Reversed: ymaedoC
Explanation: Two pointers left and right start from both ends. Characters are swapped and pointers move toward the center until they meet. This is a popular approach in Java interviews.

Method 4: Reverse a String in Java Using Recursion

In this method, we use recursion. The function calls itself repeatedly, taking the first character and placing it at the end until the string is empty.


public class ReverseRecursion
{
    public static String reverseString(String str)
    {
        if (str.isEmpty())
        {
            return str;
        }
        return reverseString(str.substring(1)) + str.charAt(0);
    }

    public static void main(String[] args)
    {
        String original = "Codeamy";
        String reversed = reverseString(original);
        System.out.println("Reversed: " + reversed);
    }
}
Output

Reversed: ymaedoC
Explanation: The base case returns the string when it is empty. Otherwise, substring(1) removes the first character, and charAt(0) appends it at the end recursively.

Comparison of All Methods

Method Ease of Use Performance Best For
For Loop Easy Moderate Beginners
StringBuilder Very Easy Best Real-world use
Character Array Moderate Good Interview questions
Recursion Complex Low Concept learning

Frequently Asked Questions

Q1. What is the easiest way to reverse a string in Java?
The easiest way is using StringBuilder.reverse(). It takes just one line of code and is also the most efficient.

Q2. Can we reverse a string without using StringBuilder?
Yes. You can use a for loop or a character array swap to reverse a string without any built-in method.

Q3. Is String immutable in Java?
Yes. Java strings are immutable. When you reverse a string, you always create a new string object.

Q4. What is the time complexity of reversing a string in Java?
All the methods above have a time complexity of O(n), where n is the length of the string.

Conclusion

In this tutorial, you learned how to reverse a string in Java using four different methods — for loop, StringBuilder, character array, and recursion. For most use cases, StringBuilder's reverse() method is the best choice due to its simplicity and performance.

If this post helped you, share it with your friends and follow Codeamy for more Java tutorials!

If you have any doubts, Please let me know

Previous Post Next Post

Contact Form