Which statement is true about the code that can fill in the blank?
class MyClass { public int hashCode() { return 1; } public boolean equals(Object o) { return ; } }
D.
If two instances of a class have the same hash code, they might or might not be equal.
The reverse is not true.
If two objects are equal, they must have the same hash code in order to comply with the contracts of these methods.
The answer is none of the above because the method can't simply return true or false.
Based on the rules of equals()
, if null is passed in, the result must be false.
If an object identity is passed in, the result must be true due to reflexivity.
Option D is correct.