Per the Java documentation, the hash code for a String object is computed as:
String
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] <
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
I'm not sure, but I would guess they tested some sample of prime numbers and found that 31 gave the best distribution over some sample of possible Strings.