π 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 :