0

When does string caches it's hashcode? I found a lot of article saying, string caches it's hashcode during when we create a string literal. if this is true, then when we create a string object, why it's "hash" is showing 0? I think Once after the hashcode() method get called, the hashcode getting cached to the object header.

Can anybody clear my confusion? String hashcode is computed and getting cached during the object creation time or once the hashcode() get computed then only it's getting cached in the string object.

When we put an object on a map, during put operation the hashcode is getting calculated or it's been already calculated during the object creation, so just got used.

During debugging I found the actual string hash(string literal hash) and the hash which is getting stored in the hashmap bucket is different.So what's the logic behind that?

  • 2
    http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/lang/String.java#String.hashCode%28%29 – Jim Garrison Mar 05 '18 at 15:14
  • "I found a lot of article saying, string caches it's hashcode during when we create a string literal" Please provide links to these articles. – Andy Turner Mar 05 '18 at 15:15
  • @AndyTurner Hi,please take the below link as an instance. It's written clearly here "Since String is immutable, its hashcode is cached at the time of creation and it doesn’t need to be calculated again." https://www.journaldev.com/802/string-immutable-final-java – Abhilash Panigrahi Mar 05 '18 at 15:21
  • 1
    Well, that's simply wrong, as the link posted by @JimGarrison shows: it's calculated when you first need it. String is immutable, *and so its hashcode doesn't need to be calculated twice*; but if you don't need it at all, it isn't calculated. – Andy Turner Mar 05 '18 at 15:26
  • Thank's @AndyTurner for clarifying.So can we say, When we insert an element to a hashmap as string as a key, then actually the hash of the key gets computed first and then cached in the memory and when we invoke the get method which also needs the hash to be calculated, it just used the hash which has been already computed during the put operation. – Abhilash Panigrahi Mar 05 '18 at 15:48
  • You cannot rely on arbitrary Internet blogs. You can rely on the specifications and the Javadoc and in most cases the source code. – user207421 Mar 05 '18 at 15:50

1 Answers1

3

This is the sourcecode of getHashCode()

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/String.java#String.hashCode%28%29

public int More ...hashCode() {
1494        int h = hash;
1495        if (h == 0) {
1496            int off = offset;
1497            char val[] = value;
1498            int len = count;
1499
1500            for (int i = 0; i < len; i++) {
1501                h = 31*h + val[off++];
1502            }
1503            hash = h;
1504        }
1505        return h;
1506    }

As you can see, hashcode is computed the moment hashcode is requested.

The test on line 1495 tests if it's 0. If it's 0 it gets computed.

Tschallacka
  • 27,901
  • 14
  • 88
  • 133