This article teaches different methods to reverse the string in Java, with sample code and examples. Following is the demo of that string which we will try to transpose.

Before moving to check different methods, let's first understand the "String" class. By default, we use the "String" class to store strings. Unfortunately, the default "String" class does not have an inbuilt function to reverse a string. Therefore, we need to use other types of techniques to achieve our goal. Following are the different methods to reverse a string.

Method 1: Using String Builder

In this method, we will use the "StringBuilder" class. StringBuilder class has an inbuild function "reverse()." So, we need first to create a new instance of "StringBuilder." We can pass our string in the "StringBuilder" constructor while creating a new instance. After that, we can use the "reverse()." function.

At last, we can use the "StringBuilder" variable to print or return the output. Following is the source code. The outcome is also after the source code. You can copy the code and use it within your project.

public class ReverseStringMethod1 {

    public static void main(String[] args)
    {
        /* Input String. More about String : */
        String input = "Vlemon.com";
        /* String Builder. More about String Builder: */
        StringBuilder stringBuilder = new StringBuilder(input);
        /* Reverse the input string. */
        stringBuilder.reverse();
        /*
            Return the reversed string or print it.
            For the demo we are printing it.
         */
        System.out.println(stringBuilder);
    }

}

Method 2: Using String Buffer

For the second method, we will use the "StringBuffer" class. StringBuffer class has an inbuild function "reverse()" like "StringBuilder," as we used in the first method. So, we need first to create a new instance of "StringBuffer." The "StringBuffer" constructor needs string as an argument. After that, we can use the "reverse()" function to print the output or return it. Following is the source code and the outcome.

public class ReverseStringMethod2 {

    public static void main(String[] args)
    {
        /* Input String. More about String : */
        String input = "Mayank Sanghvi";
        /* String Buffer. More about String Buffer: */
        StringBuffer stringBuffer = new StringBuffer(input);
        /* Reverse the input string. */
        stringBuffer.reverse();
        /*
            Return the reversed string or print it.
            For the demo we are printing it.
         */
        System.out.println(stringBuffer);
    }
}

Method 3: Using ArrayList

Like the first and second methods, The ArrayList also has a built-in "reverse()" function for the Collections class. Collection class "reverse()" method can reverse a list object. We will first convert the string into a char array. After that, we will transform the character array into a Character List. 

public class ReverseStringMethod3 {

    public static void main(String[] args)
    {
        /* Input String. More about String : */
        String input = "Java Tutorial at Vlemon.com";
        /* Input character array */
        char[] inputCharacters = input.toCharArray();
        List<Character> inputList = new ArrayList<>();
        /*
            Iterate through all the items in our inputCharacters Array,
            add each item in the List.
        */
        for(char letter:inputCharacters)
            inputList.add(letter);
        /*
            Use Collection class "reverse()"
        */
        Collections.reverse(inputList);
        /*
            output string builder to save the reversed list item.
        */
        StringBuilder output= new StringBuilder();
        /*
            Iterate through all the items in our inputList,
            add append each item in the output string builder.
        */
        for(char letter:inputList)
            output.append(letter);
        /*
            Print or return the final output.
        */
        System.out.println(output);
    }

}

Method 4: Using Character Array

In the fourth method, we will convert our string into a character array. To convert a string into a character array, we will use the "String" class inbuilt function "toCharArray()." So now we have two different ways to process our character array. Either we can scan the character array from the end to the start. Or we can switch the position of characters from the left and the right place. Following is a detailed explanation for both ways.

 

Code 4.1: Scan the character array from last to the first. As shown in the following image, we have the following character array. The exact string is "JAVAFUN." If we scan the array from 0 to 6, we will get the "JAVAFUN." On the other hand, if we scan the array from 6 to 0, we will get the string in reverse order. Following is the code to achieve the above requirement.

public class ReverseStringMethod4_1 {

    public static void main(String[] args)
    {
        /* Input String. More about String : */
        String input = "JAVAFUN";
        /* Input character array */
        char[] inputCharacters = input.toCharArray();
        /*
            It is good to use String Builder for multiple string modification.
            More About String Builder:
            String vs Sting Builder:
        */
        StringBuilder output= new StringBuilder();
        /* Scan the character array from start to end. */
        for(int i=inputCharacters.length-1;i>=0;i--)
            output.append(inputCharacters[i]);
        /*
            Return the reversed string or print it.
            For the demo we are printing it.
         */
        System.out.println(output);
    }

}

Code 4.2: To explain this code, we will refer to the following image. 

As shown in the above image, we have a character array for the string "JAVA IS EASY" if we start swapping characters from left to right, we will get a reversed string. So, for example, We started from the character at the 0th and 11th positions, then the character at the 1st and 10th positions, and so on until we swapped all the characters. Following is the code for the same approach.

public class ReverseStringMethod4_2 {

    public static void main(String[] args)
    {
        /* Input String. More about String : */
        String input = "JAVA IS EASY";
        /* Input character array */
        char[] inputCharacters = input.toCharArray();
        /* int variable to store left start position and right start position. */
        int left=0, right=inputCharacters.length-1;
        /* Loop through start swapping characters from left to right. */
        for (left =0; left < right;left++,right--)
        {
            char leftCharacter = inputCharacters[left];
            inputCharacters[left] = inputCharacters[right];
            inputCharacters[right] = leftCharacter;
        }
        /*
            It is good to use String Builder for multiple string modification.
            More About String Builder:
            String vs Sting Builder:
        */
        StringBuilder output= new StringBuilder();
         /*
            Iterate through all the items in our inputCharacters,
            add append each item in the output string builder.
        */
        for(char letter:inputCharacters)
            output.append(letter);
        /*
            Print or return the final output.
        */
        System.out.println(output);
    }

}

We discussed multiple methods to reverse the string using the Java code. Following are some scenarios to test your understanding.