This is the mail archive of the java@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: String hashCode


Bryce McKinlay <bryce@albatross.co.nz> writes:

> Except that Strings where this.data != data can still be interned, for
> example "sub = str.substring(5,10); sub = sub.intern()", right? I suppose
> intern() could simply copy the string value in this case.

I think it is reasonable for intern to always copy a String when
this.data != this.  Interned strings tend to be either program
literals or Strings you expect to use more than once.  So making
a clean copy, when needed, is likely to be a win (improved cache
locality).

> I'd prefer to simply add a hashcode field, but only set it if hashcode() is
> called or the string gets interned. But, as you point out, there is the
> problem of how to determine whether the value has been set or not.
> 
> It would seem reasonable to set it to null or some other unlikely value
> initially, then bump it if the string just happens to hash to the same value
> - but this would violate the published spec of String.hashCode.

You can use 0 as a "unknown-hash" code.  If the actual hasCode is 0,
then you have to recalculate it.  But that is rare, except when the
length is 0, in which case recalculation is cheap.  Or we can use
some other unlikely value.
-- 
	--Per Bothner
per@bothner.com   http://www.bothner.com/~per/

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]