How to Compare Two Arrays for Equality in Java

Comparing two arrays is one of the most common tasks in Java, but it is also one of the most misunderstood by beginners. The biggest source of confusion is the == operator, which checks whether two array references point to the same object rather than whether their contents are identical. This guide explains why == doesn't work for content comparison and demonstrates the correct approaches for comparing one-dimensional and multidimensional arrays, as well as comparing arrays regardless of element order.


Problem Statement

Given two arrays:

 
{1, 2, 3}

{1, 2, 3}
 

Determine whether they are equal.

Advertisement

Before solving the problem, it's important to define what equal means:

  • Do both arrays contain the same elements in the same order?
  • Or do they simply contain the same elements, regardless of order?

The appropriate solution depends on that definition.


Why == Doesn't Work for Arrays

One of the most common mistakes is using the == operator.

 
int[] a = {1, 2, 3};
int[] b = {1, 2, 3};

System.out.println(a == b);
 

Output:

 
false
 

Although both arrays contain identical values, == compares references, not contents.

Since arrays are objects in Java, a and b refer to two different objects in memory, so their references are different.


Method 1: Arrays.equals() (Same Order Required)

 
import java.util.Arrays;

int[] a = {1, 2, 3};
int[] b = {1, 2, 3};

System.out.println(Arrays.equals(a, b));
 

Output:

 
true
 

Arrays.equals() compares:

  • The lengths of both arrays.
  • Each corresponding element in order.

This is the recommended approach for comparing one-dimensional arrays.


Method 2: Arrays.deepEquals() (For 2D Arrays)

 
import java.util.Arrays;

int[][] matrix1 = {
    {1, 2},
    {3, 4}
};

int[][] matrix2 = {
    {1, 2},
    {3, 4}
};

System.out.println(Arrays.equals(matrix1, matrix2));

System.out.println(Arrays.deepEquals(matrix1, matrix2));
 

Output:

 
false

true
 

Arrays.equals() compares only the references of the inner arrays.

Arrays.deepEquals() recursively compares every nested array, making it the correct choice for multidimensional arrays.


Method 3: Manual Loop Comparison

 
public static boolean arraysEqual(int[] a, int[] b) {

    if (a.length != b.length) {
        return false;
    }

    for (int i = 0; i < a.length; i++) {

        if (a[i] != b[i]) {
            return false;
        }
    }

    return true;
}
 

This method performs exactly the same logical steps as Arrays.equals().

Although the built-in method should generally be preferred, implementing it manually is useful for understanding the underlying logic and for coding interviews.


Method 4: Checking Equality Regardless of Order

Sometimes two arrays should be considered equal even if their elements appear in different orders.

 
import java.util.Arrays;

int[] a = {3, 1, 2};
int[] b = {1, 2, 3};

int[] sortedA = a.clone();
int[] sortedB = b.clone();

Arrays.sort(sortedA);
Arrays.sort(sortedB);

System.out.println(Arrays.equals(sortedA, sortedB));
 

Output:

 
true
 

Cloning prevents the original arrays from being modified.

After sorting, the comparison becomes order-sensitive again because both arrays are now in the same order.


Step-by-Step Explanation

Length Check

Before comparing elements, both arrays must have the same length.

If their lengths differ, they cannot be equal.

Element-by-Element Comparison

If the lengths match, every corresponding pair of elements is compared.

The comparison stops immediately if any mismatch is found.

Order-Independent Comparison

If order should not matter, clone both arrays and sort them.

Once sorted, compare the resulting arrays using Arrays.equals().


Internal Working (Memory View)

Suppose:

 
a → Memory Address A → [1, 2, 3]

b → Memory Address B → [1, 2, 3]
 

Using ==:

 
Memory Address A == Memory Address B

Result: false
 

Using Arrays.equals():

 
Compare

1 == 1

2 == 2

3 == 3

Result: true
 

The key difference is that == compares references, whereas Arrays.equals() compares contents.


Real-Life Analogy

Imagine two identical notebooks.

Although they contain exactly the same notes in the same order, they are still two different physical notebooks.

Asking whether they are the same notebook is like using ==.

Asking whether they contain the same information is like using Arrays.equals().


Best Practices

  • Use Arrays.equals() for one-dimensional array comparisons.
  • Use Arrays.deepEquals() for multidimensional arrays.
  • Clone arrays before sorting when performing order-independent comparisons.
  • Clearly define whether array equality should be order-sensitive or order-independent.

Common Mistakes

  1. Using == instead of Arrays.equals() for content comparison.
  2. Using Arrays.equals() on multidimensional arrays.
  3. Sorting the original arrays without cloning them first.
  4. Forgetting to compare array lengths before performing a manual comparison.

Expert Tips

  • Arrays.equals() already checks array lengths internally.
  • For arrays containing custom objects, Arrays.equals() relies on each object's equals() method, so custom classes should override equals() appropriately.
  • Objects.deepEquals() can also compare nested arrays and other objects, making it useful in generic utility methods.

Comparison Table

Method Compares Order-Sensitive Works for 2D Arrays
== References N/A N/A
Arrays.equals() 1D Array Contents Yes ❌ No
Arrays.deepEquals() Recursive Contents Yes ✅ Yes
Sort Then Compare Contents No Possible with adaptation

Frequently Asked Questions

Why does == return false for two identical arrays?

Because == compares object references rather than the values stored inside the arrays.

What is the correct way to compare two arrays?

Use Arrays.equals() for one-dimensional arrays and Arrays.deepEquals() for multidimensional arrays.

Does Arrays.equals() work for 2D arrays?

No. It compares only the references of the inner arrays.

Use Arrays.deepEquals() instead.

How can I compare two arrays regardless of element order?

Clone both arrays, sort the copies, and compare them using Arrays.equals().

Is comparing two arrays for equality the same as checking if they are the same?

Yes. In most Java problems, both phrases refer to comparing the array contents rather than their memory references.

Does Arrays.equals() work with arrays of custom objects?

Yes, provided the custom objects correctly override the equals() method.

What happens if the arrays have different lengths?

Arrays.equals() immediately returns false because arrays of different lengths cannot be equal.

Is there a built-in method for comparing arrays regardless of order?

No. The common solution is to sort copies of both arrays before comparing them, or use a frequency-counting approach when sorting is not appropriate.