posted 17 years ago
Hi,
The question was trying to figure out your understanding on hashcode and equals contract.
All the conditions are valid, choose the one that is correct from the answers
x1.hashCode() != x2.hashCode() x = x + 1;
x3.equals(x4) == false x = x + 10;
x5.equals(x6) == true x = x + 100;
x7.hashCode() == x8.hashCode() x = x + 1000;
16. System.out.println("x = " + x); ----> 1111
1111 can be got only if all the conditions are met, indirectly hinting that the above comparisons on hashcode and equals result in true, Now choose from the answers which one is correct.
Going condition by condition,
HashCode and Equals Contract
------------------------------
The hashcode and equals contract states for any two objects x,y that
if (x.hashcode() == y.hashcode() ){
x.equals() MAY_BE_EQUALS_TO y.equals()
}
if (x NOT_EQUALS y))){
x.hashcode() MAY_BE_EQUALS_TO y.hascode()
}
if (x EQUALS y)){
x.hashcode() SHOULD_BE_EQUALS_TO y.hascode()
}
if (x.hashcode() != y.hashcode() ){
x.equals() SHOULD_NOT_EQUALS_TO y.equals()
}
CASE 1:
x1.hashCode() != x2.hashCode()
SOL: x NOT_EQUALS y
CASE 2:
x3.equals(x4) = false
SOL : x.hashCode() MAY_BE_EQUALS y.hashCode()
CASE 3:
x5.equals(x6) = true
x5.hashCode() MUST_BE_EQUALS x6.hashCode()
CASE 4
x7.hashCode() == x8.hashCode()
x7 MIGHT_BE_EQUALS x8