Java Reverse String: Different Ways to Reverse A String In Java

Athena Ozanich
Athena Ozanich

Published:

Working with strings in Java is not only one of the most common tasks in many software projects, but it can also be the most fun.

Young woman learning to reverse string values in Java and discovering ways it can bolster her coding process.

String reversals are a common way software might interact with strings, and in this post, you will learn a couple of ways to do this. You will also hear an example of how you might use this process and see it done in a few examples. Each example will showcase a different approach.

Download Now: An Introduction to Java & JavaScript

Working With Strings in Java

Since strings are so commonly used in Java, there are several ways to interact with and manipulate strings. One of these ways is called string reversal, and it may not sound like it but can be a very useful trick. But before we jump into that, let’s discuss some important information about strings.

Java Strings are Immutable

In Java, Strings are immutable objects – which is to say – that once they are created, they cannot be changed. Any changes to a string results in a new string altogether; all strings are stored in the heap for accessibility while the JVM is still running the application.

This information is essential because it leaves any created string available in any state it has been in; which makes using strings much easier.

Native String Classes in Java

The native Java String classes have a number of methods available to them; these methods offer potent ways we can interact with string values. Some classes also provide methods that can be used to facilitate the string reversal process.

The Java String Class

The Java String class does not provide a reverse method for strings. However, the Java String class provides a method called toCharArray() which converts a single string to an array of char values – char being a single letter.

The Java StringBuilder Class

StringBuilder is a Java class used to create a mutable – or modifiable – succession of characters. Like StringBuffer, the StringBuilder are alternatives to the Java String Class, which only provides immutable strings.

How To Reverse Strings in Java

Knowing multiple ways to reverse strings can be helpful for unique situations where you might have restrictions or limitations on modifying a string. Next, let's look at a few ways to handle this using methods from different classes.

Check out this video to learn more about reversing strings in Java.

Java String toCharArray()

The block of code below creates a string and converts it into a char array, then the loop iterates over the array displaying them based on their index in reverse order. It’s a simple yet elegant solution using chars and arrays to get the desired effect.

Let's look at what it looks like to use the toCharArray() method to reverse a string.

public static void main(String[] args){ String str = "Let's play Chardee MacDennis!"; char[] charArr = str.toCharArray(); for (int i = charArr.length - 1; i >= 0; i--){ System.out.print(charArr[i]); } }

Let’s look at the StringBuilder class’s reverse method and how you can use it to reverse your string values.

JavaStringBuilder reverse() Method

The StringBuilder class has a reverse method used to reverse string values. This process is equally straightforward.

public static void main(String[] args){ String str = "Let's play Chardee MacDennis!"; StringBuilder strBldr = new StringBuilder(); // append a string into the StringBuilder variable strBldr.append(str); // reverse StringBuilder variable value strBldr.reverse(); // print result System.out.println(strBldr); }

Java Byte getBytes Method

The getbytes() function in java converts string values into a sequence of bytes and returns an array of bytes. These bytes are the preserved order and value of the provided string. Then using the length method, the value is converted to a byte value of equal length to the provided string. Then finally, a for loop is used to iterate over the values in reverse.

Let’s look at that process in the following code block.

public static void main(String[] args) { String str = "Let's play Chardee MacDennis!"; // getBytes() method to convert string into bytes. byte[] strByteArr = str.getBytes(); byte[] result = new byte[strByteArr.length]; // Stor results in reverse order into the // result byte[] for (int i = strAsByteArray.length -1; i >= 0; i--){ result[i] = strAsByteArray[strAsByteArray.length - i - 1]; } System.out.println(new String(result)); }

There are many other ways to reverse string values, but many are unnecessarily complex and should be avoided unless no alternative is available.

Doing More With Java Strings

Moving forward with strings, you can investigate other string classes and methods for manipulating string values. But as always, the best way to solidify programming concepts is to put them into practice. Investigate, learn and practice writing code using different ways to interact with string values.

java

Topics: Java

Related Articles

We're committed to your privacy. HubSpot uses the information you provide to us to contact you about our relevant content, products, and services. You may unsubscribe from these communications at any time. For more information, check out our Privacy Policy.

Learn more about the differences between and uses of these popular programming languages.