String
String is one of the famous and frequently used java Class from java.lang package.
If an instances of String is
created in heap memory then its state can be never changed, means the value of
the string can not be changed because String is an immutable class.
What is immutable?
The synonyms of immutable are
– unchangeable, fixed, permanent and etc...
In Java world also its refers
the same meaning, once an object is created then its state cannot be changed.
Let’s see with simple
example:
String initialization is one
of the unique features of String, the following are the few way of String
initialization.
String s1 = “abc”;
String s2 = new String (s1);
String s3 = s2;
|
Since String is Class like
other Java Classes we can create object with new operator. String is also
called literal so similar to primitive type initialization String literal can
be assigned value without new operator.
How JVM allocates memory
for String?
We know JVM is responsible for
memory allocation for all JAVA Classes but do you know JVM will always perform
some additional work w.r.t memory management. String memory management is one
of the additional feature of String Class. Lets see how JVM works for String
memory management.
Whenever Class Loader
encounter String literal ( String str = "value" ), first it will
check in the String Literal Pool/String Constant Pool (You can see what is
String pool and its mechanism below). If the value exists in the pool then
JVM will take the reference of the existing value and assigned to String
variable. If the value is not exists in the pool then JVM will create new
String instance in the heap memory and immediately the reference of the new object
will be register in String Pool for future reference.
What is String Literal Pool
/ String Constant Pool ?
Like other Java classes the
String objects is also created and maintained in heap memory, here the additional
memory management of String class is String Literal Pool /String Constant pool,
during run time the JVM will collect all the String literal reference and
register it in the String Pool which is similar to stack table.
Usually we calling this String
Pool as Perm Gen Space, it is neither heap memory nor stack memory.
Let’s see the String mechanism
with more details.
String s1 =”abc”;
String s2 = new String
(“abc”);
|
What you think about above
two statements? Will it point same object? How JVM will work for these
statements?
Statement 1: As we have
seen before “Whenever Class Loader encounter String literal (String s1 =”abc”),
first it will check whether the value is exist in the String Literal
Pool/String Constant Pool or not. If the value is exists in the pool then JVM will
take the reference of the existing value and assigned in the String variable if
not exists only the JVM will create new Object”.
Statement 2: Since new
operator is used JVM will directly create new Object in heap memory even though
if there is a String Object with same value in String Pool. As per Java rule
JVM will always give high priority for new operator.
Once the
above two statements executed then the reference and object of s1 and s2 will
be available in memory as follows
So what do
think about the output of the following statements?
System.out.println(s1.equalIgnoreCase(s1));
// ans: true
System.out.println(s1==s1);
// ans: false
|
Do you
think is there any possible way to optimize the string objects which are
created by new operator?
Yes, String’s
intern () native method can be used to optimize the String object’s
memory. Let’s see from the above statement.
s2 =
s2.intern();
|
Now check the
output of following statement
System.out.println(s1==s2);
// ans : true
|
What
intern () will do?
Whenever the
intern native method is invoked, it will check from String Literal/String
Constant pool. If the pool already exists the same value of String Object then
reference of the string will be returned if not exist then the reference will
be added in the pool.
After intern
() native method executed the reference and object of s1 and s2 will be as
follows in the memory.
Disadvantage of String Pool
Garbage Collector will never ever clear the String instance from String Pool but it is resolved in Java 1.7. <Feature of Java 1.7>


Sorry for the delay, Anyway Thanks for your positive comments.
ReplyDelete