Java: How to Reverse a String (Step-by-Step Guide)
Introduction β Why Reverse a String?
Reversing a string is a common interview question and an important part of learning string manipulation in Java. Itβs often used in problems related to palindromes, data encryption, and algorithm practice.
In this tutorial, youβll learn:
- How to reverse a string using different methods
- Manual reversal using loops
- Built-in Java APIs (
StringBuilder,Collections) - Edge case handling and performance tips
Method 1: Using StringBuilder.reverse()
public class ReverseString {
public static void main(String[] args) {
String original = "Java";
String reversed = new StringBuilder(original).reverse().toString();
System.out.println("Reversed: " + reversed);
}
}
Explanation:
StringBuilder(original)creates a mutable version of the string.reverse()reverses the characters.toString()converts it back to an immutableString.
Best for quick and efficient solutions.
Method 2: Using a for Loop (Manual Reversal)
public class ReverseStringLoop {
public static void main(String[] args) {
String original = "Developer";
String reversed = "";
for (int i = original.length() - 1; i >= 0; i--) {
reversed += original.charAt(i);
}
System.out.println("Reversed: " + reversed);
}
}
Explanation:
- Starts from the end of the string and builds a new one in reverse order.
- Demonstrates string concatenation logic.
Warning: Not recommended for large strings (inefficient due to immutability of String).
Method 3: Using Character Array
public class ReverseStringArray {
public static void main(String[] args) {
String original = "Program";
char[] chars = original.toCharArray();
for (int i = 0, j = chars.length - 1; i < j; i++, j--) {
char temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
}
System.out.println("Reversed: " + new String(chars));
}
}
Explanation:
- Converts string to array, then swaps characters from both ends.
- Memory-efficient and faster for large strings.
Method 4: Using Collections.reverse() (List-Based)
import java.util.*;
public class ReverseWithCollections {
public static void main(String[] args) {
String original = "Collection";
List<Character> list = new ArrayList<>();
for (char c : original.toCharArray()) {
list.add(c);
}
Collections.reverse(list);
StringBuilder reversed = new StringBuilder();
for (char c : list) {
reversed.append(c);
}
System.out.println("Reversed: " + reversed);
}
}
Explanation:
- Useful if you’re already working with character collections.
Collections.reverse()reverses the order of elements in-place.
Best Practices
- Use
StringBuilder.reverse()for simplicity and speed. - Use manual methods for learning or specific custom needs.
- Avoid
+operator inside loops for large strings. - Validate for null or empty strings before reversing.
Summary β String Reversal in Java
Reversing a string is a core programming task that helps you understand string manipulation, memory management, and loop constructs in Java. Java provides both built-in solutions and manual techniques for different use cases.
Key Takeaways:
- Use
StringBuilder.reverse()for quick results. - Use loops or char arrays for custom logic and learning.
- Always handle empty or null strings safely.
FAQs β Java String Reversal
What is the fastest way to reverse a string in Java?
Use StringBuilder.reverse() β itβs built-in, efficient, and readable.
Can strings be reversed without using additional memory?
No. Since String is immutable, a new object is always created.
How to reverse a string without using StringBuilder?
Use a loop or convert to a character array and swap characters manually.
Is it possible to reverse words in a sentence, not characters?
Yes. Split the string by space, then reverse the array of words.
Share Now :
