Tutorials  /  Linux Basics

What is Java String Pool?

Ccentron Redaktion · January 2024 ·3 min read ·Linux Basics, Tutorial

As the name suggests, String Pool in Java is a pool of Strings stored in Java Heap Memory. We know that String is a special class in Java and we can create String objects using a new operator as well as providing values in double-quotes.

String Pool in Java

Here is a diagram that clearly explains how String Pool is maintained in Java heap space and what happens when we use different ways to create Strings. String Pool is possible only because String is immutable in Java and its implementation of String interning concept. String pool is also an example of Flyweight design pattern. String pool helps in saving a lot of space for Java Runtime although it takes more time to create the String. When we use double quotes to create a String, it first looks for a String with the same value in the String pool, if found it just returns the reference else it creates a new String in the pool and then returns the reference. However, using the new operator, we force the String class to create a new String object in heap space. We can use the intern() method to put it into the pool or refer to another String object from the string pool having the same value.

Java Program for String Pool

Go
package com.journaldev.util;

public class StringPool {

    /**
     * Java String Pool example
     * @param args
     */
    public static void main(String[] args) {
        String s1 = "Cat";
        String s2 = "Cat";
        String s3 = new String("Cat");
        
        System.out.println("s1 == s2 :"+(s1==s2));
        System.out.println("s1 == s3 :"+(s1==s3));
    }

}

Output of the above program is:

Code
s1 == s2 :true
s1 == s3 :false

Recommended Read: Java String Class

VM

Matching infrastructure at centron

No hardware needed to follow along: ccloud³ VMs with full root access start at €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →

How many Strings are getting Created in the String Pool?

Sometimes in a Java interview, you will be asked a question around String pool. For example, how many strings are getting created in the below statement;

Code
String str = new String("Cat");

In the above statement, either 1 or 2 strings will be created. If there is already a string literal “Cat” in the pool, then only one string “str” will be created in the pool. If there is no string literal “Cat” in the pool, then it will be first created in the pool and then in the heap space, so a total of 2 string objects will be created. Read: Java String Interview Questions

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Linux Basics
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren