Java Integer hashCode() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Integer

Definition

In Java, hashCode() returns an int that hash-based collections use to place an object in a bucket. For an Integer, the hash code is simply the number itself, so Integer.valueOf(42).hashCode() is 42 and -7 gives -7.

Since Java 8 there is also a static form, Integer.hashCode(int), which gives the same answer without creating an object. It is handy when you build the hash code of your own class from several int fields.

Available both as an instance method and as a static method of java.lang.Integer. Integer lives in java.lang, so no import is needed.

Syntax

Call the instance form on an Integer, or the static form with an int.

Java
Integer boxed = 42;
int a = boxed.hashCode();           // 42
int b = Integer.hashCode(42);       // 42 (static, Java 8+)
int c = Integer.hashCode(-7);       // -7
CallWhat it does
boxed.hashCode()The hash code of an Integer object: its value.
Integer.hashCode(value)The same result for a primitive int. The static form needs Java 8.
Objects.hash(a, b)Combines several values into one hash code.
Arrays.hashCode(int[] values)The hash code of a whole int array.

The instance method has existed since Java 1.0, and Integer.hashCode(int) was added in Java 8. Equal Integer objects always return equal hash codes.

Library declarations
Java
// java.lang.Integer
public int hashCode()                    // instance
public static int hashCode(int value)    // static, Java 8 and later

Parameters

ParameterDescription
value (int)Only for the static form: the number to hash. The instance form takes no parameter.

Return Value

Return ValueDescription
intThe hash code, which equals the numeric value of the Integer.

Compare with Similar Methods

Related methods and ideas:

MethodHow it differs
equals(o)Must agree with hashCode(): equal objects need equal hash codes.
String.equals()Equal strings must also give equal hash codes, just like equal integers.
Character.hashCode()The character code of the char.
HashMapUses hashCode() of its keys to find entries quickly.

Java Examples

Example 1 — Print Integer Hash Codes

Show that the hash code of an Integer is the number itself, using both forms.

Java
public class IntegerHashCodeDemo {
    public static void main(String[] args) {
        Integer boxed = 42;
        System.out.println("instance 42:    " + boxed.hashCode());
        System.out.println("static 42:      " + Integer.hashCode(42));
        System.out.println("static -7:      " + Integer.hashCode(-7));
        System.out.println("static 0:       " + Integer.hashCode(0));
        System.out.println("MIN_VALUE:      " + Integer.hashCode(Integer.MIN_VALUE));
    }
}

How It Works

  1. Instance and static agree. Both forms return 42 for the number 42.
  2. Sign is kept. The hash code of -7 is -7, because the method returns the value unchanged.
  3. Zero and the extremes. 0 hashes to 0, and MIN_VALUE hashes to itself, -2147483648.
  4. No calculation. The method does not mix bits; for Integer, the value already works as a hash code.

Example 2 — Build a Hash Code for a Point Class

Combine two int fields into a hash code with Integer.hashCode, and compare with Objects.hash.

Java
import java.util.Arrays;
import java.util.Objects;

public class IntegerHashCodePoint {
    static class Point {
        final int x;
        final int y;

        Point(int x, int y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public boolean equals(Object o) {
            if (!(o instanceof Point)) {
                return false;
            }
            Point other = (Point) o;
            return x == other.x && y == other.y;
        }

        @Override
        public int hashCode() {
            int result = 1;
            result = 31 * result + Integer.hashCode(x);
            result = 31 * result + Integer.hashCode(y);
            return result;
        }
    }

    public static void main(String[] args) {
        Point p = new Point(3, 4);
        System.out.println("Point(3, 4):       " + p.hashCode());
        System.out.println("Objects.hash:      " + Objects.hash(3, 4));
        System.out.println("Arrays.hashCode:   " + Arrays.hashCode(new int[] {3, 4}));
        System.out.println("Point(4, 3):       " + new Point(4, 3).hashCode());
    }
}

How It Works

  1. Start at 1, multiply by 31. This is the classic recipe: take the running result, multiply by 31, then add the hash code of the next field.
  2. The arithmetic. After x: 31 × 1 + 3 = 34. After y: 31 × 34 + 4 = 1058.
  3. Library helpers agree. Objects.hash(3, 4) and Arrays.hashCode use the same recipe, so they also print 1058.
  4. Order matters. Point(4, 3) gives 31 × 35 + 3 = 1088, a different value from Point(3, 4).

Example 3 — See Hash Codes Keep Duplicates Out of a Set

Add equal Integer objects to a HashSet and see that they collapse into one entry.

Java
import java.util.HashSet;
import java.util.Set;

public class IntegerHashCodeSet {
    public static void main(String[] args) {
        Integer first = Integer.valueOf(1000);
        Integer second = Integer.valueOf(1000);

        System.out.println("same object:   " + (first == second));
        System.out.println("equals:        " + first.equals(second));
        System.out.println("same hash:     " + (first.hashCode() == second.hashCode()));

        Set<Integer> numbers = new HashSet<>();
        numbers.add(first);
        numbers.add(second);
        System.out.println("set size:      " + numbers.size());
    }
}

How It Works

  1. Two separate objects. 1000 is outside the -128 to 127 cache, so each valueOf call builds a fresh object and the two variables point to different objects.
  2. Equal and same hash. Both hold 1000, so equals is true and the hash codes are both 1000.
  3. The set checks both. It uses hashCode to find the bucket and equals to confirm a match.
  4. One entry. The second add finds an equal element and changes nothing, so the size is 1.

Frequently Asked Questions

It is the number itself. Integer.valueOf(42).hashCode() returns 42, and -7 returns -7.
The instance method works on an Integer object. The static method, added in Java 8, takes a primitive int and returns the same value without creating an object.
No. For Integer the hash code is the value, so different values have different hash codes. Other classes can have collisions.
Hash-based collections use hashCode to find a bucket and equals to confirm. If equal objects had different hash codes, lookups would fail.
Use Objects.hash(a, b, c), or the recipe result = 31 * result + Integer.hashCode(field) for each field.

Next: highestOneBit()

Keep only the leftmost 1 bit of an int.

highestOneBit() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful