Here what is written in String API for Hashcode------
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;
}
Can anybody explain how caching is happening here ?
One clue is :---- if (h == 0 && value.length > 0) { Code will only go inside this condition ,only then hashcode is calculated otherwise same is written.But how this happens for two string created with new like :
String a = new String("abc"); String b = new String("abc"); Please explain it