{"id":11877,"date":"2014-07-22T11:00:35","date_gmt":"2014-07-22T08:00:35","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=11877"},"modified":"2020-09-30T10:52:44","modified_gmt":"2020-09-30T07:52:44","slug":"java-stringbuffer-example","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/stringbuffer-java-example\/","title":{"rendered":"StringBuffer Java Example"},"content":{"rendered":"<p>In this example, we are going to present the StringBuffer <a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/lang\/StringBuffer.html\">class<\/a> in Java, and StringBuffer vs StringBuilder which is\u00a0contained in the <code>java.lang<\/code> package. We are going to show some of its most important uses and methods, and explain why and when it should be used, as well as the difference between <code>StringBuffer<\/code> and <code>String<\/code>.<\/p>\n<h2 class=\"wp-block-heading\">1. String vs StringBuffer<\/h2>\n<p>Strings in Java are<strong> immutable<\/strong>. This means that when you instantiate a new <code>String<\/code> Object, you can not ever alter it. However, some&nbsp;of the most commonly seen code in Java happens to look like this:<\/p>\n<pre class=\"brush:java\">String str = \"hello\";\nstr = str + \" world\";\n\/\/this prints \"hello world\"\n<\/pre>\n<p>So, what is going on&nbsp;here? Although it seems that we are appending&nbsp;to the String object, that in fact does not happen. What actually happens, is that the JVM creates a new <code>StringBuffer<\/code> object, which afterwards appends the different strings and in the end creates a new string, with a reference to the original. So essentially, we do not append a string to another, but <strong>we destroy the original <code>String<\/code> and use its variable to point to a whole new String<\/strong> (the concat of the first and the second one), which is also immutable.<\/p>\n<p>Although it may seem that it is not a huge difference, it is in fact very important for <strong>optimization<\/strong>. While it is easier (and sometimes even advisable) to use the + operator for simplicity, when you have to do with a huge number of string concats you should try to use something like <code>StringBuffer<\/code> to make it faster. We will show the huge speed difference in the example later.<\/p>\n<h2 class=\"wp-block-heading\">2. StringBuffer vs StringBuilder<\/h2>\n<p>Till Java 1.5 developers had a choice between <code>String<\/code> and <code>StringBuffer<\/code>. With Java 1.5 developers got a new option StringBuilder. In this section let us see the differences between StringBuffer and <code>StingBuilder<\/code>.<\/p>\n<figure class=\"wp-block-table\">\n<table class=\"has-fixed-layout\">\n<thead>\n<tr>\n<th>StringBuffer<\/th>\n<th>StringBuilder<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Thead safe as all methods are synchronised<\/td>\n<td>Not thead safe, othewise it offers same features as <code>StringBuffer<\/code><\/td>\n<\/tr>\n<tr>\n<td>Offered as pat of Java early releases<\/td>\n<td>Introduced only in Java 1.5<\/td>\n<\/tr>\n<tr>\n<td>Less performant as all methods are marked synchronised<\/td>\n<td>Offers better performance than <code>StringBuffer<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table><figcaption>StringBuffer Vs StringBuilder Table<\/figcaption><\/figure>\n<\/p>\n<p>If you operate in a single-threaded environment or don&#8217;t care about thead-safety then use<code> StringBuilder<\/code>.<\/p>\n<h2 class=\"wp-block-heading\">3. StringBuffer constructors<\/h2>\n<p><code>StringBuffer<\/code> offers below different constructors:<\/p>\n<figure class=\"wp-block-table\">\n<table class=\"has-fixed-layout\">\n<thead>\n<tr>\n<th>Constructor<\/th>\n<th>Explanation<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>StringBuffer()<\/code><\/td>\n<td>Default constructor which allocates uninitialized <code>StringBuffer<\/code> of 16 characters capacity<\/td>\n<\/tr>\n<tr>\n<td><code>StringBuffer(CharSequence seq)<\/code><\/td>\n<td>Constructs a <code>StringBuffer<\/code> with same contents as in input chracter sequence<\/td>\n<\/tr>\n<tr>\n<td><code>StringBuffer(int capacity)<\/code><\/td>\n<td>Constructs an empty <code>StringBuffer<\/code> with specified capacity number of characters as capacity<\/td>\n<\/tr>\n<tr>\n<td><code>StringBuffer(Sting s)<\/code><\/td>\n<td>Constructs a <code>StringBuffer<\/code> initialized with the specifie input <code>String<\/code> s<\/td>\n<\/tr>\n<\/tbody>\n<\/table><figcaption>StringBuffer constructors Table<\/figcaption><\/figure>\n<h2 class=\"wp-block-heading\">4. StringBuffer Java Example<\/h2>\n<p>Below Example depicts the usage of <code>StringBuffer<\/code>,<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<div class=\"wp-block-codemirror-blocks-code-block code-block\">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;file&quot;,&quot;fullScreenButton&quot;:false,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;StringBufferMain.java&quot;,&quot;language&quot;:&quot;Java&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;java&quot;}\">public class StringBufferMain {\n\n    public static void main(String[] args) {\n        StringBuffer buffer = new StringBuffer();\n        \n        \/\/ Append the string representation of the argument to the end of the buffer.\n        \/\/ In this example we use a string, but the method also accepts int, float,\n        \/\/ double, boolean, char (or char[]), as well as objects.\n        buffer.append(&quot;Hello World!&quot;);\n        System.out.println(buffer.toString());\n        \n        \/\/ Delete the specified substring by providing the start and the end\n        \/\/ of the sequence.\n        buffer.delete(5, 11);\n        System.out.println(buffer.toString());\n        \n        \/\/ Delete just one char by providing its position.\n        buffer.deleteCharAt(5);\n        System.out.println(buffer.toString());\n        \n        \/\/ Insert a string in a specified place inside the buffer.\n        buffer.insert(0, &quot;World &quot;);\n        System.out.println(buffer.toString());\n        \n        \/\/ Get the index that the specified substring starts at.\n        System.out.println(&quot;Index of Hello: &quot; + buffer.indexOf(&quot;Hello&quot;));\n        System.out.println(); \/\/ Empty line\n        \n        \n        \n        \/\/ You can also instantiate a new StringBuffer and provide\n        \/\/ the initial String in the constructor.\n        StringBuffer newBuffer = new StringBuffer(&quot;This is a Hello World string. Hello!&quot;);\n        \n        \/\/ You can use lastIndexOf(String) to get the last time that a specified\n        \/\/ substring appears in the StringBuffer.\n        System.out.println(&quot;Index of Hello: &quot; + newBuffer.indexOf(&quot;Hello&quot;));\n        System.out.println(&quot;Last index of Hello: &quot; + newBuffer.lastIndexOf(&quot;Hello&quot;));\n        \n        \/\/ You can also replace a specific sub-sequence of the StringBuffer with another string.\n        \/\/ The size does not need to be the same, as shown here.\n        newBuffer.replace(0, 4, &quot;That here&quot;);\n        System.out.println(newBuffer.toString());\n        \n        \/\/ You can replace a single char using this method here. We want to\n        \/\/ replace the last character of the string, so instead of counting the length,\n        \/\/ we will use the provided length() method, and replace the char in the last index.\n        newBuffer.setCharAt(newBuffer.length() - 1, '?');\n        System.out.println(newBuffer.toString());\n        \n        \/\/ You can reverse the StringBuffer as well!\n        newBuffer.reverse();\n        System.out.println(newBuffer.toString());\n        \n        \n        compareTime();\n    }\n\n    private static void compareTime() {\n        long startTime;\n        String str = &quot;&quot;;\n        StringBuffer buffer = new StringBuffer();\n        \n        \/\/ Using String\n        startTime = System.currentTimeMillis();\n        for (int i = 0; i &lt; 10000; i++) {\n            str += &quot;extra&quot;;\n        }\n        System.out.println(&quot;Time using String: &quot;\n                + (System.currentTimeMillis() - startTime) + &quot; ms.&quot;);\n        \n        \/\/ Using StringBuffer\n        startTime = System.currentTimeMillis();\n        for (int i = 0; i &lt; 10000; i++) {\n            buffer.append(&quot;extra&quot;);\n        }\n        System.out.println(&quot;Time using StringBuffer: &quot;\n                + (System.currentTimeMillis() - startTime) + &quot; ms.&quot;);\n    }\n}<\/pre>\n<\/div>\n<p>The first thing that you need&nbsp;to draw your attention to is the time output (the last 2 lines). You can see an enormous difference in time, which makes it pretty clear that <code>Stringbuffer<\/code> is <strong>the recommended approach when you have to deal with a large number of strings<\/strong> (in our example it is 10000). 488 ms vs. 2 ms makes a whole lot of a difference, especially in heavy applications, where 10000 strings might be the minimum threshold.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"465\" height=\"192\" src=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2020\/05\/stingbuffer-final.png\" alt=\"StringBuffer Java - Output\" class=\"wp-image-89879\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2020\/05\/stingbuffer-final.png 465w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2020\/05\/stingbuffer-final-300x124.png 300w\" sizes=\"(max-width: 465px) 100vw, 465px\" \/><figcaption>Output<\/figcaption><\/figure>\n<\/div>\n<p>Secondly, let&#8217;s take a look at some of the most interesting and important StringBuffer methods, which were used in the example:<\/p>\n<ul class=\"wp-block-list\">\n<li><code>append(String str)<\/code>: This method is used to add a string in the end of the StringBuffer. You can also use other versions of this method, such as append(int), where the String representation of the int will be added.<\/li>\n<li><code>delete(int start, int finish)<\/code>: Delete the specified substring from the StringBuffer.<\/li>\n<li><code>deleteCharAt(int position)<\/code>: Delete the character at the specified position.<\/li>\n<li><code>setCharAt(int position, char c)<\/code>: Replace a character in the StringBuffer.<\/li>\n<li><code>insert(int start, String str)<\/code>: insert a new string anywhere you want in the StringBuffer, by using the first argument of the method as the starting position of the new string.<\/li>\n<li><code>replace(int start, int finish, String str)<\/code>: You can replace a whole substring with another string in the StringBuffer. The inserted string does not need to be of the same size, which makes this method incredibly useful.<\/li>\n<li><code>reverse()<\/code>: Reverses the whole StringBuffer (first character becomes last etc).<\/li>\n<\/ul>\n<p>Of course, there are many more methods and functionality, but more or less most of them are variations of the ones presented here. By having the above methods in mind you can make use of all the important parts of StringBuffer and make your String manipulation <strong>faster, more optimized and simpler<\/strong> in many cases.<\/p>\n<h2 class=\"wp-block-heading\">5. Download the source code<\/h2>\n<p>This was an example of StringBuffer use in Java.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the example here: <a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/StringBufferExample.rar\"><strong>StringBuffer Java Example<\/strong><\/a><\/div>\n<p><strong>Last updated on May 25th, 2020<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this example, we are going to present the StringBuffer class in Java, and StringBuffer vs StringBuilder which is\u00a0contained in the java.lang package. We are going to show some of its most important uses and methods, and explain why and when it should be used, as well as the difference between StringBuffer and String. 1. &hellip;<\/p>\n","protected":false},"author":22,"featured_media":1204,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[57],"tags":[241,242],"class_list":["post-11877","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-stringbuffer","tag-string-2","tag-stringbuffer-2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>StringBuffer Java Example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"In this example, we are going to present the StringBuffer class in Java, and StringBuffer vs StringBuilder which is\u00a0contained in the java.lang package. We\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/examples.javacodegeeks.com\/stringbuffer-java-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"StringBuffer Java Example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"In this example, we are going to present the StringBuffer class in Java, and StringBuffer vs StringBuilder which is\u00a0contained in the java.lang package. We\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/stringbuffer-java-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Examples Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2014-07-22T08:00:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-09-30T07:52:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Ilias Koutsakis\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ilias Koutsakis\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/stringbuffer-java-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/stringbuffer-java-example\/\"},\"author\":{\"name\":\"Ilias Koutsakis\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/fb0976e76a1d563d8abc790ed9a1871c\"},\"headline\":\"StringBuffer Java Example\",\"datePublished\":\"2014-07-22T08:00:35+00:00\",\"dateModified\":\"2020-09-30T07:52:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/stringbuffer-java-example\/\"},\"wordCount\":704,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/stringbuffer-java-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"keywords\":[\"string\",\"stringbuffer\"],\"articleSection\":[\"StringBuffer\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/stringbuffer-java-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/stringbuffer-java-example\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/stringbuffer-java-example\/\",\"name\":\"StringBuffer Java Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/stringbuffer-java-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/stringbuffer-java-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"datePublished\":\"2014-07-22T08:00:35+00:00\",\"dateModified\":\"2020-09-30T07:52:44+00:00\",\"description\":\"In this example, we are going to present the StringBuffer class in Java, and StringBuffer vs StringBuilder which is\u00a0contained in the java.lang package. We\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/stringbuffer-java-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/stringbuffer-java-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/stringbuffer-java-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\/stringbuffer-java-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\":\"StringBuffer\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/lang\/stringbuffer\/\"},{\"@type\":\"ListItem\",\"position\":6,\"name\":\"StringBuffer Java 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\/fb0976e76a1d563d8abc790ed9a1871c\",\"name\":\"Ilias Koutsakis\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/06\/Ilias-Koutsakis-96x96.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/06\/Ilias-Koutsakis-96x96.jpg\",\"caption\":\"Ilias Koutsakis\"},\"description\":\"Ilias has graduated from the Department of Informatics and Telecommunications of the National and Kapodistrian University of Athens. He is interested in all aspects of software engineering, particularly data mining, and loves the challenge of working with new technologies. He is pursuing the dream of clean and readable code on a daily basis.\",\"sameAs\":[\"http:\/\/www.javacodegeeks.com\/\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/ilias-koutsakis\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"StringBuffer Java Example - Java Code Geeks","description":"In this example, we are going to present the StringBuffer class in Java, and StringBuffer vs StringBuilder which is\u00a0contained in the java.lang package. We","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:\/\/examples.javacodegeeks.com\/stringbuffer-java-example\/","og_locale":"en_US","og_type":"article","og_title":"StringBuffer Java Example - Java Code Geeks","og_description":"In this example, we are going to present the StringBuffer class in Java, and StringBuffer vs StringBuilder which is\u00a0contained in the java.lang package. We","og_url":"https:\/\/examples.javacodegeeks.com\/stringbuffer-java-example\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2014-07-22T08:00:35+00:00","article_modified_time":"2020-09-30T07:52:44+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","type":"image\/jpeg"}],"author":"Ilias Koutsakis","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Ilias Koutsakis","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/stringbuffer-java-example\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/stringbuffer-java-example\/"},"author":{"name":"Ilias Koutsakis","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/fb0976e76a1d563d8abc790ed9a1871c"},"headline":"StringBuffer Java Example","datePublished":"2014-07-22T08:00:35+00:00","dateModified":"2020-09-30T07:52:44+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/stringbuffer-java-example\/"},"wordCount":704,"commentCount":1,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/stringbuffer-java-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","keywords":["string","stringbuffer"],"articleSection":["StringBuffer"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/stringbuffer-java-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/stringbuffer-java-example\/","url":"https:\/\/examples.javacodegeeks.com\/stringbuffer-java-example\/","name":"StringBuffer Java Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/stringbuffer-java-example\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/stringbuffer-java-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","datePublished":"2014-07-22T08:00:35+00:00","dateModified":"2020-09-30T07:52:44+00:00","description":"In this example, we are going to present the StringBuffer class in Java, and StringBuffer vs StringBuilder which is\u00a0contained in the java.lang package. We","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/stringbuffer-java-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/stringbuffer-java-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/stringbuffer-java-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\/stringbuffer-java-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":"StringBuffer","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/lang\/stringbuffer\/"},{"@type":"ListItem","position":6,"name":"StringBuffer Java 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\/fb0976e76a1d563d8abc790ed9a1871c","name":"Ilias Koutsakis","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/06\/Ilias-Koutsakis-96x96.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/06\/Ilias-Koutsakis-96x96.jpg","caption":"Ilias Koutsakis"},"description":"Ilias has graduated from the Department of Informatics and Telecommunications of the National and Kapodistrian University of Athens. He is interested in all aspects of software engineering, particularly data mining, and loves the challenge of working with new technologies. He is pursuing the dream of clean and readable code on a daily basis.","sameAs":["http:\/\/www.javacodegeeks.com\/"],"url":"https:\/\/examples.javacodegeeks.com\/author\/ilias-koutsakis\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/11877","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/users\/22"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=11877"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/11877\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/1204"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=11877"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=11877"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=11877"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}