Next-Gen App & Browser Testing Cloud
Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

The simplest way to print a string in reverse in Java is to wrap it in a StringBuilder and call reverse(): new StringBuilder(str).reverse().toString(). Because a String is immutable you cannot reverse it in place, so every technique builds a new sequence of characters. Beyond the one-liner you can convert the string to a char array and swap from both ends with two pointers, loop from the last index while appending each character, or solve it with recursion.
In Java, a String object can never be changed after it is created. Methods like substring() or concat() do not mutate the original; they return a brand new String. That is why you cannot "reverse the string itself" and instead build a new one. It is also why concatenating characters with += inside a loop is wasteful: each iteration allocates a fresh String, giving O(n^2) behavior. Using a StringBuilder (or its thread-safe sibling StringBuffer) avoids that overhead by mutating a single internal buffer.
This is the cleanest and fastest approach to write. Key points:
public class ReverseString {
public static void main(String[] args) {
String str = "LambdaTest";
String reversed = new StringBuilder(str).reverse().toString();
System.out.println("Original: " + str);
System.out.println("Reversed: " + reversed);
}
}Output:
Original: LambdaTest
Reversed: tseTadbmaLWhen you want to see the underlying algorithm, swap characters in place on a char array. Key points:
public class ReverseTwoPointer {
public static void main(String[] args) {
String str = "LambdaTest";
char[] chars = str.toCharArray();
int left = 0, right = chars.length - 1;
while (left < right) {
char temp = chars[left];
chars[left] = chars[right];
chars[right] = temp;
left++;
right--;
}
System.out.println(new String(chars));
}
}Output:
tseTadbmaLIterating from the last character to the first is the most explicit version and is easy to extend with custom logic. Key points:
public class ReverseLoop {
public static void main(String[] args) {
String str = "LambdaTest";
StringBuilder rev = new StringBuilder();
for (int i = str.length() - 1; i >= 0; i--) {
rev.append(str.charAt(i));
}
System.out.println(rev.toString());
}
}Output:
tseTadbmaLRecursion gives an elegant, concise solution that is popular in interviews. Key points:
public class ReverseRecursion {
public static String reverse(String str) {
if (str.isEmpty()) {
return str;
}
return reverse(str.substring(1)) + str.charAt(0);
}
public static void main(String[] args) {
System.out.println(reverse("LambdaTest"));
}
}Output:
tseTadbmaLReversing characters turns "Hello World" into "dlroW olleH". A common follow-up is to reverse the order of words while keeping each word readable. Split on whitespace, walk the array backwards, and join the words with spaces.
public class ReverseWords {
public static void main(String[] args) {
String sentence = "Test on real devices with LambdaTest";
String[] words = sentence.split(" ");
StringBuilder result = new StringBuilder();
for (int i = words.length - 1; i >= 0; i--) {
result.append(words[i]);
if (i > 0) {
result.append(" ");
}
}
System.out.println(result.toString());
}
}Output:
LambdaTest with devices real on Test| Approach | Time | Space | Use when |
|---|---|---|---|
| StringBuilder.reverse() | O(n) | O(n) | Default; cleanest and fastest to write. |
| char[] two-pointer | O(n) | O(n) | You want explicit control or to learn the algorithm. |
| Loop + StringBuilder | O(n) | O(n) | You need custom logic while reversing. |
| Recursion | O(n) | O(n) stack | Teaching recursion; avoid for very long input. |
| Reverse words | O(n) | O(n) | You need word order reversed, not characters. |
String utilities like these often sit inside larger automation suites. If your Java or Selenium tests assert on reversed or formatted strings, you can run them across thousands of browser and OS combinations on the Selenium Automation cloud to confirm the behavior is consistent everywhere.
Wrap the string in a StringBuilder and call reverse(): new StringBuilder(str).reverse().toString(). It is one line, runs in O(n) time, and is the approach most Java developers reach for first.
No. String is immutable, so its internal characters cannot be modified. To swap characters in place you must first convert the string to a char array with toCharArray(), reverse that array, then build a new String from it.
Use a char array with a two-pointer swap, iterate the string backwards with a for loop, or use recursion. All three avoid StringBuilder.reverse() while still producing the reversed string in linear time.
Every standard approach runs in O(n) time because each character is visited once, and they use O(n) extra space for the new string or array. Recursion additionally consumes O(n) call-stack depth, which can overflow on very long inputs.
Split the sentence on whitespace with split(" "), iterate the resulting array from the last element to the first, and join the words back with spaces. This reverses word order while keeping each word readable, unlike character reversal.
For surrogate pairs, yes. StringBuilder.reverse() keeps high and low surrogates together, so most emoji and supplementary characters reverse safely. Sequences built from combining characters can still render unexpectedly, but the underlying code points are not corrupted.
KaneAI - Testing Assistant
World’s first AI-Native E2E testing agent.

TestMu AI forEnterprise
Get access to solutions built on Enterprise
grade security, privacy, & compliance