Java StringBuilder reverse() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.lang.StringBuilder

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.

Java
StringBuilder sb = new StringBuilder("Java");
sb.reverse();                                    // "avaJ" (in place)
String back = new StringBuilder("abc").reverse().toString(); // "cba"
CallWhat 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
// java.lang.StringBuilder
public StringBuilder reverse()

Parameters

ParameterDescription
Nonereverse() takes no parameters.

Return Value

Return ValueDescription
StringBuilderA reference to this builder after reversing in place.

Compare with Similar Methods

Ways to reverse or reorder characters:

MethodHow 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

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.

Java
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));
    }
}

How It Works

  1. Flip original characters. "Java 8" becomes "8 avaJ" inside the existing buffer.
  2. Chain on the result. Because reverse() returns this, .append("!") adds onto the reversed text.
  3. 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.

Java
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) + ")");
        }
    }
}

How It Works

  1. Wrap and flip. new StringBuilder(w).reverse().toString() produces the reversed text in one expression.
  2. Safe content comparison. Using String.equals() compares the text characters cleanly.
  3. 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.

Java
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());
    }
}

How It Works

  1. Pair stays whole. Swapping individual chars would flip high/low surrogates into an invalid sequence; reverse() restores them as one pair.
  2. Ends trade places. 'B' (0x42) moves to index 0, and 'A' (0x41) moves to index 3.
  3. Valid supplementary point. Reading index 1 with codePointAt(1) returns the full 0x1F600 code point.

Frequently Asked Questions

No. It reverses the characters inside the existing StringBuilder in place and returns this.
No. To reverse a String in Java, wrap it in a StringBuilder: new StringBuilder(str).reverse().toString().
It recognizes valid UTF-16 surrogate pairs and keeps the high surrogate before the low surrogate so supplementary characters remain valid.
Compare the original String with new StringBuilder(s).reverse().toString() using .equals().
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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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