Comparing Arrays In Java | Java Array Equals Methods

Comparing Arrays in Java

Now let’s move on to the next method, with which, we can check whether the two arrays are equally based on their contents. Well, earlier, we had done a program, in which we had created two arrays, with the same contents, and I had asked whether these reference variables are pointing to the same or different objects.

The obvious answer to the question was that the reference variables are pointing to different array objects. Well, now let us use a method from the Arrays class to check whether the two arrays are equally based on the content.

Well, earlier, we learned to use the comparison operator while we were comparing two values, but here, comparing two arrays with the help of the == operator would result in strange output. This is because we are comparing array reference variables, and the == operator will check for the reference. It will check whether the reference variables are pointing to the same object. Have a look at the two programs below. In the first program, we have two reference variables pointing to different arrays, with the same elements, and in the second program, the two reference variables are pointing to the same array.

import java.util.Arrays;
public class ArrayBasic {
public static void main(String[] args) {
int[] someArray = new int[]{56, 43, 29, 31, 14, 4, 55};
int[] anotherArray = new int[]{56, 43, 29, 31, 14, 4, 55};
System.out.println(someArray == anotherArray);
}
}

The output of the above program is going to be false, because of the obvious fact that the two reference variables are pointing to the different array objects, and we are checking whether they are pointing to same array objects, which they are not.

import java.util.Arrays;
public class ArrayBasic {
public static void main(String[] args) {
int[] someArray = new int[]{56, 43, 29, 31, 14, 4, 55};
int[] anotherArray = someArray;
System.out.println(someArray == anotherArray);
}
}

Well, This time, we are going to get the output as true, when we execute the above program, since both reference variables are now pointing to the same array object, and we are still comparing the reference variables using the == operator.

So, from the above two programs we understand that with the == operator, we were able to check whether the reference variables are pointing to the same objects. But if we want to check whether or not the arrays are equal on the basis of their contents, then we should use the method equals from the Arrays class, which helps us do this.

When we give the reference variables of the arrays to be checked, it returns true if both the arrays are equal, else it returns false.

When we say that the two arrays are equal, we mean that both the arrays contain the same number of elements and all the elements are in the same order.

Lets have a look on some programs through which we can get a clear picture –

import java.util.Arrays;
public class ArrayBasic {
public static void main(String[] args) {
int[] someArray = new int[]{56, 43, 29, 31, 14, 4, 55};
int[] anotherArray = new int[]{56, 43, 29, 31, 14, 4, 55};
System.out.println(Arrays.equals(someArray, anotherArray));
}
}

In the above program, it is obviously seen that both the arrays contain the same number of elements, and also the elements are arranged in the same order. So, the obvious output of this program is true when executed.

Now lets have a look on another case, in which there are same number of elements in the array, but not all the elements are in same order.

import java.util.Arrays;
public class ArrayBasic {
public static void main(String[] args) {
int[] someArray = new int[]{56, 43, 29, 31, 14, 3, 55};
int[] anotherArray = new int[]{55, 56, 29, 31, 3 ,43, 14};
System.out.println(Arrays.equals(someArray, anotherArray));
}
}

Well, in the above program, there are the same number of elements in both the arrays, also, if you have a look at the elements, every element in some array is also there in another array, but this time, the order is not the same, which is why the output is going to be false.

So, from this, it is clear that when we say equal, we mean that the number of elements is also same, and the order in which these elements are arranged is also same. One thing that we can modify in the above program is that we can sort both the arrays, so that the arrangement of the elements becomes same. The number of elements is also same already, so the equals method now will return true. Lets try this one now –

import java.util.Arrays;
public class ArrayBasic {
public static void main(String[] args) {
int[] someArray = new int[]{56, 43, 29, 31, 14, 3, 55};
int[] anotherArray = new int[]{55, 56, 29, 31, 3 ,43, 14};
Arrays.sort(someArray);
Arrays.sort(anotherArray);
System.out.println(Arrays.equals(someArray, anotherArray));
}
}

So, now if we execute this program, we get an output as true, since the number of elements was already the same, and we also crosschecked that both arrays have the same elements, just the problem is with the order. So we got the arrays sorted, and so now they are in the same order and the same number of elements. This defines why the equals method is giving a true output.

There are other varients of this method as well. This means that you can check for arrays of other datatypes whether they are equal or not. Now, when we have defined what do we mean by two arrays are equal, lets try some more examples, with some other datatypes.

import java.util.Arrays;
public class ArrayBasic {
public static void main(String[] args) {
char[] someArray = new char[]{‘c’, ‘o’, ‘f’, ‘f’, ‘e’, ‘e’};
char[] anotherArray = new char[]{‘c’, ‘o’, ‘f’, ‘f’, ‘e’, ‘e’};
System.out.println(Arrays.equals(someArray, anotherArray));
}
}

Here, we are checking if the two character arrays are equal. As you can see that the number of elements in the arrays is the same, and so is the order, hence the output is true. You can try executing the program for different values of elements.
Now lets try a program for string arrays –

import java.util.Arrays;
public class ArrayBasic {
public static void main(String[] args) {
String[] someArray = new String[]{“This”, “is”, “some”, “string”};
String[] anotherArray = new String[]{“This”, “is”, “Some”, “string”};
System.out.println(Arrays.equals(someArray, anotherArray));
}
}

Well, what do you think would be the output of the above program? We are checking whether or not the two string arrays are equal. If you are thinking about true, then read the array elements of both the array carefully again. The output is going to be false. The number of elements is the same, and the order is also almost the same, except that the ‘s’ from the ”Some” in another array are capital. So, the two arrays are not equal on the basis of the content, and hence the method gives false as output.

You can try this method out working with other data types as well, we took some examples related to string, character arrays, and integer arrays. You can give it a try for byte, short, double, etc arrays.

As mentioned earlier, here are some variants of the equals method –

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

You can also check for the arrays of objects like String, arrays, etc with this method.