Question:Difference between equals and deepEquals of Arrays in Java

asked Sep 13, 2013 in JSP by rajesh
edited Sep 12, 2013
0 votes

1 Answer

0 votes


This is quite clear with following code snippet from Arrays.equals() method of java.util.Arrays class :
for (int i=0; i
            Object o1 = a[i];
            Object o2 = a2[i];
            if (!(o1==null ? o2==null : o1.equals(o2)))
                return false;
 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 :
for (int i = 0; i < length; i++) {
            Object e1 = a1[i];
            Object e2 = a2[i];
if (e1 instanceof Object[] && e2 instanceof Object[])
    eq = deepEquals ((Object[]) e1, (Object[]) e2);
else if (e1 instanceof byte[] && e2 instanceof byte[])
    eq = equals((byte[]) e1, (byte[]) e2);
     eq = e1.equals(e2);
answered Sep 13, 2013 by rajesh
edited Sep 12, 2013