Check if two Byte Arrays are equal or not in Java
Declaration :
1 |
public static boolean equals(byte[] a, byte[] a2) |
Explanation :
Purpose | The java.util.Arrays.equals(byte[] a byte[] a2) method returns true if the two specified arrays of bytes 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. |
Parameters | a ===> This is the array to be tested for equality. |
a2 ===> This is the other array to be tested for equality. | |
Return Value | This method returns true if the two arrays are equal else false. |
Exception | NA |
Java Program : Example
Below example will explain java.util.Arrays.equals() method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package com.c4learn; import java.util.Arrays; public class ArrayDemo { public static void main(String[] args) { // Initialize 3 byte arrays byte[] arr1 = new byte[] { 10, 20, 30, 40, 50 }; byte[] arr2 = new byte[] { 10, 20, 40, 50 }; byte[] arr3 = new byte[] { 10, 20, 30, 40, 50 }; // Compare arrays arr1 and arr2 boolean returnV1 = Arrays.equals(arr1, arr2); System.out.println("Is arr1 and arr2 equal : " + returnV1); // Compare arrays arr1 and arr3 boolean returnV2 = Arrays.equals(arr1, arr3); System.out.println("Is arr1 and arr3 equal : " + returnV2); // Compare arrays arr2 and arr3 boolean returnV3 = Arrays.equals(arr2, arr3); System.out.println("Is arr2 and arr3 equal : " + returnV3); } } |
Output of Program :
1 2 3 |
Is arr1 and arr2 equal : false Is arr1 and arr3 equal : true Is arr2 and arr3 equal : false |