{"id":80,"date":"2016-11-13T18:06:14","date_gmt":"2016-11-13T18:06:14","guid":{"rendered":"http:\/\/codeinsightacademy.com\/cprogramming\/?p=80"},"modified":"2016-11-13T18:17:04","modified_gmt":"2016-11-13T18:17:04","slug":"why-string-is-immutable-in-java","status":"publish","type":"post","link":"https:\/\/codeinsightacademy.com\/blog\/java\/why-string-is-immutable-in-java\/","title":{"rendered":"Why String is Immutable in Java"},"content":{"rendered":"<p>Lets break it into some parts<\/p>\n<p>String s1 = &#8220;hello&#8221;;<br \/>\nThis Statement creates string containing hello and occupy space in memory i.e. in Constant String Pool and and assigned it to reference object s1<\/p>\n<p>String s2 = s1;<br \/>\nThis statement assigns the same string hello to new reference s2<\/p>\n<pre>\r\n         _______\r\n        | \u00a0 \u00a0 \u00a0 |\r\ns1 ----&gt;| hello |&lt;----- s2\r\n        |_______|\r\n<\/pre>\n<p>Both references are pointing to the same string so output the same value as follows.<\/p>\n<p>out.println(s1); \/\/ o\/p: hello<br \/>\nout.println(s2); \/\/ o\/p: hello<br \/>\nThough String is immutable, assignment can be possible so the s1 will now refer to new value stack.<\/p>\n<p>s1 = &#8220;stack&#8221;;<\/p>\n<pre>\r\n         _________\r\n        | \u00a0 \u00a0 \u00a0 \u00a0 |\r\ns1 ----&gt;| stack \u00a0 |\r\n        |_________|\r\n<\/pre>\n<p>But what about s2 object which is pointing to hello it will be as it is.<\/p>\n<pre>\r\n         __________\r\n        | \u00a0 \u00a0 \u00a0 \u00a0 \u00a0|\r\ns2 ----&gt;| hello \u00a0 \u00a0|\r\n        |__________|\r\n<\/pre>\n<p>out.println(s1); \/\/ o\/p: stack<br \/>\nout.println(s2); \/\/ o\/p: hello<br \/>\nSince String is immutable Java Virtual Machine won&#8217;t allow us to modify string s1 by its method. It will create all new String object in pool as follows.<\/p>\n<p>s1.concat(&#8221; overflow&#8221;);<\/p>\n<pre>\r\n                 ___________________\r\n                | \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 |\r\ns1.concat ----&gt; | stack overflow \u00a0 \u00a0|\r\n                |___________________|\r\n<\/pre>\n<p>out.println(s1); \/\/ o\/p: stack<br \/>\nout.println(s2); \/\/ o\/p: hello<br \/>\nout.println(s1.concat); \/\/ o\/p: stack overflow<br \/>\nNote if String would be mutable then the output would have been<\/p>\n<p>out.println(s1); \/\/ o\/p: stack overflow<br \/>\nNow you might be surprised why String has such methods like concat() to modify. Following snippet will clear your confusion.<\/p>\n<p>s1 = s1.concat(&#8221; overflow&#8221;);<br \/>\nHere we are assigning modified value of string back to s1 reference.<\/p>\n<pre>\r\n         ___________________\r\n        | \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 |\r\ns1 ----&gt;| stack overflow \u00a0 \u00a0|\r\n        |___________________|\r\n<\/pre>\n<p>out.println(s1); \/\/ o\/p: stack overflow<br \/>\nout.println(s2); \/\/ o\/p: hello<br \/>\nThat&#8217;s why Java decided String to be a final class Otherwise anyone can modify and change the value of string. Hope this will help little bit.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lets break it into some parts String s1 = &#8220;hello&#8221;; This Statement creates string containing hello and occupy space in memory i.e. in Constant String Pool and and assigned it to reference object s1 String s2 = s1; This statement assigns the same string hello to new reference s2 _______ | \u00a0 \u00a0 \u00a0 | [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/80"}],"collection":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/comments?post=80"}],"version-history":[{"count":8,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/80\/revisions"}],"predecessor-version":[{"id":88,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/80\/revisions\/88"}],"wp:attachment":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/media?parent=80"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/categories?post=80"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/tags?post=80"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}