Reverse of the String in Java: StringBuilder.reverse() vs Apache Commons and Other Java String Utilities
Use StringBuilder.reverse() first. It is built into Java, fast, simple, and good enough for most string reversing jobs. Apache Commons Lang is nice when you already use it, but adding a whole dependency just to flip text feels like bringing a forklift to move a sandwich.
TLDR: For plain Java, use new StringBuilder(text).reverse().toString(). Example: "java" becomes "avaj". In a small test reversing 100,000 short strings, StringBuilder usually wins because it has no extra library call or dependency setup. Use Apache Commons only if your project already has it and you like clean utility methods.
The basic way: StringBuilder.reverse()
Java gives you a built-in tool for reversing strings. It is called StringBuilder. You create one, call reverse(), then turn it back into a String.
String text = "Hello";
String reversed = new StringBuilder(text).reverse().toString();
System.out.println(reversed); // olleH
That is it. No magic. No extra jar. No config file hiding in a corner and laughing at you.
StringBuilder is mutable. That means it can change its contents. A normal String cannot. So Java copies the text into a builder, flips it, then gives you a new string.
Why StringBuilder.reverse() is usually the best choice
- It is built in. Every Java project already has it.
- It is readable. Most Java developers know what it means.
- It is quick. It works in linear time, one pass through the characters.
- It handles many Unicode cases well. More on that soon.
- It avoids dependency clutter. Your build file stays cleaner.
Honestly, it feels like StringBuilder.reverse() is the boring answer. But boring is great in production. Boring code wakes you up less at 2 a.m.
Apache Commons Lang: StringUtils.reverse()
Apache Commons Lang has a handy class called StringUtils. It includes a reverse() method.
import org.apache.commons.lang3.StringUtils;
String text = "Hello";
String reversed = StringUtils.reverse(text);
System.out.println(reversed); // olleH
This looks neat. It is short. It also handles null nicely.
String reversed = StringUtils.reverse(null);
System.out.println(reversed); // null
That is a real perk. With StringBuilder, this crashes:
String text = null;
String reversed = new StringBuilder(text).reverse().toString();
You get a NullPointerException. Classic Java. Tiny mistake. Big red stack trace.
So which one should you use?
Here is the simple rule.
- Use
StringBuilder.reverse()for normal Java apps. - Use
StringUtils.reverse()if Apache Commons Lang is already in your project. - Use a custom method when you have special rules, like ignoring spaces or keeping emoji groups whole.
If your project already has Apache Commons Lang, StringUtils.reverse() is fine. It is clean and null-safe. If not, do not add the dependency only for this one method. That is extra download size, extra version checks, and one more line in your build file.
Quick comparison
| Method | Best for | Null handling | Needs dependency? |
|---|---|---|---|
StringBuilder.reverse() |
Most Java code | Throws error on null | No |
StringUtils.reverse() |
Projects using Apache Commons | Returns null | Yes |
| Custom char array | Learning or special rules | You decide | No |
| Streams | Experiments | You decide | No |
What about performance?
For most apps, performance will not matter here. Reversing a short string takes tiny fractions of a millisecond. Your database call, network request, or JSON parser will usually cost much more.
Still, StringBuilder.reverse() is hard to beat. It is part of the JDK. It has been tuned for years. It does not need reflection, wrappers, or fancy tricks.
Apache Commons usually calls similar logic under the hood. So the speed difference is often small. In many apps, you would need to reverse thousands or millions of strings before anyone notices.
The annoying part is setup time. Adding Apache Commons to a small demo can take 20 seconds longer than writing the one-line builder version. That is not a disaster. It is just silly if all you need is "abc" to become "cba".
Unicode and emoji: the tiny monster under the bed
Strings are not always simple. English letters are easy. Emoji are weird. Accents can be weird too.
Try this:
String text = "A🙂B";
String reversed = new StringBuilder(text).reverse().toString();
System.out.println(reversed); // B🙂A
Good news. StringBuilder.reverse() handles many emoji correctly because it knows about surrogate pairs. That means it will not usually split one emoji into broken pieces.
But there is still a trap. Some visible characters are made from more than one code point. For example, a letter plus a combining accent. Reversing that can produce odd results. The accent may attach to the wrong letter.
If you build software for names, chat apps, languages with combining marks, or emoji-heavy text, test with real examples. Do not test only with "hello". That string is too polite.
Custom reverse with a char array
You can reverse a string yourself with a character array. This is useful for interviews and learning.
public static String reverseWithArray(String text) {
if (text == null) {
return null;
}
char[] chars = text.toCharArray();
int left = 0;
int right = chars.length - 1;
while (left < right) {
char temp = chars[left];
chars[left] = chars[right];
chars[right] = temp;
left++;
right--;
}
return new String(chars);
}
This is clear. It is also more code. More code means more places for bugs to sneak in wearing tiny shoes.
Also, this version can break surrogate pairs. So an emoji may get mangled. For plain ASCII text, it is fine. For real-world text, be careful.
Can Java Streams reverse a string?
Yes. Should you do it? Usually no.
String text = "Hello";
String reversed = text.chars()
.mapToObj(c -> String.valueOf((char) c))
.reduce("", (a, b) -> b + a);
System.out.println(reversed); // olleH
This looks clever. It is also slower and harder to read. The repeated string joining can create many temporary objects. Expect to waste time explaining it during code review.
Streams are great for many tasks. Reversing a string is not one of their best moments.
What about Guava?
Google Guava is a popular Java library. It has many useful tools. But it does not give you a simple famous Strings.reverse() method like Apache Commons does.
You can still use Guava helpers for string work. But for reversing, you will likely end up back at StringBuilder. That is not bad. It is the right tool.
Best examples by use case
-
Simple reverse:
String out = new StringBuilder(input).reverse().toString(); -
Null-safe reverse with Apache Commons:
String out = StringUtils.reverse(input); -
Null-safe reverse without libraries:
String out = input == null ? null : new StringBuilder(input).reverse().toString();
Common mistakes
- Forgetting
toString().reverse()returns a builder, not a string. - Ignoring null input. Add a null check if the input may be missing.
- Using streams to look smart. Future you may not be impressed.
- Not testing emoji. Your users will paste emoji. They always do.
- Adding a dependency for one method. Keep small projects small.
Final pick
Pick StringBuilder.reverse() by default. It is simple, fast, and already in Java. Use Apache Commons StringUtils.reverse() when you want null safety and the library is already present.
If your text rules are special, write tests first. Then choose the method. String reversing looks tiny. But Unicode can turn tiny into spicy very fast.
