Hashcode of an int
Asked Answered
T

4

40

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();
}
Tetracycline answered 9/8, 2012 at 19:43 Comment(5)
You can't call methods on primitives. Although it can be autoboxed, and then as an Integer (or similar) you'll get Integer.hashCode.Crowded
In that case, we could be more helpful and add what the hashCodes would be for the wrapper classes.Educt
The hashcode of an integer is the integer itself.Marshamarshal
@DennisMeng Can you not read the API docs?Crowded
I could, but the point of my comment was that we shouldn't shut the OP down just because of a minor technicality. The point of my comment was that we should probably be giving answers closer to the actual spirit of the problem.Educt
B
50

For the hashCode of an int the most natural choice is to use the int itself. A better question is what to use for the hashCode of a long since it doesn't fit into the int-sized hashcode. Generally, your best source for that—and all hashCode-related questions—would be Effective Java.

Here's what Effective Java recommends (and the JDK uses) for the long type:

(int) (value ^ (value >>> 32))
Bingle answered 9/8, 2012 at 19:45 Comment(4)
I got curious about the long hash code and have looked it up, it's: (int)(value ^ (value >>> 32));Getraer
@platzhirsch Yes, that's how it's implemented in java.lang.Long, and that's what Effective Java recommends.Bingle
@MarkoTopolnik actually I think this is probably a lot more useful.Mikesell
@veer You mean browsing through the JDK code? There will be no mention of a primitive type's hashcode there. Maybe you mean inspecting hashCode of primitive wrappers---in that case I'd recommend grepcode over the unwieldy download.Bingle
G
48

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.

Getraer answered 9/8, 2012 at 19:48 Comment(1)
Note: It's correct but the question was meant for primitive int and not Integer object holding 'integer' valueSanderson
S
9

No hashCode() method for primitive type int available.

Integer is Wrapper class type and hashcode() returns an int

Sandry answered 9/8, 2012 at 19:44 Comment(0)
D
1

The java.lang.Integer.hashCode() method returns a hash code value for primitive value of int but represented as an Integer object.

/**
 * Returns a hash code value for an Integer,
 * equal to the primitive int value it represents.
 */
public class IntegerDemo {

    public static void main(String[] args){
        Integer i = new Integer("20");
        System.out.println("Value = " + i.hashCode());
    }

}`

Results:

Value = 20

Source Link: http://www.tutorialspoint.com/java/lang/integer_hashcode.htm

Driedup answered 15/1, 2016 at 14:18 Comment(1)
the retval intermediary variable isn't really necessary in this code as it is only being used once.Tamanaha

© 2022 - 2024 — McMap. All rights reserved.