IntBuffer equals() method in Java

The equals() method of java.nio.IntBuffer Class is used to check whether or not the given buffer is equal to another object.

Two int 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 int elements a and b to be equal if (a == b) || (Int.isNaN(a) && Int.isNaN(b)). The values -0 and +0 are considered to be equal, unlike Int.equals(Object).

A int 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 IntBuffer 1
        int capacity1 = 10;
  
        // Declaring the capacity of the  IntBuffer 2
        int capacity2 = 10;
  
        // Creating the IntBuffer
        try {
  
            // creating object of Intbuffer 1
            // and allocating size capacity
            IntBuffer ib1 = IntBuffer.allocate(capacity1);
  
            // creating object of Intbuffer 2
            // and allocating size capacity
            IntBuffer ib2 = IntBuffer.allocate(capacity2);
  
            // putting the value in Intbuffer 1
            ib1.put(8);
            ib1.put(2, 9);
            ib1.rewind();
  
            // putting the value in Intbuffer 2
            ib2.put(8);
            ib2.put(2, 9);
            ib2.rewind();
  
            // print the IntBuffer 1
            System.out.println(" IntBuffer 1:  "
                               + Arrays.toString(ib1.array()));
  
            // print the IntBuffer 2
            System.out.println(" IntBuffer 2:  "
                               + Arrays.toString(ib2.array()));
  
            // checking the equality of both IntBuffer
            boolean ibb = ib1.equals(ib2);
  
            // checking if else condition
            if (ibb)
                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");
        }
    }
}

chevron_right


Output:

IntBuffer 1:  [8, 0, 9, 0, 0, 0, 0, 0, 0, 0]
 IntBuffer 2:  [8, 0, 9, 0, 0, 0, 0, 0, 0, 0]
Both are equal

Examples 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 IntBuffer 1
        int capacity1 = 10;
  
        // Declaring the capacity of the  IntBuffer 2
        int capacity2 = 5;
  
        // Creating the IntBuffer
        try {
  
            // creating object of Intbuffer 1
            // and allocating size capacity
            IntBuffer ib1 = IntBuffer.allocate(capacity1);
  
            // creating object of Intbuffer 2
            // and allocating size capacity
            IntBuffer ib2 = IntBuffer.allocate(capacity2);
  
            // putting the value in Intbuffer 1
            ib1.put(8);
            ib1.put(2, 9);
            ib1.rewind();
  
            // putting the value in Intbuffer 2
            ib2.put(8);
            ib2.put(2, 9);
            ib2.rewind();
  
            // print the IntBuffer 1
            System.out.println(" IntBuffer 1:  "
                               + Arrays.toString(ib1.array()));
  
            // print the IntBuffer 2
            System.out.println(" IntBuffer 2:  "
                               + Arrays.toString(ib2.array()));
  
            // checking the equality of both IntBuffer
            boolean ibb = ib1.equals(ib2);
  
            // checking if else condition
            if (ibb)
                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");
        }
    }
}

chevron_right


Output:

IntBuffer 1:  [8, 0, 9, 0, 0, 0, 0, 0, 0, 0]
 IntBuffer 2:  [8, 0, 9, 0, 0]
Both are not equal

Examples 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 IntBuffer 1
        int capacity1 = 10;
  
        // Declaring the capacity of the  IntBuffer 2
        int capacity2 = 10;
  
        // Creating the IntBuffer
        try {
  
            // creating object of Intbuffer 1
            // and allocating size capacity
            IntBuffer ib1 = IntBuffer.allocate(capacity1);
  
            // creating object of Intbuffer 2
            // and allocating size capacity
            IntBuffer ib2 = IntBuffer.allocate(capacity2);
  
            // putting the value in Intbuffer 1
            ib1.put(8);
            ib1.put(2, 9);
            ib1.rewind();
  
            // putting the value in Intbuffer 2
            ib2.put(8);
            ib2.put(2, 9);
            ib2.put(3, 7);
            ib2.put(4, 4);
            ib2.rewind();
  
            // print the IntBuffer 1
            System.out.println(" IntBuffer 1:  "
                               + Arrays.toString(ib1.array()));
  
            // print the IntBuffer 2
            System.out.println(" IntBuffer 2:  "
                               + Arrays.toString(ib2.array()));
  
            // checking the equality of both IntBuffer
            boolean ibb = ib1.equals(ib2);
  
            // checking if else condition
            if (ibb)
                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");
        }
    }
}

chevron_right


Output:

IntBuffer 1:  [8, 0, 9, 0, 0, 0, 0, 0, 0, 0]
 IntBuffer 2:  [8, 0, 9, 7, 4, 0, 0, 0, 0, 0]
Both are not equal


My Personal Notes arrow_drop_up

Stepping steps of my ownself

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.