VOOZH about

URL: https://www.geeksforgeeks.org/java/java-util-arrays-equals-java-examples/

⇱ Arrays.equals() in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Arrays.equals() in Java with Examples

Last Updated : 13 Apr, 2026

The Arrays.equals() method comes under the Arrays class in Java. It is used to check whether two arrays, whether single-dimensional or multi-dimensional array are equal or not.


Output
arr1 equals to arr2: true
arr1 equals to arr3: false

Syntax

public static boolean equals(int[] a, int[] a2)

Another overloaded version

public static boolean equals(int[] a, int fromIndex, int toIndex, int[] b, int fromIndex2, int toIndex2)

Parameters:

  • a-> The first int[] array to be compared.
  • a2-> The second int[] array to be compared.

Return Type:

  • boolean -> Returns true if both arrays are equal, otherwise false.

Note:

If both arrays are null, the method returns true.
If one array is null and the other is not, it returns false.

Some Common Examples of Arrays.equals() Method

Examples 1: Comparing Arrays of User-Defined Objects

we can compare arrays of objects, such as Student, by overriding the equals() method to define equality based on object attributes.


Output
arr1 equals to arr2: true
arr1 equals to arr3: false

Explanation:

  • Compares arrays of Student objects using Arrays.equals().
  • The equals() method is overridden to check roll number, name, and address.
  • arr1 and arr2 have the same students-> returns true.
  • arr1 and arr3 differ in the third student -> returns false.
  • Shows how Arrays.equals() works for object arrays when equals() is properly defined.

Example 2: Comparing Multidimensional Arrays


Output
is arr1 equals to arr2: false
is arr1 deepequals to arr2: true

Explanation:

  • arr1 and arr2 are 2D arrays containing the same elements.
  • Arrays.equals() performs a shallow comparison -> returns false.
  • Arrays.deepEquals() performs a deep, recursive comparison -> returns true.
  • Use Arrays.deepEquals() for multidimensional arrays to check equality correctly.

Note: Arrays.equals() can be used with multidimensional arrays, but it performs a shallow comparison (compares references of inner arrays). For deep comparison of contents, use Arrays.deepEquals().

Array.equals() vs. Arrays.deepEquals()

FeatureArrays.equals()Arrays.deepEquals()
Works for1-D arrays onlyMultidimensional arrays
ComparisonShallow, element by elementDeep, recursive comparison
Suitable forPrimitives & Object arraysNested arrays, 2D+ arrays
Comment
Article Tags:
Article Tags: