The equals() method of java.nio.ByteBuffer class is used to check whether or not the given buffer is equal to another object.
Two byte buffers are equal if, and only if,
- They have the same element type,
- They have the same number of remaining elements, and
- The two sequences of remaining elements, considered independently of their starting positions, are pointwise equal.
A byte buffer is not equal to any other type of object.
Syntax:
public boolean equals(Object ob)
Parameters: This method takes the ob(The object to which this buffer is to be compared) as a parameter.
Return Value: This method returns true if, and only if, this buffer is equal to the given object.
Below are the examples to illustrate the equals() method:
Examples 1:
// Java program to demonstrate// equals() method  import java.nio.*;import java.util.*;  public class GFG {      public static void main(String[] args)    {          // Declaring the capacity of the ByteBuffer 1        int capacity1 = 5;          // Declaring the capacity of the ByteBuffer 2        int capacity2 = 5;          // Creating the ByteBuffer        try {              // creating object of ByteBuffer 1            // and allocating size capacity            ByteBuffer bb1 = ByteBuffer.allocate(capacity1);              // creating object of ByteBuffer 2            // and allocating size capacity            ByteBuffer bb2 = ByteBuffer.allocate(capacity2);              // putting the int to byte typecast value in ByteBuffer 1            bb1.put((byte)20);            bb1.put((byte)30);            bb1.put((byte)40);            bb1.rewind();              // putting the value in ByteBuffer 2            bb2.put((byte)20);            bb2.put((byte)30);            bb2.put((byte)40);            bb2.rewind();              // print the ByteBuffer 1            System.out.println(" ByteBuffer 1: "                               + Arrays.toString(bb1.array()));              // print the ByteBuffer 2            System.out.println(" ByteBuffer 2: "                               + Arrays.toString(bb2.array()));              // checking the equality of both ByteBuffer            boolean b = bb1.equals(bb2);              // checking if else condition            if (b)                System.out.println(" both are equal");            else                System.out.println(" both are not equal");        }          catch (IllegalArgumentException e) {            System.out.println("Exception thrown : " + e);        }          catch (ReadOnlyBufferException e) {            System.out.println("Exception thrown : " + e);        }    }} |
ByteBuffer 1: [20, 30, 40, 0, 0] ByteBuffer 2: [20, 30, 40, 0, 0] both are equal
Examples 2:
// Java program to demonstrate// equals() method  import java.nio.*;import java.util.*;  public class GFG {      public static void main(String[] args)    {          // Declaring the capacity of the ByteBuffer 1        int capacity1 = 5;          // Declaring the capacity of the ByteBuffer 2        int capacity2 = 3;          // Creating the ByteBuffer        try {              // creating object of ByteBuffer 1            // and allocating size capacity            ByteBuffer bb1 = ByteBuffer.allocate(capacity1);              // creating object of ByteBuffer 2            // and allocating size capacity            ByteBuffer bb2 = ByteBuffer.allocate(capacity2);              // putting the int to byte typecast value in ByteBuffer 1            bb1.put((byte)20);            bb1.put((byte)30);            bb1.put((byte)40);            bb1.rewind();              // putting the value in ByteBuffer 2            bb2.put((byte)20);            bb2.put((byte)30);            bb2.put((byte)40);            bb2.rewind();              // print the ByteBuffer 1            System.out.println(" ByteBuffer 1: "                               + Arrays.toString(bb1.array()));              // print the ByteBuffer 2            System.out.println(" ByteBuffer 2: "                               + Arrays.toString(bb2.array()));              // checking the equality of both ByteBuffer            boolean b = bb1.equals(bb2);              // checking if else condition            if (b)                System.out.println(" both are equal");            else                System.out.println(" both are not equal");        }          catch (IllegalArgumentException e) {            System.out.println("Exception thrown : " + e);        }          catch (ReadOnlyBufferException e) {            System.out.println("Exception thrown : " + e);        }    }} |
ByteBuffer 1: [20, 30, 40, 0, 0] ByteBuffer 2: [20, 30, 40] both are not equal
