Syntax error Compare Two Java long Arrays

Compare Two Java long Arrays



To compare two Java long arrays in Java, use Arrays.equals() method. Let's say we have the following long arrays.

long[] arr1 = new long[] { 767, 568, 555, 897, 678 };
long[] arr2 = new long[] { 456, 756, 555, 999, 678};
long[] arr3 = new long[] { 767, 568, 555, 897, 678 };

Now, we can compare the equality of these arrays using the equals() method.

Arrays.equals(arr1, arr2);
Arrays.equals(arr2, arr3);
Arrays.equals(arr1, arr3);  

The following is the complete example.

Example

Live Demo

import java.util.*;
public class Demo {
   public static void main(String []args) {
      long[] arr1 = new long[] { 767, 568, 555, 897, 678 };
      long[] arr2 = new long[] { 456, 756, 555, 999, 678};
      long[] arr3 = new long[] { 767, 568, 555, 897, 678 };
      // comparing
      System.out.println(Arrays.equals(arr1, arr2));
      System.out.println(Arrays.equals(arr2, arr3));
      System.out.println(Arrays.equals(arr1, arr3));   
   }
}

Output

false
false
true
Updated on: 2019-07-30T22:30:23+05:30

547 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements