Besttechindia is reader-supported. When you buy through links on our site, we may earn an affiliate commission. Learn more.

How to Reverse a String in Java? (2020)

Java is amongst the most prevalent general-purpose programming languages in the world. Learning Java has become essential for IT engineers all over the world for the past few decades.

The main advantages of Java lie in the fact that it has a standard protocol of implementation, which allows it to run on any machine in the world once the code has been developed.

The requirement of a recompilation of a Java code, if it is run on a different machine, is little to none.

Other languages such as C and C++ are similar in syntax and implementation to Java, but Java has significantly fewer low-level characteristics as compared to both of them. 

Java

Especially for client-server web applications, Java is amongst the most used programming languages of the world. Beginners learning Java will generally learn very basic commands. However, as is the case with any programming language, each action in Java can be done in multiple ways.

The action of reversing a string is amongst the first that is taught to any programming beginner. But, there are several ways in which this command can be given. Here is how a string can be reversed in Java:

It must be noted that string objects are unchangeable in Java. The StringBuilder class in Java has a reverse() method built into it, while the same cannot be said for the string class.

Conversely, the StringBuilder class in Java does not have the toCharArray() technique, while the string class does have the toCharArray() technique.

  • Conversion of String into Bytes: The string that needs to be reversed can be converted into bytes through the getBytes() technique. To follow this method, first, create a byte[] that is the same length as the string that has been input. Into this, you can store the bytes of the original string in reverse order. Then transfer the result from byte[] to the new string.
  • The reverse() Method: The StringBuilder class has the feature of the reverse method, which a normal string class doesn’t. So, to follow this method, the string first needs to be converted into a StringBuilder. Once the string has been converted into a StringBuilder, the reverse() keyword can be used. The reverse() keyword can be used to transfer the contents of the StringBuilder into another variable of the same type in reversed order. This can then be printed out as usual.
  • Conversion of String to Character: The string class has the feature of being compatible with the toCharArray() command. This allows any string to be converted into a character array by using this keyword. The new character array can then be stored in another such array but in the reverse order, using a for loop. Now, this new array can be printed out.
  • Using the CharAt() Method: The CharAt() command allows the user to extract characters from a particular index of the string. This is very useful if individual characters need to be extracted from the string. The algorithm of this method is such that the user extracts a single letter from the string one at a time in reverse order. This letter is then stored in another variable. This variable can then be displayed, and it will show the string in reverse order.
  • Recursion: Recursion is an often-used method in Java. A recursive function is a function that calls itself. The reverse method rev() can be called inside a recursive function to store and print the string in reverse order.
  • ArrayList Object: The toCharArray() method can be used to convert the string into an array. The components of this array can then be added to an ArrayList object. A ListIterator object is then created through the usage of the listIterator() method in the ArrayList object. The object that has now been created can be used to iterate over the list. This helps iterate over the list that has been reversed and then print it on the screen character by character.

Conclusion

There are several ways by which a string can be reversed in Java. You can opt for any as per your knowledge and comfort.

Piyush

Follow me here

About the Author

Piyush Kashyap is a Ph.D student at Sant Longowal Institute of Engineering and Technology, Sangrur. He is a budding editor/ writer and has been working as a part-time reviewer for online content. He loves to read tech-based articles and has a knack for reviewing such articles He likes to stay updated about the latest trends in technology. He has also been working as a reviewer for many scientific journals. He also writes articles based on science. Know More About Piyush


{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
>