java.util.Arrays class provides equals() and deepEquals() method to compare two Arrays in Java. Both of these are overloaded method to compare primitive arrays e.g. int, long, float, double and Object arrays e.g. Arrays.equals(Object , Object). Arrays.equals() returns true if both Arrays which it is comparing are null, If both array pointing to same Array Object or they must be of same length and contains same element in each index. In all other cases it returns false. Arrays.equals() calls equals() method of each Object while comparing Object arrays. One of the tricky question in Java related to Array comparison is Difference between Arrays.equals() and Arrays.deepEquals() method. Since both equals and deepEquals is used for array comparison, what is difference between them becomes important. Short answer of this questions is that, Array's equals() method does not perform deep comparison and fails logical comparison in case of nested Array, on other hand deepEquals() perform deep comparison and returns logical comparison in case of nested array.
Difference between equals and deepEquals of Arrays in Java
As I said earlier, difference between deepEquals() vs equals() method is a good Java Interview question and main difference difference between them comes while comparing nested array i.e. Array inside Array. Arrays.equals() method does not compare recursively if array contains another array on other hand Arrays.deepEquals() method compare recursively if array contains another array. Arrays.equals() check is if element is null or not and then calls equals() method, it does not check for Array type. Which means if any item in array is another array itself then call to equals() goes to default java.lang.Object equals(), which compares reference of two Object and does not perform logical comparison and can return false even if two object arrays are logically equals, as seen in following Object Array comparison. On the other hand Arrays.deepEquals() method performs lot of checks and calls Arrays.equals() for non array comparison and recursively call Arrays.deepEquals() for array type comparison, which allows it to compare nested array logically in Java. It's better to use Arrays.equals() to compare non nested Array and Arrays.deepEquals() to compare nested Array, as former is faster than later in case of non nested Array.
This is quite clear with following code snippet from Arrays.equals() method of java.util.Arrays class :
You can see that equals() method of java.util.Arrays class does not check if element is Array type or not and simply calls equals(), which in case of array act similar to == operator. Now let's see Arrays.deepEquals() code from java.util.Arrays class :
Arrays.deepEquals() calls deepEquals() recursively if one of element is of type Object. I have not included complete code but you can check complete code of java.util.Arrays class in Eclipse IDE by using Eclipse shortcut Ctrl + T as discussed in Top 30 Eclipse shortcut for Java programmers. Even after pressing Ctrl+T you only see class file and not Java source than you need to attach JDK source code with rt.jar. See Eclipse tutorial How to attach source in Eclipse for any JAR to learn more about attaching source in Eclipse.
Array Comparison Example using equals() and deepEquals()
In this section we will see couple of example of comparing arrays in Java. We will compare both primitive and object array as well as nested array to see How array comparison works.Primitive and Object arrays are compared using Arrays.equals() method while nested array is compared using Arrays.deepEquals() method to get logical comparison.
That's all on how to compare two Arrays in Java. We have seen example of comparing both primitive and object array and also seen difference between equals() and deepEquals() method of Arrays class. In summary use Arrays.equals() for comparing non nested arrays, it has overloaded method for primitive array and performs better than deepEquals() but always use deepEquals() method to compare nested array in Java to get logical comparison.
Other Java Collection Tutorials you may like