1 Understanding the Word "Intern"
Before we understand "String Interning" in Java, let's first understand the meaning of the word "intern".
The English word "intern" has several meanings. In the context of programming:
To intern = يَحتَجِز / يَحجِز في مكان مُخصَّص
Think of it like this:
"To intern a person for investigation"
احتجاز شخص للتحقيق
The person is kept in a special, designated place.
Similarly, String Interning means:
حَجز النصوص في مكان مُخصَّص (String Pool) لإعادة استخدامها
Reserving strings in a designated place (String Pool) for reuse
String Interning is a method of storing only one copy of each distinct String value in a special memory area called the String Pool.
This helps save memory when the same String value appears multiple times in a program.
2 What is the String Pool?
The String Pool (also called String Constant Pool or String Intern Pool) is a special area in the Heap memory where Java stores String literals.
Special area for interned Strings
Objects created with 'new'
Key Points about the String Pool:
- It is a special partition within the Heap memory
- It acts as a repository for String literals
- Each unique String value is stored only once
- Multiple variables can share the same String object
3 Real-World Example: Saudi Population Database
Let's imagine we're building a database to store names of the Saudi population. Consider this scenario:
Imagine we have 1,000,000 people and 300,000 of them are named "Abdul Mohsin"!
"Abdul Mohsin" = 12 characters = approximately 24 bytes per String
300,000 separate String objects
Memory Used: ~7.2 MB
300,000 × 24 bytes
Only 1 String object shared by all!
Memory Used: ~24 bytes
Just 1 × 24 bytes
Memory Saved: Over 7 MB!
This is why String interning is so important for large-scale applications!
4 String Literal vs new String()
There are two ways to create a String in Java. The difference is critical for understanding String interning!
- Automatically stored in String Pool
- If same value exists, reuses existing object
- s1 and s2 point to same object
- Memory efficient
- Always creates new object in Heap
- Never reuses existing objects
- s3 and s4 are different objects
- Wastes memory if same value
s1ands2both point to address 0x100 (same object!)s3points to address 0x200 (new object)s4points to address 0x300 (another new object)- Using
new String()creates 2 extra objects with the same content!
5 Proving String Interning with Code
Let's write code to prove that literal Strings share the same object while new String() creates separate objects.
Output:
s1 == s2: true s1 == s3: false s2 == s3: false s1.equals(s3): true
| Comparison | Result | Explanation |
|---|---|---|
s1 == s2 |
true | Same address (both from String Pool) |
s1 == s3 |
false | Different addresses (s3 is in regular Heap) |
s1.equals(s3) |
true | Same content (both contain "Java") |
==compares addresses (are they the same object?).equals()compares content (do they have the same characters?)- For Strings, always use
.equals()to compare content!
6 The intern() Method
You can manually intern a String using the intern() method:
What does intern() do?
- Checks if an equal String already exists in the String Pool
- If yes, returns a reference to the pooled String
- If no, adds this String to the pool and returns its reference
7 Summary
- String Pool is a special memory area in the Heap for storing unique Strings
- String literals (e.g.,
"Java") are automatically interned - new String() always creates a new object in regular Heap
- Interning saves memory by reusing String objects
- Use
==to compare addresses,.equals()to compare content - Use
.intern()to manually add a String to the pool
What's Next?
In the next lesson, we'll explore essential String methods for manipulation, comparison, searching, and conversion!