• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following is from K & B Mock exam 1

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

All my answers counted as wrong, and the correct answer is
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic