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.

10 Responses to “String constant pool”

  1. แนทโวย » Blog Archive » ทำงานกับจาวา Says:

    [...] http://wsjoung.wordpress.com/2006/11/26/string-constant-pool/ [...]

  2. Raj Says:

    Great!
    Helped me out regarding String

  3. 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

  4. Munish Gogna Says:

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

  5. แนท » ทำงานกับจาวา Says:

    [...] http://wsjoung.wordpress.com/2006/11/26/string-constant-pool/ [...]

  6. 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

  7. Ram Says:

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

  8. srikanth Says:

    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

  9. Niks Says:

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

  10. Nemgyday Says:

    i am gonna show this to my friend, bro

Leave a Reply