Strings in Switch
Ulf Zibis
Ulf.Zibis at gmx.de
Wed Dec 9 10:29:47 PST 2009
Yes, but ONLY if hash code is not calculated yet AND equals() is invoked
1st time.
I assume, that all interned Strings e.g. constants yet have the hash
value precalculated.
This is to avoid (1) the little more expensive hash code computation if
a short living string is only equated once, and (2) an additional field
in each string object to save a kind of threshold marker.
The win of adding hash code compare into String#equals() would come to
account, if a string would be equated often.
-Ulf
Am 09.12.2009 19:05, Neal Gafter schrieb:
> Do you really want to set the has code of every string in the world to -1?
>
> On Wed, Dec 9, 2009 at 9:58 AM, Ulf Zibis <Ulf.Zibis at gmx.de
> <mailto:Ulf.Zibis at gmx.de>> wrote:
>
> public boolean equals(Object anObject) { ...
>
> if (hash == 0)
> hash = -1; // mark 1st invokation of
> equals()
> else if (anotherString.hash == 0)
> anotherString.hash == -1; // mark 1st invokation "
> ... }
>
> public int hashCode() {
> int h = hash;
> if (h == 0 || ++h == 0) { ...
> }
> return h;
> }
>
>
More information about the coin-dev
mailing list