Title: Java reverse String Example - Java Code Geeks
Open Graph Title: Java reverse String Example - Java Code Geeks
Description: In this example, we are going to see how to reverse a String in Java. We are going to see how to this, very conveniently using a StringBuffer, and two
Open Graph Description: In this example, we are going to see how to reverse a String in Java. We are going to see how to this, very conveniently using a StringBuffer, and two
Mail addresses
support@javacodegeeks.com
Opengraph URL: https://examples.javacodegeeks.com/java-reverse-string-example/
Generator: WordPress 6.9.4
Domain: examples.javacodegeeks.com
{"@context":"https://schema.org","@graph":[{"@type":"Article","@id":"https://examples.javacodegeeks.com/java-reverse-string-example/#article","isPartOf":{"@id":"https://examples.javacodegeeks.com/java-reverse-string-example/"},"author":{"name":"Nikos Maravitsas","@id":"https://examples.javacodegeeks.com/#/schema/person/686a494fbb577a8d1231862b139cda6b"},"headline":"Java reverse String Example","datePublished":"2014-04-03T14:51:23+00:00","dateModified":"2020-08-11T08:15:54+00:00","mainEntityOfPage":{"@id":"https://examples.javacodegeeks.com/java-reverse-string-example/"},"wordCount":486,"commentCount":0,"publisher":{"@id":"https://examples.javacodegeeks.com/#organization"},"image":{"@id":"https://examples.javacodegeeks.com/java-reverse-string-example/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","articleSection":["String"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https://examples.javacodegeeks.com/java-reverse-string-example/#respond"]}]},{"@type":"WebPage","@id":"https://examples.javacodegeeks.com/java-reverse-string-example/","url":"https://examples.javacodegeeks.com/java-reverse-string-example/","name":"Java reverse String Example - Java Code Geeks","isPartOf":{"@id":"https://examples.javacodegeeks.com/#website"},"primaryImageOfPage":{"@id":"https://examples.javacodegeeks.com/java-reverse-string-example/#primaryimage"},"image":{"@id":"https://examples.javacodegeeks.com/java-reverse-string-example/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","datePublished":"2014-04-03T14:51:23+00:00","dateModified":"2020-08-11T08:15:54+00:00","description":"In this example, we are going to see how to reverse a String in Java. We are going to see how to this, very conveniently using a StringBuffer, and two","breadcrumb":{"@id":"https://examples.javacodegeeks.com/java-reverse-string-example/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https://examples.javacodegeeks.com/java-reverse-string-example/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/java-reverse-string-example/#primaryimage","url":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","width":150,"height":150,"caption":"Bipartite Graph"},{"@type":"BreadcrumbList","@id":"https://examples.javacodegeeks.com/java-reverse-string-example/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://examples.javacodegeeks.com/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https://examples.javacodegeeks.com/category/java-development/"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https://examples.javacodegeeks.com/category/java-development/core-java/"},{"@type":"ListItem","position":4,"name":"lang","item":"https://examples.javacodegeeks.com/category/java-development/core-java/lang/"},{"@type":"ListItem","position":5,"name":"String","item":"https://examples.javacodegeeks.com/category/java-development/core-java/lang/string/"},{"@type":"ListItem","position":6,"name":"Java reverse String Example"}]},{"@type":"WebSite","@id":"https://examples.javacodegeeks.com/#website","url":"https://examples.javacodegeeks.com/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https://examples.javacodegeeks.com/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://examples.javacodegeeks.com/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https://examples.javacodegeeks.com/#organization","name":"Exelixis Media P.C.","url":"https://examples.javacodegeeks.com/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/#/schema/logo/image/","url":"https://examples.javacodegeeks.com/wp-content/uploads/2022/06/exelixis-logo.png","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2022/06/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https://examples.javacodegeeks.com/#/schema/logo/image/"},"sameAs":["https://www.facebook.com/javacodegeeks","https://x.com/javacodegeeks"]},{"@type":"Person","@id":"https://examples.javacodegeeks.com/#/schema/person/686a494fbb577a8d1231862b139cda6b","name":"Nikos Maravitsas","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/#/schema/person/image/","url":"https://examples.javacodegeeks.com/wp-content/uploads/2013/10/Nikos-Maravitsas-80x96.jpg","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2013/10/Nikos-Maravitsas-80x96.jpg","caption":"Nikos Maravitsas"},"description":"Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. During his studies he discovered his interests about software development and he has successfully completed numerous assignments in a variety of fields. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.","sameAs":["http://www.javacodegeeks.com/"],"url":"https://examples.javacodegeeks.com/author/nikos-maravitsas/"}]}
{"@context":"http:\/\/schema.org","@type":"BreadcrumbList","@id":"#Breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"item":{"name":"Home","@id":"https:\/\/examples.javacodegeeks.com\/"}},{"@type":"ListItem","position":2,"item":{"name":"Java Development","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"}},{"@type":"ListItem","position":3,"item":{"name":"Core Java","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/"}},{"@type":"ListItem","position":4,"item":{"name":"lang","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/lang\/"}},{"@type":"ListItem","position":5,"item":{"name":"String","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/lang\/string\/"}}]}
{"@context":"http:\/\/schema.org","@type":"Article","dateCreated":"2014-04-03T17:51:23+03:00","datePublished":"2014-04-03T17:51:23+03:00","dateModified":"2020-08-11T11:15:54+03:00","headline":"Java reverse String Example","name":"Java reverse String Example","keywords":[],"url":"https:\/\/examples.javacodegeeks.com\/java-reverse-string-example\/","description":"In this example, we are going to see how to reverse a String in Java. We are going to see how to this, very conveniently using a StringBuffer, and two other solutions using recursion and simply using","copyrightYear":"2014","articleSection":"String","articleBody":"\nIn this example, we are going to see how to reverse a String in Java. We are going to see how to this, very conveniently using a StringBuffer, and two other solutions using recursion and simply using a character array.\n\n\n\nLet's see the examples.\n\n\n\n1. Reversing a String using StringBuffer\n\n\n\nLet's see how you can use a StringBuffer to reverse a String in a single line, using its reverse API method: \n\n\n\nStringReverseExample.java\n\n\n\n\n\n\n\n\n\n01\n02\n03\n04\n05\n06\n07\n08\n09\n10\n11\n12\n13\n\n\n\npackage com.javacodegeeks.core.string;\n \npublic class StringReverseExample {\n \n public static void main(String[] args) {\n \n String str = \"This was a String to reverse\";\n \n String reverse = new StringBuffer(str).reverse().toString();\n \n System.out.println(\"Normal String is : \" + str + \" \\nReverse String is : \"+reverse);\n }\n}\n\n\n\n\n\n\n\n\n\n\nThis will output:\n\n\n\nNormal String is : This was a String to reverse \nReverse String is : esrever ot gnirtS a saw sihT\n\n\n\nThis is the easiest way you can reverse a String in Java.\n\n\n\n2. Reversing a String using iteration through characters\n\n\n\nIn this case we are first going to obtain a character array from the String in order to handle its characters individually. Then, we can either choose to create another array of the same length and simply copy the last character of the first array to first character of the second, then the second to last character of the first array to the second character of the second e.t.c. Another way would be to swap the characters in place in the same array.\n\n\n\nLet's see how:\n\n\n\nStringReverseExample.java\n\n\n\n\n\n\n\n\n\n01\n02\n03\n04\n05\n06\n07\n08\n09\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n\n\n\npackage com.javacodegeeks.core.string;\n \npublic class StringReverseExample {\n \n public static void main(String[] args) {\n \n String str = \"This was a String to reverse\";\n \n char[] characters = str.toCharArray();\n char[] reverseChars = new char[characters.length];\n \n for( int i = 0 ; i < characters.length ; i++){\n reverseChars[i] = characters[(characters.length-1)-i]; \n }\n \n String reverse = new String(reverseChars);\n System.out.println(\"Normal String is : \" + str + \" \\nReverse String is : \"+reverse);\n \n \/\/ If you don't want to use double the memory\n \/\/ swap the characters in place\n for( int i = 0 ; i < characters.length\/2 ; i++){\n char t = characters[i];\n characters[i] = characters[(characters.length-1)-i];\n characters[(characters.length-1)-i] = t;\n }\n \n System.out.println(\"Normal String is : \" + str + \" \\nReverse String is : \"+String.valueOf(characters));\n }\n}\n\n\n\n\n\n\n\n\n\n\nThis will output:\n\n\n\nNormal String is : This was a String to reverse \nReverse String is : esrever ot gnirtS a saw sihT\nNormal String is : This was a String to reverse \nReverse String is : esrever ot gnirtS a saw sihT\n\n\n\n3. Reversing a String using recursion\n\n\n\nThis is how you can use recursion to compute the reverse of String:\n\n\n\nStringReverseExample.java\n\n\n\n\n\n\n\n\n\n01\n02\n03\n04\n05\n06\n07\n08\n09\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n\n\n\npackage com.javacodegeeks.core.string;\n \npublic class StringReverseExample {\n \n public static void main(String[] args) {\n \n String str = \"Java\";\n String reverseStr = reverseRecurive(str);\n System.out.println(\"Normal String is : \" + str + \" \\nReverse String is : \"+reverseStr); \n }\n \n public static String reverseRecurive(String str) {\n \n if (str.length() <= 1) {\n return str;\n }\n \n return reverseRecurive(str.substring(1)) + str.charAt(0);\n \n }\n}\n\n\n\n\n\n\n\n\n\n\nThis will output:\n\n\n\nNormal String is : Java\nReverse String is : avaJ\n\n\n\nAt first sight, the above recursive algorithm, might not be straight forward. Let's see how this works. We can model the recursive calls like this\n\n\n\n1st call - reverseRecurive(\"Java\") : will return reverseRecurive(\"ava\") + \"J\"2nd call - reverseRecurive(\"ava\") : will return reverseRecurive(\"va\") + \"a\"3rd call - reverseRecurive(\"va\") : will return reverseRecurive(\"a\") + \"v\"4th call - reverseRecurive(\"a\") : will return \"a\"3rd call - will return : reverseRecurive(\"a\") + \"v\" = \"a\" + \"v\"2nd call - will return : reverseRecurive(\"va\") + \"a\" = \"a\" + \"v\" + \"a\"1st call - will return : reverseRecurive(\"ava\") + \"J\" = \"a\" + \"v\" + \"a\" + \"J\"\n\n\n\nThat should make it a bit more clear. As you can see, in this example the depth of the recursion is equal to the length of the String. If the String is very long and stack size is of major concern, Divide and Conquer is the way to go:\n\n\n\nStringReverseExample.java\n\n\n\n\n\n\n\n\n\n01\n02\n03\n04\n05\n06\n07\n08\n09\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n\n\n\npackage com.javacodegeeks.core.string;\n \npublic class StringReverseExample {\n \n public static void main(String[] args) {\n \n String str = \"Hello World\";\n \n String reverseStr = reverseRecursiveDivide(str);\n System.out.println(\"Normal String is : \" + str\n + \" \\nReverse String is : \" + reverseStr);\n }\n \n \n public static String reverseRecursiveDivide(String str) {\n if (str.length() <= 1) {\n return str;\n }\n \n return reverseRecursiveDivide(str.substring(str.length() \/ 2, str.length())) + reverseRecursiveDivide(str.substring(0, str.length() \/ 2));\n }\n}\n\n\n\n\n\n\n\n\n\n\nThis will output:\n\n\n\nNormal String is : Hello World \nReverse String is : dlroW olleH\n\n\n\nThe logic here is that you take the first half of the String, you reverse it, take the other half of the String and reverse it, and finally concatenate them with the reverse order ( reversed second half + reversed first half). And that's it.\n\n\n\nTo sum up, there is no particular reason why you shouldn't use the simple first method with StringBuffer. But if you don't want to do that, for example in an interview question, as you can see there are plenty of fast solutions.\n\n\n\n4. Download Source Code\n\n\n\nThis was a Java String reverse Example. \n\n\n\nDownload\nYou can download the source code of this example here: Java reverse String Example\n\n\n\nLast updated on Apr. 08th, 2020\n","publisher":{"@id":"#Publisher","@type":"Organization","name":"Examples Java Code Geeks","logo":{"@type":"ImageObject","url":"\/wp-content\/uploads\/2013\/05\/JavaCodeGeeks-logo.png"},"sameAs":["https:\/\/feeds.feedburner.com\/examplesjavacodegeeks","https:\/\/www.facebook.com\/javacodegeeks","https:\/\/twitter.com\/javacodegeeks","https:\/\/www.linkedin.com\/groups\/3810709\/","https:\/\/www.youtube.com\/channel\/UCxoUc7Rar2q90Gu0nT2ffuQ","https:\/\/github.com\/javacodegeeks\/"]},"sourceOrganization":{"@id":"#Publisher"},"copyrightHolder":{"@id":"#Publisher"},"mainEntityOfPage":{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/java-reverse-string-example\/","breadcrumb":{"@id":"#Breadcrumb"}},"author":{"@type":"Person","name":"Nikos Maravitsas","url":"https:\/\/examples.javacodegeeks.com\/author\/nikos-maravitsas\/"},"image":{"@type":"ImageObject","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","width":1200,"height":150}}
| og:locale | en_US |
| og:type | article |
| og:site_name | Examples Java Code Geeks |
| article:publisher | https://www.facebook.com/javacodegeeks |
| article:published_time | 2014-04-03T14:51:23+00:00 |
| article:modified_time | 2020-08-11T08:15:54+00:00 |
| og:image | https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg |
| og:image:width | 150 |
| og:image:height | 150 |
| og:image:type | image/jpeg |
| author | Nikos Maravitsas |
| twitter:card | summary_large_image |
| twitter:creator | @javacodegeeks |
| twitter:label1 | Written by |
| twitter:data1 | Nikos Maravitsas |
| twitter:label2 | Est. reading time |
| twitter:data2 | 6 minutes |
| None | IE=edge |
| msapplication-TileImage | https://examples.javacodegeeks.com/wp-content/uploads/2013/05/JavaCodeGeeks-favIcon.png |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1