A string is a sequence of characters that behave like an object in Java and stores the data in a character array. It is one of the most commonly used data structures after arrays.
To create a string object, you need the java.lang.String class. Java programming uses UTF -16 to represent a string. Strings are immutable, so their internal state remains constant after the object is created. The string object performs various operations, but string reverse functions in Java are the most widely used function.
What is String Reverse Function in Java?
Reverse function in Java is a common programming task that reverses the order of characters in a given string. This can be achieved in several ways in Java, using different techniques like iteration, recursion, or built-in functions. One common method is to convert the string into a character array, reverse it, and then convert it back to a string.
Another popular approach is using Java's built-in StringBuilder or StringBuffer class, which provides a reverse() method to reverse the string. Reversing a string is useful in various applications, such as checking for palindromes, manipulating strings for encoding/decoding, and more.
Example:
HELLO string reverse gives the output as OLLEH
How to Reverse a String in Java?
Strings in Java are immutable objects; you must create another string to reverse them. The string class doesn't have a reverse method; it has a toCharArray() method to do the reverse.
There are diverse methods to reverse a string in Java. Read on to understand each of them in detail.
1. Using toCharArray() to Reverse a String in Java
The toCharArray() method in Java converts a string into an array of characters. This method returns a new array where each element represents a character in the string. It is useful when you need to manipulate individual characters of a string, such as reversing the string or performing character-based operations.
Code Example:

Output:
Original String: Java Certification
Reversed String: noitacifitreC avaJ
Explanation:
- The toCharArray() method converts the input string into a character array
- We then loop through the character array in reverse order, appending each character to the result string
- Finally, the reversed string is printed to the console
2. Using StringBuilder to Reverse a String in Java
StringBuilder is a mutable sequence of characters. Unlike strings, which are immutable, StringBuilder allows you to modify its content without creating new objects. It provides methods like append(), insert(), and reverse(), making it ideal for string manipulation and reversal.
Code Example:

Output:
Original String: Tech Expert
Reversed String: trepxE hceT
Explanation:
- The StringBuilder class is used to create a mutable sequence of characters
- The reverse() method of StringBuilder is called to reverse the characters in the string
- Finally, the original and reversed strings are printed
3. Using While Loop and For Loop to Reverse a String in Java
Both while and for loops are commonly used in Java to iterate over elements in a collection or array. To reverse a string, you can iterate through the string from the last character to the first. A for loop is generally more compact, while a while loop provides flexibility to control iteration conditions, allowing custom exit criteria.
While Loop Code Example:

Output:
Original String: Java Developer
Reversed String: repoleveD avaJ
Explanation:
- The program starts by initializing the original string str and an empty string reversed to store the reversed string
- The while loop starts from the last character of the string (index = str.length() - 1) and appends each character to the reversed string
- The loop continues until it reaches the first character (index >= 0)
- Finally, the reversed string is printed out
For Loop Code Example:

Output:
Original String: Software Development & Engineering
Reversed String: gnireenignE & tnempoleveD erawtfoS
Explanation:
- The program starts by defining an input string "Software Development & Engineering"
- It then uses a for loop to iterate through the string, starting from the last character (input.length()—1) and moving backward to the first character (i >= 0)
- The program appends the current character to the reversed string during each iteration
- Finally, the reversed string is printed to the console
4. Converting a String to Bytes
Converting a string to bytes in Java can be done using the getBytes() method. This method encodes the string into a sequence of bytes using a specified charset, such as UTF-8. The resulting byte array can be useful for tasks like file writing or network transmission, where byte-level operations are required.
Code Example:

Output:
Original String: Technology, Solutions
Reversed String: snoituloS ,ygolonhceT
Explanation:
- The program first converts the input string into a byte array using getBytes()
- It then reverses the byte array by swapping the elements
- Finally, the byte array is converted back to a string using a new String(byteArray) and printed
5. Using ArrayList Object to Reverse a String in Java
An ArrayList is a dynamic array in Java that can store elements of any type. To reverse a string, you can convert the string to a list of characters and then use the Collections.reverse() method. This approach provides flexibility, as ArrayList allows for easy manipulation of elements, including adding, removing, and rearranging them.
Code Example:

Output:
Original String: Simplilearn
Reversed String: nraelilpmiS
Explanation:
- The program converts the string into an ArrayList of characters
- It then reverses the ArrayList using the Collections.reverse() method
- Finally, it converts the reversed ArrayList back into a string and prints the result
6. Using StringBuffer to Reverse a String in Java
StringBuffer is similar to StringBuilder but is synchronized, making it thread-safe. Like StringBuilder, it allows efficient string manipulation, including appending, inserting, and reversing characters. The reverse() method in StringBuffer is commonly used to reverse a string without creating new objects, ensuring better performance than immutable String operations.
Code Example:

