cmbhatt
SCJP, SCWCD, SCBCD
SCJP, SCWCD, SCBCD
You should use the rules from equals() too. This method violatesOriginally posted by Deepak Jain:
b) return theval%3 == 0? true :false;
This option is correct, Becuase in hashCode() the hashCode of two objects
will be same if theVal is divisble by 3, so now just take the reverse
of this in equls() method, If two objects with theVal that is divisible by
3 then they are equal.
g1.theVal = 1;
g2.theVal = 2;
Here g1.equals(g2) is false and g1.hashCode()!=g2.hashCode() but this is not
mandatory, Unequal objects can have same hashCode, this will cause iefficient hashTables.
So this option is correct.
Try it with different values for option b.Originally posted by Deepak Jain:
I think both b and e are correct, Please correct me if am wrong.
SCJP, SCWCD, SCBCD
cmbhatt
And as result it will violate the hashCode() contract sooner or later.Originally posted by Chandra Bhatt:
I agree that to compare two objects this equals method is not suitable
anyway, because it violates the symmetric property
cmbhatt
cmbhatt
They are equal according to the equals() method.Originally posted by Chandra Bhatt:
What value you have given, the objects are not equal with respect to what
hashCode() and equals() say.
Don't play dumb with me! But you can try this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|