<?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 Ishwariyabooraja on Medium]]></title>
        <description><![CDATA[Stories by Ishwariyabooraja on Medium]]></description>
        <link>https://medium.com/@ishwariyabooraja?source=rss-6d03a35fcacc------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*3YA9NBXJv166Cu0e</url>
            <title>Stories by Ishwariyabooraja on Medium</title>
            <link>https://medium.com/@ishwariyabooraja?source=rss-6d03a35fcacc------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 02:53:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ishwariyabooraja/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[STRING LITERAL POOL]]></title>
            <link>https://medium.com/@ishwariyabooraja/string-literal-pool-9f0ebfb6743d?source=rss-6d03a35fcacc------2</link>
            <guid isPermaLink="false">https://medium.com/p/9f0ebfb6743d</guid>
            <dc:creator><![CDATA[Ishwariyabooraja]]></dc:creator>
            <pubDate>Mon, 06 Nov 2023 09:05:56 GMT</pubDate>
            <atom:updated>2023-11-06T09:05:56.889Z</atom:updated>
            <content:encoded><![CDATA[<p>Strings are a sequence of characters. In Java, strings are objects. To represent them, String class is used. They are implemented as an instance of the “String” class. They are immutable, meaning that once they are created, they cannot be modified.</p><p>There are two ways to create string objects in Java:</p><ol><li>AS STRING LITERAL</li></ol><p>String literals are a series of characters that are enclosed in double quotes.</p><p>String str= “Objects”;</p><p>If we use String literal in our program, then an object will be created and the value will be assigned to it.</p><ol><li>USING ‘NEW’ KEYWORD</li></ol><p>Like any other object, strings can also be created using the new keyword.</p><p>String str1= new String(“Objects”);</p><p>Where are the String objects stored?</p><p>When String objects are created as literals, they are stored in a special storage space called a String pool or String constant pool. The reference variable will be stored in the stack. String pool resides in the heap memory. If the string objects are created using the ‘new’ operator, then the object is allocated space in the heap memory with the value. The reference with type class will be created in the stack.</p><p>In the above example where str is created as a String literal, the object allocation looks like below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*fc0gBVYGh1ghCQK4" /></figure><p>String str1 which was created using new operator will get allocated in the heap like below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*uDDI8v3OouybsrDx" /></figure><p>Why do we need String literal pools to store String literals?</p><p>If we create a string in a heap, it uses little amount of memory in the heap. If we keep creating a lot of strings, then it keeps utilizing a lot of memory. In order to reduce memory usage and also to improve performance, JVM stores the string literals in the string constant pool.</p><p>How does String literal pool reduce heap memory usage?</p><p>When a string literal is found in the program, before creating an object and storing the value in it, JVM will check for the existence of the string object with similar contents. If it is present, then the reference to that particular string object will be sent.The string literal pool is actually implemented using a hash map. Because if we search through the pool every time to check whether the same string is present, it will take a lot of time and is inefficient. Hence the hash map approach will allow JVM to shortcut this complex process.</p><p><strong>The Hash Map Implementation:</strong></p><p>String pool can be imagined as a set of buckets. There are roughly around 65K buckets in the pool. When a string literal is found using the Hashcode of the particular string, a calculation(to each index of the bucket, list of strings with similar hash code is added) is made to determine which bucket the string literal actually belongs to. Many Strings can be stored in each bucket. Hence every time when we need to search whether a particular string literal is there, we just want to determine the bucket to which it will belong and then look through the list of strings in the bucket to find out whether the string is already present in the pool or not.</p><p>The possible issue with this implementation is the size of the hashmap. The hashmap size starts with 16 buckets and goes till 65k buckets. If the no. of buckets is small, then the number of strings in the bucket will be more and it is inefficient to check whether a string is present in the pool or not.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*sWw8DPunuHyBe0TJ" /></figure><p>So now, when we create two string literals with the same value, then both the reference variables will get stored in the stack. But both these reference variables will point to a single string object in the String literal pool.</p><p>String a=”Java”;</p><p>String b=”Java”;</p><p>First using the hashcode of the literal Java, the bucket to which it belongs will be determined. Let us consider that the literal “Java” belongs to bucket 1. The strings in bucket 1 will be searched to find if the literal “Java” exists. Since this is the first statement in the program, and the literal does not exist in the pool, a new string object will be created in this case and the value “Java” will be stored in the object. The string object is placed in the bucket 1. Its reference is returned to the variable stored in the stack.</p><p>Now another String b with the same literal is found in the program. Again, using the hashcode of the literal “Java”, the bucket to which the literal belongs is determined. This time when we search through the list of strings in the bucket, we will get the object “Java” since it is already created and is referenced by the variable ‘a’. So the reference of the already existing object will be returned to the reference variable ‘b’ in stack.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*4FVIXjsXmESrzrWa" /></figure><p>But when strings are created using the new operator, it forces JVM to create a new instance in the heap (outside the String literal pool) even though the same value was previously stored in another string object created using the same value. Reusing instances are not allowed and hence it consumes more memory.</p><p>String x=new String(“Object”);</p><p>String y=new String(“Object”);</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*wrXCnK-cWKgrmOxv" /></figure><p>To reduce the memory consumption by the String created using the new operator, String interning method is used. It is used to ensure that the strings that have the same contents share the same memory. i.e) in the above example, when interning is used, then only one object will be created instead of two different objects.</p><p>When the string intern method is executed, then JVM will check whether an equivalent string object with the same literal value exists in the string literal pool or not. If it exists, then the reference of that object is returned, else an exact copy of the object in the heap is created and it is stored in the string literal pool.</p><p>String w=new String(”object”);</p><p>String x=new String(”object”);</p><p>String y=w.intern();</p><p>String z=x.intern();</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*FwwWrD-riRlSXuSU" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9f0ebfb6743d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>