Methods to Reverse a String in Java
Approach 1: Traversing the string
The most naive approach to answering the question of how to reverse a string in Java is to reverse it. The idea is to traverse the string from index i = 0 to length-1, keep a temporary string “ans”, and keep adding s[i] to ans as ans = s[i] + ans.
Java implementation
import java.io.*;
import java.util.Scanner;
class Main {
public static void main (String[] args) {
String s= "umbrella", ans="";
/* Traverse the string from i=0 to length-1*/
for (int i=0; i<s.length(); i++)
{
char current_character= s.charAt(i);
/* Keep adding the current character to the ans string in the front */
ans = current_character + ans;
}
/* Print the reversed string*/
System.out.println("Reversed string: "+ ans);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
allerbmu
Complexities
Time complexity
O(n), where n is the length of the input string
Reason: We're traversing the whole string once, taking O(n) time. All the other times taken are constant. Thus, the time complexity to reverse a string in java using this approach is O(n).
Space complexity
O(1)
Reason: All the spaces taken are constant. Thus, the space complexity to reverse a string in java using this approach is O(1).
Approach 2: By converting the string into bytes
Another method to reverse a string in java is by using the getBytes() function. The getBytes() function takes the string and copies its characters into an array. So, the idea is to copy the character of the string into an array (using getBytes()) “temp”, declare another bytes array, “ans”, and then for each from i = 0 to n, ans[i] = temp[n-i-1]. At last, print the ans array.
Java implementation
import java.lang.*;
import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args)
{
String s = "umbrella";
int n = s.length();
/* convert the string into byte array using getBytes() */
byte[] temp = s.getBytes();
byte[] ans = new byte[n];
/* set ans[i] = temp[n-i-1], for each i from 0 to n-1 */
for (int i = 0; i < n; i++)
ans[i] = temp[n - i - 1];
/* Print the ans array */
System.out.println(new String(ans));
}
}

