This content has been marked as final. Show 17 replies
So, when 2 Strings have the same value, there is only one constant in the poolIf 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"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.
s2 ----X> "ab"
Am I clear?
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.
Whether the VM ends up making both string referencesIt should be VM independent since string literals, and the string pool is described in the JLS:
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.
But in my example, the constant "abc" should alreadySo 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:
be on the pool, no?
So when I create 's2', the value should be retreived
on that pool, am I wrong (again)??
String s = "boo";
String t = new String("boo");
boolean eq = (s == t);
you'd expect eq == true?
Exactly, that's what I'm trying to understand.So you'd also expect String.clone() (if it were accessible) to do nothing except:
Isn't it the r�le of this Pool: check if it can reuse
the Strings already created?
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).
Isn't it the r�le of this Pool: check if it can reuse the Strings already created?The JLS is clear about this:
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.
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