Here’s How To Reverse A String In Java – Explained With Examples

reverse string in java blog image

I’ll explain how to reverse a string in Java in this blog post. Reversing a string means inverting the characters’ positions so that the final character becomes the first, the second to the last character becomes the second, and so on until the entire string is flipped around. We’ll look at various Java methods for reversing strings in this blog post.

Different ways to reverse a string in Java

There are several ways you can reverse a string. Let’s look at them one by one.

Reversing a string using a For Loop

This is the most popular and simple method to reverse a string. We are using a For loop as shown n this example.

public static String reverseStringForLoop(String str) {
    String reversed = "";
    for (int i = str.length() - 1; i >= 0; i--) {
        reversed += str.charAt(i);
    }
    return reversed;
}

This method creates a new string called “reversed” and iterates through the original string “str” in reverse order using the “for” loop. You can see that each character in “str” is appended to “reversed” until the complete string is reversed which is returned to the caller finally.

Reversing a string using StringBuilder

Another way to reverse a string in Java is by using the StringBuilder class. Here’s how:

public static String reverseStringBuilder(String str) {
    StringBuilder sb = new StringBuilder(str);
    sb.reverse();
    return sb.toString();
}

This method creates a new StringBuilder object called “sb” with the original string “str” as its initial value. The “reverse()” method is then called on the StringBuilder object to reverse the string, and the reversed string is returned as a regular string using the “toString()” method.

Reverse a string in Java using recursion

We can also use recursion to reverse a string.

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

In the above example, the method uses recursion to reverse the string. It is simply a method that calls itself until it reaches its goal. Here this method starts with one character and then swaps it calling the same method again. Finally, it stops when all characters are swapped from start to end when the strong length becomes empty.

Which Method Should You Use?

Although each of the three approaches is an acceptable way to reverse a string in Java, their levels of the performance vary. For small to medium-sized strings, the for-loop approach and the StringBuilder method are typically faster and more memory-efficient, however, the recursion method may be slower and less memory-efficient for large strings. For certain developers, the recursion method, however, may be more effective and simple.

Conclusion

We looked at three distinct approaches—using a for loop, StringBuilder, and recursion—to reverse a string in Java. Each technique has its own pros and cons.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *