Page 1 of 1

About Question enthuware.ocpjp.v7.2.1340 :

Posted: Thu May 29, 2014 8:08 am
by kashyapa
return this == obj? true : (theval%3 == 0 && ((GoodOne)obj).theval%3==0) ? true :false;
Please someone explain me this statement using if condition :) .

Thanx.

Re: About Question enthuware.ocpjp.v7.2.1340 :

Posted: Thu May 29, 2014 10:06 am
by admin

Code: Select all

boolean temp = false;
if(this == obj)
{
  temp = true;
}
else{
   if(theval%3 == 0 && ((GoodOne)obj).theval%3==0) {
      temp =  true;
   }else{
      temp = false;
   }
}
return temp;
HTH,
Paul.

Re: About Question enthuware.ocpjp.v7.2.1340 :

Posted: Sat May 21, 2016 11:16 am
by codecodecode67
Why are equal()'s properties considered here when the question asks only about the contract between hashCode() and equals()?

The contract between them only states that, as said in the solution explanation:
If the equals() method returns true, the hashCode() of the two objects MUST be the same. The reverse is desirable but not necessary.
Meaning that, according to the equal() method's individual properties - an equals() method that always returns false will not be a valid equals() method as it will not be reflexive and further to that - we won't be able to see the equal objects. However, according to the contract between the two methods - not the two method's individual properties - the only property of the contract that needs to be true is the one in the quote above. Meaning that equals() returning false should satisfy the question...

Please let me know what you think.

Thanks

Re: About Question enthuware.ocpjp.v7.2.1340 :

Posted: Sat May 21, 2016 8:20 pm
by admin
For a valid equals-hashcode contract, the equals method has to be valid has well. By your logic, an equals method with a compilation error could also be valid option, if it satisfies the equals-hashcode contract. But obviously, that is not correct because implicit in the requirement is the fact that equals method should compile.

HTH,
Paul.