java - equals and hashCode -


I am running a question about equal and hashcode contracts: here

Is:

  class SortOf {String name; Int hair; String code; Small rate; Public Ent Hashod () {Return (Code: Lang () * Child); } Public boolean equivalent (object o) {// insert code here}}  

Which of the following will meet the (and) Hashod () contracts equal to this class? (Select all that apply.)

Correct answer C:

  return (sortoff) .code.length () * ((SortOf) o) . == this.code.length () * this.bal;  

D:

  return (sortoff) .cod.line () * ((sortoff) o) .bill * ((sortoff) O) .rate == this.code.length () * this.bal * this.rate;  

I have a question about the last option D, if two objects

a: code.length = 10, bal = 10, rate = 100

B: code.length = 10, bal = 100, rate = 10

Then we get the A using the par () method in D Is true correct to evaluate .equals (B) ? But then they get a different hashod because they have different remains? Is it that I somewhere misunderstood the concept? Can anyone explain this to me?

You are right - D will be inappropriate for this reason.

More generally, hashode and equal basically take the same field in the same way, in the same way. This is a very weird equivalent to begin with implementation, of course - you should check the similarity between each field normally included in some cases field related to the difference. Which allows multiplication etc., but I would not expect that string length be included ...

An important point that often confuses people that it is is unequal Vas Valid to the same hash code for Uon; This is the case that you highlighted (same object with different hash code) which is unacceptable.


Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -