- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to compare two Byte Arrays
To compare two Byte Arrays, use the Arrays.equals() method. Here we have declared and initialized a total of 4 arrays.
byte[] arr1 = new byte[] { 11, 13, 30, 45, 77, 89 }; byte[] arr2 = new byte[] { 12, 13, 34, 87, 99, 33}; byte[] arr3 = new byte[] { 11, 13, 30, 45, 77, 89 }; byte[] arr4 = new byte[] { 13, 16, 56, 78, 98, 99 };
Now, using the Arrays.equals() method, we will be compare two arrays.
Arrays.equals(arr1, arr2);
In the same way, other arrays would be compared one by one.
Let us see the complete example to compare two Byte Arrays.
Example
import java.util.*; public class Demo { public static void main(String[] args) { byte[] arr1 = new byte[] { 11, 13, 30, 45, 77, 89 }; byte[] arr2 = new byte[] { 12, 13, 34, 87, 99, 33}; byte[] arr3 = new byte[] { 11, 13, 30, 45, 77, 89 }; byte[] arr4 = new byte[] { 13, 16, 56, 78, 98, 99 }; System.out.println("Array1 is equal to Array2 = "+Arrays.equals(arr1, arr2)); System.out.println("Array1 is equal to Array3 = "+Arrays.equals(arr1, arr3)); System.out.println("Array1 is equal to Array4 = "+Arrays.equals(arr1, arr4)); System.out.println("Array2 is equal to Array3 = "+Arrays.equals(arr2, arr3)); } }
Output
Array1 is equal to Array2 = false Array1 is equal to Array3 = true Array1 is equal to Array4 = false Array2 is equal to Array3 = false
- Related Articles
- Compare two-byte arrays in a single line in Java
- Java Program to compare Two Java float Arrays
- Java Program to compare Two Java short Arrays
- Java Program to compare two Java char Arrays
- Java Program to compare two Boolean Arrays
- Compare Two Java long Arrays
- Compare Two Java double arrays
- How to compare two arrays in Java?
- Compare Two Java int Arrays in Java
- How do we compare two arrays in Java?
- Java Program to Compare Two Objects
- Java Program to Compare Two Strings
- Java Program to compare two sets
- Java Program to Concatenate Two Arrays
- Program to Compare two strings in Java

Advertisements