How to reverse a string in java | java reverse string – Best Tips

how to reverse a string in java, we will discuss different techniques to reverse a string in this article. As we all know that String is made from characters. And we know that it is the most used data structure. it is the object which stores the data in the character Array.

Basically, we can say that String is the combination of the character array. If you the String in the form of a character Array then the manipulation will be easy for you. And you will have a clear understanding of String.

A string is an immutable class So the Internal State of the String can not be changed. If you want to create the String object then you need to use java.lang.String class. Now we will see the most widely used things like “how to reverse a string in java“.

How to reverse a string in java
Reverse a String

Reversing A String Example

Example1 – “Ram” —-> “maR”

Example 2 – “Hub Spoting” —-> “gnitopS buH”

Example 3- “Go For It” —–> “tI roF oG”

Multiple Ways “How to reverse a string in java”

As we all know that String has not a reverse method. For reversing a String We can use the toCharArray() method.

1.Reverse a String Using toCharArray() method

Using toCharArray() we can easily reverse a string. the logic behind that is

  • take a string
  • use the toCharArray() method to convert it into a char Array
  • now Iterate the char Array from the last index to 0

Now check the Examples—

Example1-

// program to reverse a String Using toCharArray() method

public class ReverseAString{

public static void main(String[] arg) {

// declaring the String Variable

String str= “Ram”;

// convert String to character array by using toCharArray

char[] charArray= str.toCharArray();

// Iterate through Array in reverse order to get result

for (int i = charArray.length – 1; i >= 0; i–)

// print reversed String

System.out.print(charArray[i]); } }

Output

maR

2. Reverse a String By Using While Loop or For Loop | reverse string java

We can reverse a string very easily using for loop and while loop. For doing this we have to use the charAt() function. and firstly we have to take the length -1 at the index position and in each iteration, we have to reduce the length by 1 until the length is equal to 0. Let’s See the Code Example

Example 1:

Code Using While Loop

// Java program to reverse a string using While loop

public class ReverseAString{

public static void main(String[] args)

{

// Declaring a String
String str = “Hub Spoting”;

//Now get the length for the given String
int len=str.length();

//Using While loop

while(len>0)

{

System.out.print(str.charAt(len-1));

len–;

}

}

}

Output

gnitops buH

Code Using For Loop

// Java program to reverse a string using For loop

public class ReverseAString{

public static void main(String[] args) {

String str= “Hub Spoting”;

//Get the String length

int len=str.length();

//Using For loop

for(len=str.length();len >0; len–) {

System.out.print(str.charAt(len-1));

}

}

}

Output

gnitops buH

3. Reverse a String By Using StringBuilder | reverse string java |How to reverse a string in java

Java provides two more string classes Which are the StringBuilder class and the StringBuffer class. This class has in-built methods reverse(). This method is used to reverse a character from a given String object. reverse method reverses the All given character and replaces it with the given String. The reverse() method Logic is Designed to reverse a String character.

As we all know that String Builder objects are mutable. In terms of memory management, it is very efficient. but String Builder is not thread-safe.

For Apply the reverse() method on a given String firstly we have to create the object of the String. Then need to pass the string in the String builder. And Then we can Apply the reverse method on a given String Builder Object to reverse a String or get the desired result.

Now let’s see the code below…

This is a program to Reverse a String Using String Builder

public class ReverseASting{

public static void main(String[] arg) {

// define a String String str= “Shiway”;

// Now create String Builder object

StringBuilder stringBuilderObject= new StringBuilder();

//Using the Append method Insert the String Object into String Builder Object stringBuilderObject.append(input);

//Now its time for magic Apply the reverse method on String Builder Object stringBuilderObject.reverse();

//Print the StringBuilder Object to get the desired result

System.out.println( stringBuilderObject);

}

}

Output

yawihs

you have heard the String Buffer class right. this is also an alternative to the String builder class. But String Buffer is a mutable class means we can extend this object easily. but in the case of String length is fixed.

String Buffer class also provides the reverse method. moreover, we can easily reverse a String object using String Buffer But the issue is, that it is not efficient as the String Builder class. secondly, it is thread-safe.

Now lets See the Example of String Buffer

This is a program to Reverse a String Using String Buffer

public class ReverseASting{

public static void main(String[] arg) {

// define a String
String str= “Shiway”;

         // creating StringBuffer object

        StringBuffer stringBuffervarible = new StringBuffer();

        // append a string into StringBuilder stringBuffervarible

        //append is inbuilt method to append the data

        stringBuffervarible .append(input);

        // reverse is inbuilt method in stringBuffervarible to use reverse the string 

        stringBuffervarible .reverse();

        // print reversed String

        System.out.println( stringBuffervarible );

}

}

OutPut

“yawiS”

Also Read –

How to Iterate Hashmap in java – 5 ways

FAQ.

how to reverse a string in java

there are multiple ways to reverse the for loop please check this article for details on how to reverse a string in java.

Final Verdict on How to reverse a string in java | reverse string java :

This is all from my side! Look at these blog’s to ensure you’re on the right path to your digital success. 

Look at these services you may need for your website.

In this article, we answered the questions “How to reverse a string in java”, “java reverse string” reverse string java” and “how to reverse the string in java”.

Do you have any questions Regarding How to reverse a string in java? 

Drop your queries and comments section below, or post them on HubSpoting Forums, and we will be happy to answer.

Disclaimer:  How to reverse a string in java

Our content is reader-supported and friendly. so This means if you click on any one of our links, then we may earn a commission. We are so happy that you support us.

Leave a Comment