CSDT BLOG

DISCOVER COLLECTIONS AND BLOGS THAT MATCH YOUR INTERESTS.




Share ⇓




What is String Pool in java Programming?

Bookmark

What is String Pool in java Programming?

String Pool in Java is a pool of Strings which is stored in Java Heap Memory.

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("Ravi");

Using a string literal or constant expression. 

For example,
String s1="Ravi"; (string literal) 

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.

How Does String pool work in Java?

When you create a new string like this:

String s1 = “Ranjan”;

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

  1. . if yes, it occupies the already existing value.
  2. . 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("Ranjan")

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.

Note: 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 us grasp what happens here step by step:

  1. The class is loaded when JVM is invoked.
  2. JVM looks for all the string literals in the program
  3. First, it finds the variable s1 which refers to the literal “Apple” and it gets created in the memory
  4. A reference for the literal “Apple” is then placed in the string constant pool memory.
  5. Then it finds another variable s2 which refers to the same string literal “Mango”.
  6. Then it finds another variable s3 which refers to the literal “Apple”
  7. 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”.

👨‍🏫 About the Course: Python with Project Classes – Basic to Advance

Bookmark

👨‍🏫 About the Course: Python with Project Classes – Basic to Advance

Python Course

At CSDTCentre, we believe in learning by doing. That’s why our course includes:

Complete Python Syllabus

  • Variables, Data Types, Loops, and Functions

  • Object-Oriented Programming (OOPs)

  • File Handling & Exception Management

  • Working with Modules and Packages

  • GUI with Tkinter

  • MySQL & SQLite Database Connectivity

Real-Time Projects
Students will build practical projects like:

  • Mini Calculator

  • Library Management System

  • Inventory Tracker

  • Student Registration App

Industry-Expert Trainers
Our experienced instructors bring real-world knowledge and guide students with hands-on training.

Flexible Timings & Small Batches
We ensure personalized attention with limited batch sizes and flexible class timings.

Certification & Career Support
After course completion, students receive a recognized certification and career assistance including interview preparation and resume building.


📍 Course Details

  • Location: CSDTCentre, Boring Road, Patna

  • Mode: Offline (Classroom-Based)

  • Duration: 6 weeks

  • Eligibility: Anyone interested in coding (No prior experience required!)

  • Contact: 7070090551

  • Enroll Now: https://csdt.co.in/IT_Jobs/CsdtEnquiry.aspx


💬 Why Choose CSDTCentre, Patna?

✔ Trusted by 1000 Students
✔ 100% Practical Training
✔ Affordable Fees
✔ Positive Student Reviews
✔ Career-Oriented Approach


⭐ Student Testimonials

"CSDTCentre helped me build strong Python skills with hands-on projects. The instructors are really supportive!" – Amit, Patna

"I landed my first internship after completing the Python course here. Highly recommend!" – Priya, Student


🚀 Ready to Code Your Future?

Start your journey into the world of programming with CSDTCentre’s Python Project Classes in Patna. Don’t miss out on this opportunity to gain one of the most valuable skills in today’s tech-driven world.

📞 Call Now: 7070090551
🌐 Visit Us: https://www.csdt.co.in/Python_institute_patna.aspx


#PythonTrainingPatna #LearnPython #PythonWithProjects #CSDTCentre #CodingClassesPatna #PythonDevelopment #JobOrientedCourses #ProgrammingInPatna #PythonInstitutePatna


0

Our Recent Coment