How to Reverse a String in Java
Introduction
Today we'll look three different ways of how to reverse a String in Java.
Even though this task doesn't often come up in real-world applications, chances are, you may easily face this task in an interview session when you apply for a Java developer position.
StringBuilder with For Loop
Perhaps this is the most obvious solution:
public void reverseString(String word) {
if (word == null || word.isEmpty()) {
return;
}
StringBuilder sb = new StringBuilder();
for (int i = word.length() - 1; i >= 0; i--) {
sb.append(word.charAt(i));
}
System.out.println(sb.toString());
}

Let's try to explain the code how it works. First, we create a StringBuilder object where we store the result. Then, starting from the very last position in the string, character by character, we go back to the first position in the string and add each character to our StringBuilder instance pointed by variable sb.
Now we have a StringBuilder object containing the characters in the reverse order. The toString() method will take care of converting it to a String instance that finally we can display on the standard input.
Though it’s a very simple task, there might be at least two typical questions:
Why do you use
StringBuilder?You could've also created a new
Stringand concatenated the characters to it!
Answer: Yes, but String is an immutable type — that is, you cannot modify an existing String object.
In other words, every time when you concatenate it, you actually create a brand new String instance.
Concatenating String types turns to be an expensive operation and we would like to avoid it if possible.
However, the recent Java compilers may be able to recognize the concatenations under the hood and convert them to StringBuilder objects. This combines the best of both worlds: There’s no performance penalty for using concatenations and you can also have a clear, verbose-less syntax.
Why do you start the loop from
word.length() - 1? Why notword.length()?
Answer: Because, as in many other programming languages, the indexing of the string starts from zero. So the position length() - 1 actually refers to the last element.
Using char Array
Here's another solution to reverse a String in Java: Let's first create an array of characters from the input string, then reverse (swap) the characters in the char array in-place.
public void reverseString(String word) {
if (word == null || word.isEmpty()) {
return;
}
char[] arr = word.toCharArray();
char aux;
int end = arr.length - 1;
for (int begin = 0; begin < end; begin++, end--) {
aux = arr[end];
arr[end] = arr[begin];
arr[begin] = aux;
}
System.out.println(new String(arr));
}

The reverse Method of StringBuilder
And here’s a bonus, one-liner solution to this problem:
public void reverseStringAlternative(String word) {
if (word == null || word.isEmpty()) {
return;
}
System.out.println(new StringBuilder(word).reverse().toString());
}

The StringBuilder class, unlike the String class, happens to have a reverse() method that does the most of the work for us.
Also, regarding this solution it's worth noting that it also works with StringBuffer, however, it's not recommended — StringBuffer is not thread-safe.
Summary
In this post we've seen 3 ways of reversing a string in Java. It's unlikely that you'll encounter such tasks in your daily job as a programmer, however, this is a great task when you're practicing for an interview or just want to practice Java in general.