ShortBuffer equals() method in Java with Examples
The equals() method of java.nio.ShortBuffer Class is used to check whether or not the given buffer is equal to another object.
Two short 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 short 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:
Program 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 ShortBuffer 1 int capacity1 = 10; // Declaring the capacity of the ShortBuffer 2 int capacity2 = 10; // Creating the ShortBuffer try { // creating object of Shortbuffer 1 // and allocating size capacity ShortBuffer sb1 = ShortBuffer.allocate(capacity1); // creating object of Shortbuffer 2 // and allocating size capacity ShortBuffer fb2 = ShortBuffer.allocate(capacity2); // putting the value in Shortbuffer 1 sb1.put((short)856); sb1.put(2, (short)961); sb1.rewind(); // putting the value in Shortbuffer 2 fb2.put((short)856); fb2.put(2, (short)961); fb2.rewind(); // print the ShortBuffer 1 System.out.println(" ShortBuffer 1: " + Arrays.toString(sb1.array())); // print the ShortBuffer 2 System.out.println(" ShortBuffer 2: " + Arrays.toString(fb2.array())); // checking the equality of both ShortBuffer boolean fbb = sb1.equals(fb2); // checking if else condition if (fbb) 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"); } } } |
ShortBuffer 1: [856, 0, 961, 0, 0, 0, 0, 0, 0, 0] ShortBuffer 2: [856, 0, 961, 0, 0, 0, 0, 0, 0, 0] both are equal
Program 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 ShortBuffer 1 int capacity1 = 10; // Declaring the capacity of the ShortBuffer 2 int capacity2 = 5; // Creating the ShortBuffer try { // creating object of Shortbuffer 1 // and allocating size capacity ShortBuffer sb1 = ShortBuffer.allocate(capacity1); // creating object of Shortbuffer 2 // and allocating size capacity ShortBuffer sb2 = ShortBuffer.allocate(capacity2); // putting the value in Shortbuffer 1 sb1.put((short)856); sb1.put(2, (short)961); sb1.rewind(); // putting the value in Shortbuffer 2 sb2.put((short)856); sb2.put(2, (short)431); sb2.rewind(); // print the ShortBuffer 1 System.out.println(" ShortBuffer 1: " + Arrays.toString(sb1.array())); // print the ShortBuffer 2 System.out.println(" ShortBuffer 2: " + Arrays.toString(sb2.array())); // checking the equality of both ShortBuffer boolean fbb = sb1.equals(sb2); // checking if else condition if (fbb) 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"); } } } |
ShortBuffer 1: [856, 0, 961, 0, 0, 0, 0, 0, 0, 0] ShortBuffer 2: [856, 0, 431, 0, 0] both are not equal
Recommended Posts:
- ShortBuffer toString() method in Java with Examples
- ShortBuffer limit() method in Java with Examples
- ShortBuffer compareTo method in Java with Examples
- ShortBuffer arrayOffset() method in Java With Examples
- ShortBuffer duplicate() method in Java With Examples
- ShortBuffer order() Method in Java with Examples
- ShortBuffer allocate() method in Java With Examples
- ShortBuffer array() Method in Java with Examples
- ShortBuffer hasArray() method in Java with Examples
- ShortBuffer slice() method in Java with Examples
- ShortBuffer rewind() method in Java with Examples
- ShortBuffer mark() method in Java with Examples
- ShortBuffer put(int, short) method in Java With Examples
- ShortBuffer compact() method in Java With Examples
- ShortBuffer asReadOnlyBuffer() method in Java with Examples
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.



