Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

10
  • 23
    I think they're just being cool and optimizing it. :) "(multiplier << 5) - multiplier" is just 31 * multiplier, after all ... Commented Apr 24, 2009 at 11:32
  • 1
    But to make it clear from my side... Never rely on the hashcode because the hashcode is something internal. Commented Apr 24, 2009 at 12:16
  • 1
    what are the variables of "offset", "count" and "hashCode" mean? i suppose "hashcode" is used as a cached value, to avoid future calculations, and that "count" is the number of characters, but what is the "offset" ? suppose I wish to use this code so that it will be consistent, given a string, what should i do to it? Commented Nov 18, 2013 at 23:07
  • @androiddeveloper See here: stackoverflow.com/questions/38546623/… Commented Dec 13, 2016 at 13:52
  • 1
    @androiddeveloper Now THAT'S an interesting question -- although I should have guessed it, based on your username. From the Android docs it looks like the contract is the same: s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] Unless I am mistaken, this is because Android uses Sun's implementation of the String object with no changes. Commented Dec 16, 2016 at 14:04