I had a similar problem and spent a while figuring this stuff out. You guys are right, but I didn't see you point until I had already figured it out myself. The thing is the difference between using s="string" and s=new String("string").
public void testStringPool(){
String s1 = "foo";
String s2 = "foo";
assertTrue(s1==s2); // *1
String s3 = new String("foo");
assertFalse(s1==s3); // *2
assertTrue(s1.equals(s3)); // *3
/* notes:
* 1: s1 and s2 ARE the same, because getting a string using s="string"
* will actually get the string from
Java's String pool.
* 2: s1 and s3 are NOT the same, because using s=new String("string")
* will (apparently) cause Java to instantiate a new String, and not
* get it from the pool.
* 3: That's why
you should always use equals instead of == to compare
* strings if you want to compare the contents of the strings.
*/
}
Pretty dodgy stuff, no?
[ October 27, 2004: Message edited by: Fredrik Petersson ]