What is the ByteBuffer equals() method in Java?

The java.nio.ByteBuffer is a class we can use to store a buffer of bytes. The equals() method of this class checks whether two buffers are equal.

Two buffers are equal if:

  • They have the same type of elements.
  • They have the same number of elements.
  • They have the same sequence of remaining elements.

Declaration

The ByteBuffer.equals() method can be declared as follows:

buff1.equals(buff2);
  • buff1: The first buffer checked to see if it is equal to buff2.
  • buff2: The second buffer checked to see if it is equal to buff1.

Return value

The ByteBuffer.equals() method returns a boolean such that:

  • The return value is true if the two buffers, buff1 and buff2, are equal.
  • The return value is false if the two buffers, buff1 and buff2, are not equal.

Code

Example 1

Consider the code snippet below, which demonstrates the use of the ByteBuffer.equals() method.

import java.nio.*;
import java.util.*;
public class main {
public static void main(String[] args) {
int n1 = 4;
int n2 = 4;
try {
ByteBuffer buff1 = ByteBuffer.allocate(n1);
buff1.put((byte)1);
buff1.put((byte)4);
System.out.println("buff1: " + Arrays.toString(buff1.array()));
ByteBuffer buff2 = ByteBuffer.allocate(n2);
buff2.put((byte)1);
buff2.put((byte)4);
System.out.println("buff2: " + Arrays.toString(buff2.array()));
boolean foo = buff1.equals(buff2);
System.out.println("\nbuff1 equal to buff2: " + foo);
} catch (IllegalArgumentException e) {
System.out.println("Error!!! IllegalArgumentException");
} catch (ReadOnlyBufferException e) {
System.out.println("Error!!! ReadOnlyBufferException");
}
}
}

Explanation

  • A ByteBuffer buff1 is declared in line 8. Two bytes are written to buff1 in lines 9-10.
  • A ByteBuffer buff2 is declared in line 13. Two bytes are written to buff2 in lines 14-15.
  • The ByteBuffer.equals() method is used in line 18 to check if the buffers buff1 and buff2 are equal. TheByteBuffer.equals() method returns true because buff1 and buff2 are equal.

Example 2

Consider the code snippet below, which compares two unequal buffers.

import java.nio.*;
import java.util.*;
public class main {
public static void main(String[] args) {
int n1 = 4;
int n2 = 5;
try {
ByteBuffer buff1 = ByteBuffer.allocate(n1);
buff1.put((byte)1);
buff1.put((byte)4);
System.out.println("buff1: " + Arrays.toString(buff1.array()));
ByteBuffer buff2 = ByteBuffer.allocate(n2);
buff2.put((byte)1);
buff2.put((byte)4);
System.out.println("buff2: " + Arrays.toString(buff2.array()));
boolean foo = buff1.equals(buff2);
System.out.println("\nbuff1 equal to buff2: " + foo);
} catch (IllegalArgumentException e) {
System.out.println("Error!!! IllegalArgumentException");
} catch (ReadOnlyBufferException e) {
System.out.println("Error!!! ReadOnlyBufferException");
}
}
}

Explanation

  • A ByteBuffer buff1 is declared in line 8. Two bytes are written to buff1 in lines 9-10.
  • A ByteBuffer buff2 is declared in line 13. Two bytes are written to buff2 in lines 14-15.
  • The ByteBuffer.equals() method is used in line 18 to check if the buffers buff1 and buff2 are equal. TheByteBuffer.equals() method returns false because the number of elements of buff1 is 4 and the number of elements of buff2 is 5, which is not equal.

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved