equals vs Arrays.equals in Java

前端 未结 8 1425
走了就别回头了
走了就别回头了 2020-11-21 23:29

When comparing arrays in Java, are there any differences between the following 2 statements?

Object[] array1, array2;
array1.equals(array2);
Arrays.equals(ar         


        
8条回答
  •  忘掉有多难
    2020-11-21 23:57

    The equals() of arrays is inherited from Object, so it does not look at the contents of the arrrays, it only considers each array equal to itself.

    The Arrays.equals() methods do compare the arrays' contents. There's overloads for all primitive types, and the one for objects uses the objects' own equals() methods.

提交回复
热议问题