Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.java.programmer Subject: Re: hashCode Date: Fri, 31 Aug 2012 17:55:32 +0200 Lines: 33 Message-ID: References: <563f186a-edb3-4311-ae48-3af7decfce2c@googlegroups.com> <502598d0$0$287$14726298@news.sunsite.dk> <5028191b$0$290$14726298@news.sunsite.dk> <50317b03$0$281$14726298@news.sunsite.dk> <503bfd0f$0$282$14726298@news.sunsite.dk> <3zc%r.5945$pd4.2658@newsfe21.iad> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net 5FcM3fBs9D2iM9rSfxJpPQgJKcKE6/UMOE7t8QsAOef/6h+Qv5n6AO5N6X/E9Uf0U= Cancel-Lock: sha1:70VoqFURVzDBhd04gHAMEn5Eem8= User-Agent: Mozilla/5.0 (Windows NT 6.0; WOW64; rv:15.0) Gecko/20120824 Thunderbird/15.0 In-Reply-To: Xref: csiph.com comp.lang.java.programmer:18477 On 31.08.2012 17:38, Eric Sosman wrote: > On 8/31/2012 11:08 AM, Jim Janney wrote: >> Daniele Futtorovic writes: >> >>> On 31/08/2012 03:43, Eric Sosman allegedly wrote: >>>> On 8/30/2012 6:52 PM, Daniele Futtorovic wrote: >>>> [...] >>>> As an example of why a hasher might want access to a strictly-private >>>> field, I offered String: [...] >>> >>> Imposing that classes should expose all information "relevant" (says >>> who??) to hashing is utter rubbish IMNSHO. >> >> Objects that compare equal must hash to the same value. It follows that >> if the hash function uses a value, so must the comparison method. > > Since java.lang.String had already been mentioned, it's sort > of too bad you didn't look at it before posting. Had you done so, > you'd have found that [1] hashCode() uses the private field `hash' > and [2] equals() does not. Well, Jim's wording may not be correct to the last bit but the message is still true: String's hash member just caches the hash code derived from the characters of the String. And obviously equals() must compare the characters. So basically both recur to the same underlying data. Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/