Re: String storage
Dra?en Gemi? wrote:
Does anyone know how the strings are stored in Java.
Let's say that there are two strings:
String s1="abc-def";
String s2="abc-def";
Do s1 and s2 reference to the one or two objects ?
String constant expressions (not just String literals) with the same
value reference the same object.
Your s1 and s2 reference the same object, as would "abc-"+"def".
Since the srings are immutable, it would make sense
to have one object referenced by two variables.
Is rhe character sequence "abc-def" stored (and memory allocated) only
once or twice ?
For String constant expressions, definitely only once.
For dynamically built strings, it depends on how they are built. Inside
the String implementation, there is a reference to a char[] with offset
and length, so a substring can use the same char[] as the string from
which it was derived.
I need to be sure, because I have an application with lots of repeating
strings (people names).
You can force same object behavior by using the String method intern().
Suppose you had a String reference s3 that was built some other way, not
has a substring, and has value "abc-def". It would reference a different
String object, and might reference a different char[]. However,
s3.intern() would reference the same object as s1 and s2.
I would do measurements to see whether the net performance using intern
is better or worse than without. It does cost some extra bookkeeping.
Patricia