programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Ron McLeod
• Paul Clapham
• Devaka Cooray
• Liutauras Vilda
Sheriffs:
• Jeanne Boyarsky
• paul wheaton
• Henry Wong
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Tim Moores
• Carey Brown
• Mikalai Zaikin
Bartenders:
• Lou Hamers
• Piet Souris
• Frits Walraven

# Mock doubt, a Q about equals contract

Ranch Hand
Posts: 102
• Number of slices to send:
Optional 'thank-you' note:
The following is from K & B Mock exam 1

if the output is "x = 1111", which of the following will always be true ?

x2.equals(x1) == true;

This is an error isn't it, specially this one should always be false
if it become true, then the equals() contract will not be applied.

Ranch Hand
Posts: 381
• Number of slices to send:
Optional 'thank-you' note:
I think this must be
x7.equals(x8) == true or x8.equals(x7) == true;
x2.equals(x1) may results to true or false.

Costa lamona
Ranch Hand
Posts: 102
• Number of slices to send:
Optional 'thank-you' note:
Thanks

Originally posted by Sanjeev Kumar Singh:
I think this must be
x2.equals(x1) may results to true or false.

Actually, I beleive it can be true because the contrapositive of one of
the statments in the hashCode() contract is

if x.hashCode() != y.hashCode() returns true, then x.equals(y) must return false.

K & B on page 534.

Do you agree ?

Sanjeev Singh
Ranch Hand
Posts: 381
• Number of slices to send:
Optional 'thank-you' note:
I revisited the hashCode() and equal() contract.I think I have given wrong explanation.

if x.hashCode() != y.hashCode() returns true, then x.equals(y) must return false.

Yes I agree on this.So it is not neccessary that x.equals(y)must not return true.
Now coming to the original question

x3.hashCode()==x4.hashCode() must return true and
x5.hashCode()==x6.hashCode() must return true

 Story like this gets better after being told a few times. Or maybe it's just a tiny ad: We need your help - Coderanch server fundraiser https://coderanch.com/wiki/782867/Coderanch-server-fundraiser