About this question
How do you reverse a string in Java, and what are the different ways to achieve it? Java provides multiple approaches—using StringBuilder, loops, or even streams—to flip a string and read it backwards.
How do you reverse a string in Java, and what are the different ways to achieve it? Java provides multiple approaches—using StringBuilder, loops, or even streams—to flip a string and read it backwards.
Log in to share your answer and help other learners.
Log in to answerBest Answer · By JanBask Java Expert
Answered on Sep 14, 2025
Reversing a string in Java is a common programming task, and there are several ways to achieve it depending on the situation. Since strings in Java are immutable, you can’t directly modify them, but you can use different techniques to create a reversed version.
Here are some of the most common methods:
Using StringBuilder or StringBuffer:
The easiest and most efficient approach is to use StringBuilder’s built-in .reverse() method.
String text = "Hello Java";
StringBuilder sb = new StringBuilder(text);
String reversed = sb.reverse().toString();
System.out.println(reversed); // Output: avaJ olleHUsing a for loop:
You can manually reverse a string by iterating from the last character to the first.
String text = "Hello Java";
String reversed = "";
for (int i = text.length() - 1; i >= 0; i--) {
reversed += text.charAt(i);
}
System.out.println(reversed); // Output: avaJ olleHUsing character arrays:
Converting the string to a char[], swapping characters, and then reconstructing it is another option.
Using Java 8 Streams:
With streams, you can reverse using collectors and lambdas, though it’s less efficient compared to StringBuilder.
In short, the most recommended method is using StringBuilder.reverse(), as it is concise and efficient. However, understanding loop-based or array-based methods is useful for interviews and strengthening your grasp of Java fundamentals.
Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.
Most-read guides across JanBask
Common Java interview questions, answered
Guides, tips and career advice on Java from JanBask experts.
Java Frequently Asked J2EE Interview Questions and Answers You Must Read
Top 45 frequently asked Java J2EE interview questions to ace your interview. These J2EE questions are useful for freshers and…
Java Top 100+ Angular Interview Questions and Answers
Are you preparing for your next Angular interview? We have compiled the list of the top 100+ Angular Interview Questions &…
Java Top 51+ Simple Java Project Ideas for Beginners In 2025
Enhance your Java skills with our top 51+ project ideas! From beginner-friendly exercises to advanced applications, fuel your…
Java How Long Does It Takes To Learn Java?
Confused about How long does it take to learn Java ? Well, No worries! This blog will guide you with valuable information and…