CopyPastehas never been so tasty!

Compare byte arrays using Arrays class in java

by anonymous

  • 0
  • 0
  • 0
94 views

Compare byte arrays using Arrays class in java

Byte Type:The byte type consists of 8 bit 2’s singed integer. It has a minimum value of -128 and a maximum value of 127. The byte data type can be useful for storing memory in large arrays. They can also be used as int for clarification of your code, where variable range is limited.Equal method of arrays class:The equal() method of java.lang.object acts the same as the == operator. The basic function of the  equal() method is that it tests for equality not identity so most classes will override  equal() that will compare what to return true or false.Comparison of byte arrays using Arrays class in Java:For comparison of byte array using array class we have to make a class.Then you have to make 2 byte arrays, if the size of array is equal to other then output will be true else it will be false. In the below code byte arrays are equal that’s why output is true.The java.util.Arrays class is actually a set of static methods that are all useful for working with arrays.The Arrays class contains various methods for manipulating arrays such as sorting and searching.Here is sample code for comparison of byte arrays using array class: 

import java.util.Arrays; 

public class Main

  public static void main(String[] args) {

    byte[] s1 = new byte[] { 9, 21, 18 };

    byte[] s2 = new byte[] { 9, 21, 18 };

     System.out.println(Arrays.equals(s1, s2));

  }

}

 This simple article that compares byte arrays using array class in Java.

Add A Comment: