-1

I've read that this is the source code for String.hashCode()

   public int hashCode() {
   int h = hash;
   if (h == 0) {
       int off = offset;
       char val[] = value;
       int len = count;

       for (int i = 0; i < len; i++) {
           h = 31*h + val[off++];
       }
       hash = h;
   }
   return h;
   }

My question is, what are "offset" & "hash"? I can tell that "value" is the actual String and "length" is its length, but I don't understand what the other two are.

6
  • 4
    You could take a look at the rest of the String source code for context. Commented Jul 23, 2016 at 21:35
  • 1
    @AndyTurner Thanks, had checked 8 and 7 Commented Jul 23, 2016 at 21:39
  • 1
    Read the comments in the source code next to offset and hash. Commented Jul 23, 2016 at 21:42
  • /** The offset is the first index of the storage that is used. */ private final int offset; What is storage referring to? Commented Jul 23, 2016 at 22:18
  • 1
    @KartikChughヅ Read 2 lines above the offset variable. Commented Jul 23, 2016 at 22:46

1 Answer 1

0

The offset is the first index of the value[] array of chars of the String. The hash variable is a field for caching the hashCode so as to be more efficient (the hashCode() method only computes the hash if it's current value is 0; else it returns the cached hash)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.