问题
Why does this code give a negative hashcode?
import java.util.HashSet;
import java.util.Set;
public class Ab {
/**
* @param args
*/
public static void main(String[] args) {
String s1="Operations on a dynamic set can be grouped into two categories queries, which simply return information about the set, and modifying operations, which change the set. Here is a list of typical operations. Any specific application will usually require only a few of these to be implemented Some dynamic sets presuppose that the keys are drawn from a totally ordere, such as the real numbers, or the set of all words under the usual alphabetic ordering. A total ordering allows us to define the minimum element of the set, for example, or to speak of the next element larger than a given element in a set.Operations on dynamic sets Operations on a dynamic set can be grouped into two categories: q";
System.out.println(s1.hashCode());
String s2="abc";
System.out.println(s2.hashCode());
}
}
回答1:
The String class overrides hashCode()
to produce deterministic results. The result has nothing to do with memory addresses. The String.hashCode() Javadoc shows the formula used to calculate it:
The hash code for a String object is computed as s[0]*31^(n-1) + s1*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.)
Note that for even relatively short strings the value can get too big for an integer. During the calculations, whenever an overflow occurs only the least significant 32 bits are kept. At the end of the calculation, if the most significant bit of the resulting integer is set then the integer is negative, if not then positive.
回答2:
I had googled many times and getting confused with this thing that hashcode is hex code of memory representation that i know memory address is always positive number then it means hashcode is just a code of content of object then where jvm stores ??
That's not entirely correct. It is the case that the default implementation of Object.hashCode
returns a representation of the memory address of the receiving object. However, many classes override the default implementation, and String
is one of them. The override of Object.hashCode
for String
is not an identity hash code, but is a value hash code. Thus, it is not a representation of the memory address of the receiving object, but is instead a representation of the value of the String
.
Of course, even the conversion of a memory address to a hash code (for the default implementation of Object.hashCode
) could produce a negative hash code, it is clearly the case that an overriding definition of Object.hashCode
could produce a negative hash code.
In fact, this trivial hash code is terrible, but 100% legal:
@Override
public int hashCode() { return -42; }
That is, it is consistent with the "contract" of Object.hashCode
.
回答3:
simply saying, hashcode is a number that is returned by hash function used to map variable length data to fixed lenght.
You can find good information about hashcodes here http://www.thejavageek.com/2013/06/27/what-are-hashcodes/
and for hashcode in java programming see following link
http://www.thejavageek.com/2013/06/28/significance-of-equals-and-hashcode/
来源:https://stackoverflow.com/questions/17481069/hashcode-is-memory-address-or-integer-number-of-content-of-memory-address