What is the CharBuffer equals() method in Java?
java.nio.CharBuffer is a class that we can use to store a buffer of characters. The equals() method of the class java.nio.CharBuffer checks if two buffers are equal or not.
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.
Note:
- Unlike
Char.equals(Object), -0 and +0 are considered equal by theCharBuffer.equals()method.- Two characters f1 and f2 are considered equal by
CharBuffer.equals()method if (f1 == f2) || (Char.isNaN(f1) && Char.isNaN(f2)).
Declaration
The CharBuffer.equals() method can be declared as follows:
buff1.equals(buff2);
buff1: The first buffer checked to see if it is equal tobuff2.buff2: The second buffer checked to see if it is equal tobuff1.
Return value
The CharBuffer.equals() method returns a boolean such that:
- The return value is
trueif the two buffersbuff1andbuff2are equal. - The return value is
falseif the buffersbuff1andbuff2are notequal.
Examples
Example 1
Consider the code snippet below, which demonstrates the use of the CharBuffer.equals() method.
import java.nio.*;import java.util.*;public class main {public static void main(String[] args) {int n1 = 4;int n2 = 4;try {CharBuffer buff1 = CharBuffer.allocate(n1);buff1.put('a');buff1.put('c');System.out.println("buff1: " + Arrays.toString(buff1.array()));CharBuffer buff2 = CharBuffer.allocate(n2);buff2.put('a');buff2.put('c');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
CharBufferbuff1is declared in line 8. Two characters are written tobuff1in lines 9-10. - A
CharBufferbuff2is declared in line 13. Two characters are written tobuff2in lines 14-15. - The
CharBuffer.equals()method is used in line 18 to check if the buffersbuff1andbuff2are equal. TheCharBuffer.equals()method returnstruebecausebuff1andbuff2are 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 {CharBuffer buff1 = CharBuffer.allocate(n1);buff1.put('a');buff1.put('c');System.out.println("buff1: " + Arrays.toString(buff1.array()));CharBuffer buff2 = CharBuffer.allocate(n2);buff2.put('a');buff2.put('c');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
CharBufferbuff1is declared in line 8. Two characters are written tobuff1in lines 9-10. - A
CharBufferbuff2is declared in line 13. Two characters are written tobuff2in lines 14-15. - The
CharBuffer.equals()method is used in line 18 to check if the buffersbuff1andbuff2are equal. TheCharBuffer.equals()method returnsfalsebecause the number of elements ofbuff1is 4 and the number of elements ofbuff2is 5, which is not equal.
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved