Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6833633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:00:10+00:00 2026-05-26T23:00:10+00:00

I am trying to override equals method in Java. I have a class People

  • 0

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.

Solution

I used == operator as suggested and my problem is solved.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:00:10+00:00Added an answer on May 26, 2026 at 11:00 pm
    //Written by K@stackoverflow
    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("StackOverflow", 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);
                }
            }
        }
    }
    
    //written by K@stackoverflow
    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;
        }
    }
    

    Output:

    run:

    — Subash Adhikari – VS – K false

    — Subash Adhikari – VS – StackOverflow false

    — Subash Adhikari – VS – Subash Adhikari true

    — K – VS – StackOverflow false

    — K – VS – Subash Adhikari false

    — StackOverflow – VS – Subash Adhikari false

    — BUILD SUCCESSFUL (total time: 0 seconds)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to override equals method for a parameterized class. @Override public boolean equals(Object
I am trying to override the DataGridViewTextBoxCell's paint method in a derived class so
Trying to override a tostring in one of my classes. return string.Format(@ name =
I'm trying to find documentation on how I can override a property name in
Assume I have a Posts table with field Owner of type Person (which has
I am trying to create a class say MyStack that would implement a java.util.collections
We have implemented our domain model using the accountability pattern, which we are trying
I am trying to override Equals so that it compares based on a variable
When trying to call on a method from another class my method as a
I am trying to get a quicksort method to sort some or partial data.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.