Java Arrays.equals() Method

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

Definition

In Java, Arrays.equals(a, b) returns true when both arrays have the same length and every pair of elements at the same index is equal. Order matters: {1, 2} and {2, 1} are not equal.

You need it because a == b and a.equals(b) only check whether a and b are the same array object. Two separate arrays with identical contents are "different" to both of those, but equal to Arrays.equals.

A static method of java.util.Arrays (Java 1.2+): call it on the class, as in Arrays.equals(...); there is no Arrays object to create. Java 9 added overloads that compare a range of each array and that compare object elements with a Comparator.

Syntax

Pass two arrays of the same type.

Java
int[] a = {1, 2, 3};
int[] b = {1, 2, 3};
boolean same = Arrays.equals(a, b);      // true
boolean ref = (a == b);                  // false: two objects
Arrays.equals(a, new int[] {3, 2, 1});   // false: order matters
CallWhat it does
Arrays.equals(a, b)Same length and equal elements, index by index.
Arrays.equals(null, null)true (with typed null variables).
Arrays.equals(a, 0, 2, b, 1, 3)Java 9+: compares a[0..2) with b[1..3).
Arrays.equals(x, y, comparator)Java 9+: object elements are equal when the comparator returns 0.

The two-array form exists for every primitive type and for Object[]. Object elements are compared with Objects.equals(e1, e2), so two null elements count as equal.

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

// Java 9+: range form equals(a, aFrom, aTo, b, bFrom, bTo), and
public static <T> boolean equals(T[] a, T[] a2, Comparator<? super T> cmp)

Parameters

ParameterDescription
a, a2The two arrays to compare. Either may be null.
range indexes (Java 9+)From (inclusive) and to (exclusive) indexes for each array.
cmp (Java 9+)A comparator that decides element equality for object arrays.

Return Value

Return ValueDescription
booleantrue if both are null, or both have the same length and equal elements; otherwise false.

Compare with Similar Methods

Related comparison methods:

MethodHow it differs
Arrays.deepEquals(a, b)For nested arrays such as int[][].
Arrays.mismatch(a, b)Tells you where the arrays first differ (Java 9).
Arrays.compare(a, b)Tells you which array comes first in order (Java 9).
Arrays.hashCode(a)Content-based hash code that agrees with Arrays.equals.

Java Examples

Example 1 — ==, equals(), and Arrays.equals Side by Side

Three arrays: two with the same numbers and one with the same numbers in a different order.

Java
import java.util.Arrays;

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

        System.out.println("a == b:        " + (a == b));
        System.out.println("a.equals(b):   " + a.equals(b));
        System.out.println("Arrays a, b:   " + Arrays.equals(a, b));
        System.out.println("Arrays a, c:   " + Arrays.equals(a, c));
    }
}

How It Works

  1. Two objects. a and b are created separately, so a == b compares two different references and gives false.
  2. Inherited equals. Arrays do not override equals, so a.equals(b) behaves exactly like ==.
  3. Content check. Arrays.equals(a, b) compares 3 with 3, 1 with 1, and 2 with 2, and returns true.
  4. Order matters. c holds the same numbers sorted, but index 0 has 1 instead of 3, so the result is false.

Example 2 — Same Members in Any Order, and null Arrays

Compare two teams regardless of order by sorting copies, then see how null is treated.

Java
import java.util.Arrays;

public class ArraysEqualsUnordered {
    public static void main(String[] args) {
        String[] team1 = {"Kim", "Lee", "Ana"};
        String[] team2 = {"Ana", "Kim", "Lee"};
        System.out.println("same order? " + Arrays.equals(team1, team2));

        String[] s1 = team1.clone();
        String[] s2 = team2.clone();
        Arrays.sort(s1);
        Arrays.sort(s2);
        System.out.println("same members? " + Arrays.equals(s1, s2));

        String[] none1 = null;
        String[] none2 = null;
        System.out.println("both null? " + Arrays.equals(none1, none2));
        System.out.println("one null? " + Arrays.equals(team1, none2));
    }
}

How It Works

  1. Position by position. team1 starts with Kim and team2 with Ana, so the direct comparison fails at index 0.
  2. Sort copies, not originals. clone() makes copies so that sorting does not change the teams; both sorted copies read Ana, Kim, Lee.
  3. Two nulls are equal. Arrays.equals first checks whether both references are the same, and two null references are.
  4. One null is not. A real array and null give false instead of a NullPointerException.

Example 3 — How Doubles Are Compared

Arrays.equals treats NaN and negative zero differently from ==.

Java
import java.util.Arrays;

public class ArraysEqualsDoubles {
    public static void main(String[] args) {
        double[] x = {Double.NaN, 0.0};
        double[] y = {Double.NaN, 0.0};
        double[] z = {Double.NaN, -0.0};

        System.out.println("NaN == NaN:      " + (x[0] == y[0]));
        System.out.println("equals(x, y):    " + Arrays.equals(x, y));
        System.out.println("0.0 == -0.0:     " + (x[1] == z[1]));
        System.out.println("equals(x, z):    " + Arrays.equals(x, z));
    }
}

How It Works

  1. NaN is never == itself. By the floating-point rules, NaN == NaN is false.
  2. Arrays.equals says yes. It compares doubles the way Double.equals does, where NaN equals NaN, so x and y are equal.
  3. Two zeros. 0.0 == -0.0 is true with the == operator.
  4. Different bit patterns. Double.equals tells 0.0 and -0.0 apart, so Arrays.equals(x, z) is false.

Frequently Asked Questions

Arrays inherit equals() from Object, which only checks whether both references point to the same array. Use Arrays.equals(array1, array2) to compare contents.
Not as you might expect. It compares the rows as objects, so two separate but identical 2D arrays are usually not equal. Use Arrays.deepEquals.
No. Elements must match index by index. Sort copies of both arrays first if order should not matter.
true if both are null, false if only one is null. It does not throw.
Yes, from Java 9: Arrays.equals(a, aFrom, aTo, b, bFrom, bTo) compares two ranges.

Next: Arrays.fill()

Put the same value into every slot, or into a range.

fill() 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.

7 people found this page helpful