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.
Home » coding »
core java »
java collection tutorial »
programming
» How to compare Arrays in Java – Equals vs deepEquals Example
Langganan:
Posting Komentar (Atom)
0 Response to "How to compare Arrays in Java – Equals vs deepEquals Example"
Posting Komentar