J
John Galt
I have noticed a rather strange thing when working with Strings and
StringBuffers:
$ cat s.java
public class s{
public static void main(String argv[])
{
String s1, s2;
StringBuffer sb1, sb2;
s1 = new String(argv[0]);
s2 = new String(argv[0]);
sb1 = new StringBuffer(s1);
sb2 = new StringBuffer(s1);
System.out.println(
" s1 = " + s1.hashCode() +
" s2 = " + s2.hashCode() +
" sb1 = " + sb1.hashCode() +
" sb2 = " + sb2.hashCode());
}
}
When I run it:
$ java s alpha
s1 = 92909918 s2 = 92909918 sb1 = 12386568 sb2 = 9360485
$ java s bravo
s1 = 93998218 s2 = 93998218 sb1 = 12386568 sb2 = 9360485
$ java s charlie
s1 = 739067762 s2 = 739067762 sb1 = 12386568 sb2 = 9360485
$ java s delta
s1 = 95468472 s2 = 95468472 sb1 = 12386568 sb2 = 9360485
My questions:
1. Two Strings always hash the same if they are constructed from the
same "source" string. True? (The hashCode() is what's used to put
stuff into a Hashtable, right?)
2. If many more Strings are created from the original two Strings,
they all will also hash the same. True?
3. Two StringBuffers, even if created from the same String (or
"source" string), aren't guaranteed to hash the same. True? (It looks
to me from my experience that they use the address of the StringBuffer
or something - "alpha" can't possibly hash the same as "charlie".)
What is going on here?
This problem bit me bad when I was putting StringBuffers into a
Hashtable. I am guessing I should use a String from now on. But I have
another question here.
Suppose that the following things happen in my program:
- I create a bunch of Strings, and put them into a Vector.
- I iterate over the Vector and put some those Strings into a
Hashtable.
- I then set the Vector to null. Will my Hashtable be affected? (I
tend to think no, coz those Strings that _did_ go into the Hashtable
are still referenced by the Hashtable and hence won't be garbage
collected.)
TIA,
John Galt.
StringBuffers:
$ cat s.java
public class s{
public static void main(String argv[])
{
String s1, s2;
StringBuffer sb1, sb2;
s1 = new String(argv[0]);
s2 = new String(argv[0]);
sb1 = new StringBuffer(s1);
sb2 = new StringBuffer(s1);
System.out.println(
" s1 = " + s1.hashCode() +
" s2 = " + s2.hashCode() +
" sb1 = " + sb1.hashCode() +
" sb2 = " + sb2.hashCode());
}
}
When I run it:
$ java s alpha
s1 = 92909918 s2 = 92909918 sb1 = 12386568 sb2 = 9360485
$ java s bravo
s1 = 93998218 s2 = 93998218 sb1 = 12386568 sb2 = 9360485
$ java s charlie
s1 = 739067762 s2 = 739067762 sb1 = 12386568 sb2 = 9360485
$ java s delta
s1 = 95468472 s2 = 95468472 sb1 = 12386568 sb2 = 9360485
My questions:
1. Two Strings always hash the same if they are constructed from the
same "source" string. True? (The hashCode() is what's used to put
stuff into a Hashtable, right?)
2. If many more Strings are created from the original two Strings,
they all will also hash the same. True?
3. Two StringBuffers, even if created from the same String (or
"source" string), aren't guaranteed to hash the same. True? (It looks
to me from my experience that they use the address of the StringBuffer
or something - "alpha" can't possibly hash the same as "charlie".)
What is going on here?
This problem bit me bad when I was putting StringBuffers into a
Hashtable. I am guessing I should use a String from now on. But I have
another question here.
Suppose that the following things happen in my program:
- I create a bunch of Strings, and put them into a Vector.
- I iterate over the Vector and put some those Strings into a
Hashtable.
- I then set the Vector to null. Will my Hashtable be affected? (I
tend to think no, coz those Strings that _did_ go into the Hashtable
are still referenced by the Hashtable and hence won't be garbage
collected.)
TIA,
John Galt.