Best way to reverse a String in Java for beginners in Java.

Author: neptune | 12th-Dec-2021 | views: 66

I believe you already know what is String in Java if not then "String is a sequence of characters inside double quotation marks" like "NeptuneWorld". In Java String is considered as object.

We can perform multiple operations in String, Widely used operation is reversal of string. 

There are multiple way to reverse a String in Java. We will explore two widely used methods:

  1. Using String Buffer / String Builder
  2. Using CharAt method 

Using String Buffer / String Builder

String Buffer used inbuild method to perform reversal of String. First, we create an object of String Builder class then append the original string into the String Builder object. It convert the String into String Builder then we perform the reverse operation using in-built reverse method. 

Using CharAt method 

Here we use CharAt() method it return the character at the specified index . We extract the character from the string then append the character at the front of empty string perform this process for all characters. Finally we get a reversed string.   

I Hope you find it useful, If you have something in mind write in comment section.

Thanks for Reading !

Related Blogs
25 Basic Java interview questions.
Author: neptune | 28th-Sep-2021 | views: 146
#Interview #Java
We will explore 25 basic Java interview questions...
View More