-1
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.

3
  • 4
    where did you see that the hashcode is calculated 100 times?
    – M. Abbas
    Jul 24, 2012 at 16:06
  • Please excuse the close vote - I misread the question. Jul 24, 2012 at 16:09
  • You are mistaken. It is calculated once, except in the degenerate case where it computes to zero, for example a zero length string.
    – user207421
    Jul 25, 2012 at 12:11

3 Answers 3

1

The hashcode is calculated only once, please see the body of the java.lang.String.hashCode() function:

public int hashCode() {
    int h = hash;
    if (h == 0) { 
        int off = offset;
        char val[] = value;
        int len = count;

            for (int i = 0; i < len; i++) {
                h = 31*h + val[off++];
            }
            hash = h;
        }
        return h;
    }

The second time, you called str.hashCode() the variable h is different than 0 and the body of the if statement is not executed.

1

java.lang.String class object does cache hash value. It assigns the instance variable hash the first time hashcode is called, see below :

public int hashCode() {
        int h = hash;
        if (h == 0 && count > 0) {
            int off = offset;
            char val[] = value;
            int len = count;

            for (int i = 0; i < len; i++) {
                h = 31*h + val[off++];
            }
            hash = h;
        }
        return h;
    }

and any further requests to hashcode just return h, via hash, skipping the calculation. Its right there in the code I can see it!

5
  • I have gone through the code that is where my question lies and if you try debug and put a break point on if(h==0 && count>0) and iterate through the loop you will see that it calculates hashcode multiple times. Jul 24, 2012 at 16:56
  • @lordlupine it blatantly doesn't Jul 24, 2012 at 17:13
  • did you even try it because i tried again now.. any who looks at the code can say clearly it doesnt calculate the hashcode over and over. Can you try it in eclipse or some where else if you can? Jul 24, 2012 at 17:21
  • lots of people can say that. You are misreading the debugger, it will step over the if statement and go straight to the return statement. Jul 24, 2012 at 17:41
  • you can keep a breakpoint inside the if say at int off=offset; Jul 24, 2012 at 17:50
0

My bad.

What's happening was since i had the breakpoint in the hashCode when you start debugging, the hashCode method is not invoked by my forloop but by some System initial static methods are invoking String hashCode method.

Admin's please close the quesiton

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.