Java Utility Library

Java Arrays - equals() Method



The java.util.Arrays.equals() method is used to check whether the two specified arrays of doubles 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(double[] a, double[] 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 doubles are equal or not.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating three double arrays
    double Arr1[] = {10.1, 2.34, -3.6, 35.01, 56.23};
    double Arr2[] = {10.1, 2.34, -3.6, 35.01, 56.23};
    double Arr3[] = {10, 2.3, -3, 35, 56.2};

    //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