Coding Villa

Compare byte arrays using Arrays class in java

Author : Jal Pari

Share with Friends
Views : 887
Ratings : 0
Bookmarks : 0
Farourities : 0

Description


In this article you will learn how to compare byte arrays using Arrays class in Java.


Advertisements


 

 

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 equals () method of java.lang.Object acts the same as the == operator. The basic function of the equals () method is that it tests for equality not identity so most classes will override equals () 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.

 

 Get This Book Now

Java 2 Database Programming for Dummies Java 2 Database Programming for Dummies Java 2 Database Programming for Dummies Java 2 Dat



Article Tags
Share with Friends

Comments




Leave a Reply

Name (required)  
Mail (will not be published) (required)   
 
Enter This Code
Captcha
 

Advertisements

Article Categories

.NET


Databases


Mobile Development


Operating Systems


Web Development


Coding Villa on Facebook