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.

Advertisements

15 Responses to “String constant pool”

  1. Great!
    Helped me out regarding String

  2. Munish Gogna Says:

    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 Says:

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

  4. nittin gupta Says:

    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. it no more points to abc ? When did it point either ? s2 was pointing to “ab” but not “abc”

  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. 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. i am gonna show this to my friend, bro

  9. @ 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

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

  11. sorry
    in the above post read nikhil as Niks..

  12. Vawani sankar pati Says:

    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

  13. Hi – I don’t comment on many sites but had to on yours. It’s really nice! I really like how you write – very to the point, unlike a lot of other blogs. I don’t have time to read everything here right now, I found your site while looking for something else on ask.com. But I’ve bookmarked your homepage and will visit it regularly to read the latest. Thanks for having this site. I love reading other sites about web development. I really like what you wrote in this post, and some of your other articles are interesting. I’ll have to read more. I have been learning JavaScript programming lately. It’s such an exciting area. I’ll bookmark it and visit regularly. Thanks again for a very informative site! Click here if you’d like to check out my site.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: