{"id":109,"date":"2019-01-20T16:37:30","date_gmt":"2019-01-20T16:37:30","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/java\/?p=109"},"modified":"2019-03-02T04:38:15","modified_gmt":"2019-03-02T04:38:15","slug":"22-java-stringbuffer-and-stringbuilder","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/java\/22-java-stringbuffer-and-stringbuilder\/","title":{"rendered":"Java &#8211; StringBuffer &#038; StringBuilder"},"content":{"rendered":"<p>You already know by now that Strings in Java are immutable. This means we can&#8217;t modify them once created. Hence any changes to them would create a new object in the memory.<\/p>\n<p>&nbsp;<\/p>\n<p>We would cover the following:<\/p>\n<ol>\n<li><a href=\"#t1\">Difference between StringBuilder and StringBuffer<\/a><\/li>\n<li><a href=\"#t2\">How to Create a StringBuilder<\/a><\/li>\n<li><a href=\"#t3\">Methods of the StringBuilder Class<\/a><\/li>\n<li><a href=\"#t4\">Some Examples<\/a><\/li>\n<li><a href=\"#t5\">Other StringBuilder Methods<\/a><\/li>\n<li><a href=\"#t6\">Watch the Video<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Difference between StringBuilder and StringBuffer<\/strong><\/h4>\n<p>However, Java provides the StringBuilder and StringBuffer classes to handle this problem. So with this classes, you can create a string that you can modify.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>What then is the difference between the two?<\/strong><\/p>\n<p>Note the following differences:<\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li>StringBuffer methods are thread-safe while StringBuilder methods are not<\/li>\n<li>StringBuffer methods are Synchronized while StringBuilder methods are not<\/li>\n<li>StringBuilder object perform better (are faster) than StringBuffer<\/li>\n<li>You will always have\u00a0 to use StringBuilder. This is recommended unless thread-safety is required.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. How then do you create a StringBuilder Object?<\/strong><\/h4>\n<p>You do this by using the new keyword and calling the StringBuilder constructor. Then you provide a new string. The code below illustrates this<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">StringBuilderTest<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String<span style=\"color: #333333;\">[]<\/span> args<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t<span style=\"color: #888888;\">\/\/Creating a StringBuilder object<\/span>\r\n\t\tStringBuilder sbd <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> StringBuilder<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Java\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t\tsbd<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">append<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\" Tutorials \"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t\t\r\n\t\t<span style=\"color: #888888;\">\/\/Creating a StringBuffer object<\/span>\r\n\t\tStringBuffer sbf <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> StringBuffer<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Java\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t\tsbf<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">append<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\" Tutorials\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t\t\r\n\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span>sbd<span style=\"color: #333333;\">);<\/span>\r\n\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span>sbf<span style=\"color: #333333;\">);<\/span>\r\n\t<span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Listing 1.0: StringBuilder and StringBuffer<\/p>\n<p>&nbsp;<\/p>\n<p>Code in Listing 1.0 creates a StringBuilder string. The next line then appends additional string to the end of the string. We do the same for the StringBuffer<\/p>\n<p>You can see that StringBuilder and StringBuffer are exactly the same in terms of syntax. So based on this, we are just going to focus on StringBuffer.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Methods of StringBuilder Class<\/strong><\/h4>\n<p>We have a number of useful method provided by the StringBuilder class. You probably should take some time to master some of these methods.<\/p>\n<p>They are listed in Table 1.0 below.<\/p>\n<p>&nbsp;<\/p>\n<table class=\"table table-bordered\" align=\"center\">\n<tbody>\n<tr style=\"background-color: #f7f6f3;\">\n<th>SN<\/th>\n<th>Methods and brief description<\/th>\n<\/tr>\n<tr>\n<td class=\"ts\">1<\/td>\n<td><strong>append(String s)<\/strong>Modifies a string by appending a new string to the end. Takes boolean, char, int, long, Strings etc as parameter. It returns the modified StringBuffer object.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">2<\/td>\n<td><strong>reverse()<\/strong>Reverses the value of the given StringBuffer object. For example, abcd becomes dcba. It then returns the modified object.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">3<\/td>\n<td><strong>delete(int start, int end)<\/strong>Deletes a string from the given StringBuilder object starting from the start index to the end index.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">4<\/td>\n<td><strong>insert(int offset, int i)<\/strong>Inserts a string\u00a0<b>s<\/b>\u00a0into\u00a0 the position specified by the offset.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">5<\/td>\n<td><strong>replace(int start, int end, String str)<\/strong>Replaces the characters in the substring of the StringBuilder with characters in the given String.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Table 1.0: StringBuilder Methods<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t4\">4. Some Examples\u00a0<\/strong><\/p>\n<p>Let&#8217;s now demonstrate how some of these methods work. Take a look at the program below. I recommend you make sure to try it yourself. Also look at the comments to see exactly what is happening.<\/p>\n<p>Already you know about the append method. So we\u00a0 examine other methods.<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">StringBuilderTest<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String<span style=\"color: #333333;\">[]<\/span> args<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\tStringBuilder sbd <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> StringBuilder<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Java\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t\t\r\n\t\t<span style=\"color: #888888;\">\/\/1. The append method<\/span>\r\n\t\tsbd<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">append<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\" Tutorials \"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"1. : \"<\/span> <span style=\"color: #333333;\">+<\/span> sbd<span style=\"color: #333333;\">);<\/span>\r\n\t\t\r\n\t\t<span style=\"color: #888888;\">\/\/2. The reverse method<\/span>\r\n\t\tsbd<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">reverse<\/span><span style=\"color: #333333;\">();<\/span>\r\n\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"2. :\"<\/span> <span style=\"color: #333333;\">+<\/span> sbd<span style=\"color: #333333;\">);<\/span>\r\n\t\t\r\n\t\t<span style=\"color: #888888;\">\/\/3. Reverse it back to normal<\/span>\r\n\t\tsbd<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">reverse<\/span><span style=\"color: #333333;\">();<\/span>\r\n\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"3. : \"<\/span> <span style=\"color: #333333;\">+<\/span> sbd<span style=\"color: #333333;\">);<\/span>\r\n\r\n\t\t<span style=\"color: #888888;\">\/\/4. Deletes the first 4 characters<\/span>\r\n\t\tsbd<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">delete<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">4<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"4. :\"<\/span> <span style=\"color: #333333;\">+<\/span> sbd<span style=\"color: #333333;\">);<\/span>\r\n\t\t\r\n\t\t<span style=\"color: #888888;\">\/\/5. Insert JavaScript in the first index of 0<\/span>\r\n\t\tsbd<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">insert<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"JavaScript\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"5. : \"<\/span> <span style=\"color: #333333;\">+<\/span> sbd<span style=\"color: #333333;\">);<\/span>\r\n\t\t\r\n\t\t<span style=\"color: #888888;\">\/\/6. Replace JavaScript with AngularJs<\/span>\r\n\t\tsbd<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">replace<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">10<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"AngularJS\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"6. : \"<\/span> <span style=\"color: #333333;\">+<\/span> sbd<span style=\"color: #333333;\">);<\/span>\r\n\t<span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Listing 1.1: Demonstrating the StringBuilder Methods<\/p>\n<p>&nbsp;<\/p>\n<p>I have added comments to clarify the code. So if you run this code, you will get the output given below.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">.<\/span> <span style=\"color: #333333;\">:<\/span> Java Tutorials \r\n<span style=\"color: #0000dd; font-weight: bold;\">2<\/span><span style=\"color: #333333;\">.<\/span> <span style=\"color: #333333;\">:<\/span> slairotuT avaJ\r\n<span style=\"color: #0000dd; font-weight: bold;\">3<\/span><span style=\"color: #333333;\">.<\/span> <span style=\"color: #333333;\">:<\/span> Java Tutorials \r\n<span style=\"color: #0000dd; font-weight: bold;\">4<\/span><span style=\"color: #333333;\">.<\/span> <span style=\"color: #333333;\">:<\/span> Tutorials \r\n<span style=\"color: #0000dd; font-weight: bold;\">5<\/span><span style=\"color: #333333;\">.<\/span> <span style=\"color: #333333;\">:<\/span> JavaScript Tutorials \r\n<span style=\"color: #0000dd; font-weight: bold;\">6<\/span><span style=\"color: #333333;\">.<\/span> <span style=\"color: #333333;\">:<\/span> AngularJS Tutorials \r\n<\/pre>\n<p>Output of Listing 1.1 Program<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t5\">5. Other StringBuilder Methods<\/strong><\/h4>\n<p>I then provide a list of other methods in the StringBuilder class. So try to play around with them. In this way you can get your head around them. And it becomes\u00a0 clearer to you as well.<\/p>\n<p>Also note that most of these methods apply also to the String class.<\/p>\n<p>&nbsp;<\/p>\n<table class=\"table table-bordered\" align=\"center\">\n<tbody>\n<tr style=\"background-color: #f7f6f3;\">\n<th>SN<\/th>\n<th>Method and description<\/th>\n<\/tr>\n<tr>\n<td class=\"ts\">1<\/td>\n<td><b>capacity()<\/b><\/p>\n<p>Returns the current capacity of the String buffer.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">2<\/td>\n<td><b>charAt(int index)<\/b><\/p>\n<p>The given character of the string currently represented by the string buffer, as specified by the index argument, is returned. Returns an int.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">3<\/td>\n<td><b>void ensureCapacity(int minimumCapacity)<\/b><\/p>\n<p>Sets the the capacity of the buffer is at least equal to the specified value.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">4<\/td>\n<td><b>void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)<\/b><\/p>\n<p>Characters are copied from this string buffer into the destination given by the char array dst.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">5<\/td>\n<td><b>indexOf(String str)<\/b><\/p>\n<p>Returns the particular index within the calling string of the first occurrence of the given substring str.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">6<\/td>\n<td><b>int indexOf(String str, int fromIndex)<\/b><\/p>\n<p>Returns the index within the calling string of the first occurrence of the given substring str, starting at the specified index.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">7<\/td>\n<td><b>int lastIndexOf(String str)<\/b><\/p>\n<p>Returns the index within the calling string of the rightmost occurrence of the given substring str.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">8<\/td>\n<td><b>int lastIndexOf(String str, int fromIndex)<\/b><\/p>\n<p>Returns the index within the calling string of the last occurrence of the given substring str.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">9<\/td>\n<td><b>int length()<\/b><\/p>\n<p>Returns the lengthof this StringBuilder. That is number of characters<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">10<\/td>\n<td><b>void setCharAt(int index, char ch)<\/b><\/p>\n<p>Sets the specified index of this string builder is set to ch.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">11<\/td>\n<td><b>void setLength(int newLength)<\/b><\/p>\n<p>Sets the length of this StringBuilder.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">12<\/td>\n<td><b>CharSequence subSequence(int start, int end)<\/b><\/p>\n<p>Returns a new character sequence that is a subsequence of this calling string.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">13<\/td>\n<td><b>String substring(int start)<\/b><\/p>\n<p>Returns a new String that contains a subsequence of characters currently contained in this StringBuilder. The substring begins at the specified index and extends to the end of the StringBulder.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">14<\/td>\n<td><b>String substring(int start, int end)<\/b><\/p>\n<p>Returns a new String that contains a subsequence of characters currently contained in this StringBuilder.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">15<\/td>\n<td><b>String toString()<\/b><\/p>\n<p>Converts to a string representing the data in the calling StringBuilder.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Table 1.1: List of other StringBuilder Methods<\/p>\n<p>&nbsp;<\/p>\n<h4><a href=\"https:\/\/www.youtube.com\/watch?v=4G6hkhkZ6Vg\"><strong>Watch the Video<\/strong><\/a><\/h4>\n<p><iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/4G6hkhkZ6Vg\" width=\"100%\" height=\"400px\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><span data-mce-type=\"bookmark\" style=\"display: inline-block; width: 0px; overflow: hidden; line-height: 0;\" class=\"mce_SELRES_start\">\ufeff<\/span><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You already know by now that Strings in Java are immutable. This means we can&#8217;t modify them once created. Hence any changes to them would &hellip; <\/p>\n","protected":false},"author":395,"featured_media":133,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-109","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java - StringBuffer &amp; StringBuilder - Java Tutorials<\/title>\n<meta name=\"description\" content=\"In this tutorial you learn about The StringBuilder and StringBuffer classes and methods provided by these classe. Also the difference\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.kindsonthegenius.com\/java\/22-java-stringbuffer-and-stringbuilder\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java - StringBuffer &amp; StringBuilder - Java Tutorials\" \/>\n<meta property=\"og:description\" content=\"In this tutorial you learn about The StringBuilder and StringBuffer classes and methods provided by these classe. Also the difference\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.kindsonthegenius.com\/java\/22-java-stringbuffer-and-stringbuilder\/\" \/>\n<meta property=\"og:site_name\" content=\"Java Tutorials\" \/>\n<meta property=\"article:published_time\" content=\"2019-01-20T16:37:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-03-02T04:38:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/StringBuffer-and-StringBuilder-in-Java.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"953\" \/>\n\t<meta property=\"og:image:height\" content=\"518\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"kindsonthegenius\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"kindsonthegenius\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/22-java-stringbuffer-and-stringbuilder\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/22-java-stringbuffer-and-stringbuilder\\\/\"},\"author\":{\"name\":\"kindsonthegenius\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\"},\"headline\":\"Java &#8211; StringBuffer &#038; StringBuilder\",\"datePublished\":\"2019-01-20T16:37:30+00:00\",\"dateModified\":\"2019-03-02T04:38:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/22-java-stringbuffer-and-stringbuilder\\\/\"},\"wordCount\":852,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/22-java-stringbuffer-and-stringbuilder\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/wp-content\\\/uploads\\\/sites\\\/9\\\/2019\\\/01\\\/StringBuffer-and-StringBuilder-in-Java.jpg\",\"articleSection\":[\"Java Tutorial\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/22-java-stringbuffer-and-stringbuilder\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/22-java-stringbuffer-and-stringbuilder\\\/\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/22-java-stringbuffer-and-stringbuilder\\\/\",\"name\":\"Java - StringBuffer & StringBuilder - Java Tutorials\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/22-java-stringbuffer-and-stringbuilder\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/22-java-stringbuffer-and-stringbuilder\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/wp-content\\\/uploads\\\/sites\\\/9\\\/2019\\\/01\\\/StringBuffer-and-StringBuilder-in-Java.jpg\",\"datePublished\":\"2019-01-20T16:37:30+00:00\",\"dateModified\":\"2019-03-02T04:38:15+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\"},\"description\":\"In this tutorial you learn about The StringBuilder and StringBuffer classes and methods provided by these classe. Also the difference\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/22-java-stringbuffer-and-stringbuilder\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/22-java-stringbuffer-and-stringbuilder\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/22-java-stringbuffer-and-stringbuilder\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/wp-content\\\/uploads\\\/sites\\\/9\\\/2019\\\/01\\\/StringBuffer-and-StringBuilder-in-Java.jpg\",\"contentUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/wp-content\\\/uploads\\\/sites\\\/9\\\/2019\\\/01\\\/StringBuffer-and-StringBuilder-in-Java.jpg\",\"width\":953,\"height\":518,\"caption\":\"StringBuffer and StringBuilder\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/22-java-stringbuffer-and-stringbuilder\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java &#8211; StringBuffer &#038; StringBuilder\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#website\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/\",\"name\":\"Java Tutorials\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\",\"name\":\"kindsonthegenius\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"caption\":\"kindsonthegenius\"},\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/author\\\/kindsonthegenius-2\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java - StringBuffer & StringBuilder - Java Tutorials","description":"In this tutorial you learn about The StringBuilder and StringBuffer classes and methods provided by these classe. Also the difference","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.kindsonthegenius.com\/java\/22-java-stringbuffer-and-stringbuilder\/","og_locale":"en_US","og_type":"article","og_title":"Java - StringBuffer & StringBuilder - Java Tutorials","og_description":"In this tutorial you learn about The StringBuilder and StringBuffer classes and methods provided by these classe. Also the difference","og_url":"https:\/\/www.kindsonthegenius.com\/java\/22-java-stringbuffer-and-stringbuilder\/","og_site_name":"Java Tutorials","article_published_time":"2019-01-20T16:37:30+00:00","article_modified_time":"2019-03-02T04:38:15+00:00","og_image":[{"width":953,"height":518,"url":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/StringBuffer-and-StringBuilder-in-Java.jpg","type":"image\/jpeg"}],"author":"kindsonthegenius","twitter_card":"summary_large_image","twitter_misc":{"Written by":"kindsonthegenius","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.kindsonthegenius.com\/java\/22-java-stringbuffer-and-stringbuilder\/#article","isPartOf":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/22-java-stringbuffer-and-stringbuilder\/"},"author":{"name":"kindsonthegenius","@id":"https:\/\/www.kindsonthegenius.com\/java\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2"},"headline":"Java &#8211; StringBuffer &#038; StringBuilder","datePublished":"2019-01-20T16:37:30+00:00","dateModified":"2019-03-02T04:38:15+00:00","mainEntityOfPage":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/22-java-stringbuffer-and-stringbuilder\/"},"wordCount":852,"commentCount":0,"image":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/22-java-stringbuffer-and-stringbuilder\/#primaryimage"},"thumbnailUrl":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/StringBuffer-and-StringBuilder-in-Java.jpg","articleSection":["Java Tutorial"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.kindsonthegenius.com\/java\/22-java-stringbuffer-and-stringbuilder\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.kindsonthegenius.com\/java\/22-java-stringbuffer-and-stringbuilder\/","url":"https:\/\/www.kindsonthegenius.com\/java\/22-java-stringbuffer-and-stringbuilder\/","name":"Java - StringBuffer & StringBuilder - Java Tutorials","isPartOf":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/22-java-stringbuffer-and-stringbuilder\/#primaryimage"},"image":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/22-java-stringbuffer-and-stringbuilder\/#primaryimage"},"thumbnailUrl":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/StringBuffer-and-StringBuilder-in-Java.jpg","datePublished":"2019-01-20T16:37:30+00:00","dateModified":"2019-03-02T04:38:15+00:00","author":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2"},"description":"In this tutorial you learn about The StringBuilder and StringBuffer classes and methods provided by these classe. Also the difference","breadcrumb":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/22-java-stringbuffer-and-stringbuilder\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.kindsonthegenius.com\/java\/22-java-stringbuffer-and-stringbuilder\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.kindsonthegenius.com\/java\/22-java-stringbuffer-and-stringbuilder\/#primaryimage","url":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/StringBuffer-and-StringBuilder-in-Java.jpg","contentUrl":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/StringBuffer-and-StringBuilder-in-Java.jpg","width":953,"height":518,"caption":"StringBuffer and StringBuilder"},{"@type":"BreadcrumbList","@id":"https:\/\/www.kindsonthegenius.com\/java\/22-java-stringbuffer-and-stringbuilder\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.kindsonthegenius.com\/java\/"},{"@type":"ListItem","position":2,"name":"Java &#8211; StringBuffer &#038; StringBuilder"}]},{"@type":"WebSite","@id":"https:\/\/www.kindsonthegenius.com\/java\/#website","url":"https:\/\/www.kindsonthegenius.com\/java\/","name":"Java Tutorials","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.kindsonthegenius.com\/java\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.kindsonthegenius.com\/java\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2","name":"kindsonthegenius","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","caption":"kindsonthegenius"},"url":"https:\/\/www.kindsonthegenius.com\/java\/author\/kindsonthegenius-2\/"}]}},"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/109","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/users\/395"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/comments?post=109"}],"version-history":[{"count":6,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/109\/revisions"}],"predecessor-version":[{"id":178,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/109\/revisions\/178"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/media\/133"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/media?parent=109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/categories?post=109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/tags?post=109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}