You can also try this code with Online Java Compiler
Run Code
Output
allerbmu
Complexities
Time complexity
O(n), where n is the length of the input string
Reason: For each i from 0 to n-1, we set the value of ans[i]. This for loop takes O(n) time. getBytes() method also takes O(n) time. Thus, the time complexity to reverse a string in Java using this approach is O(n).
Space complexity
O(n), where n is the length of the input string
Reason: The space taken by the ans array is O(n). All the other spaces taken are constant. Thus, the space complexity to reverse a string in Java using this approach is O(n).
Approach 3: By using the in-built reverse() method
There are some in-built java methods that help in answering the question of how to reverse a string in java. The reverse() method can be used to reverse a string in Java. The reverse() method is an in-built method of the StringBuilder class. For implementing this, we need first to convert the input string into the type StringBuilder using the append method of the StringBuilder class and then call the reverse() method on the new StringBuilder object. At last, print the reversed string.
Note: Instead of using the StringBuilder, we can also use StringBuffer here. It works the same way.
Java implementation
import java.lang.*;
import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args)
{
String s = "umbrella";
/* Declare a new StringBuilder object temp */
StringBuilder temp = new StringBuilder();
/* append the string s into temp */
temp.append(s);
/* call the reverse method on temp */
temp.reverse();
/* print the reversed string */
System.out.println(temp);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
allerbmu
Complexities
Time complexity
O(n), where n is the length of the input string
Reason: reverse() method takes O(n) time to reverse the string. Thus, the time complexity to reverse a string in Java using this approach is O(n).
Space complexity
O(n), where n is the length of the input string
Reason: The space taken by temp is O(n). All the other spaces taken are constant. Thus, the space complexity to reverse a string in Java using this approach is O(n).
Approach 4: By converting the string to a character array
The approach to reverse a string in Java is straightforward. We convert the string to a character array using the toCharArray() method. Then, we traverse the character array from the back and print the characters.
Java implementation
import java.lang.*;
import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args)
{
String s = "umbrella";
int n = s.length();
/* convert the input string into a character array */
char[] temp = s.toCharArray();
/* Traverse the array from the back, and keep printing the characters. */
for (int i = n - 1; i >= 0; i--)
System.out.print(temp[i]);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
allerbmu
Complexities
Time complexity
O(n), where n is the length of the input string
Reason: The toCharArray method takes O(n) time to convert the string to an array. Thus, the time complexity to reverse a string in java using this approach is O(n).
Space complexity
O(n), where n is the length of the input string
Reason: The space taken by temp is O(n). All the other spaces taken are constant. Thus, the time complexity to reverse a string in java using this approach is O(n).
Approach 5: By Using toCharArray()
One simple way to reverse a string is by converting it into a character array and swapping elements.
Java Implementation
public class ReverseStringToCharArray {
public static String reverseString(String str) {
char[] charArray = str.toCharArray();
int left = 0, right = charArray.length - 1;
while (left < right) {
// Swap characters
char temp = charArray[left];
charArray[left] = charArray[right];
charArray[right] = temp;
left++;
right--;
}
return new String(charArray);
}
public static void main(String[] args) {
String str = "Hello";
System.out.println(reverseString(str)); // Output: "olleH"
}
}

You can also try this code with Online Java Compiler
Run CodeComplexity Analysis
Time Complexity: O(n)O(n)O(n) – Single pass through the string.
Space Complexity: O(n)O(n)O(n) – Due to the character array storage.
Reasoning:
toCharArray() helps in easy manipulation since strings in Java are immutable.
Efficient in terms of time and space compared to creating multiple substrings.
Approach 6: By Using a While Loop or For Loop
Using a simple loop to construct the reversed string character by character.
Java Implementation
public class ReverseStringLoop {
public static String reverseString(String str) {
String reversed = "";
for (int i = str.length() - 1; i >= 0; i--) {
reversed += str.charAt(i); // String concatenation
}
return reversed;
}
public static void main(String[] args) {
String str = "Java";
System.out.println(reverseString(str)); // Output: "avaJ"
}
}

You can also try this code with Online Java Compiler
Run CodeComplexity Analysis
Time Complexity: O(n2)O(n^2)O(n2) – Due to string concatenation (Strings are immutable).
Space Complexity: O(n)O(n)O(n) – Stores reversed string.
Reasoning:
Using StringBuilder instead of String for concatenation would improve performance.
Not the most efficient approach due to immutable string handling.
Approach 7: By Using ArrayList Object
Using ArrayList and Collections.reverse() method.
Java Implementation
import java.util.ArrayList;
import java.util.Collections;
public class ReverseStringArrayList {
public static String reverseString(String str) {
ArrayList<Character> charList = new ArrayList<>();
for (char c : str.toCharArray()) {
charList.add(c);
}
Collections.reverse(charList);
StringBuilder reversed = new StringBuilder();
for (char c : charList) {
reversed.append(c);
}
return reversed.toString();
}
public static void main(String[] args) {
String str = "World";
System.out.println(reverseString(str)); // Output: "dlroW"
}
}

You can also try this code with Online Java Compiler
Run CodeComplexity Analysis
Time Complexity: O(n)O(n)O(n) – Iteration and reversing.
Space Complexity: O(n)O(n)O(n) – Storage in ArrayList.
Reasoning:
The ArrayList allows for easy reversal using Collections.reverse().
However, extra space usage makes it less efficient than other methods.
Approach 8: By Using StringBuffer
StringBuffer has a built-in reverse() method.
Java Implementation
public class ReverseStringBuffer {
public static String reverseString(String str) {
return new StringBuffer(str).reverse().toString();
}
public static void main(String[] args) {
String str = "Buffer";
System.out.println(reverseString(str)); // Output: "reffuB"
}
}

You can also try this code with Online Java Compiler
Run CodeComplexity Analysis
Time Complexity: O(n)O(n)O(n) – Efficient reversing mechanism.
Space Complexity: O(n)O(n)O(n) – Stores reversed string.
Reasoning:
StringBuffer is mutable, making it faster than manual concatenation.
Thread-safe but slightly slower than StringBuilder.
Approach 9: By Using Stack
Using Stack data structure (LIFO).
Java Implementation
import java.util.Stack;
public class ReverseStringStack {
public static String reverseString(String str) {
Stack<Character> stack = new Stack<>();
for (char c : str.toCharArray()) {
stack.push(c);
}
StringBuilder reversed = new StringBuilder();
while (!stack.isEmpty()) {
reversed.append(stack.pop());
}
return reversed.toString();
}
public static void main(String[] args) {
String str = "Stack";
System.out.println(reverseString(str)); // Output: "kcatS"
}
}

You can also try this code with Online Java Compiler
Run CodeComplexity Analysis
Time Complexity: O(n)O(n)O(n) – Pushing and popping elements.
Space Complexity: O(n)O(n)O(n) – Stack storage.
Reasoning:
Stack follows Last-In-First-Out (LIFO), making it easy to reverse strings.
Extra space usage makes it less optimal.
Approach 10: By Using substring() Method
Using recursion with substring().
Java Implementation
public class ReverseStringSubstring {
public static String reverseString(String str) {
if (str.isEmpty()) {
return str;
}
return reverseString(str.substring(1)) + str.charAt(0);
}
public static void main(String[] args) {
String str = "Recursion";
System.out.println(reverseString(str)); // Output: "noisruceR"
}
}

You can also try this code with Online Java Compiler
Run CodeComplexity Analysis
Time Complexity: O(n2)O(n^2)O(n2) – Due to substring creation.
Space Complexity: O(n)O(n)O(n) – Recursive call stack.
Reasoning
Uses recursion, but inefficient due to new substring creation in each call.
Not suitable for large strings due to stack overflow risk.
Frequently Asked Questions
How do I reverse a string in Java?
You can create a StringBuilder with your text, call its reverse() method, then convert back to a string using toString().
Can you use reverse() on a string?
Direct call on String isn’t possible. Instead, wrap the string in StringBuilder or StringBuffer, use reverse(), and get a new string.
What is the getByte() method in Java?
In Java, the getByte() method decodes a string into a sequence of bytes and stores the character in a byte array.
What is the toCharArray() method in Java?
The toCharArray() method converts a string into a character array. It stores the characters of the string in an array.
Conclusion
In this article, we discussed how to reverse a string in Java. We hope that this blog has enhanced your knowledge of converting a string to an int in Java.
Recommended Readings: