Java String Reverse Example

This tutorial will show you a few different methods on how to perform a String Reverse to reverse the character order of a String. If you looked at the Java String Class page you will notice there is no reverse() method that can be directly applied to a String object. This may have something to do with the fact that Strings are immutable in Java.


Java String Reverse StringBuilder

The sample method below uses the StringBuilder option to reverse the order of a String. Unlike the String class, the StringBuilder class does in fact have a reverse() method.

public static void reverseStringBuilder()
	{
	
		//Define our String
		String jcdString = "Java Code Depot";
		 
		//Define a StringBuilder Object
        StringBuilder jcdStringBuilder = new StringBuilder();
 
        //Add our String to StringBuilder object
        jcdStringBuilder.append(jcdString);
 
        //Reverse the StringBuilder Object
        jcdStringBuilder = jcdStringBuilder.reverse();
 
        System.out.println(jcdStringBuilder);
	}
topeD edoC avaJ


Java String Reverse Byte Array

This sample method stores our String to reverse in a byte array. We then take that byte array and add each character to a new byte array starting at the end and moving to the beginning.

public static void reverseStringByteArray()
	{
		//Define our String
		String jcdString = "Java Code Depot";
				
		//Convert our String to a byte array
		byte[] jcdStringByteArray = jcdString.getBytes();
		
		//Define empty byte array based on size we need
	    byte[] reversedByteArray = new byte[jcdStringByteArray.length];
	
	    //Add characters to our byte array in reverse order
	    for (int x = 0; x<jcdStringByteArray.length; x++)
	    {
	    	reversedByteArray[x] = jcdStringByteArray[jcdStringByteArray.length -x -1];
	    }
	    
	    System.out.println(new String(reversedByteArray));
	}
topeD edoC avaJ


Java String Reverse Source Code

This is the full Java source code to reverse a string that uses both methods that have been discussed already.

package com.jcd.java.tutorials;

public class JavaStringReverse 
{

	public static void main(String[] args) 
	{

		//Reverse Using StringBuilder
		reverseStringBuilder();
		
		//Reverse Using Byte Array
		reverseStringByteArray();
        
    }
	
	public static void reverseStringBuilder()
	{
	
		//Define our String
		String jcdString = "Java Code Depot";
		 
		//Define a StringBuilder Object
        StringBuilder jcdStringBuilder = new StringBuilder();
 
        //Add our String to StringBuilder object
        jcdStringBuilder.append(jcdString);
 
        //Reverse the StringBuilder Object
        jcdStringBuilder = jcdStringBuilder.reverse();
 
        System.out.println(jcdStringBuilder);
	}
	
	
	public static void reverseStringByteArray()
	{
		//Define our String
		String jcdString = "Java Code Depot";
				
		//Convert our String to a byte array
		byte[] jcdStringByteArray = jcdString.getBytes();
		
		//Define empty byte array based on size we need
	    byte[] reversedByteArray = new byte[jcdStringByteArray.length];
	
	    //Add characters to our byte array in reverse order
	    for (int x = 0; x<jcdStringByteArray.length; x++)
	    {
	    	reversedByteArray[x] = jcdStringByteArray[jcdStringByteArray.length -x -1];
	    }
	    
	    System.out.println(new String(reversedByteArray));
	}

}



Concluding Comments

As mentioned earlier there is no String reverse() method, but Java does provide for at least two other options to reverse a String.



Java References:

Add a Comment

Your email address will not be published. Required fields are marked *