String str="abc";
for (int i = 0; i < 100; i++)
{
System.out.println(str.hashCode());
}
1) String is an immutable class and its hashCode is cached in its private variable hash.
2) Since string str is a literal this string object created is added to stringpool in permgen space. So when ever referencing str it should give me the same object.
Debugging through the process in hashCode method of string, based on above two points when i call str.hashCode() it should enter into calculating the hash only once and the next 99 times it should return me the "cached hash private variable of the string object". It doesnt go according to the point 1. Can some one please let me know about this behavior?
Debugging through this you will notice that hashCode is calculated 100 times and I am printing hashCode value to see if the objects are have same hashCode.