This post will discuss how to reverse a string in Java in 10 different ways. For example, the reverse of “Techie Delight” is “thgileD eihceT”.

We have covered 10 different ways (and 15 different implementations) to reverse a string in Java:

1. Using StringBuilder/StringBuffer

We can use the StringBuilder.reverse() method to reverse a Java string efficiently. Alternatively, we can also use the StringBuffer.reverse() method. Using StringBuilder is suggested as it’s not synchronized and faster than StringBuffer.

Download  Run Code

Output:

The reverse of the given string is: thgileD eihceT

2. Using Stack

We can take the help of Stack data structure to reverse a string in Java. Following are the complete steps:

  1. Create an empty stack of characters.
  2. Convert given string into character array using String.toCharArray() method and push each character of it into the stack.
  3. Remove characters from the stack until it becomes empty and assign them back to the character array. As stack follows FILO order, characters will be inserted in the reverse order.
  4. Finally, convert the character array into string using String.copyValueOf(char[]) and return the formed string.

The following program demonstrates it:

Download  Run Code

Output:

The reverse of the given string is: thgileD eihceT

 
We can also use StringBuilder instead of a character array, as shown below:

Download  Run Code

Output:

The reverse of the given string is: thgileD eihceT

3. Using Java Collections Framework reverse() method

We can use Collections.reverse() to reverse a string in Java. Following are the complete steps:

  1. Create an empty ArrayList of characters and initialize it with characters of the given string using String.toCharArray().
  2. Reverse the list using java.util.Collections reverse() method.
  3. Finally, convert the ArrayList into a string using StringBuilder and return it.

The following program demonstrates it:

Download  Run Code

Output:

The reverse of the given string is: thgileD eihceT

 
The above solution first converts ArrayList into StringBuilder, then StringBuilder into a string. We can directly convert ArrayList into a string by removing square brackets, commas, and single space from it. The following code uses String.replaceAll() to achieve that. But this approach is highly inefficient and should be avoided at all costs.

Download  Run Code

Output:

The reverse of the given string is: thgileD eihceT

4. Using character array

We know that we cannot make any change in the String object as string is immutable in Java. But we can use a character array that can be modified easily:

  1. Create an empty character array of the same size as that of the given string.
  2. Fill the character array backward with characters of the given string.
  3. Finally, convert the character array into string using String.copyValueOf(char[]) and return it.

The following program demonstrates it:

Download  Run Code

Output:

The reverse of the given string is: thgileD eihceT

5. Using character array and swap() method

Following is another efficient way to reverse a string in Java using character array:

  1. Create a character array and initialize it with characters of the given string using String.toCharArray().
  2. Start from the two endpoints l and h of the given string. Run the loop till two endpoints intersect (l <= h). In each iteration of the loop, swap values present at indexes l & h and increment l & decrement h.
  3. Finally, convert the character array into string using String.copyValueOf(char[]) and return.

The following program demonstrates it:

Download  Run Code

Output:

The reverse of the given string is: thgileD eihceT

6. Using + (string concatenation) operator

We can use the string concatenation operator + to reverse a string in Java by reading characters from the end of it and concatenating them at the beginning of a new string. Please note that to increase the performance of repeated string concatenation, a Java compiler may use the StringBuffer class or a similar technique to reduce the total number of intermediate string objects created by evaluation of an expression[1].

Download  Run Code

Output:

The reverse of the given string is: thgileD eihceT

7. Using Unicode Right-to-left override (RLO) character

We can use Unicode Right-to-left override (RLO) character to convert a string into its reverse, as shown below:

Download  Run Code

Output:

The reverse of the given string is: thgileD eihceT

8. Using a Byte Array

The idea is very simple – convert the given string into bytes, and then in-place rearranges the byte array, as shown in Approach #5. Finally, we convert the byte array back into a string.

Download  Run Code

Output:

The reverse of the given string is: thgileD eihceT

9. Using Recursion

As seen in Approach #2, we can easily reverse a string in Java using the stack data structure. As the stack is involved, we can easily convert the code to use the recursion call stack. As string is immutable, we first convert the given string into a character array, then reverse the character array and finally convert the character array back into a string.

Download  Run Code

Output:

The reverse of the given string is: thgileD eihceT

 
The above solution uses a static variable, which is not recommended. We can easily solve this problem without using any static variable. This approach is almost similar to Approach #3 discussed here.

Download  Run Code

Output:

The reverse of the given string is: thgileD eihceT

10. Using substring() method

We can use String.substring(int, int) method to recursively reverse a string in Java. The following code uses the String.charAt(int) method to isolate the first or last character of the string and recur for the remaining string using substring().

⮚ Approach #1

Download  Run Code

Output:

The reverse of the given string is: thgileD eihceT

⮚ Approach #2

Download  Run Code

Output:

The reverse of the given string is: thgileD eihceT

That’s all about different ways to reverse a String in Java.

 
Reference: String Javadoc SE 8