Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Below is the People class which has 2 data fields name and age. I want to override the equals method in Java to check between 2 People objects. 

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

I get an error when I write age.equals(other.age) as  equals method can only compare String and age is Integer.

1 Answer

0 votes
by (19.7k points)

You should use == operator like below:

public class Main {

    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

        // TODO code application logic here

        ArrayList<Person> people = new ArrayList<Person>();

        people.add(new Person("Subash Adhikari", 28));

        people.add(new Person("K", 28));

        people.add(new Person("manpreet", 4));

        people.add(new Person("Subash Adhikari", 28));

        for (int i = 0; i < people.size() - 1; i++) {

            for (int y = i + 1; y <= people.size() - 1; y++) {

                boolean check = people.get(i).equals(people.get(y));

                System.out.println("-- " + people.get(i).getName() + " - VS - " + people.get(y).getName());

                System.out.println(check);

            }

        }

    }

}

public class Person {

    private String name;

    private int age;

    public Person(String name, int age){

        this.name = name;

        this.age = age;

    }

    @Override

    public boolean equals(Object obj) {

        if (obj == null) {

            return false;

        }

        if (obj.getClass() != this.getClass()) {

            return false;

        }

        final Person other = (Person) obj;

        if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {

            return false;

        }

        if (this.age != other.age) {

            return false;

        }

        return true;

    }

    @Override

    public int hashCode() {

        int hash = 3;

        hash = 53 * hash + (this.name != null ? this.name.hashCode() : 0);

        hash = 53 * hash + this.age;

        return hash;

    }

    public int getAge() {

        return age;

    }

    public void setAge(int age) {

        this.age = age;

    }

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

}

Interested in Java? Check out this Java Certification by Intellipaat.  

Related questions

0 votes
1 answer
asked Jan 23, 2020 in Java by angadmishra (6.5k points)
0 votes
1 answer
asked Apr 27, 2021 in Java by sheela_singh (9.5k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...