- "", - , .
, , , :
Object r = new Rational();
boolean b = r.equals("x");
Rational, , String Object.equals(Object), , String .
, Rational String - ! Java. , , "" .
, , , "obj" Rational: Rational rhs = (Rational)obj; - , , instanceof , obj Rational, - (, String!).
if (obj instanceof Rational) {
Rational r = (Rational)obj;
....
, :
- ,
r.equals("x"): : " , r Rational"? "": Java . , . , , , , , "" - , Rational . equals , : null, , .. instanceof - getClass() == obj.getClass() . - Java . : hashCode Java?- , Java , " ".
myMethod(String) myMethod(Object) (.. - ), . , Java ; . :
contra-variance ?
hashCode(): , , , hashcode . equals 95% .
Edit: in the updated question, you provide a hashCode method that ... thinks a lot! You can split it into return (31 + num) * 31 + denom;or similar. Because of this, I use 31: Why does the Java () hash code in String use 31 as a multiplier? .
source
share