I was seeing the implementation of String class in Java, and the constructor of this goes like this
public String(String var1) {
this.value = var1.value;
this.hash = var1.hash;
}
So, if you'll do
String s1 = "String";
String s2 = new String("String");
even though s1 and s2 are two different objects, s1.hashCode() == s2.hashCode()
returns true
as expected, because in it's constructor, String Class takes hash from it's String literal.
I've deduced that hash of this String literal is computed when String literal is created in String literal Pool, and not when String object is created.
My question is, how and where the hash of this String literal is computed in Java?
is there some native implementation for this?
Copyright Notice:Content Author:「Shashi」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/35317546/how-hash-of-a-string-literal-in-string-literal-pool-computed