ALL >> Computer-Programming >> View Article
Different Ways To Reverse A String In Java
In this topic, we will learn about different ways to reverse a string in Java with examples.
different_ways_to_reverse_a_string_in_java
There are the following ways to reverse a String in Java:
• Using the String’s charAt() method
• Using the String’s toCharArray() method
• Using the String’s getBytes() method
• Using the StringBuilder’s reverse() method
• Using the StringBuffer’s reverse() method
Using the String’s charAt() method
We can reverse a String using the charAt() method of the String class. A String object is immutable and doesn’t have the in-built reverse() method. StringBuilder and StringBuffer classes have in-built reverse() methods.
Example
In this example, we will get each character of the String and add them to the font of the new String.
public class SpringJava{
public static void main (String[] args) {
String str= "springjava", nstr="";
char ch;
System.out.println("Original String: "+str);
for(int i=0; i= 0; i--)
System.out.print(charArr[i]);
...
... }
}
Output
avajgnirps
Using the String’s getBytes() method
We can reverse a String by using the getBytes() method in Java's String class.
Example
In this example, we will convert String to byte array to reverse String.
public class SpringJava {
public static void main(String[] args){
String input = "springjava";
//converting String to byte array
byte[] bytSArr = input.getBytes();
byte[] bytArr = new byte[bytSArr.length];
for (int i = 0; i < bytSArr.length; i++)
bytArr[i] = bytSArr[bytSArr.length - i - 1];
System.out.println(new String(bytArr));
}
}
Output
avajgnirps
Using the StringBuilder’s reverse() method
We can reverse a String by using the in-built reverse() method of the StringBuilder class in Java.
Example
In this example, we will take String and StringBuilder then append String to StringBuilder and apply the reverse() method of the StringBuilder to reverse String.
public class SpringJava {
public static void main(String[] args){
String input = "springjava";
StringBuilder sb = new StringBuilder();
//appending a string into StringBuilder
sb.append(input);
//reverse StringBuilder
sb.reverse();
//printing reversed String
System.out.println(sb);
}
}
Output
avajgnirps
Using the StringBuilder’s reverse() method
We can reverse String by using the in-built reverse() method of the StringBuffer class in Java.
Example
In this example, we will convert String to StringBuffer and then apply the reverse() method to reverse String in Java.
public class SpringJava {
public static void main(String[] args){
String str = "springjava";
//converting a String object to StringBuffer
StringBuffer sbr = new StringBuffer(str);
//to reverse that string
sbr.reverse();
System.out.println(sbr);
}
}
Output
avajgnirps
FAQ
Q1: How a string is reversed in Java?
Ans. The string class does have an in-built reverse() method but the StringBuffer class has a reverse() method. We can convert String to StringBuffer and then use the reverse() method to reverse a String in Java.
Q2: How do you reverse a word in Java?
Ans. Using the StringBuilder class's reverse() method, we can reverse a word in Java.
Conclusion
In this topic, we learned different ways to reverse String in Java
Add Comment
Computer Programming Articles
1. Fast-track Web Development Course At Tcci - Best Computer TrainingAuthor: TCCI - Tririd Computer Coaching Institute
2. Hiring Android Programmers And The Changing Nature Of Mobile Communication
Author: Shree Kumar
3. React Native Tutorial: Everything You Need To Build Modern Mobile Apps
Author: Tech Point
4. Artificial Intelligence Tutorial: A Complete Guide To Understanding Ai Concepts
Author: Tech Point
5. Why Education App Development Matters For Kids In Modern Tech Era 2026
Author: Kuldeep Singh Tomar
6. Stop Experimenting, Start Profiting: How Azure Ai 2026 Delivers Real Business Roi
Author: ECF Data
7. Advanced C Programming Tips & Tricks | Programming Classes
Author: TCCI - Tririd Computer Coaching Institute
8. Step-by-step Angular 8 Tutorial: Features, Benefits, And Easy Learning Guide
Author: Tech Point
9. Reactjs Tutorial Explained: Learn Components, State, And Ui Building
Author: Tech Point
10. Python With Django For Backend Development: Best Computer Classes
Author: TCCI - Tririd Computer Coaching Institute
11. Explore Latest Cybersecurity Trends In 2026
Author: Orson Amiri
12. Master Data Structures (dsa) For Coding Tests - Tcci
Author: TCCI - Tririd Computer Coaching Institute
13. Full Stack Web Dev: Mern Stack Course Iskcon Cross Road, Ahmedabad
Author: TCCI - Tririd Computer Coaching Institute
14. Robusta Coffee Beans: The Bold Brew Driving Global Coffee Culture
Author: komal
15. Legacy System Migration: A Comprehensive Guide To Modernization
Author: Tech Gazebos






