Deep Analysis of Array Comparison in Java: equals vs Arrays.equals

Nov 30, 2025 · Programming · 11 views · 7.8

Keywords: Java array comparison | equals method | Arrays utility class | content comparison | reference comparison | multi-dimensional arrays

Abstract: This article provides an in-depth exploration of two array comparison methods in Java: array.equals() and Arrays.equals(). Through detailed analysis of Object class's default equals implementation and Arrays utility class's specialized implementation, it reveals the fundamental differences in comparison semantics. The article demonstrates practical effects of reference comparison versus content comparison with code examples, extends to multi-dimensional array scenarios, and introduces the deep comparison mechanism of Arrays.deepEquals(). Finally, it summarizes best practices to help developers avoid common array comparison pitfalls.

Introduction

In Java programming, array comparison is a fundamental but often confusing operation. Many developers encounter unexpected results when using the equals method to compare arrays. This article analyzes the essential differences between array.equals() and Arrays.equals() from the perspective of Java language design.

Default Behavior of Object.equals

In Java, arrays are objects that inherit from the Object class. Since array classes do not override the Object.equals() method, calling equals on an array actually invokes the default implementation from Object.

Object[] array1 = new Object[]{1, 2, 3};
Object[] array2 = new Object[]{1, 2, 3};

// Reference comparison, returns false
boolean result1 = array1.equals(array2);

// Equivalent to reference comparison, also returns false
boolean result2 = array1 == array2;

This comparison method checks whether two array references point to the same object in memory, rather than comparing whether the array contents are identical. For most array comparison scenarios, this semantics does not meet developers' expectations.

Content Comparison with Arrays.equals

The java.util.Arrays utility class provides specialized equals methods for array content comparison. These methods compare corresponding elements of two arrays one by one.

Object[] array1 = new Object[]{1, 2, 3};
Object[] array2 = new Object[]{1, 2, 3};

// Content comparison, returns true
boolean result = Arrays.equals(array1, array2);

The implementation logic of Arrays.equals() includes: first checking if two array references are identical, returning true immediately if so; then checking if array lengths are equal, returning false if lengths differ; finally comparing array elements one by one.

Element-Level Equality Determination

For object arrays, Arrays.equals() uses the elements' equals methods for comparison. This means if array elements do not properly implement the equals method, comparison results may still not meet expectations.

class CustomObject {
    private int value;
    
    public CustomObject(int value) {
        this.value = value;
    }
    
    // If equals method is not overridden, Object.equals is used by default
}

CustomObject[] arr1 = {new CustomObject(1)};
CustomObject[] arr2 = {new CustomObject(1)};

// If CustomObject doesn't override equals, result is false
boolean result = Arrays.equals(arr1, arr2);

Multi-dimensional Array Comparison Challenges

For multi-dimensional arrays (such as 2D arrays), the situation becomes more complex. Since 2D arrays are essentially "arrays of arrays" and array's equals method itself has issues, Arrays.equals() cannot work correctly in multi-dimensional array scenarios.

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

// For 2D arrays, Arrays.equals() may not work correctly
boolean result = Arrays.equals(array2D1, array2D2);

Deep Comparison Solution

To address comparison problems with multi-dimensional arrays, the Arrays class provides the deepEquals() method. This method can recursively compare all levels of arrays, including nested arrays.

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

// Use deepEquals for deep comparison
boolean result = Arrays.deepEquals(array2D1, array2D2);

Arrays.deepEquals() recursively calls itself to handle nested arrays until it encounters primitive types or objects that properly implement the equals method.

Best Practices and Recommendations

Based on the above analysis, the following best practices can be summarized:

  1. Avoid using array's equals method: Since arrays do not override Object.equals(), completely avoid using array.equals() for array comparison.
  2. Use Arrays.equals for 1D arrays: For one-dimensional arrays, use Arrays.equals() for content comparison.
  3. Use Arrays.deepEquals for multi-dimensional arrays: For multi-dimensional arrays or object arrays containing array elements, use Arrays.deepEquals().
  4. Ensure elements properly implement equals: If arrays contain custom objects, ensure these objects properly override equals and hashCode methods.
  5. Consider using collection classes: For complex comparison requirements, consider converting arrays to List and using collection comparison methods.

Performance Considerations

From a performance perspective, Arrays.equals() has a time complexity of O(n), where n is the array length. For large arrays, this linear time complexity operation is relatively efficient. Arrays.deepEquals(), due to involving recursive calls, may have a worst-case time complexity of O(m×n), where m is the nesting depth.

Conclusion

Understanding the difference between array.equals() and Arrays.equals() is crucial for writing correct Java programs. The former performs reference comparison, while the latter performs content comparison. In multi-dimensional array scenarios, Arrays.deepEquals() must be used. By following the best practices introduced in this article, developers can avoid common array comparison pitfalls and write more robust and maintainable code.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.