Hashcode of an int

Ed Lee picture Ed Lee · Aug 9, 2012 · Viewed 53.2k times · Source

What is the hashcode of a primitive type, such as int?

for example, let's say num was an interger.

int hasCode = 0;

if (num != 0) {
  hasCode = hasCode + num.hashCode();
}

Answer

Konrad Reiche picture Konrad Reiche · Aug 9, 2012

Taken from the Integer.class source code:

/**
 * Returns a hash code for this {@code Integer}.
 *
 * @return  a hash code value for this object, equal to the
 *          primitive {@code int} value represented by this
 *          {@code Integer} object.
 */
public int hashCode() {
    return value;
}

Where value is the value of the integer.