<div dir="ltr" style="text-align: justify;">
<div class="separator" style="clear: both; text-align: center;"><img title="How to reverse String in Java" src="https://dineshonjava.com/wp-content/uploads/2017/02/How-to-Reverse-String-in-Java.jpg" border="0" /></div>
<p>How to reverse String in Java without StringBuffer is one of the popular core java interview question. If you are using StringBuffer or StringBuilder to reverse string then it is too easy because both classes have reverse() method to achieving it. But if you are not using reverse() method of these classes then it is some tricky question which requires you to reverse String by applying logic. You can reverse a string by two ways one you can use loop and another way you could use recursion because string reverse is a recursive job. Here in this java tutorial we are going to see how to reverse String using StringBuffer, StringBuilder and using loop with logic. Let&#8217;s see example.</p>
<h2><b>Example of Reverse String in Java</b></h2>
<p>Following program has complete code to reverse any String in Java. Here we have used StringBuffer and StringBuilder&#8217;s reverse() method first to reverse a String. Then we have used own logic to reverse String. In own logic we have used toCharArray() method of String class which return character array of String as shown in following example.</p>
<div style="background-color: #f2f9fc; border:1px solid #c9e6f2;border-radius:3px;padding:16px;line-height:1.45;">
<span style="color: red;font-size: x-large;text-align: center;"><b>Popular Tutorials</b></span></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/spring-tutorial/"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-spring-boot-a-spring-boot-complete-guide/"><strong>Spring Boot Tutorial</strong> </a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint/"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-hateoas-hypermedia-driven-restful-web-service/"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/microservices-with-spring-boot/"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-web-service-tutorial/"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-batch-process-with-example/"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<pre class="highlight">/** 
 * @author Dinesh.Rajput 
 * 
 */ 
public class ReverseStringExample { 
 
 public static void main(String args[]) { 
 
 //Reverse String in Java with Using StringBuffer 
 String str = "Dinesh on Java"; 
 String reverse = new StringBuffer(str).reverse().toString(); 
 System.out.printf(" original String : %s , reversed String %s %n", str, reverse); 
 
 //Reverse String in Java with Using StringBuilder 
 str = "Dinesh on Java"; 
 reverse = new StringBuilder(str).reverse().toString(); 
 System.out.printf(" original String : %s , reversed String %s %n", str, reverse); 
 
 //Reverse String in Java without Using StringBuffer or StringBuilder 
 str = "Dinesh on Java"; 
 reverse = reverse(str); 
 System.out.printf(" original String : %s , reversed String %s %n", str, reverse); 
 } 
 
 
 public static String reverse(String source){ 
 if(source == null || source.isEmpty()){ 
 return source; 
 } 
 String reverse = ""; 
 for(int i = source.length() -1; i>;=0; i--){ 
 reverse = reverse + source.charAt(i); 
 } 
 
 return reverse; 
 } 
 
} 
</pre>
<p><b>Output</b><br />
<i>original String : Dinesh on Java , reversed String avaJ no hseniD </i><br />
<i>original String : Dinesh on Java , reversed String avaJ no hseniD </i><br />
<i>original String : Dinesh on Java , reversed String avaJ no hseniD </i></p>
<p>Above example shown many ways to reverse a String in java but as java programmer always use StringBuffer or StringBuilder method to reverse String for real project.</p>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;">
<p><b>String Related Posts</b></p>
<ol style="text-align: left;">
<li><b><a href="https://dineshonjava.com/2014/11/how-to-make-java-class-immutable.html">How to make Immutable Class in Java</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/core-java-interview-questions.html">Core Java Interview Questions</a></b></li>
<li><b><a href="https://dineshonjava.com/2013/02/string-class-in-java.html">String Class in Java</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/why-string-is-immutable-and-final-in-java.html">Why String is Immutable and Final in Java</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/convert-string-to-integer-to-string-in-java-with-example.html">Convert String to Integer to String in Java with Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/convert-double-to-string-to-double-in-java-with-example.html">Convert Double to String to Double in Java with Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/convert-date-to-string-in-java-with-example.html">Convert Date to String in Java with Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/simpledateformat-in-java-format-date-to-string-to-date-with-example.html">SimpleDateFormat in Java- format Date to String to Date with Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/differences-between-string-stringbuffer-and-stringbuilder-in-java.html">Differences between String, StringBuffer and StringBuilder in Java</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/difference-between-stringbuilder-and-stringbuffer-in-java.html">Differences StringBuffer and StringBuilder</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/how-to-compare-two-strings-in-java.html">How to Compare two Strings in Java</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/hashcode-and-equals-methods-in-java.html">Difference between hashCode() and equals() methods in Java<br />
</a></b></li>
<li><b><a href="https://dineshonjava.com/2013/03/java-string-substring-method.html"> Java &#8211; String substring() Method<br />
</a></b></li>
<li><b><a href="https://dineshonjava.com/2013/05/difference-between-wait-and-sleep.html"> Difference between wait() and sleep()<br />
</a></b></li>
</ol>
</div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/best-practices-for-java-naming-convention-in-programming/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/spring-security-interview-questions-and-answers/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '63'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…