Core JavaJava

Reverse a string in java

If you are looking for the following, this article will be of help :

– Reverse a string in Java without library functions
– String reversal algorithm in java
– Reverse a string using StringBuffer or StringBuilder


Reverse a String in Java


Java provides library methods in StringBuffer and StringBuilder to reverse a string.

String reversal can be done as follows :

new StringBuilder("techkatak").reverse().toString()


But during interviews, mostly interviewee are asked to write the program without using library functions.

Since String is immutable in Java, its not possible to change its contents. So, we have to create a new String that would hold the characters in reverse order.

This can be done using iterating over the characters of the String in reverse order and populating a StringBuffer or StringBuilder.


Here is the program for the same:


© 2015, All rights reserved.