How to override equals method in java

Posted by Subash Adhikari on Stack Overflow See other posts from Stack Overflow or by Subash Adhikari
Published on 2011-11-18T09:41:39Z Indexed on 2011/11/18 9:50 UTC
Read the original article Hit count: 423

Filed under:
|

I am trying to override equals method in java. I have a class People which basically has 2 data fields name and age. Now I want to override equals method so that I can check between 2 People objects.

My code is as follows

public boolean equals(People other){
    boolean result;
    if((other == null) || (getClass() != other.getClass())){
        result = false;
    } // end if
    else{
        People otherPeople = (People)other;
        result = name.equals(other.name) &&  age.equals(other.age);
    } // end else

    return result;
} // end equals

But when I write age.equals(other.age) it gives me error as equals method can only compare String and age is Integer.

Please help me fix this.

Thanks is Advance.

© Stack Overflow or respective owner

Related posts about java

Related posts about equals