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


The equals(int[] a, int[] a2) method of java.util.Arrays returns true if the two specified arrays of integers are equal to one another. Two arrays are equal if they contain the same elements in the same order. Two array references are considered equal if both are null.

Example

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {
      int[] arr1 = new int[] { 10, 12, 5, 6 };
      int[] arr2 = new int[] { 10, 12, 5, 6 };
      int[] arr3 = new int[] { 10, 5, 6, 12 };
      boolean retval = Arrays.equals(arr1, arr2);
      System.out.println("arr1 and arr2 equal: " + retval);
      boolean retval2 = Arrays.equals(arr2, arr3);
      System.out.println("arr2 and arr3 equal: " + retval2);
   }
}

Output

arr1 and arr2 equal: true
arr2 and arr3 equal: false

Monica Mona
Monica Mona

Student of life, and a lifelong learner

Updated on: 20-Feb-2020

189 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements