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 8754799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:42:38+00:00 2026-06-13T13:42:38+00:00

Possible Duplicate: Overriding equals and hashCode in Java If I have class A {

  • 0

Possible Duplicate:
Overriding equals and hashCode in Java

If I have

class A {
    int x = 1;
}
...
A a1 = new A();
A a2 = new A();
a1.equals(a2);

If I compare 2 instances of A without override the equals method, will I get expected result?

  • 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-06-13T13:42:40+00:00Added an answer on June 13, 2026 at 1:42 pm

    If I compare 2 instances of A without override the equals method, will I get expected result?

    That depends on what you expect 🙂

    The default implementation will give you reference equality – in other words, when you compare two references, equals will only return true if they’re references to the same object.

    You would normally override equals to implement “value equality” – where two distinct objects are deemed equal, usually by virtue of having equal field values themselves. The exact meaning of equality will depend on your design – the two objects could still be distinguishable in other ways, for example.

    If you override equals, you should also override hashCode to be consistent with equals, such that if a.equals(b) is true, then a.hashCode() == b.hashCode(). This will allow instances of your class to be used as keys in hash-based collections (e.g. HashMap) so that you can look up a value based on a key which is equal to the original one, rather than having to use a reference to the exact original key object.

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

Sidebar

Related Questions

Possible Duplicate: Overriding equals and hashCode in Java All, I have defined my class
Possible Duplicate: Returning false from Equals methods without Overriding One of my colleague asked
Possible Duplicate: Why is it important to override GetHashCode when Equals method is overridden?
Possible Duplicate: Can we override static method in Java? We cannot override the static
Possible Duplicate: Overriding global php.ini file I have looked but can't seem to find
Possible Duplicate: Overriding static variables when subclassing I have a set of classes that
Possible Duplicate: Selecting class by Maven build profile I have two packages called com.app.util
Possible Duplicate: Java: “implements Runnable” vs. “extends Thread” I have two questions about multithreaded
Possible Duplicate: C++ Overriding… overwriting? What's the difference between override and overwrite ? I've
Possible Duplicate: operator overloading and overriding in java I want to know if it

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.