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 ?
Identical *string literals* generate references to the
same String object, so in your example there is just one
String and two references to it.
I need to be sure, because I have an application with lots of repeating
strings (people names).
Strings that are generated at run-time are another matter.
If you build a String by reading it from an input file or
manufacturing it programatically, you can get multiple distinct
Strings with identical contents:
String s3 = "-def";
s3 = "abc" + s3;
At this point s3 refers to a String whose contents are "abc-def",
but it is not the same String s1 and s2 refer to. Try it:
System.out.println("s3.equals(s1) = " + s3.equals(s1));
System.out.println("s3 == s1 = " + (s3 == s1));
Quite likely, all you need to do is be sure to use equals()
and not == when you want to test whether two Strings have equal
value. People's names are -- what? twenty characters? thirty?
forty? A million forty-character names will occupy about eighty
megabytes, which is not an amount to worry about unduly. Just
write the code in an ordinary way and don't worry about duplicates.
Then measure it to see whether you have a problem: if you do, you
can *then* think about ways to detect sets of duplicates and turn
them into references to a single "canonical" String. But you
should do this sort of thing last, not first.
--
Eric Sosman
esosman@acm-dot-org.invalid