Compare Two Byte Arrays Java

Compare Two Byte Arrays Java | The byte is one of the eight primitive data types supported by the Java programming language. It is an eight-bit signed two’s complement integer. The value of the byte is between -128 to 127 both inclusive. The default value field for byte is 0. 

The byte array is an array of bytes i.e. the data type of the array is the byte. We can use a byte array to store the collection of the byte data. In this section, we will compare two-byte arrays. To do this we use Arrays.equals() method, there are many forms of Arrays.equals() but we use only the below method:-

Method Syntax:- public static boolean equals(byte[] a1, byte a2[])
Where a1 = array1 and a2 = array2

The return type of the above method is boolean. It returns true if the arrays are equal or else it returns false.

Lets us see some examples to understand more about compare two byte arrays Java:-

Example-1:-
byte arr1[ ] = {10,20,30,40}
byte arr2[ ] = {10,20,30,40}
Arrays.equals(arr1, arr2);
Result:- true

Example-2:-
byte arr1[ ] = {10,20,30,40}
byte arr2[ ] = {10,20,30,0}
Arrays.equals(arr1, arr2);
Result:- false

Example-3:-
byte arr1[ ] = {101,200,30999888,404444}
byte arr2[ ] = {10,20,30,0}
Arrays.equals(arr1, arr2);
Result:- This gives the error in arr1 as the bytes range is from -128 to 127.

Compare Two Byte Arrays Java Code

Now let us see compare two byte arrays Java code. We will take two-byte arrays and initialized them directly in the program. After that, we will call Arrays.equals() method to compare both arrays.

import java.util.Arrays;

public class Main {
   public static void main(String[] args) {
      byte[] array1 = { 2, 1, 2, 4 };
      byte[] array2 = { 2, 1, 2, 4 };
      System.out.println(Arrays.equals(array1, array2));
   }
}

Output:

true

Test case where the output is false

import java.util.Arrays;
public class Main {
   public static void main(String[] args) {
      byte[] array1 = { 2, 1, 2, 4 };
      byte[] array2 = { 1, 1, 2, 4 };
      System.out.println(Arrays.equals(array1, array2));
   }
}

Output:

false

Test case where the code gives error

import java.util.Arrays;
public class Main {
   public static void main(String[] args) {
      byte[] array1 = { 2, 1, 2, 4 };
      byte[] array2 = { 100, 9001, 2, 4 };
      System.out.println(Arrays.equals(array1, array2));
   }
}

Output:

Exception in thread “main” java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from int to byte

This gives an error in array2 as the byte range is only from -127 to 128, and the range of arrays2 exceeds more than 127. The number 9001 is out of the range of byte data type, therefore we have to cast them to byte type.

import java.util.Arrays;

public class Main {
   public static void main(String[] args) {
      byte[] array1 = { 2, 1, 2, 4 };
      byte[] array2 = { (byte) 258, (byte) 257, 2, 4 };
      System.out.println(Arrays.equals(array1, array2));
   }
}

Output:-

true

After type casting 258 gives 2 (128 + 128 + 2). Similarly, the number 257 is cast to 1 in byte data type. Hence both byte arrays are equal. Hence Arrays.equals() method return true.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *