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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:34:02+00:00 2026-05-25T14:34:02+00:00

Suppose a class MyClass has 3 variables, a , b and c . This

  • 0

Suppose a class MyClass has 3 variables, a,b and c. This class also overrides hashCode() that calculates return value using Joshua Bloch’s approach.

Assuming the above, would it be correct to assume that MyClass() and new MyClass() are deeply equal if their hashcodes match?

For the purposes of the example, assume that both objects are initialized with the same parameters

  • 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-25T14:34:03+00:00Added an answer on May 25, 2026 at 2:34 pm

    Certainly not.

    hashCode is required to yield the same value for equivalent objects, but different objects may yield the same value. So, the following is a correct (but inefficient) hashCode implementation:

    public int hashCode()
    {
        return 42;
    }
    

    Even when you know a specific hashing method is used, it is a bad idea to assume hashCode is perfect: later, some refactoring/subclass might replace the hashCode implementation with something else. If you want this, create a perfectHashCode method that does always return a perfect hashcode, and use that for implementing hashCode

    To answer your initial question: no, even Bloch’s approach afaics does not yield a perfect hashCode, because (because of wrapping over MAXINT) 2 different paths might eventually collide into the same hash code.

    This is easy to imagine: say your object holds 2 integers. The hash code is only 1 integer. So it can never yield a different value for each combination of 2 integers.

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

Sidebar

Related Questions

Suppose I have: class myclass: def __init__(self): self.foo = bar where the value of
Suppose I have a class like this: function myClass(q) { this.someFunction = function(e) {
Let's suppose I have this object: [Serializable] public class MyClass { public int Age
Suppose the class MyClass has constructor: MyClass::MyClass( ... ) { var = new OtherClass(
Suppose I have a class public class MyClass { private Set<String> set = new
Suppose class B extends class A . I have a List<A> that I happen
Suppose my repository class looks like this: class myRepository : IDisposable{ private DataContext _context;
Suppose I have a LimitedValue class which holds a value, and is parameterized on
Suppose, i have class, define('property', 'test'); class myClass { public $test; } $obj=new myClass;
Suppose I have a class MyClass to which I want to add certain 'observer'

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.