In this Java tutorial, you will learn how to reverse a string in Java using StringBuilder.reverse(), String.charAt(), and String.getBytes(), with complete programs, outputs, and notes on when each approach is suitable.
Reverse a String in Java
There are many ways in which you can reverse a String in Java. The most common and recommended approach is to create a StringBuilder object and call its reverse() method.
Reversing a string means the last character of the original string becomes the first character of the new string, the second last character becomes the second character, and so on. For example, the reverse of "Java" is "avaJ".
One important point is that Java’s String class does not provide a direct reverse() method. The reverse() method is available in StringBuilder and StringBuffer.
Quick Syntax to Reverse a String in Java using StringBuilder
String str = "Hello";
String reversed = new StringBuilder(str).reverse().toString();
The variable reversed contains "olleH". This is the shortest practical way to reverse a normal Java string.
Java String Reverse Examples
The following examples show three common approaches. Use StringBuilder.reverse() for regular application code. Use manual loops when you are learning character traversal or when an interview question asks you to reverse a string without built-in reverse methods.
1. Reverse string using String.getBytes
In this example program, we shall get the bytes of given string using String.getBytes() function. Then using a For loop, read each character one by one and arrange them in reverse order.
ReverseString.java
public class ReverseString {
public static void main(String[] args) {
String str = "www.tutorialkart.com";
String reverseStr="";
for(byte c:str.getBytes()){
reverseStr=(char)c+reverseStr;
}
System.out.println("Original String is : "+str);
System.out.println("Reversed String is : "+reverseStr);
}
}
When the above program is run, output to the console window shall be as shown below.
Output
Original String is : www.tutorialkart.com
Reversed String is : moc.traklairotut.www
This method is simple for basic ASCII text, but it is not the best general solution because a Java character and a byte are not always the same thing. For text with non-ASCII characters, prefer StringBuilder or a character-based loop.
2. Reverse string using String.charAt()
In this example, we shall traverse through the string from first character to last character using for loop, incrementing index and String.charAt() function.
ReverseString.java
public class ReverseString {
public static void main(String[] args) {
String str = "www.tutorialkart.com";
String reverseStr="";
for(int i=0;i<str.length();i++) {
reverseStr = str.charAt(i) + reverseStr;
}
System.out.println("Original String is : "+str);
System.out.println("Reversed String is : "+reverseStr);
}
}
Run the above program and you should get the output printed to the console as shown below.
Output
Original String is : www.tutorialkart.com
Reversed String is : moc.traklairotut.www
The above program builds the reversed string by adding each new character at the beginning of reverseStr. This is easy to understand, but repeated string concatenation can create many temporary String objects.
3. Reverse string using StringBuilder
StringBuilder is a very useful class when you doing string operations like concatenation, reverse, etc., on large strings.
In this example, we shall create a StringBuilder with the given string and use its function StringBuilder.reverse() to reverse the original string.
ReverseString.java
public class ReverseString {
public static void main(String[] args) {
String str = "www.tutorialkart.com";
StringBuilder sb = new StringBuilder(str);
sb.reverse();
String reverseStr = sb.toString();
System.out.println("Original String is : "+str);
System.out.println("Reversed String is : "+reverseStr);
}
}
Run the program.
Output
Original String is : www.tutorialkart.com
Reversed String is : moc.traklairotut.www
Reverse a String in Java without using reverse()
If you want to reverse a string without using StringBuilder.reverse(), loop from the last index to the first index and append each character to a StringBuilder. This avoids adding characters to the front of a String repeatedly.
ReverseWithoutReverseMethod.java
public class ReverseWithoutReverseMethod {
public static void main(String[] args) {
String str = "Java";
StringBuilder reversed = new StringBuilder();
for (int i = str.length() - 1; i >= 0; i--) {
reversed.append(str.charAt(i));
}
System.out.println("Original String is : " + str);
System.out.println("Reversed String is : " + reversed);
}
}
Output
Original String is : Java
Reversed String is : avaJ
Reverse a String from User Input in Java
The following program reads a string from the user and reverses it using StringBuilder.reverse(). This is useful when the string is not fixed in the program.
ReverseInputString.java
import java.util.Scanner;
public class ReverseInputString {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = scanner.nextLine();
String reversed = new StringBuilder(str).reverse().toString();
System.out.println("Reversed String is : " + reversed);
scanner.close();
}
}
Example Output
Enter a string: tutorial
Reversed String is : lairotut
StringBuilder reverse() and String reverse() Difference in Java
A common question is whether reverse() is a Java String method. It is not. A String object is immutable, so methods on String do not change the same object in place. To reverse a string, convert it to StringBuilder, call reverse(), and then call toString() if you need the result as a String.
String reversed = new StringBuilder(str).reverse().toString();
Which Java String Reverse Method Should You Use?
| Requirement | Recommended approach | Reason |
|---|---|---|
| Reverse a normal string in application code | StringBuilder.reverse() | Short, readable, and built for this operation |
| Learn how character indexing works | charAt() with a loop | Shows how to access characters by index |
| Reverse without built-in reverse method | Loop from length() - 1 to 0 | Common interview-style solution |
| Reverse simple byte-oriented ASCII text | getBytes() method | Works for basic examples, but not ideal for all Unicode text |
Common Mistakes while Reversing a String in Java
- Calling
str.reverse():Stringhas noreverse()method. Usenew StringBuilder(str).reverse(). - Using
getBytes()for all text: Bytes can behave differently from characters for non-ASCII input. - Forgetting
toString():StringBuilder.reverse()returns aStringBuilder. UsetoString()when aStringis required. - Building large reversed strings with
char + stringrepeatedly: Prefer appending to aStringBuilderfor cleaner and more efficient code.
FAQs on Reversing a String in Java
Is reverse() a String method in Java?
No. reverse() is not a method of the Java String class. It is available in StringBuilder and StringBuffer.
What is the easiest way to reverse a string in Java?
The easiest way is new StringBuilder(str).reverse().toString(). It converts the string to a StringBuilder, reverses it, and converts the result back to String.
How do you reverse a string in Java without using StringBuilder reverse()?
Start a loop from str.length() - 1, move down to 0, and append each character using charAt(i). This manually reads the characters from right to left.
Can I reverse a string in Java using recursion?
Yes, recursion can be used, but it is usually not preferred for simple string reversal because it adds method-call overhead and can be less clear for beginners.
Does reversing a Java string change the original string?
No. Java strings are immutable. Reversing creates a new result; the original String value remains unchanged.
QA Checklist for Java Reverse String Tutorial
- Confirm that examples do not claim
Stringhas a directreverse()method. - Check that each Java program compiles with the class name shown above it.
- Verify that output blocks match the exact input strings used in the examples.
- Use
StringBuilder.reverse()as the recommended approach for general Java code. - Mention the limitation of
getBytes()for non-ASCII text.
Conclusion
In this Java Tutorial, we have learnt how to reverse a given String in Java. For most programs, use StringBuilder.reverse(). For learning and interview practice, use a loop with charAt() to understand how characters are read from the end of the string to the beginning.
TutorialKart.com