Hashcode is a memory address or an integer of the contents of a memory address?

Why is this code giving a negative hash code?

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());
    }

}

      

+2


source to share


3 answers


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:

The hash code for the 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 i-th 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 may become too large for an integer. During computation, whenever an overflow occurs, only the least significant 32 bits are stored. At the end of the computation, if the most significant bit of the resulting integer is specified, the integer is negative if not positive.

+3


source


simply put, hashcode is a number that is returned by a hash function used to map variable length data to fixed length.

You can find good information on 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/

0


source


I searched google many times and got confused with this thing that hashcode is a hexadecimal memory representation code which I know the memory address is always positive then that means hashcode is just the code of the object's content then where jvm stores

This is not entirely correct. In this case Object.hashCode

, the default implementation returns the memory address representation of the receiving object. However, many classes override the default implementation and String

is one of them. The override Object.hashCode

for is String

not an identifier hash, but a value hash. So it is not a representation of the memory address of the receiving object, but instead a representation of a value String

.

Of course, even converting a memory address to a hash code (for the default implementation Object.hashCode

) can result in a negative hash code, obviously overriding the definition Object.hashCode

could result in a negative hash code.

Actually, this trivial hashcode is terrible, but 100% legal:

@Override
public int hashCode() { return -42; }

      

That is, it is consistent with the "contract" Object.hashCode

.

0


source







All Articles