Today I encoutered a problem to compare to user-defined instances for value equavelence.
It is necessary to override the equals() method derived from System.Object.
I tried to make it as simple as
public boolean equals(userDefinedClass p){
return this.v1==p.v1&&this.v2==p.v2;
}
but it refuses to work.
After carefully explored the context, I found 2mistakes:
1. "==" is used for reference compare, which is against my intention.
2. this function is not overriding the derived equals(), but a overload with different parameters.
So, this works
public boolean equals(Object q){ userDefinedClass p = (userDefinedClass)q; return this.v1.equals(p.v1)&&this.v2.equals(p.v2); }