Java Program to Reverse a String
Program to reverse a string in Java
In this article we will learn how to reverse string in Java
Reversing a string is a technique so that the 1st character becomes the last character and so on.
Suppose we have a string called “prepinsta” so we have to find reverse of that string is “atsniperp” we can do this in two way by using reverse method of StringBuilder class and by using normal for loop with charAt() method.

Reverse a String in Java
Reversing a string is a common programming task that helps in understanding Java’s String handling, manipulation, and built in methods. Whether you’re preparing for coding interviews or brushing up on Java fundamentals, mastering different ways to reverse a string is essential.

Method 1: Using String Builder’s reverse() Method
The easiest and most efficient way to reverse string in Java is using the StringBuilder class.
Code:
class ReverseString { public static void main(String[] args) { String original = "PrepInsta"; StringBuilder sb = new StringBuilder(original); String reversed = sb.reverse().toString(); System.out.println("Reversed String: " + reversed); } }
Output:
Reversed String: atsnIperP
Why this works:
- StringBuilder is mutable and has a built-in .reverse() method.
- It is fast and memory efficient.
Method 2: Using a For Loop (Manual Reversal)
Ideal for beginners who want to understand how strings work at a character level.
Code:
class ReverseString { public static void main(String[] args) { String original = "PrepInsta"; String reversed = ""; for (int i = original.length() - 1; i >= 0; i--) { reversed = reversed + original.charAt(i); } System.out.println("Reversed String: " + reversed); } }
Output:
Reversed String: atsnIperP
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Method 3: Using Character Array
Convert the string into an array of characters and reverse it.
Code:
class ReverseString { public static void main(String[] args) { String original = "PrepInsta"; char[] characters = original.toCharArray(); System.out.print("Reversed String: "); for (int i = characters.length - 1; i >= 0; i--) { System.out.print(characters[i]); } } }
atsnIperP
Why use this:
- Directly manipulates character data.
- Useful in memory sensitive environments.
Method 4: Using Recursion
A more academic method that uses the concept of recursion.
Code:
class ReverseString { 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) { String original = "PrepInsta"; String reversed = reverse(original); System.out.println("Reversed String: " + reversed); } }
Output:
atsnIperP
Method 5: Using Collections.reverse() (for List of Characters)
This method is useful when you are already working with a List<Character>.
Code:
import java.util.*; class ReverseString { public static void main(String[] args) { String original = "PrepInsta"; ListcharList = new ArrayList<>(); for (char c : original.toCharArray()) { charList.add(c); } Collections.reverse(charList); System.out.print("Reversed String: "); for (char c : charList) { System.out.print(c); } } }
Output:
atsnIperP
Final Thoughts
Reversing a string in Java may seem simple, but knowing multiple ways to approach the problem prepares you for various real-world and interview scenarios. Always choose the method best suited to your use case whether it’s performance, readability, or just academic learning.
FAQs
The most efficient way is using StringBuilder or StringBuffer with the .reverse() method. It’s fast, uses less memory, and is preferred in real world applications.
Yes, you can reverse a string manually using loops or by converting it into a character array. These methods are great for understanding how strings work internally.
You can reverse a string in Java using a for loop by iterating from the end to the start and appending characters. This method helps beginners understand string manipulation without built-in methods.
To reverse words in a string in Java, split the string by spaces, reverse the word array, and join them back. This is useful for problems like reversing sentence word order.
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
public class Reverse_String {
public static void main(String[] args) {
String s1=”Welcome”;
String rev=””;
for(int i=s1.length()-1;i>=0;i–)
{
rev=rev+s1.charAt(i);
}
System.out.println(“Reversed string is”);
System.out.println(rev);
}
}
public class ReverseFunction {
public static String reverse(String str){
String temp = “”;
for (int i=0; i<str.length(); i++){
temp = str.charAt(i) + temp;
}
return temp;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the String : ");
String string = scanner.next();
String reverse = reverse(string);
if (string.equals(reverse)){
System.out.println("Palindrome");
}else
System.out.println("Not");
}
}
public class ReverseString {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(“Enter the String : “);
String string = scanner.next();
String reverse = “”;
for (int i=0; i<string.length(); i++){
reverse = string.charAt(i) + reverse;
}
System.out.println("Reverse = " + reverse);
}
}
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
String rev=””;
for(int i=s.length()-1;i>=0;i–){
char c=s.charAt(i);
rev+=c;
}
System.out.print(rev);
}
}
We can also reverse a string using StringBuffer :
public class StringBufferReverseExample {
public static void main(String[] args) {
String input = “Hello world”;
StringBuffer sb = new StringBuffer(input);
sb.reverse();
String reversed = sb.toString();
System.out.println(reversed);
}
}
import java.util.*;
public class Main
{
public static void main(String[] args) {
String name = “chinmai”;
String res = “”;
Stack s = new Stack();
for(char c : name.toCharArray()){
s.add(c);
}
while(!s.isEmpty()){
res+=s.pop();
}
System.out.println(res);
}
}