CharBuffer equals() method in Java
The equals() method of java.nio.CharBuffer Class is used to check whether or not the given buffer is equal to another object.
Two char 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.
This method considers two char elements ‘a’ and ‘b’ to be equal if (‘a’ == ‘b’) || (Char.isNaN(a) && Char.isNaN(b)). The values -0 and +0 are considered to be equal, unlike Char.equals(Object).
A char 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 CharBuffer 1 int capacity1 = 10; // Declaring the capacity of the CharBuffer 2 int capacity2 = 10; // Creating the CharBuffer try { // creating object of Charbuffer 1 // and allocating size capacity CharBuffer cb1 = CharBuffer.allocate(capacity1); // creating object of Charbuffer 2 // and allocating size capacity CharBuffer cb2 = CharBuffer.allocate(capacity2); // putting the value in Charbuffer 1 cb1.put('a'); cb1.put(2, 'b'); cb1.rewind(); // putting the value in Charbuffer 2 cb2.put('a'); cb2.put(2, 'b'); cb2.rewind(); // print the CharBuffer 1 System.out.println(" CharBuffer 1: " + Arrays.toString(cb1.array())); // print the CharBuffer 2 System.out.println(" CharBuffer 2: " + Arrays.toString(cb2.array())); // checking the equality of both CharBuffer boolean cbb = cb1.equals(cb2); // checking if else condition if (cbb) System.out.println("Both are equal"); else System.out.println("Both are not equal"); } catch (IllegalArgumentException e) { System.out.println("IllegalArgumentException catched"); } catch (ReadOnlyBufferException e) { System.out.println("ReadOnlyBufferException catched"); } } } |
CharBuffer 1: [a,, b,,,,,,, ] CharBuffer 2: [a,, b,,,,,,, ] 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 CharBuffer 1 int capacity1 = 10; // Declaring the capacity of the CharBuffer 2 int capacity2 = 5; // Creating the CharBuffer try { // creating object of Charbuffer 1 // and allocating size capacity CharBuffer cb1 = CharBuffer.allocate(capacity1); // creating object of Charbuffer 2 // and allocating size capacity CharBuffer cb2 = CharBuffer.allocate(capacity2); // putting the value in Charbuffer 1 cb1.put('a'); cb1.put(2, 'b'); cb1.rewind(); // putting the value in Charbuffer 2 cb2.put('a'); cb2.put(2, 'b'); cb2.rewind(); // print the CharBuffer 1 System.out.println(" CharBuffer 1: " + Arrays.toString(cb1.array())); // print the CharBuffer 2 System.out.println(" CharBuffer 2: " + Arrays.toString(cb2.array())); // checking the equality of both CharBuffer boolean cbb = cb1.equals(cb2); // checking if else condition if (cbb) System.out.println("Both are equal"); else System.out.println("Both are not equal"); } catch (IllegalArgumentException e) { System.out.println("IllegalArgumentException catched"); } catch (ReadOnlyBufferException e) { System.out.println("ReadOnlyBufferException catched"); } } } |
CharBuffer 1: [a,, b,,,,,,, ] CharBuffer 2: [a,, b,, ] Both are not equal
Examples 3:
// 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 CharBuffer 1 int capacity1 = 10; // Declaring the capacity of the CharBuffer 2 int capacity2 = 10; // Creating the CharBuffer try { // creating object of Charbuffer 1 // and allocating size capacity CharBuffer cb1 = CharBuffer.allocate(capacity1); // creating object of Charbuffer 2 // and allocating size capacity CharBuffer cb2 = CharBuffer.allocate(capacity2); // putting the value in Charbuffer 1 cb1.put('a'); cb1.put(2, 'b'); cb1.rewind(); // putting the value in Charbuffer 2 cb2.put('a'); cb2.put(2, 'b'); cb2.put(3, 'c'); cb2.put(4, 'd'); cb2.rewind(); // print the CharBuffer 1 System.out.println(" CharBuffer 1: " + Arrays.toString(cb1.array())); // print the CharBuffer 2 System.out.println(" CharBuffer 2: " + Arrays.toString(cb2.array())); // checking the equality of both CharBuffer boolean cbb = cb1.equals(cb2); // checking if else condition if (cbb) System.out.println("Both are equal"); else System.out.println("Both are not equal"); } catch (IllegalArgumentException e) { System.out.println("IllegalArgumentException catched"); } catch (ReadOnlyBufferException e) { System.out.println("ReadOnlyBufferException catched"); } } } |
CharBuffer 1: [a,, b,,,,,,, ] CharBuffer 2: [a,, b, c, d,,,,, ] Both are not equal
Recommended Posts:
- CharBuffer wrap() method in Java
- CharBuffer array() method in Java
- CharBuffer compareTo() method in Java
- CharBuffer compact() method in Java
- CharBuffer hasArray() method in Java
- CharBuffer arrayOffset() method in Java
- CharBuffer duplicate() method in Java
- CharBuffer asReadOnlyBuffer() method in Java
- CharBuffer slice() method in Java
- CharBuffer allocate() method in Java with Examples
- CharArrayReader read(CharBuffer) method in Java with Examples
- Reader read(CharBuffer) method in Java with Examples
- StringReader read(CharBuffer) method in Java with Examples
- CharsetEncoder encode(CharBuffer in) method in Java with Examples
- AbstractSequentialList equals() method in Java with Example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



