How to Reverse a String in Java
Fast recall
Quick revision
Core answer
new StringBuilder(s).reverse().toString() is concise and runs in O(n) time. A manual char[] two-pointer solution also runs in O(n) time but the array itself uses O(n) extra space. Define what 'character' means: char reversal can split Unicode surrogate pairs, while StringBuilder.reverse() preserves valid surrogate pairs; grapheme clusters such as emoji sequences need a Unicode text library.Whether you know the StringBuilder one-liner and can also implement the manual approach if asked to avoid built-in methods.
Trying to modify the original String character by character — String is immutable, you cannot do that. You must either use StringBuilder or build a new String.
Mention that you can also use a char array and swap in place for O(n) extra space for the character array. Also cover the palindrome check follow-up: compare the string to its reverse.
Interview-ready guide
Interview answer
I'd reach for the built-in first: new StringBuilder(str).reverse().toString(). You wrap the String in a mutable buffer, flip it, and turn it back into a String. It's one line, it's O(n) in time and space, and it's what most interviewers want to hear — so I'd say it, then offer to do it by hand if they want the mechanics.
If they ban StringBuilder, I'd switch to a two-pointer swap on a char[]. Convert with toCharArray(), put one pointer at the start and one at the end, swap them, and move both inward until they cross. That's still O(n) time but O(1) extra space, which is the better answer when they're probing for space efficiency. The thing I'd watch is the loop bound — while (left < right), not <=, otherwise on an odd-length string you swap the middle character with itself and waste a step.
For edge cases I'd guard null so I don't hit a NullPointerException, and note that an empty or single-character string is already its own reverse. One important edge case is Unicode: a simple char[] swap can split surrogate pairs such as emoji, while StringBuilder.reverse() preserves valid surrogate pairs. The built-in is therefore both shorter and safer for that case.
How to Reverse a String in Java
Direct Answer
For ordinary Java text, new StringBuilder(s).reverse().toString() is concise and runs in O(n) time. A manual char[] two-pointer solution also runs in O(n) time but the array itself uses O(n) extra space. Define what 'character' means: char reversal can split Unicode surrogate pairs, while StringBuilder.reverse() preserves valid surrogate pairs; grapheme clusters such as emoji sequences need a Unicode text library.
Code Example
public class ReverseString {
// Approach 1: StringBuilder built-in — preferred
static String reverseBuiltin(String s) {
return new StringBuilder(s).reverse().toString();
}
// Approach 2: Loop from end
static String reverseLoop(String s) {
StringBuilder sb = new StringBuilder();
for (int i = s.length() - 1; i >= 0; i--) {
sb.append(s.charAt(i));
}
return sb.toString();
}
// Approach 3: char array swap — O(n) extra space for the character array
static String reverseSwap(String s) {
char[] chars = s.toCharArray();
int left = 0, right = chars.length - 1;
while (left < right) {
char temp = chars[left];
chars[left] = chars[right];
chars[right] = temp;
left++;
right--;
}
return new String(chars);
}
public static void main(String[] args) {
System.out.println(reverseBuiltin("hello")); // olleh
System.out.println(reverseLoop("hello")); // olleh
System.out.println(reverseSwap("hello")); // olleh
// Palindrome check — compare string to its reverse
String word = "racecar";
boolean isPalindrome = word.equals(reverseBuiltin(word));
System.out.println(isPalindrome); // true
}
}
Common Mistakes & Pitfalls
- Trying to mutate the original
String—Stringis immutable, so there is no method to change a character in place; reaching for an in-place edit is the classic beginner error. Use aStringBuilderor achar[]instead. - Concatenating with
+inside a loop —result += s.charAt(i)looks harmless but allocates a freshStringevery iteration, making the whole thing O(n²). Append to aStringBuilderinstead. - Off-by-one in the manual loop or swap — starting the index at
s.length()instead oflength() - 1throwsStringIndexOutOfBoundsException; usingwhile (left <= right)re-swaps the middle character on odd-length strings. The correct bound isleft < right. - Assuming character reversal is Unicode-safe — a hand-rolled
char[]swap corrupts surrogate pairs (emoji, some CJK).StringBuilder.reverse()handles them correctly, which is why it is the safer default.
Check your understanding
Answer these without looking back at the article.
- How do you check if a String is a palindrome in Java?
- How would you reverse only the words in a sentence, not each word itself?
- What is the time complexity of reversing a String?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.