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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:19:18+00:00 2026-06-05T12:19:18+00:00

I recently have to override the equals and hashCode methods in Java. I hence

  • 0

I recently have to override the equals and hashCode methods in Java. I hence looked for a fast and efficient way to compute hash codes.

Java developers seem to aggree on the following method :

    int hash = 23;
    hash = hash * 37 + paramOne;
    hash = hash * 37 + paramTwo;
    // And so on...

It might be simple arithmetic but I don’t really get it. What are the garantees ? What are the corner cases ? Is there a better (rather simple) way to do it ?

Thank you !

  • 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-05T12:19:20+00:00Added an answer on June 5, 2026 at 12:19 pm

    In words of Joshua Bloch (explaining the default implementation of hashCode() method in String class , that is : s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]):

    The value 31 was chosen because it is an odd prime. If it were even and the multiplication overflowed, information would be lost, as multiplication by 2 is equivalent to shifting. The advantage of using a prime is less clear, but it is traditional. A nice property of 31 is that the multiplication can be replaced by a shift and a subtraction for better performance: 31 * i == (i << 5) – i. Modern VMs do this sort of optimization automatically.

    For further reading , refer this and this.

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

Sidebar

Related Questions

I recently have a problem that my java code works perfectly ok on my
I have a pretty standard Java Desktop Application in NetBeans. I recently added a
I have recently experimented with creating an easy way to open a ProgressDialog up
I have recently started Android development, and i already know Java. So i was
I have been practicing TDD in C# for several years now and recently have
I'm using Visual Studio 2005 on a Windows XP SP3 machine and recently have
Recently I have been learning about WMI and WQL. I found out the list
Recently I have asked a question about what I should use to create self-contained
Recently I have heard the bellow statement. Can someone please elaborate on it? With
Recently I have read a lot about different NoSQL databases and how they are

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.