This discussion is archived
1 2 Previous Next 17 Replies Latest reply: Oct 3, 2005 7:03 AM by 807587 RSS

String Constant Pool...

807587 Newbie
Currently Being Moderated
Just a little question about the "String Constant Pool".
I've read that it's a place where Strings objects are placed, to use memory space more efficiently.
So, when 2 Strings have the same value, there is only one constant in the pool:
String s1 = "abc";
String s2 = "abc";
if (s1 == s2)
     System.out.println("String s1 == s2");          // The message is displayed
But now, the 2 Strings are not equals at the first time. Then I try to create a new String (s2) equals to the first (follow me??),
shouldn't he retreive this String ("abc") constant in the "String Pool"??
String s1 = "abc";
String s2 = "ab";
s2 = s2 + "c";
if (s1 == s2)
     System.out.println("String s1 == s2");          // The 2 references are different
Schematically:
s1 ----+-----> "abc"
       |
       |
s2 ----+--X--> "ab"
Am I clear? Or am I completely wrong with the mechanism of the "String Constant Pool"??

Thank you very much.
  • 1. Re: String Constant Pool...
    796447 Newbie
    Currently Being Moderated
    Why worry about it? If you need to check for string equality, use the equals method. If you want to check for object reference equality, use the == operator.
  • 2. Re: String Constant Pool...
    807587 Newbie
    Currently Being Moderated
    Well, I just want to understand how this Pool is working.
    I know it's a strange question...
  • 3. Re: String Constant Pool...
    807587 Newbie
    Currently Being Moderated
    So, when 2 Strings have the same value, there is only one constant in the pool
    If they are compile-time literals, or have been interned. Otherwise they may not be equal to the constant in the pool; String foo = String.valueOf(7) is not guarenteed to create a pool constant.
    Schematically: s1 ----+-----> "abc"
    |
    |
    s2 ----X> "ab"
    Am I clear?
    I don't have a clue what the X and the vertical line in that diagram is supposed to represent, but AFAIK there's no attempt to share common substrings in the pool, so there should be no link between s1 and s2.


    Pete
  • 4. Re: String Constant Pool...
    796447 Newbie
    Currently Being Moderated
    Whether the VM ends up making both string references point at the same string or not, is not your concern. I'm not even sure different versions of the VM would have to agree, so maybe at runtime depending on which VM you used you might get different results.

    It's really not worth worrying about. Concentrate on making your code correct, and let the VM do what it does best without you worrying about what it is doing internally.
  • 5. Re: String Constant Pool...
    807587 Newbie
    Currently Being Moderated
    Sure, but it's exactly what I'm trying to understand: internals of the VM.
    But It's more clear now. The String Pool only contains compile-time litterals...is it correct??
  • 6. Re: String Constant Pool...
    791266 Explorer
    Currently Being Moderated
    Whether the VM ends up making both string references
    point at the same string or not, is not your concern.
    I'm not even sure different versions of the VM would
    have to agree, so maybe at runtime depending on which
    VM you used you might get different results.
    It should be VM independent since string literals, and the string pool is described in the JLS:
    http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.10.5

    /Kaj
  • 7. Re: String Constant Pool...
    807587 Newbie
    Currently Being Moderated
    But It's more clear now. The String Pool only contains compile-time litterals...is it correct??
    No, you can also add a String to the pool using the intern() method.


    Pete
  • 8. Re: String Constant Pool...
    807587 Newbie
    Currently Being Moderated
    But in my example, the constant "abc" should already be on the pool, no?
    So when I create 's2', the value should be retreived on that pool, am I wrong (again)??
  • 9. Re: String Constant Pool...
    796447 Newbie
    Currently Being Moderated
    But in my example, the constant "abc" should already
    be on the pool, no?
    So when I create 's2', the value should be retreived
    on that pool, am I wrong (again)??
    So you think that every time a String is created the VM is supposed to check the pool and point the reference at an existing value? So in this:

    String s = "boo";
    String t = new String("boo");
    boolean eq = (s == t);

    you'd expect eq == true?
  • 10. Re: String Constant Pool...
    807587 Newbie
    Currently Being Moderated
    Exactly, that's what I'm trying to understand.
    Isn't it the r�le of this Pool: check if it can reuse the Strings already created?
  • 11. Re: String Constant Pool...
    796447 Newbie
    Currently Being Moderated
    Exactly, that's what I'm trying to understand.
    Isn't it the r�le of this Pool: check if it can reuse
    the Strings already created?
    So you'd also expect String.clone() (if it were accessible) to do nothing except:
    return this;
    and violate the general contract for clone as well? (Read the specification for Object.clone())

    See what a can of worms you're opening to worry about? Want to keep worrying about it? Are you really "needing" to know what the VM does internally (I doubt it).
  • 12. Re: String Constant Pool...
    3004 Newbie
    Currently Being Moderated
    Isn't it the r�le of this Pool: check if it can reuse the Strings already created?
    The JLS is clear about this:

    (�3.10.5):

    Literal strings within the same class (�8) in the same package (�7) represent references to the same String object (�4.3.1).

    Literal strings within different classes in the same package represent references to the same String object.

    Literal strings within different classes in different packages likewise represent references to the same String object.

    Strings computed by constant expressions (�15.28) are computed at compile time and then treated as if they were literals.

    Strings computed at run time are newly created and therefore distinct.

    The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents.


    Your example isn't a constant expression.
  • 13. Re: String Constant Pool...
    3004 Newbie
    Currently Being Moderated
    Your example isn't a constant expression.
    To illustrate this point:
    final String a = "a";
    final String b = "b";
    String ab = "ab";
    System.out.println(a+b == ab);  // returns true: Constant expression.
    
    String a2 = "a";
    String b2 = "b"
    System.out.println(a2 + b2 = ab); // returns false: Not a constant expression
  • 14. Re: String Constant Pool...
    807587 Newbie
    Currently Being Moderated
    I was stupid, I'll read all that stuff again later.
    Yes, I really need to understand that. But It's too late now. I'll check that tomorrow...
    Thank you very much for your help.
1 2 Previous Next