Reputation: 948
I'm reading Effective Java 2nd Edition by Joshua Bloch.
In this paragraph, he mentions that:
Many classes in the Java platform libraries, such as String, Integer, and Date, include in their specifications the exact value returned by their hashCode method as a function of the instance value. This is generally not a good idea, as it severely limits your ability to improve the hash function in future releases. If you leave the details of a hash function unspecified and a flaw is found or a better hash function discovered, you can change the hash function in a subsequent release, confident that no clients depend on the exact values returned by the hash function.
Can anyone please share some insights what he means by 'exact' values. I had a look at the String implementation class but still unable to understand what he means...
Thanks in advance!
Upvotes: 2
Views: 393
Reputation: 73558
From String.hashCode()
:
Returns a hash code for this string. The hash code for a String object is computed as
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
By giving out the definition in the javadoc, people may write code that depends on exactly that hashing algorithm. Changing the hash algorithm in a future release would then break that code.
Upvotes: 8
Reputation: 206876
If you look at the API documentation of java.lang.String.hashCode()
, it describes exactly how the method is implemented:
Returns a hash code for this string. The hash code for a String object is computed as
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
using int arithmetic, where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation. (The hash value of the empty string is zero.)
What Bloch says, is that it is a mistake that classes such as String
describe the implementation details in the API documentation, because this means that programmers can count on the hashCode
method being implemented this way. If, in a future Java release, Oracle wants to implement a different, maybe more efficient algorithm to calculate a hash code for a string, then that would be a backward compatibility problem - the behaviour might change compared to previous Java versions.
By describing the implementation in detail in the API documentation, the way it is implemented has become part of the official specification of the Java API.
In general, API documentation should just describe what the purpose is of the method, and not exactly how it is implemented.
Upvotes: 2
Reputation: 140329
It means that the value returned by hashCode()
is prescribed in the Javadoc.
e.g.
Returns a hash code for this string. The hash code for a String object is computed as
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
[Returns] a hash code value for this object, equal to the primitive int value represented by this Integer object.
Returns a hash code value for this object. The result is the exclusive OR of the two halves of the primitive long value returned by the getTime() method. That is, the hash code is the value of the expression:
(int)(this.getTime()^(this.getTime() >>> 32))
Upvotes: 2