{"id":536,"date":"2010-07-05T12:09:26","date_gmt":"2010-07-05T06:39:26","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=536"},"modified":"2010-07-05T12:09:26","modified_gmt":"2010-07-05T06:39:26","slug":"immutable-nature-of-string-java","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/immutable-nature-of-string-java\/","title":{"rendered":"Immutable nature of String &#8211; JAVA"},"content":{"rendered":"<p>Immutable nature of string is charecterised by it&#8217;s property that <strong>one&#8217;s a memory location is<\/strong> <strong>reserved (allocated) to a string \u00a0and if string value changes then old value is not over written by new one<\/strong>.<\/p>\n<p>Lets discuss it,<\/p>\n<p>For example : Create an instance of type String as s1.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/07\/String-1.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-537 alignleft\" title=\"Create instance of string in JAVA\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/07\/String-1.png?resize=366%2C99&#038;ssl=1\" alt=\"Create instance of string in JAVA\" width=\"366\" height=\"99\" \/><\/a><\/p>\n<p>It will reserve a memory location in <strong>Heap Memory<\/strong> with s1 storing the address of memory location as shown,<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/07\/Immutable-String.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-538\" title=\"Immutable String in JAVA\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/07\/Immutable-String.png?resize=166%2C178&#038;ssl=1\" alt=\"Immutable String in JAVA\" width=\"166\" height=\"178\" \/><\/a><\/p>\n<p>Now let&#8217;s change the content of s1 ,in case of primitive data type like int, float , char etc .If we change the containt \u00a0of one variable the privious value is overwritten by the new value, but in case of String type instances this is not possible ,here a new memeory\u00a0 location is created which will point to the new value as shown..<\/p>\n<p><a href=\"https:\/\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/07\/Immutable-String.bmp\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-539\" title=\"Immutable String in JAVA\" src=\"https:\/\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/07\/Immutable-String.bmp\" alt=\"Immutable String in JAVA\" \/><\/a><\/p>\n<p>So new memory location is created for storing new value (new string)<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/07\/String.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-540\" title=\"Immutable String JAVA\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/07\/String.png?resize=210%2C142&#038;ssl=1\" alt=\"Immutable String JAVA\" width=\"210\" height=\"142\" \/><\/a><\/p>\n<p>This\u00a0behavior\u00a0of String Instances is reffered as <strong>Immutable<\/strong> nature of String.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Immutable nature of String &#8211; JAVA<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[3],"tags":[329],"class_list":["post-536","post","type-post","status-publish","format-standard","hentry","category-java","tag-java"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":2700,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/how-to-create-immutable-class-in-java\/","url_meta":{"origin":536,"position":0},"title":"How to create Immutable Class in Java","author":"Jitendra","date":"February 3, 2012","format":false,"excerpt":"creating the Immutable class in Java with simple source code example","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":460,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/java-destructor-finalize\/","url_meta":{"origin":536,"position":1},"title":"Java Destructor &#8211; finalize()","author":"Jitendra","date":"July 1, 2010","format":false,"excerpt":"Concept of destructor in JAVA. finalize() method.","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Java Destructor finalize()","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/07\/Trash.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1683,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/reading-and-writing-custom-annotation\/","url_meta":{"origin":536,"position":2},"title":"Reading and Writing Custom Annotation","author":"Jitendra","date":"March 16, 2011","format":false,"excerpt":"Article on Reading and Writing Custom Annotation with example","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1943,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/what-is-the-need-to-override-hashcode-and-equals-method\/","url_meta":{"origin":536,"position":3},"title":"What is the need to Override Hashcode() and equals() method","author":"Jitendra","date":"April 11, 2011","format":false,"excerpt":"Although there are lots of material are available on internet and API document about the necessity of the overriding the hashcode() and equals() method in Java but lots of new developers still not able to understand the necessity of hashcode() method. In this article, I will try to explain step\u2026","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"How Hashcode works in java","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/04\/How-Hashcode-works-in-java.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2501,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/very-useful-tips-and-tricks-of-the-apex-salesforce-interview-questions-part-4\/","url_meta":{"origin":536,"position":4},"title":"Latest Salesforce Interview Questions &#8211; Part 4 &#8211; Related to Dynamic Apex","author":"Jitendra","date":"November 27, 2011","format":false,"excerpt":"Most Frequently Asked interview questions of Apex, Dynamic Apex, SOSL, Visualforce, SOQL in Salesforce.com SFDC","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1917,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/a-simple-program-to-send-email-in-java\/","url_meta":{"origin":536,"position":5},"title":"A simple program to send email in JAVA","author":"Jitendra","date":"April 6, 2011","format":false,"excerpt":"A Simple SMTP Email Program in JAVA","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/536","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=536"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/536\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=536"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=536"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}