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
February 4, 2009 at 12:17 pm
@ Munish Gogna Says:
August 28, 2007 at 6:20 am
hi manish.as u r taking strings inside double quotes so they become constants ..so s2 = s2 + “c” this wont wrk..
or change the class .. make it StringBuilder there u can make modification in the string itself
February 4, 2009 at 12:24 pm
@——Niks Says:
———May 2, 2008 at 12:44 am
hi nikhil
String s2 = s2;
this line wont compiled
make it String s2 = s1;then evrything will work nd giv result
true
true
February 4, 2009 at 12:26 pm
sorry
in the above post read nikhil as Niks..
August 11, 2009 at 5:52 am
hai guy
look at this code some body asked abt it
String s1 = “abc”;
String s2 = “ab”;
s2 = s2 + “c”;
if (s1 == s2)
System.out.println(”Equal”);
else
System.out.println(”false”);
ok this will show false
because when u are trying to add to string a new object will be created so the principle given on the top will work now, as it will create a object in the heap and the heap object refrence will be given back so the s1 and s2 will not at the same address,so it will show u false but if u want to refer to the “abc” in the string constant pool then you can use the “intern()” method as follows so both will refer to same address
String s1=”abc”;
String s2=”ab”;
s2=s2+”c”;
s2=s2.intern();
if(s1==s2)
{
System.out.println(“true”);
}
else
{
System.out.println(“false”);
}
regards
vawani S pati