Basic Java Program<\/strong> of converting the String to character array and then traversing the character array in backwards.<\/p>\n\n\n\nUnlike in C\/C++, we can’t traverse a string like an array of characters. In Java, we first need to convert the String to a char array by using toCharArray() method of the String class.<\/p>\n\n\n\n
Once, the char array is formed, we can simply traverse the array backwards and concatenate, character by character to form a reverse String.<\/p>\n\n\n\n
So, this is one of the way to Reverse a String in Java<\/strong><\/p>\n\n\n\n\n import java.util.*;\n import java.lang.*;\n import java.io.*;\n\n\n public class UsingForLoop\n {\n\tpublic static void main (String[] args) throws java.lang.Exception\n\t{\n\t\tScanner sc = new Scanner(System.in);\n\t\tString str = sc.nextLine();\n\t\t\/\/ Convert String to character array\n\t\tchar ch[]=str.toCharArray(); \n\n\t\t\/\/ Reversing the String\n \t\tString rev=\"\"; \n \t\tfor(int i=ch.length-1;i>=0;i--){ \n \t\trev+=ch[i]; \n \t\t} \n\t\tSystem.out.println(rev);\n\t}\n }<\/code><\/pre>\n\n\n\n<\/span>Using String Builder :<\/strong><\/span><\/h2>\n\n\n\nLet’s see How you can Reverse a String in Java<\/strong> using String Builder Class in Java. Now, let me tell you about String Builder class in brief. String Builder in Java are also known as mutable or modifiable strings. As Strings in java are non mutable, StringBuilders are mutable.<\/p>\n\n\n\nNow, To Reverse a String in Java, we will convert the given String into String Builder and then you the reverse() method of the String Builder class. At last we convert the reversed String Builder object to a String object by using its toString() method. By this way you can Reverse a String in Java.<\/p>\n\n\n\n
So, this is the code to Reverse a String in Java<\/strong> using StringBuilder<\/strong> class.<\/p>\n\n\n\n\n import java.util.*;\n import java.lang.*;\n import java.io.*;\n\n\n public class UsingStringBuilder\n {\n\tpublic static void main (String[] args) throws java.lang.Exception\n\t{\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t\/\/Input the String we want to reverse\n\t\tString s = sc.nextLine();\n\n\t\t\/\/ Converting the String to a StringBuilder object\n\t\tStringBuilder sb = new StringBuilder(s);\n\n\t\t\/\/ Reversing the StringBuilder object using its reverse() method\n\t\tStringBuilder reverseSb = sb.reverse();\n\n\t\t\/\/ Converting StringBuilder back to String using toString() method\n\t\tString reverseString = reverseSb.toString();\n\n\t\t\/\/ Printing the Reversed String\n\t\tSystem.out.println(reverseString);\n\t}\n } <\/code><\/pre>\n\n\n\n<\/span>Using String Buffer :<\/strong><\/span><\/h2>\n\n\n\nLet’s see How you can Reverse a String in Java<\/strong> using String Buffer Class in Java. Just like using String Builder class we can use String Buffer class to Reverse a String in Java.<\/p>\n\n\n\nStringBuffer Class in Java are also mutable or modifiable Strings. Now, you would be wondering what is the difference between StringBuffer and StringBuilder class in Java.<\/p>\n\n\n\n
StringBuffer are thread-safe, synchronized Java class whereas, StringBuilder are not thread-safe and are also non-synchronized. But StringBuffer are Slow<\/strong> and StringBuilder are Fast<\/strong>. That’s why StringBuilder were built and are used to speed up the process if there is no need of thread safety and sync in your code.<\/p>\n\n\n\nSo, this is the code to Reverse a String in Java<\/strong> using StringBuffer<\/strong> class.<\/p>\n\n\n\n\n import java.util.*;\n import java.lang.*;\n import java.io.*;\n\n\n public class UsingStringBuffer\n {\n\tpublic static void main (String[] args) throws java.lang.Exception\n\t{\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t\/\/Input the String we want to reverse\n\t\tString s = sc.nextLine();\n\n\t\t\/\/ Converting the String to a StringBuffer object\n\t\tStringBuffer sb = new StringBuffer(s);\n\n\t\t\/\/ Reversing the StringBuffer object using its reverse() method\n\t\tStringBuffer reverseSb = sb.reverse();\n\n\t\t\/\/ Converting StringBuffer back to String using toString() method\n\t\tString reverseString = reverseSb.toString();\n\n\t\t\/\/ Printing the Reversed String\n\t\tSystem.out.println(reverseString);\n\t}\n } <\/code><\/pre>\n\n\n\n<\/span>Using Stack :<\/strong><\/span><\/h2>\n\n\n\nThere is one more to Reverse a String in Java and that is using the Stack Data structure present as a Collections in Java.<\/p>\n\n\n\n
As we know, Stack uses LIFO<\/strong> that stands for “Last IN First OUT” <\/strong>to store data. By using this property of Java we can easily reverse a String. We will convert the String into a character array and then push every character while traversing the array first to last, and then, when you pop the data from stack the character will be returned in LIFO manner, i.e. popping the last pushed character first and so on.<\/p>\n\n\n\nSo, let’s see the code to Reverse a String in Java<\/strong> using Stack Collection<\/strong>.<\/p>\n\n\n\n\n import java.util.*;\n import java.lang.*;\n import java.io.*;\n\n\n public class UsingStack\n {\n\tpublic static void main (String[] args) throws java.lang.Exception\n\t{\n\t\tScanner sc = new Scanner(System.in);\n\n \/\/Input the String we want to reverse\n\t\tString s = sc.nextLine();\n\n \/\/Convert String into char array using toCharArray() method\n\t\tchar charArray[] = s.toCharArray(); \n\n \/\/Declare a stack and push each character in it\n\t\tStack<Character> stack = new Stack<>();\n\t\tfor(int i=0;i<s.length();i++){\n\t\t\tstack.add(charArray[i]);\n\t\t}\n\t\tString reverseString = \"\";\n\n \/\/ Empty the stack and concatinate the char to form string\n\t\twhile(stack.empty()==false){\n\t\t\treverseString += stack.pop();\n\t\t}\n\n \/\/ Print the reversed string\n\t\tSystem.out.println(reverseString);\n\t}\n }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Today, we are going to learn the basic java program of How to Reverse a String in Java ? Strings in java are sequence of characters or you can also call it as an array of character, e.g. “Java” string is an array of 4 character type. In Java, Strings object are immutable (unchangeable). We […]<\/p>\n","protected":false},"author":3,"featured_media":547,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}},"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[31,422],"tags":[22,61,62],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/www.codingbroz.com\/wp-content\/uploads\/2021\/01\/how-to-reverse-a-string-in-java-codingbroz.png","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/posts\/202"}],"collection":[{"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/comments?post=202"}],"version-history":[{"count":5,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/posts\/202\/revisions"}],"predecessor-version":[{"id":548,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/posts\/202\/revisions\/548"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/media\/547"}],"wp:attachment":[{"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/media?parent=202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/categories?post=202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/tags?post=202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}