Comparing Java Arrays using equals and deepEquals

Joe • March 9, 2019

Arrays can be compared using the java.util.Arrays.equals(….) and java.util.Arrays .deepEquals(a1,a2) method.


  • Returns true if the two specified arrays of Objects are equal to one another. The two arrays are considered equal if they contain the same elements in the same order. Also, two array references are considered equal if both are null.
  • Comparing one-dimensional Arrays only.


  • Returns true if the two specified arrays are deeply equal to one another. Two array references are considered deeply equal if both are null, or if they refer to arrays that contain the same number of elements and all corresponding pairs of elements in the two arrays are deeply equal.
  • Used in comparing nested arrays of arbitrary depth or multi-dimensional Arrays.



    public static void main(String[] args) {

        String[] arr1 = {"1", "2"};
        String[] arr2 = {"1", "2"};

        String[][] arr3 = {{"1", "2"}, {"3", "4"}};
        String[][] arr4 = {{"1", "2"}, {"3", "4"}};

        String[][] arr5 = {{"3", "4"}, {"1", "2"}};

        System.out.println(Arrays.equals(arr1, arr2)); //prints: true
        System.out.println(Arrays.equals(arr3, arr4)); //prints: false

        System.out.println(Arrays.deepEquals(arr1, arr2)); //prints: true
        System.out.println(Arrays.deepEquals(arr3, arr4)); //prints: true
        System.out.println(Arrays.deepEquals(arr3, arr5)); //prints: false



Similar Posts ..

Subscribe to our monthly newsletter. No spam, we promise !