Copyright

Java String Constant Pool: Concept & Mechanism

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Java Constants: Definition, Declaration & Types

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:03 Java String Constant Pool
  • 1:05 Creating a New…
  • 1:37 Note About Immutability
  • 2:00 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Timeline
Autoplay
Autoplay
Speed Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

When strings or String objects are created in Java, Java makes use of a pool in memory. This lesson will discuss how this pool works and how strings are handled.

Java String Constant Pool

When you declare a new string in Java, there are some interesting things that happen behind the scenes. This is a basic string declaration. We create a new string variable called employee and give it a value, like you can see in the code:

String employee = "Edgar Allen Poe";

Not only will Java create the variable employee, it will allocate space in the memory for the literal value 'Edgar Allen Poe.' This area in memory is called the string constant pool. It's like a pool of string values that are available to other parts of the program.

Now, if you created another variable, say employee2, and ALSO gave it a value of 'Edgar Allen Poe,' Java simply re-uses the value that's already in the pool.

Java string constant pool literal

You'll notice the string constant pool sits inside a section of memory is called the heap. This is a part of memory that is used for run-time operations, working with classes and objects. Think of a heap like a patch of garden soil you can easily take dirt and plants from as you plant a garden. Java places these new objects there. If you create a hundred more objects, Java will create a hundred more literals atop the heap.

Creating a New Instance of String

If you create a new instance of the String class instead, the constant pool works differently. Let's stay with the employee example and create yet another variable, employee3, and also give it the same literal value. This time, however, we will create a new instance of the String class:

String employee3 = new String("Edgar Allen Poe");

When this code is processed, Java will act a little differently. Instead of re-using the same literal again, it will create a new value in memory. In this case, it does not create it in the string constant pool, but in the memory heap.


Java constant string pool objects


To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support