Java/J2EE and SOA (348 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (345 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

What is the concept of String Pool in java?

Last updated on Jun 06,2023 42.1K Views

1 / 12 Blog from Java Strings

Have you heard about the term “String pool in Java?” Well, if no, you have landed at the right place. String Pool in Java is a pool of Strings which is stored in Java Heap Memory. Let us dig a bit deeper and understand this concept of Java String pool in detail.

Following pointers will be discussed in this article:

Let’s begin!

First of all, let us understand how exactly is a string object created!

How to create a string?

To create a String object in Java, there are two ways:

  • Using the new operator. For example,
String s1 = new String("Joey");
  • Using a string literal or constant expression. For example,
String s1="Joey"; (string literal) or
String s1="Joe" + "y"; (string constant expression)

Now, what is this String pool that I am talking about and how is the creation of a string in Java related to this. Let me cut down the clutter!

What is String Pool in Java?

String Pool is a storage area in Java heap.

String allocation, like all object allocation, proves to be a costly affair in both the cases of time and memory. The JVM performs some steps while initializing string literals to increase performance and decrease memory overhead. To decrease the number of String objects created in the JVM, the String class keeps a pool of strings.

Each time a string literal is created, the JVM checks the string literal pool first. If the string already exists in the string pool, a reference to the pooled instance returns. If the string does not exist in the pool, a new String object initializes and is placed in the pool.

After learning the concept theoretically, let me tell you how does a String pool work in Java step by step with the help of simple instances!

Discover the power of Flutter and learn how to leverage its features in a Flutter Development Course.

How Does String pool work in Java?

When you create a new string like this:

String s1 = “Rachel”

JVM automatically checks if the same value exists in the string constant pool or not.

  • if yes, it occupies the already existing value.
  • If no, it creates a new string by itself and adds it to the string pool.

If you want to halt this behavior, create a string using new operator:

String s1 = new String(“Rachel”)

Now, if you are willing to add this string to the string literal pool, Java provides you with a method called, intern() method; you can call native intern() method like this:

S1.intern();

Now, I will show you the implementation and working of string pool through an example.

But before that, a short reminder!

As you know if you’re comparing 2 objects using ==  operator it compares addresses in the memory.

So we will compare the strings using ==  to be completely sure that it’s the same object or not.

Now, let’s hop onto our implementation process.

String Pool in Java: Flow Diagram

String-pool - Edureka

Now let us grasp what happens here step by step:

  • The class is loaded when JVM is invoked.
  • JVM looks for all the string literals in the program
  • First, it finds the variable s1 which refers to the literal “Apple” and it gets created in the memory
  • A reference for the literal “Apple” is then placed in the string constant pool memory.
  • Then it finds another variable s2 which refers to the same string literal “Mango”.
  • Then it finds another variable s3 which refers to the literal “Apple”
  • Now that JVM has already found a string literal “Apple”, both the variables s1 and s3 will refer to the same object i.e. “Apple”.

Java program for String Pool 

public class StringPoolExperiment {
 
    public static void main(String[] args) {
        String s1 = "Rachel";
        String s2 = "Rachel";
        String s3 = new String("Rachel");
        String s4 = new String("Rachel").intern();
 
        System.out.println(s1 == s2); // true
        System.out.println(s1 == s3); // false
        System.out.println(s1 == s4); // true
    }
}
 

Output:

True
False
True

In the above example, you can clearly see the usage of string initialization in all the three ways; i.e;

String s1 = "Rachel";
String s2 = "Rachel";
String s3 = new String("Rachel");
String s4 = new String("Rachel").intern();

The internal working of the program perhaps should be clear now.

With this, I have reached towards the end of my blog. I hope the contents elaborated here helped you in widening your knowledge base. We will keep diving into the Java world. Stay tuned!

Check out the Java Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this “String Pool in Java” blog and we will get back to you as soon as possible.

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 4th May,2024

4th May

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 25th May,2024

25th May

SAT&SUN (Weekend Batch)
View Details
Comments
1 Comment
  • sushil says:

    thankue for this …

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

What is the concept of String Pool in java?

edureka.co