<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sathiswaran on Medium]]></title>
        <description><![CDATA[Stories by Sathiswaran on Medium]]></description>
        <link>https://medium.com/@sathiswaran?source=rss-487778418a11------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Sathiswaran on Medium</title>
            <link>https://medium.com/@sathiswaran?source=rss-487778418a11------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 11:16:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sathiswaran/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Deep Dive into Java String Memory Management]]></title>
            <link>https://medium.com/@sathiswaran/deep-dive-into-java-string-memory-management-04dd6b59ed22?source=rss-487778418a11------2</link>
            <guid isPermaLink="false">https://medium.com/p/04dd6b59ed22</guid>
            <category><![CDATA[java-programming]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[efficient-coding]]></category>
            <category><![CDATA[java-strings]]></category>
            <category><![CDATA[memory-management]]></category>
            <dc:creator><![CDATA[Sathiswaran]]></dc:creator>
            <pubDate>Thu, 04 Sep 2025 10:24:44 GMT</pubDate>
            <atom:updated>2025-09-04T10:24:44.415Z</atom:updated>
            <content:encoded><![CDATA[<p>If you’ve written any Java code, chances are you’ve worked with Strings. But have you ever wondered <strong>how Strings are stored in memory</strong>, or why &quot;Hello&quot; == &quot;Hello&quot; is sometimes true and sometimes false?</p><p>Let’s break it down step by step — with code, diagrams, and JVM internals — so you’ll never be confused again.</p><h3>Strings in Java</h3><ul><li>Strings in Java are <strong>immutable</strong>. Once created, they cannot be changed.</li><li>JVM manages Strings in two main areas:</li></ul><ol><li><strong>String Pool (String Constant Pool, SCP)</strong> → stores unique literals, reused across the program.</li><li><strong>Heap memory (outside the pool)</strong> → stores new String(...) objects.</li></ol><p>This design makes Strings <strong>memory efficient</strong> and <strong>thread-safe</strong>.</p><p>Example Code</p><pre>public class StringMemoryExample {<br>    public static void main(String[] args) {<br>        String s1 = &quot;Hello&quot;;          <br>        String s2 = &quot;Hello&quot;;          <br>        String s3 = new String(&quot;Hello&quot;); <br>        String s4 = s3.intern();      <br><br>        System.out.println(s1 == s2); // true<br>        System.out.println(s1 == s3); // false<br>        System.out.println(s1 == s4); // true<br>    }<br>}</pre><p>Step-by-Step Execution in JVM</p><p>Let’s see what happens behind the scenes for each line.</p><h4>Step 1: String s1 = &quot;Hello&quot;;</h4><ul><li>JVM checks the <strong>String Pool</strong>.</li><li>&quot;Hello&quot; not found → creates a new pooled object.</li><li>s1 points to it.</li></ul><pre>Pool:  &quot;Hello&quot;  ← s1</pre><h4>Step 2: String s2 = &quot;Hello&quot;;</h4><ul><li>JVM checks pool again.</li><li>&quot;Hello&quot; already exists → reuses the same object.</li><li>s2 points to the same reference.</li></ul><pre>Pool:  &quot;Hello&quot;  ← s1, s2</pre><p>✅ s1 == s2 → true</p><h4>Step 3: String s3 = new String(&quot;Hello&quot;);</h4><ul><li>new String(...) <strong>forces a new object</strong> in heap memory.</li><li>Even though &quot;Hello&quot; comes from the pool, a <strong>copy</strong> is made.</li></ul><pre>Pool:  &quot;Hello&quot;  ← s1, s2<br>Heap:  new &quot;Hello&quot;  ← s3</pre><p>❌ s1 == s3 → false (different objects)<br> ✅ s1.equals(s3) → true (same characters)</p><h4>Step 4: String s4 = s3.intern();</h4><ul><li>intern() checks the pool: &quot;Hello&quot; already exists.</li><li>Returns the pooled reference.</li><li>s4 now points to the same object as s1 and s2.</li></ul><pre>Pool:  &quot;Hello&quot;  ← s1, s2, s4<br>Heap:  new &quot;Hello&quot;  ← s3</pre><p>✅ s1 == s4 → true<br> ❌ s3 == s4 → false</p><h4>Why this design?</h4><ul><li><strong>Immutability</strong> → Safe to share Strings across threads.</li><li><strong>Pooling</strong> → Saves memory by reusing common values.</li><li><strong>Interning</strong> → Lets you control canonical representations (useful in large datasets).</li></ul><h4><strong>Extra Nuances You Should Know</strong></h4><p><strong>Compile-time constants are pooled</strong></p><pre>String a = &quot;He&quot; + &quot;llo&quot;;   // optimized to &quot;Hello&quot;<br>String b = &quot;Hello&quot;;<br>System.out.println(a == b); // true</pre><p><strong>Runtime concatenation is not pooled</strong></p><pre>String x = &quot;He&quot;;<br>String y = x + &quot;llo&quot;;      // runtime build → new object<br>System.out.println(y == &quot;Hello&quot;); // false<br>System.out.println(y.intern() == &quot;Hello&quot;); // true</pre><p><strong>new String(&quot;Hello&quot;) always creates a new object</strong></p><p>→ Avoid unless you explicitly need a distinct reference.</p><p><strong>Java 9+ uses Compact Strings</strong></p><p>Strings internally store characters in a byte[] (Latin-1 or UTF-16) for better memory use.</p><p>Mental Picture (All Steps Together)</p><pre>Step 1: s1 = &quot;Hello&quot;<br>Pool: [&quot;Hello&quot;]  ← s1<br><br>Step 2: s2 = &quot;Hello&quot;<br>Pool: [&quot;Hello&quot;]  ← s1, s2<br><br>Step 3: s3 = new String(&quot;Hello&quot;)<br>Pool: [&quot;Hello&quot;]  ← s1, s2<br>Heap: [new &quot;Hello&quot;] ← s3<br><br>Step 4: s4 = s3.intern()<br>Pool: [&quot;Hello&quot;]  ← s1, s2, s4<br>Heap: [new &quot;Hello&quot;] ← s3</pre><h3>Key Takeaways</h3><ul><li>&quot;Hello&quot; (literal) → goes to the <strong>pool</strong>.</li><li>new String(&quot;Hello&quot;) → creates a <strong>new object</strong> on the heap.</li><li>intern() → ensures you get the <strong>pooled reference</strong>.</li><li>Use literals for efficiency; use intern() when managing duplicates in large datasets.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=04dd6b59ed22" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>