This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
K&B's book mentions that the hashCode() method implementation can be described in three ways- 1. LEGAL 2. APPROPRIATE 3. CORRECT
1. By LEGAL I understand that the method must meet the method contract.
2. By APPROPRIATE I understand that the generated hashCode() should be unique or as random as possible so as to obtain a good distribution of values.
3. I do not then understand what is meant by a CORRECT implementation. Don't the above two adjectives describe it sufficiently? Or, is CORRECT a 'mild' form of APPROPRIATE - for e.g. an implementation that returns a constant for all values would be CORRECT, but since it is absolutely inefficient, it can't be termed as APPROPRIATE.
1. I think that we both understand the same that LEGAL means not only is hashCode() overriden and the code compiles, but that if you call hashCode() on the same object, the same value is returned unless the object has been mutated in such a way that it is not equals() to what it was before it was mutated.
2. I believe that K & B is using APPROPRIATE & CORRECT as synonyms. I think that you and I have the same understanding, yet I would not use the word "random". I think that more accurate terminology would be "uniform distribution" of the values of the hashCode() method since being "random" breaks the hashCode() method's contract.
K&B use (at least) four terms in relation to hashCode overrides: legal, appropriate, correct and efficient. In the summary notes at the end of chapter 7, they say that "an appropriate hashCode() override sticks to the hashCode() contract" and "an efficient hashCode() override distributes keys evenly across its buckets". Unfortunately they don't set out definitively what they mean by "correct" and "legal" (unless I missed it).