In this article, we will learn how to reverse a string in Java. We can reverse a string either by using the built-in StringBuilder class, or by implementing an algorithm to reverse the string manually.

Using StringBuilder.reverse() to reverse a string

The java.util.StringBuilder class provides a number of methods for string manipulation. To reverse a string, we can use the reverse() method provided by the StringBuilder class, as shown below:

String string = "myCompiler";
String reversedString = new StringBuilder(string).reverse().toString();
System.out.println(reversedString); // prints: relipmoCym

Using a loop to reverse a string

To reverse a string manually, we can convert the string into an array of characters. Assuming the length of the original string is n, the length of the array of characters would also be n. Next, we can use a loop to swap the characters on the left with its corresponding characters on the right. So, we would swap the 0th character with the (n - 1)th character, the 1st character with the (n - 2)th character, and the ((n / 2) - 1)th character with the ((n - 2) + 1)th character. Then, we can convert the character array with swapped characters into a string.

String string = "myCompiler";

char[] arr = string.toCharArray();
for (int n = arr.length, i = 0; i < n / 2; i++) {
    char tmp = arr[i];
    arr[i] = arr[n - i - 1];
    arr[n - i - 1] = tmp;
}

String reversedString = new String(arr);
System.out.println(reversedString); // prints: relipmoCym

Another way to reverse a string is to iterate through the string of length n in reverse order (from the index n - 1 to 0), and use string concatenation to build the reversed string.

String string = "myCompiler";

String reversedString = "";
for (int i = string.length() - 1; i >= 0; i--) {
    reversedString += string.charAt(i);
}

System.out.println(reversedString); // prints: relipmoCym

Even though this method may look simple, this method of reversing a string is inefficient. Since String objects in Java is immutable, so using the + or += operators on a String creates a new object. For a string of length n, this causes the generation of n unreachable objects in memory, causing high memory usage. These unreachable objects remain in memory till the garbage collector kicks in.

To avoid this issue, we can append the characters in reverse order into a StringBuilder. The StringBuilder is a mutable object which can be used to build strings incrementally.

String s = "myCompiler";
StringBuilder sb = new StringBuilder();

for (int i = s.length() - 1; i >= 0; i--) {
    sb.append(s.charAt(i));
}

String reversedString = sb.toString();
System.out.println(reversedString); // prints: relipmoCym

Using a loop to print the reverse of a string

If we are only asked to print the reverse of a string, we can do so without having to maintain an intermediate String object, StringBuilder object, or character array. We can simply iterate the string in the reverse order and print the characters one by one, as shown below:

String string = "myCompiler";

for (int i = string.length() - 1; i >= 0; i--) {
    System.out.print(string.charAt(i));
}
// prints: relipmoCym

Here, we've used the System.out.print() method instead of System.out.println() method, since the latter would print each character on its own line.