Home Java StringBuilder Methods reverse() Java StringBuilder reverse() Method Definition In Java, sb.reverse() reverses the character sequence inside a StringBuilder in place and returns this. You do not need to write a loop or build a second buffer when flipping text.
Unlike a naive swap loop over char values, reverse() keeps valid UTF-16 surrogate pairs intact so supplementary characters like emoji stay valid after reversal.
An instance method of the java.lang.StringBuilder class (since Java 1.5, JDK 1.5): a final, mutable sequence of UTF-16 characters that is not thread-safe . Call it on an existing builder, as in sb.reverse(...). No import is needed because java.lang is always visible.
Syntax Call reverse() with no arguments on a StringBuilder.
StringBuilder sb = new StringBuilder("Java");
sb.reverse(); // "avaJ" (in place)
String back = new StringBuilder("abc").reverse().toString(); // "cba" Call What it does sb.reverse()Reverses sb in place and returns this. new StringBuilder(s).reverse().toString()Reverses an immutable String in one expression. word.equals(new StringBuilder(word).reverse().toString())Checks whether word is a palindrome.
Because reverse() modifies the existing object, make a copy first (new StringBuilder(sb)) if you still need the original order.
Library declarations // java.lang.StringBuilder
public StringBuilder reverse() Parameters Parameter Description None reverse() takes no parameters.
Return Value Return Value Description StringBuilderA reference to this builder after reversing in place.
Compare with Similar Methods Ways to reverse or reorder characters:
Method How it differs toString()Freezes the reversed builder back into a String. append(x)Often used in a loop before calling reverse() once (e.g., building base digits). Collections.reverse(list)Reverses elements of a List rather than characters of a builder.
Java Examples 1. Reverse in place 2. Palindrome check 3. Surrogate pair preservation Example 1 — Reverse a Builder In Place and Chain Further Edits See how reverse() updates the builder itself and returns this for another method call.
public class StringBuilderReverseInPlace {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Java 8");
StringBuilder same = sb.reverse().append("!");
System.out.println("Reversed: " + sb);
System.out.println("Same object? " + (sb == same));
}
} Reversed: 8 avaJ!
Same object? true How It Works Flip original characters. "Java 8" becomes "8 avaJ" inside the existing buffer.Chain on the result. Because reverse() returns this, .append("!") adds onto the reversed text.Identity check. sb == same is true, confirming no new StringBuilder was created.Example 2 — Check Palindromes With reverse().toString() Compare each test word against a reversed copy without changing the original strings.
public class StringBuilderReversePalindrome {
public static void main(String[] args) {
String[] words = {"radar", "level", "java"};
for (String w : words) {
String reversed = new StringBuilder(w).reverse().toString();
System.out.println(w + " -> " + reversed + " (palindrome: " + w.equals(reversed) + ")");
}
}
} radar -> radar (palindrome: true)
level -> level (palindrome: true)
java -> avaj (palindrome: false) How It Works Wrap and flip. new StringBuilder(w).reverse().toString() produces the reversed text in one expression.Safe content comparison. Using String.equals() compares the text characters cleanly.Original untouched. Each String in words remains immutable and unchanged.Example 3 — How reverse() Preserves UTF-16 Surrogate Pairs Reverse "A\uD83D\uDE00B" and verify with code point hex values that the emoji pair was not broken apart.
public class StringBuilderReverseSurrogate {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("A\uD83D\uDE00B");
sb.reverse();
System.out.println("length = " + sb.length());
System.out.println("cp at 0 = 0x" + Integer.toHexString(sb.codePointAt(0)).toUpperCase());
System.out.println("cp at 1 = 0x" + Integer.toHexString(sb.codePointAt(1)).toUpperCase());
System.out.println("cp at 3 = 0x" + Integer.toHexString(sb.codePointAt(3)).toUpperCase());
}
} length = 4
cp at 0 = 0x42
cp at 1 = 0x1F600
cp at 3 = 0x41 How It Works Pair stays whole. Swapping individual chars would flip high/low surrogates into an invalid sequence; reverse() restores them as one pair.Ends trade places. 'B' (0x42) moves to index 0, and 'A' (0x41) moves to index 3.Valid supplementary point. Reading index 1 with codePointAt(1) returns the full 0x1F600 code point.Frequently Asked Questions Does StringBuilder reverse() create a new StringBuilder? No. It reverses the characters inside the existing StringBuilder in place and returns this.
Does String have a reverse() method? No. To reverse a String in Java, wrap it in a StringBuilder: new StringBuilder(str).reverse().toString().
How does reverse() handle emoji and surrogate pairs? It recognizes valid UTF-16 surrogate pairs and keeps the high surrogate before the low surrogate so supplementary characters remain valid.
How do I check if a string is a palindrome using StringBuilder? Compare the original String with new StringBuilder(s).reverse().toString() using .equals().
Can reverse() ever throw an exception? No. Even calling reverse() on an empty StringBuilder safely does nothing and returns this.
Next: setCharAt() Overwrite one char at an index in place (returns void, so it cannot be chained).
setCharAt() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
15 people found this page helpful
Helpful Share Copy link Suggestion