Arrays.equals() – Java Array Equals

In Java to check given array are equals or not, we can use the pre-defined method Arrays.equals(). The Arrays.equals() method in Java returns true if the two specified arrays are equal to one another.

Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. In other words, two arrays are equal if they contain the same elements in the same order. Also, two array references are considered equal if both are null.

There are 10 overloaded forms of Arrays.equals() method and they are listed here,

  • public static boolean equals(byte[] a, byte[] b)
  • public static boolean equals(short[] a, short[] b)
  • public static boolean equals(int[] a, int[] b)
  • public static boolean equals(long[] a, long[] b)
  • public static boolean equals(float[] a, float[] b)
  • public static boolean equals(double[] a, double[] b)
  • public static boolean equals(char[] a, char[] b)
  • public static boolean equals(boolean[] a, boolean[] b)
  • public static boolean equals(Object[] a, Object[] b)

In these methods the parameter,

  • a:- array1 which to be tested for equality.
  • b:- array2 that to be tested for equality.

Return value:- It return true if both arrays are equal else it return false.

Java Arrays.equals() Example

Let us demonstrate some examples of the Arrays.equals() method in Java. Note that it is comparing two arrays based on its content, not based on its reference.

import java.util.Arrays;

public class CompareArray {
  
  // main method
  public static void main(String[] args) {

    // declare and initialize arrays
    int arr1[] = {10,20,30,40,50};
    int arr2[] = arr1;
    int arr3[] = {10,20,30,40,50};
    int arr4[] = {15,25,35,45,55};
    
    // compare arrays using Arrays.equals() method
    // compare arr1 and arr2
    if(Arrays.equals(arr1, arr2))
      System.out.println("arr1 & arr2 are same");
    else 
      System.out.println("arr1 & arr2 are not same");
    
    // compare arr1 and arr3
    if(Arrays.equals(arr1, arr3))
      System.out.println("arr1 & arr3 are same");
    else 
      System.out.println("arr1 & arr3 are not same");
    
    // compare arr1 and arr4
    if(Arrays.equals(arr1, arr4))
      System.out.println("arr1 & arr4 are same");
    else 
      System.out.println("arr1 & arr4 are not same");
  }
}

Output:-

arr1 & arr2 are same
arr1 & arr3 are same
arr1 & arr4 are not same

Let us see more Java examples with another array and equals().

// declare and initialize arrays
char arr1[] = {'K','N','O','W'};
char arr2[] = {'K','N','O','W'};
char arr3[] = {'k','n','o','w'};
char arr4[] = {'P','R','O','G','R','A','M'};

// compare arrays using Arrays.equals() method
System.out.println(Arrays.equals(arr1, arr2));
System.out.println(Arrays.equals(arr1, arr3));
System.out.println(Arrays.equals(arr1, arr4))

true
false
false

Additional forms of equals()

Addition to the previously given forms, it also contains more forms to check equality of two arrays over the specified ranges. These forms were introduced in Java 9 version.

  • public static boolean equals(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex)
  • public static boolean equals(long[] a, int aFromIndex, int aToIndex, long[] b, int bFromIndex, int bToIndex)
  • public static boolean equals(byte[] a, int aFromIndex, int aToIndex, byte[] b, int bFromIndex, int bToIndex)
  • public static boolean equals(short[] a, int aFromIndex, int aToIndex, short[] b, int bFromIndex, int bToIndex)
  • public static boolean equals(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex)
  • public static boolean equals(long[] a, int aFromIndex, int aToIndex, long[] b, int bFromIndex, int bToIndex)
  • public static boolean equals(float[] a, int aFromIndex, int aToIndex, float[] b, int bFromIndex, int bToIndex)
  • public static boolean equals(double[] a, int aFromIndex, int aToIndex, double[] b, int bFromIndex, int bToIndex)
  • public static boolean equals(char[] a, int aFromIndex, int aToIndex, char[] b, int bFromIndex, int bToIndex)
  • public static boolean equals(boolean[] a, int aFromIndex, int aToIndex, boolean[] b, int bFromIndex, int bToIndex)
  • public static boolean equals(Object[] a, int aFromIndex, int aToIndex, Object[] b, int bFromIndex, int bToIndex)

In these forms, the parameters are,

  • a:- the first array to be tested for equality
  • aFromIndex:- the index (inclusive) of the first element in the first array to be tested
  • aToIndex:- the index (exclusive) of the last element in the first array to be tested
  • b:- the second array to be tested for equality
  • bFromIndex:- the index (inclusive) of the first element in the second array to be tested
  • bToIndex:- the index (exclusive) of the last element in the second array to be tested

Let us see an example for this form,

// Declare and initialize arrays
int arr1[] = {10,20,30,40,50};
int arr2[] = {10,20,33,44,55};
int arr3[] = {11,22,30,40,50};
int arr4[] = {15,25,10,20,30};

// Check Equality of arrays
// Check 1st and 2nd elements of arr1 & arr2 
System.out.println(Arrays.equals(arr1, 0, 2, arr2, 0, 2));

// Check 3rd, 4th, and 5th elements of arr1 & arr3
System.out.println(Arrays.equals(
                      arr1, 2, arr1.length, arr3, 2, arr3.length));

// Check 1st and 2nd elements of arr1 & arr4
System.out.println(Arrays.equals(arr1, 0, 2, arr4, 0, 2));

// Check 1st, 2nd, 3rd element of arr1 and 
// 3rd, 4th, 5th elements of arr4
System.out.println(Arrays.equals(arr1, 0, 3, arr4, 2, arr4.length));

Output:-

true
true
false
true

It also contains two more forms, and these were also given Java 9 version onwards.

  • public static <T> boolean equals(T[] a, T[] a2, Comparator<? super T> cmp)
  • public static <T> boolean equals(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex, Comparator<? super T> cmp)

Note:- Arrays.equals() method is only capable of checking the equality of a single dimensional array. For multidimensional array, we need to use Arrays.deepEquals() method. It is discussed in detail here:- How to compare two arrays in Java.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Reply