Open In App

FloatBuffer equals() method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

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

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

A float 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 FloatBuffer 1
        int capacity1 = 10;
  
        // Declaring the capacity of the  FloatBuffer 2
        int capacity2 = 10;
  
        // Creating the FloatBuffer
        try {
  
            // creating object of floatbuffer 1
            // and allocating size capacity
            FloatBuffer fb1 = FloatBuffer.allocate(capacity1);
  
            // creating object of floatbuffer 2
            // and allocating size capacity
            FloatBuffer fb2 = FloatBuffer.allocate(capacity2);
  
            // putting the value in floatbuffer 1
            fb1.put(8.56F);
            fb1.put(2, 9.61F);
            fb1.rewind();
  
            // putting the value in floatbuffer 2
            fb2.put(8.56F);
            fb2.put(2, 9.61F);
            fb2.rewind();
  
            // print the FloatBuffer 1
            System.out.println(" FloatBuffer 1:  "
                               + Arrays.toString(fb1.array()));
  
            // print the FloatBuffer 2
            System.out.println(" FloatBuffer 2:  "
                               + Arrays.toString(fb2.array()));
  
            // checking the equality of both FloatBuffer
            boolean fbb = fb1.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");
        }
    }
}


Output:

FloatBuffer 1:  [8.56, 0.0, 9.61, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
FloatBuffer 2:  [8.56, 0.0, 9.61, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
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 FloatBuffer 1
        int capacity1 = 10;
  
        // Declaring the capacity of the  FloatBuffer 2
        int capacity2 = 5;
  
        // Creating the FloatBuffer
        try {
  
            // creating object of floatbuffer 1
            // and allocating size capacity
            FloatBuffer fb1 = FloatBuffer.allocate(capacity1);
  
            // creating object of floatbuffer 2
            // and allocating size capacity
            FloatBuffer fb2 = FloatBuffer.allocate(capacity2);
  
            // putting the value in floatbuffer 1
            fb1.put(8.56F);
            fb1.put(2, 9.61F);
            fb1.rewind();
  
            // putting the value in floatbuffer 2
            fb2.put(8.56F);
            fb2.put(2, 9.61F);
            fb2.rewind();
  
            // print the FloatBuffer 1
            System.out.println(" FloatBuffer 1:  "
                               + Arrays.toString(fb1.array()));
  
            // print the FloatBuffer 2
            System.out.println(" FloatBuffer 2:  "
                               + Arrays.toString(fb2.array()));
  
            // checking the equality of both FloatBuffer
            boolean fbb = fb1.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");
        }
    }
}


Output:

FloatBuffer 1:  [8.56, 0.0, 9.61, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
FloatBuffer 2:  [8.56, 0.0, 9.61, 0.0, 0.0]
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 FloatBuffer 1
        int capacity1 = 10;
  
        // Declaring the capacity of the  FloatBuffer 2
        int capacity2 = 10;
  
        // Creating the FloatBuffer
        try {
  
            // creating object of floatbuffer 1
            // and allocating size capacity
            FloatBuffer fb1 = FloatBuffer.allocate(capacity1);
  
            // creating object of floatbuffer 2
            // and allocating size capacity
            FloatBuffer fb2 = FloatBuffer.allocate(capacity2);
  
            // putting the value in floatbuffer 1
            fb1.put(8.56F);
            fb1.put(2, 9.61F);
            fb1.rewind();
  
            // putting the value in floatbuffer 2
            fb2.put(8.56F);
            fb2.put(2, 9.61F);
            fb2.put(3, 7.861F);
            fb2.put(4, 4.31F);
            fb2.rewind();
  
            // print the FloatBuffer 1
            System.out.println(" FloatBuffer 1:  "
                               + Arrays.toString(fb1.array()));
  
            // print the FloatBuffer 2
            System.out.println(" FloatBuffer 2:  "
                               + Arrays.toString(fb2.array()));
  
            // checking the equality of both FloatBuffer
            boolean fbb = fb1.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");
        }
    }
}


Output:

FloatBuffer 1:  [8.56, 0.0, 9.61, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
FloatBuffer 2:  [8.56, 0.0, 9.61, 7.861, 4.31, 0.0, 0.0, 0.0, 0.0, 0.0]
both are not equal


Last Updated : 06 Dec, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads