Reverse a string in Java
This article discusses 5 different ways to reverse a string in Java with examples.
Examples:

Prerequisite : String vs StringBuilder vs StringBuffer in Java
Following are some interesting facts about String and StringBuffer classes :
1. Objects of String are immutable.
2. String class in Java does not have reverse() method, however StringBuilder class has built in reverse() method.
3. StringBuilder class do not have toCharArray() method, while String class does have toCharArray() method.
-
Converting String into Bytes: getBytes() method is used to convert the input string into bytes[].
Method:
1. Create a temporary byte[] of length equal to the length of the input string. 2. Store the bytes (which we get by using getBytes() method) in reverse order into the temporary byte[] . 3. Create a new String abject using byte[] to store result.
// Java program to ReverseString using ByteArray.importjava.lang.*;importjava.io.*;importjava.util.*;// Class of ReverseStringclassReverseString{publicstaticvoidmain(String[] args){String input ="GeeksforGeeks";// getBytes() method to convert string// into bytes[].byte[] strAsByteArray = input.getBytes();byte[] result =newbyte[strAsByteArray.length];// Store result in reverse order into the// result byte[]for(inti =0; i<strAsByteArray.length; i++)result[i] =strAsByteArray[strAsByteArray.length-i-1];System.out.println(newString(result));}}chevron_rightfilter_noneOutput:
skeeGrofskeeG
-
Using built in reverse() method of the StringBuilder class: String class does not have reverse() method, we need to convert the input string to StringBuilder, which is achieved by using the append method of StringBuilder. After that, print out the characters of the reversed string by scanning from the first till the last index.
// Java program to ReverseString using StringBuilderimportjava.lang.*;importjava.io.*;importjava.util.*;// Class of ReverseStringclassReverseString{publicstaticvoidmain(String[] args){String input ="Geeks for Geeks";StringBuilder input1 =newStringBuilder();// append a string into StringBuilder input1input1.append(input);// reverse StringBuilder input1input1 = input1.reverse();// print reversed StringSystem.out.println(input1);}}chevron_rightfilter_noneOutput:
skeeG rof skeeG
- Converting String to character array: The user input the string to be reversed.
Method:1. First, convert String to character array by using the built in Java String class method toCharArray(). 2. Then, scan the string from end to start, and print the character one by one.
// Java program to Reverse a String by// converting string to characters one// by oneimportjava.lang.*;importjava.io.*;importjava.util.*;// Class of ReverseStringclassReverseString{publicstaticvoidmain(String[] args){String input ="GeeksForGeeks";// convert String to character array// by using toCharArraychar[] try1 = input.toCharArray();for(inti = try1.length-1; i>=0; i--)System.out.print(try1[i]);}}chevron_rightfilter_noneOutput:
skeeGrofskeeG
-
Convert the input string into character array by using the toCharArray(): Convert the input string into character array by using the toCharArray() – built in method of the String Class. Then, scan the character array from both sides i.e from the start index (left) as well as from last index(right) simultaneously.
1. Set the left index equal to 0 and right index equal to the length of the string -1. 2. Swap the characters of the start index scanning with the last index scanning one by one. After that, increase the left index by 1 (left++) and decrease the right by 1 i.e., (right--) to move on to the next characters in the character array . 3. Continue till left is less than or equal to the right.
// Java program to Reverse a String using swapping// of variablesimportjava.lang.*;importjava.io.*;importjava.util.*;// Class of ReverseStringclassReverseString{publicstaticvoidmain(String[] args){String input ="Geeks For Geeks";char[] temparray = input.toCharArray();intleft, right=0;right = temparray.length-1;for(left=0; left < right ; left++ ,right--){// Swap values of left and rightchartemp = temparray[left];temparray[left] = temparray[right];temparray[right]=temp;}for(charc : temparray)System.out.print(c);System.out.println();}}chevron_rightfilter_noneOutput:
skeeG roF skeeG
-
Using ArrayList object: Convert the input string into the character array by using toCharArray() built in method. Then, add the characters of the array into the ArrayList object. Java also has built in reverse() method for the Collections class. Since Collections class reverse() method takes a list object , to reverse the list , we will pass the ArrayList object which is a type of list of characters.
1. We copy String contents to an object of ArrayList. 1. We create a ListIterator object by using the listIterator() method on the ArrayList object. 2. ListIterator object is used to iterate over the list. 3. ListIterator object helps us to iterate over the reversed list and print it one by one to the output screen.
// Java program to Reverse a String using ListIteratorimportjava.lang.*;importjava.io.*;importjava.util.*;// Class of ReverseStringclassReverseString{publicstaticvoidmain(String[] args){String input ="Geeks For Geeks";char[] hello = input.toCharArray();List<Character> trial1 =newArrayList<>();for(charc: hello)trial1.add(c);Collections.reverse(trial1);ListIterator li = trial1.listIterator();while(li.hasNext())System.out.print(li.next());}}chevron_rightfilter_noneOutput:
skeeG roF skeeG
Related Article:
Different methods to reverse a string in C/C++
This article is contributed by Mr. Somesh Awasthi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Reverse words in a given String in Java
- Create a new string by alternately combining the characters of two halves of the string in reverse
- Reverse vowels in a given string
- Different methods to reverse a string in C/C++
- Reverse words in a given string
- Reverse the given string in the range [L, R]
- Reverse middle words of a string
- Reverse String according to the number of words
- Reverse words in a given String in Python
- Print reverse of a string using recursion
- Reverse string without using any temporary variable
- Reverse alternate k characters in a string
- Reverse every word of the string except the first and the last character
- Reverse string in Python (5 different ways)
- Print reverse string after removing vowels


