String constant pool
Everybody knows about String constant pool which is for efficient memory management in java. Basically most of objects are managed on heap area but String object. In most of ordinary application, programmers use String object quite often and this String object quite frequently need to be changed or it occupies large amounts of memory. Therefore instead of managing String object on heap area, they introduced String constant pool.
One of important characteristic of String constant pool is that it doesn’t create same String object if there is already String constant in the pool.
String var1 = “This is String Literal”;
String var2 = “This is String Literal”;
For above two String objects, JVM creates only one object in the String constant pool and for the second string reference variable (var2), it points the string object which is created for var1. In this case, (var1 == var2) is true.
But one thing, people make confused is that. It works only when it encounter on String Literal with double quote.
String var3 = new String(“This is String Literal”);
In this case, a regular object will be created by new keyword on heap area and it will be placed in the String constant pool. Finally it will be assigned to the reference variable, var3. This process is just by passing from String constant pool management. Therefore, (var1 == var3) is false.
April 4, 2007 at 4:19 pm
[...] http://wsjoung.wordpress.com/2006/11/26/string-constant-pool/ [...]
July 6, 2007 at 8:30 am
Great!
Helped me out regarding String
August 28, 2007 at 6:20 am
String s1 = “abc”;
String s2 = “ab”;
s2 = s2 + “c”;
if (s1 == s2)
System.out.println(”Equal”);
else
System.out.println(”Equal”);
Ho According to you s1 is in String constant pool and s2 which is Sring literal created without using new operator should point to s1.
According to you it should display “EQUAL” , but hey buddy run this program and see the behavior
August 28, 2007 at 6:23 am
sorry … in hurry i forgot to write not equal…above program will print not equal…
September 11, 2007 at 5:48 am
[...] http://wsjoung.wordpress.com/2006/11/26/string-constant-pool/ [...]
October 4, 2007 at 10:50 am
this above code prints not equal becoz
when we are giving statement s2=s2+”c”
prevoius s2 loose its reference ie it no more points to abc
a new object is created as abc and s2 point to it
October 8, 2007 at 1:31 pm
it no more points to abc ? When did it point either ? s2 was pointing to “ab” but not “abc”
October 29, 2007 at 2:01 am
s1=”java”
s2=”";
s2=s1+s2;
if the above condition is checked it will return false. but how it is possible could any one tell me
May 2, 2008 at 12:44 am
String s1=”abs”;
String s2 = s2;
System.out.println(s1==s2);
Output:
True
but
String s1=”abs”;
String s2 = s2;
s2=”abs”;
System.out.println(s1==s2);
Output:
false
note that here s2 and s1 point to same memory location till statment String s2=s1;
but when i assign s2=”abs”;
which has same value as s1 and right now its has same memoru location
then its pointing to different object?
why?
its means that while assign s2=”abs”…. it will make new object in constant pool???
September 22, 2008 at 1:09 pm
i am gonna show this to my friend, bro