{"id":1279,"date":"2016-05-05T10:53:01","date_gmt":"2016-05-05T10:53:01","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?p=1279"},"modified":"2016-05-05T10:53:01","modified_gmt":"2016-05-05T10:53:01","slug":"diference-string-stringbuffer","status":"publish","type":"post","link":"https:\/\/abhiandroid.com\/java\/diference-string-stringbuffer.html","title":{"rendered":"Difference Between String And StringBuffer in JAVA With Example"},"content":{"rendered":"<h4>\u00a0Difference between String Class and String buffer class<\/h4>\n<ul>\n<li>String class is Immutable whereas String Buffer class is Mutable.<\/li>\n<li>String class consumes more memory whenever we append too many strings, whereas String buffer consumes very less memory.<\/li>\n<li>String class performance is low as compared to string Buffer class when we concatenate too many strings, as proven in the following program of performance testing.<\/li>\n<li>Contents of String object can be compared by equals() method, as it overrides this method, whereas String buffer class do not override equals() method.<\/li>\n<\/ul>\n<hr \/>\n<h4><u><\/u><strong>Performance Test of String and StringBuffer<\/strong><\/h4>\n<p>Let us do a performance testing of String class and String Buffer Class and see what the result is. We have taken objects of both String class and String Buffer Class, than we have appended String value= \u201cAndroid\u201d to both for same time period, and checked time taken.<\/p>\n<pre>public class PerformanceTesting{\r\npublic static String concatinateString(){\r\nString string = \"Abhi\";\r\nfor (int i=0; i&lt;10000; i++){\r\nstring = string + \"Android\";\r\n}\r\nreturn string;\r\n}\r\n\r\npublic static String concatinateStringBuffer(){\r\nStringBuffer stringbuffer = new StringBuffer(\"Abhi\");\r\nfor (int i=0; i&lt;10000; i++){\r\nstringbuffer.append(\"Android\");\r\n}\r\nreturn stringbuffer.toString();\r\n}\r\n\u00a0\r\npublic static void main(String[] args){\r\nlong startTime = System.<em>currentTimeMillis<\/em>();\r\n<em>concatinateString<\/em>();\r\n\r\nSystem.<em>out<\/em>.println(\"Time taken for Concatination with String: \"+(System.<em>currentTimeMillis<\/em>()-startTime)+\"ms\");\r\n\r\nstartTime = System.<em>currentTimeMillis<\/em>();\r\n\r\n<em>concatinateStringBuffer<\/em>();\r\n\r\nSystem.<em>out<\/em>.println(\"Time taken for Concatination with\u00a0 StringBuffer: \"+(System.<em>currentTimeMillis<\/em>()-startTime)+\"ms\");\r\n}\r\n}\r\n<\/pre>\n<p><strong><span style=\"color: #008000;\">Output:<\/span><\/strong><\/p>\n<pre>Time taken for Concatination with String: 465ms\r\n\r\nTime taken for Concatination with\u00a0 StringBuffer: 1ms\u00a0\r\n<\/pre>\n<p>From output as shown above it is clear that String class takes more time than String Buffer class.<\/p>\n<hr \/>\n<h4><strong>String and StringBuffer HashCode Test<\/strong><\/h4>\n<p>Let us do Hash Code testing of String class and String Buffer Class and see what the result is. We have taken objects of both String class and String Buffer Class, than we have appended String value= \u201cAndroid\u201d to both objects. As shown in the following program.<\/p>\n<pre>public class HashCodeTesting{\r\n\r\npublic static void main(String args[]){\r\nSystem.<em>out<\/em>.println(\"Hashcode testing of String:\");\r\nString string=\"Abhi\";\r\nSystem.<em>out<\/em>.println(string.hashCode());\r\nstring=string+\"Android\";\r\nSystem.<em>out<\/em>.println(string.hashCode());\r\n\r\nSystem.<em>out<\/em>.println(\"Hashcode testing of StringBuffer:\");\r\nStringBuffer stringbuffer=new StringBuffer(\"Abhi\");\r\nSystem.<em>out<\/em>.println(stringbuffer.hashCode());\r\nstringbuffer.append(\"Android\");\r\nSystem.<em>out<\/em>.println(stringbuffer.hashCode());\r\n}\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Hashcode testing of String:\r\n\r\n2033922\r\n\r\n1496841997\r\n\r\nHashcode testing of StringBuffer:\r\n\r\n31168322\r\n\r\n31168322\r\n<\/pre>\n<p>From output it is clear that hashcode of String after appending is changed, whereas hashcode of String Buffer remains same after appending another string.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00a0Difference between String Class and String buffer class String class is Immutable whereas String Buffer class is Mutable. String class consumes more memory whenever we append too many strings, whereas String buffer consumes very less memory. String class performance is low as compared to string Buffer class when we concatenate too many strings, as proven &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/diference-string-stringbuffer.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Difference Between String And StringBuffer in JAVA With Example<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,79],"tags":[],"class_list":["post-1279","post","type-post","status-publish","format-standard","hentry","category-archieve","category-java-basics"],"psp_head":"<title>Difference Between String And StringBuffer in JAVA with Examples \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Basic differences are String class is Immutable whereas String Buffer class is Mutable. String class consumes more memory whenever we append too many strings, whereas String buffer consumes very less memory.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/diference-string-stringbuffer.html\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/1279","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/comments?post=1279"}],"version-history":[{"count":0,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/1279\/revisions"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=1279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/categories?post=1279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/tags?post=1279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}