What does the method equals(obj[] a1, obj[] a2) do in java?

Java 8Object Oriented ProgrammingProgramming

The equals(Object[] a, Object[] a2) method of the java.util.Arrays class returns true if the two specified arrays of objects are equal to one another. The two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. Two objects e1 and e2 are considered equal if (e1==null ? e2==null : e1.equals(e2)). The two arrays are equal if they contain the same elements in the same order. Two array references are considered equal if both are null.


import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {
      Object[] arr1 = new Object[] { 1, 123 };
      Object[] arr2 = new Object[] { 1, 123, 22, 4 };
      Object[] arr3 = new Object[] { 1, 123 };
      boolean retval=Arrays.equals(arr1, arr2);
      System.out.println("arr1 and arr2 equal: " + retval);
      boolean retval2=Arrays.equals(arr1, arr3);
      System.out.println("arr1 and arr3 equal: " + retval2);


arr1 and arr2 equal: false
arr1 and arr3 equal: true
Published on 16-Jan-2018 16:49:07