Home > Programming > String constant pool

String constant pool

November 26th, 2006 admin Leave a comment Go to comments

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.

Categories: Programming Tags: , ,
  1. Raj
    July 6th, 2007 at 08:30 | #1

    Great!
    Helped me out regarding String

  2. Munish Gogna
    August 28th, 2007 at 06:20 | #2

    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

  3. Munish Gogna
    August 28th, 2007 at 06:23 | #3

    sorry … in hurry i forgot to write not equal…above program will print not equal…

  4. nittin gupta
    October 4th, 2007 at 10:50 | #4

    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

  5. Ram
    October 8th, 2007 at 13:31 | #5

    it no more points to abc ? When did it point either ? s2 was pointing to “ab” but not “abc”

  6. srikanth
    October 29th, 2007 at 02:01 | #6

    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

  7. Niks
    May 2nd, 2008 at 00:44 | #7

    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???

  8. abhiroop
    February 4th, 2009 at 12:17 | #8

    @ 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

  9. abhiroop
    February 4th, 2009 at 12:24 | #9

    @——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

  10. abhiroop
    February 4th, 2009 at 12:26 | #10

    sorry
    in the above post read nikhil as Niks..

  11. Vawani sankar pati
    August 11th, 2009 at 05:52 | #11

    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

  12. Naresh Hode
    January 29th, 2010 at 06:24 | #12

    in this case also it is giving true

  13. March 5th, 2010 at 12:11 | #13

    thank u for providing this concept. i have doubt in this concept . i am writing two program differently
    program 1
    class Test{
    public static void main(String args[])
    {
    int i;
    String s1=”Hello”;
    String s2=”Hello”;
    String s3=new String(“Hello”);
    String s4=new String(“Hello”);
    i=s1.hashCode();
    System.out.println(“String s1->”+i); //365478
    i=s2.hashCode();
    System.out.println(“String s2->”+i); //365478
    i=s3.hashCode();
    System.out.println(“String s3->”+i); //365478
    i=s4.hashCode();
    System.out.println(“String s4->”+i); //365478
    }
    }

    That comment is output. that means all hashcode are same .that mean entire content with respect heap and string constant pool are maintain single hashcode. If similar content is available w.r.t to heap as well as string constant pool it is pointed to single hashcode.

    ok

    observe this program

    class Test{
    public static void main(String args[])
    {
    String s1=”hello”;
    String s2=”hello”;
    String s3=new String(“hello”);
    String s4=new String(“hello”);
    System.out.println(“w.r.t to heap & string constant pool”);
    if(s1==s3)
    System.out.println(“String s1 is equal to s”);
    else
    System.out.println(“String s1 is not equals to s”); // this line executes
    System.out.println(“w.r.t string constant pool”);
    if(s2==s1)
    System.out.println(“String s1 is equal to s”); // this line executes
    else
    System.out.println(“String s1 is not equals to s”);
    System.out.println(“w.r.t to heap “);
    if(s3==s4)
    System.out.println(“String s1 is equal to s”);
    else
    System.out.println(“String s1 is not equals to s”); // this line executes
    }
    }

    Observing this two program i got confused . plz clarify this doubt

  14. Sam Quiring
    March 12th, 2010 at 10:43 | #14

    String.hashCode() returns a value computed from the characters in the string, not the address of the object.

  15. Ariel Diaz Molina
    April 16th, 2010 at 09:32 | #15

    hey guys why did u call Sring litral to a “thing” that is not an “String literal”!! this post is pretty good!

  1. April 4th, 2007 at 16:19 | #1
  2. September 11th, 2007 at 05:48 | #2