Java Arrays.hashCode() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.util.Arrays

Definition

In Java, Arrays.hashCode(a) returns an int calculated from the elements of a. Two arrays for which Arrays.equals returns true always get the same hash code.

The array's own a.hashCode() is based on the object's identity, so two arrays with the same numbers usually get different values. Arrays.hashCode is the content-based version you need when an array is part of a hashCode() method or a hash-based key.

A static method of java.util.Arrays (Java 5+): call it on the class, as in Arrays.hashCode(...); there is no Arrays object to create.

Syntax

Pass the array; the result depends only on its elements and their order.

Java
int[] a = {1, 2, 3};
int h = Arrays.hashCode(a);             // 30817
Arrays.hashCode(new int[] {1, 2, 3});   // 30817 again
Arrays.hashCode(new int[0]);            // 1
Arrays.hashCode((int[]) null);          // 0
CallWhat it does
Arrays.hashCode(int[] a)Hash of the values; same overload for every primitive type.
Arrays.hashCode(Object[] a)Uses each element's own hashCode(); null elements count as 0.
Objects.hash(x, y, z)Shortcut that wraps the values in an array and calls Arrays.hashCode.
return Arrays.hashCode(values);Typical line inside a class's hashCode() method.

The algorithm starts at 1 and, for each element, computes result = 31 * result + elementHash. For int the element hash is the value itself; for other types it is the matching wrapper's hash (for example Long.hashCode or Boolean.hashCode). For int[] the result equals the hash code of a List<Integer> with the same elements.

Library declarations
Java
// java.util.Arrays (since Java 5)
public static int hashCode(int[] a)
public static int hashCode(long[] a)
public static int hashCode(double[] a)
public static int hashCode(boolean[] a)
public static int hashCode(Object[] a)
// also short[], char[], byte[], float[]

Parameters

ParameterDescription
aThe array whose contents are hashed. May be null.

Return Value

Return ValueDescription
intA content-based hash code; 1 for an empty array and 0 for null.

Compare with Similar Methods

Hash and equality helpers:

MethodHow it differs
Arrays.deepHashCode(a)For nested arrays such as int[][].
Arrays.equals(a, b)The equality check that matches hashCode.
a.hashCode()Identity-based; ignores the contents.
Objects.hash(...)Convenience wrapper around Arrays.hashCode(Object[]).

Java Examples

Example 1 — Same Contents, Same Hash Code

Two arrays with the same numbers and one with the numbers reversed.

Java
import java.util.Arrays;

public class ArraysHashCodeDemo {
    public static void main(String[] args) {
        int[] a = {1, 2, 3};
        int[] b = {1, 2, 3};
        int[] c = {3, 2, 1};

        System.out.println("a: " + Arrays.hashCode(a));
        System.out.println("b: " + Arrays.hashCode(b));
        System.out.println("c: " + Arrays.hashCode(c));
    }
}

How It Works

  1. Content, not identity. a and b are two separate objects, but they hold the same values, so both get 30817.
  2. Working it out. Starting from 1: 31 * 1 + 1 = 32, then 31 * 32 + 2 = 994, then 31 * 994 + 3 = 30817.
  3. Order changes the result. For c: 31 + 3 = 34, 31 * 34 + 2 = 1056, 31 * 1056 + 1 = 32737.
  4. Stable across runs. For int[] the result depends only on the numbers, so these exact values appear every time.

Example 2 — Recompute the Hash by Hand

Implement the 31 * h + x loop yourself and compare it with the library, plus the empty and null cases.

Java
import java.util.Arrays;

public class ArraysHashCodeFormula {
    public static void main(String[] args) {
        int[] data = {5, 10};
        int h = 1;
        for (int x : data) {
            h = 31 * h + x;
        }
        System.out.println("by hand:  " + h);
        System.out.println("library:  " + Arrays.hashCode(data));
        System.out.println("empty:    " + Arrays.hashCode(new int[0]));
        System.out.println("null:     " + Arrays.hashCode((int[]) null));
    }
}

How It Works

  1. Start at 1. The running value h begins at 1, exactly like the library implementation.
  2. Two steps. 31 * 1 + 5 = 36, then 31 * 36 + 10 = 1126, which matches Arrays.hashCode(data).
  3. No elements. With an empty array the loop never runs, so the result is the starting value, 1.
  4. null is 0. A null array returns 0 instead of throwing; the cast picks the int[] overload.

Example 3 — Object Arrays and Objects.hash

Hash a String[], one with a null element, and see that Objects.hash gives the same number as Arrays.hashCode.

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

public class ArraysHashCodeObjects {
    public static void main(String[] args) {
        String[] tags = {"a", "b"};
        String[] partial = {"a", null};
        System.out.println("tags:    " + Arrays.hashCode(tags));
        System.out.println("partial: " + Arrays.hashCode(partial));

        Integer[] point = {3, 4};
        System.out.println("point:   " + Arrays.hashCode(point));
        System.out.println("Objects.hash(3, 4): " + Objects.hash(3, 4));
    }
}

How It Works

  1. String hashes. A one-letter string hashes to its character code: "a" is 97 and "b" is 98. So 31 + 97 = 128, then 31 * 128 + 98 = 4066.
  2. null counts as 0. For partial, the second step is 31 * 128 + 0 = 3968.
  3. Integer hashes are the values. For {3, 4}: 31 + 3 = 34, then 31 * 34 + 4 = 1058.
  4. Objects.hash is a wrapper. Objects.hash(3, 4) boxes the arguments into an Object[] and calls Arrays.hashCode, so it also returns 1058.

Frequently Asked Questions

An array's own hashCode() comes from Object and depends on identity. Use Arrays.hashCode(array) for a hash based on the contents.
It starts at 1 and for each element computes 31 * result + the element's hash. For an int element the hash is the value itself.
0 for a null array and 1 for an empty array.
Only shallowly: each row contributes its identity hash. Use Arrays.deepHashCode for nested arrays.
Objects.hash(values...) simply calls Arrays.hashCode on its varargs array, so both return the same number for the same values.

Next: Arrays.mismatch()

Find the first index where two arrays differ, or -1 (Java 9).

mismatch() 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