From: Jim Janney on
Daniel Pitts <newsgroup.spamfilter(a)virtualinfinity.net> writes:

> On 7/26/2010 12:12 AM, Andreas Leitgeb wrote:
>> Jim Janney<jjanney(a)shell.xmission.com> wrote:
>>> Andreas Leitgeb<avl(a)gamma.logic.tuwien.ac.at> writes:
>>>> Has anyone found e.g. an english dictionary-word with hashCode 0, yet?
>>>> Or perhaps the name of some commonly used class in Java standard library
>>>> or some other String likely occurring in innocent code?
>>> All strings of the form "\0", "\0\0", "\0\0\0", etc.
>> Well, they're *almost* trivial ;-)
>>
>>> For ordinary words, I ran the following against the
>>> dictionary file on a Linux system and got no hits.
>> Thanks for the code (I guess I'd have been too lazy, myself ;)
>>
>> I ran it against all the pure class-names (like "java.lang.String",
>> and "java/lang/String") from $JAVA_HOME/lib/rt.jar and found no
>> match, either. Not even for each "Lpath/to/ClassName;".
>>
> The conditions under which String.hashCode() would return 0:
>
> 1. Empty String
> 2. All characters in the string are \0. (Empty string is the
> trivial case of this)

To state it another way:

1) For any Java string s, s.hashCode() == ("\0" + s).hashCode()
2) "".hashCode() == 0


--
Jim Janney
From: Jim Janney on
Andreas Leitgeb <avl(a)gamma.logic.tuwien.ac.at> writes:

> Daniel Pitts <newsgroup.spamfilter(a)virtualinfinity.net> wrote:
>> 3. The string is long enough that s[0]*31^(length-1) is close enough
>> to, or over 2^32, and the rest of the characters add up just right.
>
> It's clear, that any 0-hashCode string with at least one non-\0 char
> would have to have some minimum length around 5. ("clear" may be a
> slight over-statement, as it's only a consequence of chars being treated
> as unsigned when size-extended to int)
>
> My question was not intended to ask others to do the work of finding such
> words, but rather, that if anyone had already accidentally found such a
> String in the past, that he'd share it here
> - leaving away empty or non-empty sequences of \0 (and empty sequences
> of non-\0 as well ;-) ).
>
> Of course, I also don't want to stop those, you've made it their hobby
> now, to find such a word for their own fun.
>
> Just so that one such String is named: "forty two 42 %4;2*"

Applause.

--
Jim Janney
From: Lew on
Jim Janney wrote:
> 1) For any Java string s, s.hashCode() == ("\0" + s).hashCode()
> 2) "".hashCode() == 0
>

Generally,

1) Given two Java Strings s and z where z.hashCode() == 0,
s.hashCode() == (s + z).hashCode() and
s.hashCode() == (z + s).hashCode()

3) "\0".hashCode() == 0

--
Lew
From: Lew on
Lew wrote:
> Generally,
>
> 1) Given two Java Strings s and z where z.hashCode() == 0,
>    s.hashCode() == (s + z).hashCode() and
>    s.hashCode() == (z + s).hashCode()
>
> 3) "\0".hashCode() == 0
>

It occurs to me that
s.hashCode() == (s + z).hashCode()
is wrong.

--
Lew