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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:02:41+00:00 2026-05-24T06:02:41+00:00

Whenever it is invoked on the same object more than once during an execution

  • 0

Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified

Above statement is written at http://download.oracle.com/javase/6/docs/api/java/lang/Object.html#hashCode()
under hashcode contract.

I have a question on statement i.e

provided no information used in equals comparisons on the object is
modified

Even if we modify equals method still hashcode will return the same integer . so howcome hash code value is dependent on objects equal comparison.

  • 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-24T06:02:41+00:00Added an answer on May 24, 2026 at 6:02 am

    You are adding extra meaning to that statement that is not there. It does not say that hashCode WILL change if the evaluation of equals changes. It is saying hashCode MAY change if the evaluation of equals changes. It is no longer guaranteed that the hashCode will be the same afterward. It is not wrong if it still is!

    Remember

    @Override
    public int hashCode() {
        return 1;
    }
    

    is a completely legal implementation of hashCode() that meets all requirements set forth by the documentation. hashCode is not, by definition, dependent on the equals calculation.

    In practice they will of course be coupled together. Normally you override hashcode using all the same fields used as in equals because it is necessary that two different objects whose equals are true have the same hashCode. And return 1; wouldn’t play very nice with any of the classes that actually rely on the hashCode() method.

    This more fully formed example of a legal implementation of hashCode that sometimes changes when equals changes, but not always, may be more clear.

    public class MakesHashMapsSlow {
    
        private String value;
        public String getValue() {
            return value;
        }
        public void setValue(String value) {
            this.value = value;
        }
    
        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final MakesHashMapsSlow other = (MakesHashMapsSlow) obj;
            if ((this.value == null) ? (other.value != null) : !this.value.equals(other.value)) {
                return false;
            }
            return true;
        }
        @Override
        public int hashCode() {
            if (value == null || value.isEmpty()) {
                return 0;
            } else {
                return value.charAt(0);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Signed Java Applets have the same security clearance as a normal Java application running
Whenever I'm at a break point in a certain C#.NET application in Visual Studio
I'm trying to implement a method that returns an error whenever a certain directory
How can I Invoke a method in a class whenever a method in the
I need to be able to trigger a event whenever an object is added
I have a need to create a HttpSession (via cookie) whenever a client invokes
When ever IVideoWindow::put_Visible() is invoked, it peek a message from the message queue and
Whenever I design a database, I always wonder if there is a best way
Whenever I try to add a new project to my SourceSafe repository it creates
Whenever I indent HTML in PHP mode, emacs (22.1.1, basic install on Redaht Linux

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.