Java Utility Library

Java Arrays - equals() Method



The java.util.Arrays.equals() method is used to check whether the two specified arrays of longs are equal or not. It returns true if the two arrays are equal, else returns false. The two arrays are considered equal if they contain the same elements in the same order.

Syntax

public static boolean equals(long[] a, long[] b)

Parameters

a Specify the first array to be tested for equality.
b Specify the second array to be tested for equality.

Return Value

Returns true if the two arrays are equal, else returns false.

Exception

NA.

Example:

In the example below, the java.util.Arrays.equals() method is used to check whether the two arrays of longs are equal or not.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating three long arrays
    long Arr1[] = {10, 2, -3, 35, 56};
    long Arr2[] = {10, 2, -3, 35, 56};
    long Arr3[] = {5, 1, -3, 20, 25};

    //check Arr1 and Arr2 for equality
    boolean result1 = Arrays.equals(Arr1, Arr2);
    System.out.print("Are Arr1 and Arr2 equal?: "+ result1); 

    //check Arr1 and Arr3 for equality
    boolean result2 = Arrays.equals(Arr1, Arr3);
    System.out.print("\nAre Arr1 and Arr3 equal?: "+ result2);  
  }
}

The output of the above code will be:

Are Arr1 and Arr2 equal?: true
Are Arr1 and Arr3 equal?: false

❮ Java.util - Arrays