In this core java tutorial we will learn about String pool in java.
Contents of page :
-
String pool
in java is also known as >
-
What is the
String Pool
in java?
Advantages
of string pool in java?
-
Diagram
to demonstrate String pool in java>
-
Let’s discuss step-by-step what will happen when below 5 statements will be executed >
-
About
intern method
in java
-
Why String pool in java?
-
Program to demonstrate
String pool
in java>
-
How many String will be formed in following operation in java >
String str = “abc” ;
str = str + “def”;
String pool
in java is also known as >
-
String literal pool/
-
String constant pool/
-
String intern pool.
What is the
String Pool
in java?
Advantages
of string pool in java?
When a string is created without using new operator and if it already exists in the pool, then instead of creating new string the existing string from the pool is returned.
Where does
String Pool
resides in memory?
From
java 7
String pool is a storage area in
java heap memory,
where all the other objects are created.
Prior to Java 7 String pool was created in permgen space of heap.
String s1 =
“abc”
;
String s3 =
“abc”
;
By executing above 2 statements only one string will be formed in string pool.
When [
String s1 =
“abc”
; ]
is executed
No string with “abc” is there in pool, so string will be created in
string pool
and s1 will be a reference variable which will refer to it
.
When [
String s3 =
“abc”
; ]
is executed
string with “abc” is there in pool, so s3 will be
a reference variable which will refer to
“abc” in
string pool.
About String pool from JDK docs >
In JDK 7, interned strings are no longer allocated in the permanent generation of the Java heap, but are instead allocated in the main part of the Java heap (known as the young and old generations), along with the other objects created by the application. This change will result in more data residing in the main Java heap, and less data in the permanent generation, and thus may require heap sizes to be adjusted. Most applications will see only relatively small differences in heap usage due to this change, but larger applications that load many classes or make heavy use of the String.intern() method will see more significant differences.
Diagram
to demonstrate String pool in java>
Let’s discuss step-by-step what will happen when below 5 statements will be executed >
String s1 =
“abc”
;
String s2 =
new
String(
“abc”
);
String s3 =
“abc”
;
String s4 =
new
String(
“abc”
);
String s5 =
new
String(
“abc”
).intern();
String s1 =
“abc”
;
No string with “abc” is there in pool, so JVM will create string in
string pool
and s1 will be a reference variable which will refer to it
.
String s2 =
new
String(
“abc”
);
string is created using
new
operator, it will force JVM to create new string in heap (not in string pool).
String s3 =
“abc”
;
string with “abc” is there in pool, so s3 will be
a reference variable which will refer to
“abc” in
string pool.
String s4 =
new
String(
“abc”
);
string is created using
new
operator, it will force JVM to create new string in heap (not in string pool).
String s5 =
new
String(
“abc”
).intern();
string is created using new operator but intern method has been invoked on it, so s5 will be
a reference variable which will refer to
“abc” in
string pool.
About
intern method
in java
Method is found in java.lang.
String
class
When the intern method is invoked, if the
string pool
already contains a string equal to this String object as determined by the
equals(Object)
method, then the string from the pool is returned. Otherwise, this String object is added to the
string pool
and a reference to this String object is returned.
Example in java –
String s1 =
“abc”
;
“abc”
is created in
string pool
i.e.
String object is added to the
string pool.
String s5 = new String(
“abc”
).intern();
What happens internally When above statement is executed ?
When the intern method is invoked, if the
string pool
already contains a string equal to this String object will be determined by the
equals(Object)
method.
As “abc” already exists in string pool (Because of
String s1 =
“abc”
).
So,
“abc”
.equals(
“abc”
)
will return true and
s5 will be
a reference variable which will refer to
“abc”
in
string pool.
So, s1 == s5 will always return
true.
Why String pool in java?
Strings are widely used in java code. And they are immutable which allows them to be cached in memory to
save memory
and
increase performance
.
less number number of strings are created in java heap
and
hence leaving less work for garbage collector
to be done.
Program to demonstrate
String pool
in java>
In the below program reference equality have been done using
== operator
.
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public
class
StringPoolTest {
public
static
void
main(String[] args) {
String s1 =
“abc”
;
String s2 =
new
String(
“abc”
);
String s3 =
“abc”
;
String s4 =
new
String(
“abc”
);
String s5 =
new
String(
“abc”
).intern();
System.
out
.println(
“–comparing s1–“
);
System.
out
.println(s1==s2);
//false
System.
out
.println(s1==s3);
//true
System.
out
.println(s1==s4);
//false
System.
out
.println(s1==s5);
//true
System.
out
.println(
“–comparing s2–“
);
System.
out
.println(s2==s3);
//false
System.
out
.println(s2==s4);
//false
System.
out
.println(s2==s5);
//false
System.
out
.println(
“–comparing s3–“
);
System.
out
.println(s3==s4);
//false
System.
out
.println(s3==s5);
//true
System.
out
.println(
“–comparing s4–“
);
System.
out
.println(s4==s5);
//false
}
}
/* OUTPUT
–comparing s1–
false
true
false
true
–comparing s2–
false
false
false
–comparing s3–
false
true
–comparing s4–
false
*/
How many String will be formed in following operation in java >
String str = “abc” ;
str = str + “def”;
SOLUTION >
String str = “abc” ;
JVM will create one string string pool. (
FIRST
STRING IN POOL
)
Now, here comes the tricky part>
str = str + “def”;
Internally
+
operator uses StringBuffer for concatenating strings.
Internally,
String str = new StringBuilder(str).append(“def”).toString();
[“def” will be
SECOND
STRING IN POOL
]
Now,
str=”abcdef”
[ “abcdef” will be
THIRD
STRING IN POOL
]
Summary –
So in this core java tutorial we learned about String pool in java. Different names for String pool in java. What is the String Pool in java? Advantages of string pool in java?
Diagram to demonstrate String pool in java. About intern method in java. Why String pool in java? Program to demonstrate String pool in java.
Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking
JavaMadeSoEasy.com
(
JMSE
) on
, following on
+
or
.
RELATED LINKS>