In Java
It is not correct to compare heap and constant pool. Especially using hashCode
.
Let's go step by step:
Since Java 7 string pool is inside the heap memory. Read more.
HashCode in Java is not related to memory address*
JVM has an arg to specify hashCode default algorithm
-XX:hashCode=k
Number k
could be one of:
String override default hashCode
implementation. It is based on string content.
java.lang.String
:
public int hashCode() {
int h = hash;
if (h == 0 && value.length > 0) {
char val[] = value;
for (int i = 0; i < value.length; i++) {
h = 31 * h + val[i];
}
hash = h;
}
return h;
}