Output:
Original String: MERN Developer
Reversed String: repoleveD NREM
Explanation:
- The program creates a StringBuffer object with the original string
- The reverse() method of StringBuffer is called to reverse the string
- Finally, the reversed string is printed using the toString() method to convert the StringBuffer back to a string
Also Read: Key Differences Between a StringBuffer and a StringBuilder
7. Using Stack to Reverse a String in Java
A Stack is a collection in Java that follows the Last In, First Out (LIFO) principle. To reverse a string using a Stack, each character is pushed onto the stack, and then characters are popped off the stack in reverse order. This method mimics reversing through a stack structure, providing a simple and intuitive approach.
Code Example:

Output:
Original String: Data Structures
Reversed String: serutcurtS ataD
Explanation:
- We use a Stack<Character> to push each string character onto the stack
- The stack works in a Last-In-First-Out (LIFO) manner, so when we pop characters off, we get them in reverse order
- We append each popped character to a StringBuilder to construct the reversed string
- Finally, the reversed string is printed to the console
8. Using Character Array to Reverse a String in Java
A character array is a basic array type in Java that stores characters. To reverse a string using a character array, you can convert the string to a char array and then swap characters from the beginning and end until you reach the middle. This method is direct and efficient for reversing a string without using additional collections or objects.
Code Example:

Output:
Original String: Arrays in DS
Reversed String: SD ni syarrA
Explanation:
- The program first converts the original string into a character array using the toCharArray() method
- It then uses two pointers, left and right, to swap the characters at the corresponding positions
- The loop continues swapping characters until the pointers meet in the middle
- Finally, the character array is converted back to a string using the new String(charArray) constructor, and the reversed string is printed
9. Using Recursion to Reverse a String in Java
Recursion is a programming technique where a function calls itself. To reverse a string using recursion, you break the string into smaller parts and recursively reverse each substring. Once the recursion reaches the base case (e.g., an empty string or single character), the function combines the results in reverse order to form the reversed string.
Code Example:

Output:
Original String: Fibonacci, Series
Reversed String: seireS ,iccanobiF
Explanation:
- The reverse method takes a string as input and recursively calls itself with the substring (excluding the first character)
- The base case checks if the string is empty. If it is, the recursion stops and returns the empty string
- The recursive case combines the reversed substring and the string's first character, effectively reversing it
10. Using Substring() to Reverse a String in Java
The substring() method in Java extracts a portion of a string. To reverse a string using substring(), you can repeatedly extract smaller substrings from the end of the string, concatenating them in reverse order. While this method is simple, it might not be as efficient as others because it creates new string objects.
Code Example:

Output:
Original String: Blockchain Technology
Reversed String: ygolonhceT niahckcolB
Explanation:
- The substring(i, i + 1) method extracts each character from the string from the last index to the first
- The loop iterates through the string in reverse order, and the substring() method appends each character to the reversed string
11. Using the Character Array and swap() to Reverse a String in Java
In this approach, you convert the string into a character array and swap the characters at corresponding positions from the beginning and end of the array. This swapping continues until the middle of the array is reached. The modified array is then converted back into a string, resulting in the reversed string.
Code Example:

Output:
Original String: Hello World
Reversed String: dlroW olleH
Explanation:
- The swap() method exchanges the characters at the specified positions in the array
- The reverse() method converts the string into a character array and then uses the swap() method to reverse the array by swapping characters from the two ends of the array
- Finally, the reversed character array is converted into a string and printed
12. Using the Java Collections Framework reverse() to Reverse a String in Java
The Java Collections Framework provides a reverse() method in the Collections class, which can be used to reverse the elements of a list. By converting a string into a list of characters, the Collections.reverse() method can be applied to reverse the order of the characters. Using standard Java libraries is a clean and efficient way to reverse a string.
Code Example:

Output:
Original String: Hello, World!
Reversed String: !dlroW ,olleH
Explanation:
- The program converts the input string into a list of characters (charList)
- It uses the Collections.reverse() method to reverse the list of characters
- Finally, the reversed list is converted back into a string using a StringBuilder, which is then printed as the reversed string
Conclusion
String objects are immutable, meaning their values cannot be altered once created. This immutability is a key feature of the String class, which operates under string literals. When one reference variable modifies a String object, it will impact all reference variables pointing to that same object since strings are shared through references in Java.
As we've explored, several ways exist to reverse a string in Java. For those looking for more robust solutions, third-party libraries such as Apache Commons Lang also offer convenient methods for string reversal, adding flexibility and efficiency.
If you're eager to deepen your Java expertise and advance your career as a Java developer, Simplilearn’s Full Stack Java Developer Master’s Program is an excellent choice. It includes hands-on projects and interactive labs, providing you with the practical experience and knowledge required to excel in today's competitive software development job market.