![]() |
VOOZH | about |
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.
arr1 equals to arr2: true arr1 equals to arr3: false
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:
int[] array to be compared.int[] array to be compared.Return Type:
Note:
If both arrays are null, the method returns true.
If one array is null and the other is not, it returns false.
we can compare arrays of objects, such as Student, by overriding the equals() method to define equality based on object attributes.
arr1 equals to arr2: true arr1 equals to arr3: false
Explanation:
is arr1 equals to arr2: false is arr1 deepequals to arr2: true
Explanation:
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().
| Feature | Arrays.equals() | Arrays.deepEquals() |
|---|---|---|
| Works for | 1-D arrays only | Multidimensional arrays |
| Comparison | Shallow, element by element | Deep, recursive comparison |
| Suitable for | Primitives & Object arrays | Nested arrays, 2D+ arrays |